Skip to content

Commit 382076d

Browse files
committed
Allow to use config file
1 parent 1900a69 commit 382076d

File tree

1 file changed

+37
-16
lines changed

1 file changed

+37
-16
lines changed

manage-ghar

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set -eu
55
PID_FILE="runner.pid"
66
LOG_FILE="runner.log"
77
STATE_DIRNAME="state"
8+
CONFIG_FILE=".manage_ghar.conf"
89

910
# check if runner is running based on the existence of a state file and based
1011
# on a valid PID
@@ -32,7 +33,7 @@ is_running () {
3233
start () {
3334
local runner_path="$1"
3435
local state_path="$2"
35-
local command="$3"
36+
local runner_command="$3"
3637
local force=$4
3738

3839
# check if the runner path exists
@@ -51,15 +52,15 @@ start () {
5152

5253
# running as a service, see
5354
# https://docs.github.com/en/actions/hosting-your-own-runners/managing-self-hosted-runners/configuring-the-self-hosted-runner-application-as-a-service
54-
if [[ -z $command ]]
55+
if [[ -z $runner_command ]]
5556
then
56-
command="bin/runsvc.sh"
57+
runner_command="bin/runsvc.sh"
5758
fi
5859

5960
echo "Starting runner"
6061
echo "Starting runner on $(hostname)" >>"$state_path/$LOG_FILE"
6162
cd "$runner_path"
62-
$command >>"$state_path/$LOG_FILE" 2>&1 &
63+
$runner_command >>"$state_path/$LOG_FILE" 2>&1 &
6364

6465
# create pid file
6566
echo "Creating pid file"
@@ -102,13 +103,13 @@ usage () {
102103
cat <<EOF
103104
Manage GitHub Actions Runner like a SysV init script
104105
105-
manage-ghar [-s STATE_PATH] [-c COMMAND] [-f] [-h] {start|stop|restart|status} RUNNER_PATH
106+
manage-ghar [-s STATE_PATH] [-r RUNNER_COMMAND] [-f] [-h] {start|stop|restart|status} RUNNER_PATH
106107
107108
Optional arguments:
108109
-s STATE_PATH
109110
Path to the state directory of the manager (where PID and log files are stored). Default to $STATE_DIRNAME in the runner directory.
110-
-c COMMAND
111-
Script to execute instead of running the runner directly.
111+
-r RUNNER_COMMAND
112+
Command to execute instead of running the runner directly. This command should not take any argument.
112113
-f
113114
Force start even if the runner is running.
114115
-h
@@ -127,17 +128,23 @@ Subcommands:
127128
Mandatory arguments:
128129
RUNNER_PATH
129130
Path to a GitHub Actions Runner directory.
131+
132+
If there is a configuration file $CONFIG_FILE in the runner path, then manage-ghar will use it to populate the following optional arguments:
133+
state_path
134+
runner_command
135+
force
136+
Arguments must be in the form "<key>=<value>".
130137
EOF
131138
}
132139

133140
main () {
134141
# default arguments
135142
local state_path=""
136-
local command=""
143+
local runner_command=""
137144
local force=false
138145

139146
# process optional arguments
140-
while getopts ":hs:c:f" option
147+
while getopts ":hs:r:f" option
141148
do
142149
case $option in
143150
"h")
@@ -147,8 +154,8 @@ main () {
147154
"s")
148155
state_path="$OPTARG"
149156
;;
150-
"c")
151-
command="$OPTARG"
157+
"r")
158+
runner_command="$OPTARG"
152159
;;
153160
"f")
154161
force=true
@@ -170,10 +177,23 @@ main () {
170177
exit 1
171178
fi
172179

173-
local cmd="$1"
180+
local subcommand="$1"
174181
local runner_path
175182
runner_path="$(realpath "$2")"
176183

184+
# read configuration if available
185+
if [[ -f $runner_path/$CONFIG_FILE ]]
186+
then
187+
while read -r line
188+
do
189+
if [[ "$line" == "state_path="* ]] || [[ "$line" == "runner_command="* ]] || [[ "$line" == "force="* ]]
190+
then
191+
declare "$line"
192+
fi
193+
done <"$runner_path/$CONFIG_FILE"
194+
fi
195+
196+
# process state path
177197
if [[ -z $state_path ]]
178198
then
179199
state_path="$runner_path/$STATE_DIRNAME"
@@ -183,9 +203,10 @@ main () {
183203

184204
mkdir -p "$state_path"
185205

186-
case $cmd in
206+
# process subcommand
207+
case $subcommand in
187208
"start")
188-
start "$runner_path" "$state_path" "$command" "$force"
209+
start "$runner_path" "$state_path" "$runner_command" "$force"
189210
exit 0
190211
;;
191212
"stop")
@@ -195,15 +216,15 @@ main () {
195216
"restart")
196217
stop "$state_path"
197218
sleep 1
198-
start "$runner_path" "$state_path" "$command" "$force"
219+
start "$runner_path" "$state_path" "$runner_command" "$force"
199220
exit 0
200221
;;
201222
"status")
202223
status "$state_path"
203224
exit 0
204225
;;
205226
*)
206-
echo "Unknown subcommand $cmd"
227+
echo "Unknown subcommand $subcommand"
207228
usage
208229
exit 1
209230
;;

0 commit comments

Comments
 (0)