-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmr.bash
executable file
·512 lines (467 loc) · 18.7 KB
/
mr.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#!/usr/bin/env bash
# https://github.com/vbem/multi-runners
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# common configurations
set -o pipefail
# directory and filename of this script
DIR_THIS="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
FILE_THIS="$(basename "${BASH_SOURCE[0]}")"
declare -rg DIR_THIS FILE_THIS
# only for local debug if .env file exists
# shellcheck disable=SC1091
[[ -r "$DIR_THIS/.env" ]] && source "$DIR_THIS/.env"
# environment variables for customization
# Github personal access token
declare -rg MR_GITHUB_PAT
# download URL of actions runner release, defaults to latest release on GitHub.com
declare -rg MR_RELEASE_URL
# baseurl of GitHub API, defaults to https://api.github.com
declare -rg MR_GITHUB_API_BASEURL="${MR_GITHUB_API_BASEURL:-https://api.github.com}"
# baseurl of GitHub service, defaults to https://github.com
declare -rg MR_GITHUB_BASEURL="${MR_GITHUB_BASEURL:-https://github.com}"
# runners' local username prefix, defaults to `runner-`
declare -rg MR_USER_PREFIX="${MR_USER_PREFIX:-runner-}"
# runners' local users base directory, overrides the `HOME` setting in `/etc/default/useradd`
declare -rg MR_USER_BASE
# URL of this application
declare -rg MR_URL='https://github.com/vbem/multi-runners'
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# stdlib
# Log to stderr
# https://misc.flogisoft.com/bash/tip_colors_and_formatting
# $1: level string
# $2: message string
# $?: always 0
# stderr: log message
function log::_ {
local each pos color level datetime
for each in "${FUNCNAME[@]}"; do
[[ "$each" != log::_ ]] \
&& [[ "$each" != log::failed ]] \
&& [[ "$each" != run::logFailed ]] \
&& [[ "$each" != run::log ]] \
&& [[ "$each" != main ]] \
&& [[ "$each" != *::main ]] \
&& [[ "$each" != *::_* ]] \
&& pos="/$each$pos"
done
case "$1" in
FATAL) color="5;1;91" ;;
ERR*) color="1;91" ;;
WARN*) color="95" ;;
INFO* | NOTICE) color="92" ;;
DEBUG) color="94" ;;
*) color="96" ;;
esac
datetime="\e[3;2;90m$(date -Isecond)\e[0m"
pos="\e[3;90m${pos:1}\e[0m"
level="\e[1;3;${color}m$1\e[0m"
echo -e "\e[2;97m[\e[0m$datetime ${pos} $level\e[2;97m]\e[0m \e[${color}m$2\e[0m" >&2
}
# Log if previous return code is none-zero
# $1: previous return code
# $2: message string
# $?: previous return code
# stderr: message string
function log::failed {
(("$1" != 0)) && log::_ ERROR "$2"
return "$1"
}
# Run command and log if return code is none-zero
# $@: command line
# $?: return code of command
# stdout: stdout of command
# stderr: message string
function run::logFailed {
local -i ret
"$@"
ret=$?
log::failed "$ret" "Return $ret from command: $*"
}
# Run command with log and log if return code is none-zero
# $@: command line
# $?: return code of command
# stdout: stdout of command
# stderr: message string
function run::log {
log::_ DEBUG "Running: $*"
run::logFailed "$@"
}
# Test commands exists
# $@: commands
# $?: 0 if successful and none-zero otherwise
# stderr: message string
function run::exists {
local each=''
for each in "$@"; do
command -v "$each" &>/dev/null
log::failed $? "Not found command '$each'!" || return $?
done
}
# Check if any variable value of given variable names is not empty
# $@: variable names
# $?: 0 if non-empty and non-zero otherwise
function str::anyVarNotEmpty {
local each=''
for each in "$@"; do
[[ -n "${!each}" ]]
log::failed $? "Var '$each' is empty!" || return $?
done
}
# Check if any variable value of given variable names are not empty
# $@: variable names
# $?: 0 if non-empty and non-zero otherwise
function str::allVarsNotEmpty {
local each=''
for each in "$@"; do
[[ -n "${!each}" ]] && return
done
log::failed 1 "Vars $* are all empty!" || return $?
}
# Check if variable value of given variable name is IN subsequent arguments
# $1: variable name
# $N: arguments as candidate set
# $?: 0 if it's in and non-zero otherwise
function str::varIn {
local varName="$1" varVal="${!1}" each=''
shift
str::anyVarNotEmpty varName || return $?
for each in "$@"; do
[[ "$each" == "$varVal" ]] && return
done
log::_ ERROR "Invalid value '$varVal' for variable '$varName'!"
return 1
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# functions
# Add a local user for runner
# $1: username, defaults to self-increasing username
# $?: 0 if successful and non-zero otherwise
# stdout: username
function mr::addUser {
local user="$1"
if [[ -z "$user" ]]; then
local -i index=0
while :; do
user="${MR_USER_PREFIX}$((index++))"
id -u "$user" &>/dev/null || break
done
fi
useraddArgs=(-m -s /bin/bash -G 'runners,docker')
[[ -n "$MR_USER_BASE" ]] && useraddArgs+=('-b' "$MR_USER_BASE")
run::logFailed sudo tee /etc/sudoers.d/runners <<<'%runners ALL=(ALL) NOPASSWD:ALL' >/dev/null \
&& run::logFailed sudo groupadd -f 'runners' >&2 \
&& run::logFailed sudo groupadd -f 'docker' >&2 \
&& run::log sudo useradd "${useraddArgs[@]}" "$user" >&2 || return $?
echo "$user"
}
# Print the number of processing units available to the current process
# stdout: number, defaults to 2
function mr::nproc {
local -i num=0
num="$(run::logFailed nproc)"
((num > 0)) && echo "$num" || echo 2
}
# Get time-limited registration token from PAT
# https://docs.github.com/en/actions/hosting-your-own-runners/managing-self-hosted-runners/monitoring-and-troubleshooting-self-hosted-runners#checking-self-hosted-runner-network-connectivity
# https://docs.github.com/en/actions/hosting-your-own-runners/managing-self-hosted-runners/autoscaling-with-self-hosted-runners#authentication-requirements
# https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens
# $1: enterprise
# $2: organization
# $3: repository, registration on organization if empty
# $?: 0 if successful and non-zero otherwise
# stdout: registration token
function mr::pat2token {
run::exists jq || return $?
local enterprise="$1" org="$2" repo="$3" api='' middle='' res=''
str::anyVarNotEmpty MR_GITHUB_PAT || return $?
str::allVarsNotEmpty org enterprise || return $?
if [[ -n "$enterprise" ]]; then
middle="enterprises/$enterprise"
elif [[ -z "$repo" ]]; then
middle="orgs/$org"
else
middle="repos/$org/$repo"
fi
api="$MR_GITHUB_API_BASEURL/$middle/actions/runners/registration-token"
log::_ DEBUG "Calling API: $api"
res="$(curl -Lsm 3 --retry 1 \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${MR_GITHUB_PAT}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"$api")" || log::failed $? "Call API failed: $api" || return $?
jq -Mcre .token <<<"$res" || log::failed $? "Parse registration-token failed! response: $res" || return $?
}
# Download and cache GitHub Actions Runner to local /tmp/
# https://github.com/actions/runner/releases
# https://docs.github.com/en/repositories/releasing-projects-on-github/linking-to-releases
# https://github.com/actions/runner/blob/main/docs/start/envlinux.md#install-net-core-3x-linux-dependencies
# $?: 0 if successful and non-zero otherwise
# stdout: local path of downloaded file
function mr::downloadRunner {
local url="$MR_RELEASE_URL" tarpath=''
if [[ -z "$url" ]]; then
run::exists jq || return $?
curlArgs=(-Lsm 3 --retry 1)
# https://github.com/vbem/multi-runners/pull/16
[[ -n "$MR_GITHUB_PAT" ]] && curlArgs+=('-H' "Authorization: Bearer ${MR_GITHUB_PAT}")
url="$(
curl "${curlArgs[@]}" https://api.github.com/repos/actions/runner/releases/latest \
| jq -Mcre '.assets[].browser_download_url|select(test("linux-x64-[^-]+\\.tar\\.gz"))'
)" || log::failed $? "Fetching latest version number failed, check PAT or rate limit!" || return $?
fi
tarpath="/tmp/$(run::logFailed basename "$url")" || return $?
if [[ ! -r "$tarpath" ]]; then
log::_ INFO "Downloading from $url to $tarpath"
run::logFailed curl -Lm 600 --retry 1 "$url" -o "$tarpath.tmp" \
&& run::logFailed mv -f "$tarpath.tmp" "$tarpath" \
&& run::logFailed chmod a+r "$tarpath" || return $?
log::_ INFO "Checking runner dependencies"
run::logFailed tar -Oxzf "$tarpath" './bin/installdependencies.sh' | sudo bash >&2 || return $?
fi
echo "$tarpath"
}
# Add GitHub Actions Runner by local username
# $1: username, optional
# $2: enterprise
# $3: organization
# $4: repository, optional
# $5: runner registration token, optional
# $6: extra labels, optional
# $7: group, defaults to `default`
# $8: lines to set in runner's '.env' files, optional
# $9: count of runners, optional, defaults to 1
# $10: extra options for `config.sh`, optional, such as `--no-default-labels`
# $?: 0 if successful and non-zero otherwise
function mr::addRunner {
local username="$1" enterprise="$2" org="$3" repo="$4" token="$5" extraLabels="$6" group="${7:-default}" dotenv="$8" opts="${10}" tarpath=''
local -i count="${9:-1}"
str::allVarsNotEmpty enterprise org || return $?
[[ -n "$username" ]] && ((count > 1)) && {
log::_ ERROR "Count must be 1 when username is given!"
return 1
}
[[ -z "$token" ]] && { token="$(mr::pat2token "$enterprise" "$org" "$repo")" || return $?; }
tarpath="$(mr::downloadRunner)" || return $?
local commonLabels="controller:${MR_URL#https://},hostname:$HOSTNAME"
commonLabels+=",createdtime:$(date --iso-8601=sec)"
[[ -r /etc/os-release ]] && commonLabels+=",os:$(source /etc/os-release && echo "$ID-$VERSION_ID")"
[[ -n "$extraLabels" ]] && commonLabels+=",$extraLabels"
for i in $(seq 1 "$count"); do
user="$(mr::addUser "$username")" || return $?
local name="$user@$HOSTNAME"
local labels="$commonLabels,username:$user"
local url=''
if [[ -n "$enterprise" ]]; then
url="$MR_GITHUB_BASEURL/enterprises/$enterprise"
labels+=",$enterprise"
elif [[ -z "$repo" ]]; then
url="$MR_GITHUB_BASEURL/$org"
labels+=",$org"
else
url="$MR_GITHUB_BASEURL/$org/$repo"
labels+=",$org/$repo"
fi
log::_ INFO "Installing runner $i in local user '$user' for $url"
run::logFailed sudo su --login "$user" -- -eo pipefail <<-__
mkdir -p runner/mr.d && cd runner/mr.d
echo -n '$enterprise' > enterprise && echo -n '$org' > org && echo -n '$repo' > repo && echo -n '$url' > url
echo -n '$name' > name && echo -n '$labels' > labels && echo -n '$tarpath' > tarpath
cd .. && tar -xzf "$tarpath"
echo "$dotenv" >> .env
./config.sh --unattended --replace --url '$url' --token '$token' --name '$name' --labels '$labels' --runnergroup '$group' $opts
sudo ./svc.sh install '$user'
if [[ "$(getenforce 2>/dev/null)" == "Enforcing" ]]; then
chcon -t bin_t ./runsvc.sh # https://github.com/vbem/multi-runners/issues/9
fi
sudo ./svc.sh start
__
log::failed $? "Failed installing runner $i in local user '$user' for $url!" || return $?
done
}
# Delete GitHub Actions Runner by local username
# $1: username, option
# $2: enterprise, optional
# $3: organization, optional
# $4: repository, optional
# $5: runner registration token, optional
# $6: count of runners, optional, defaults to 0 (all)
# $7: extra options for `config.sh`, optional, such as `--local`
# $?: 0 if successful and non-zero otherwise
function mr::delRunner {
local user="$1" enterprise="$2" org="$3" repo="$4" token="$5" opts="$7"
local -i count="${6:-0}"
local -a removals=()
if [[ -n "$user" ]]; then
removals+=("$user")
else
existing="$(run::logFailed getent group 'runners' | cut -d: -f4 | tr ',' '\n' | sort -g)" || return $?
while read -r each; do
[[ -z "$each" ]] && continue
((count != 0)) && ((${#removals[@]} >= count)) && break
each_enterprise="$(run::logFailed sudo -Hiu "$each" -- cat runner/mr.d/enterprise)"
each_org="$(run::logFailed sudo -Hiu "$each" -- cat runner/mr.d/org)"
each_repo="$(run::logFailed sudo -Hiu "$each" -- cat runner/mr.d/repo)"
each_url="$(run::logFailed sudo -Hiu "$each" -- cat runner/mr.d/url)"
if [[ "$enterprise" == "$each_enterprise" && "$org" == "$each_org" && "$repo" == "$each_repo" ]]; then
log::_ DEBUG "Found local user '$each' to delete for $each_url"
removals+=("$each")
fi
done <<<"$existing"
fi
for user in "${removals[@]}"; do
log::_ INFO "Deleting runner in local user '$user'"
if [[ -z "$token" ]]; then
enterprise="$(run::logFailed sudo -Hiu "$user" -- cat runner/mr.d/enterprise)"
org="$(run::logFailed sudo -Hiu "$user" -- cat runner/mr.d/org)"
repo="$(run::logFailed sudo -Hiu "$user" -- cat runner/mr.d/repo)"
token="$(mr::pat2token "$enterprise" "$org" "$repo")"
fi
run::logFailed sudo su --login "$user" -- <<-__
cd runner
sudo ./svc.sh stop && sudo ./svc.sh uninstall
./config.sh remove --token '$token' $opts
__
run::log sudo userdel -rf "$user"
done
}
# List all existing runners
# $?: 0 if successful and non-zero otherwise
# stdout: all runners
function mr::listRunners {
log::_ INFO "Listing localhost all existing runners"
run::exists jq || return $?
local users=''
users="$(run::logFailed getent group 'runners' | cut -d: -f4 | tr ',' '\n' | sort -g)" || return $?
while read -r user; do
[[ -z "$user" ]] && continue
echo -n "$user"
echo -n " $(sudo -Hiu "$user" -- du -h --summarize | cut -f1)"
echo -n " $(sudo -Hiu "$user" -- jq -Mcre .gitHubUrl runner/.runner)"
echo
done <<<"$users" # user
run::log systemctl list-units -al --no-pager 'actions.runner.*' >&2 || return $?
}
# Temporary test
function mr::test {
log::_ INFO "Self testing ..."
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# main
HELP="$FILE_THIS - $MR_URL
Environment variables:
MR_GITHUB_BASEURL=$MR_GITHUB_BASEURL
MR_GITHUB_API_BASEURL=$MR_GITHUB_API_BASEURL
MR_RELEASE_URL=${MR_RELEASE_URL:-<latest on github.com/actions/runner/releases>}
MR_USER_BASE=${MR_USER_BASE:-<default in /etc/default/useradd>}
MR_GITHUB_PAT=${MR_GITHUB_PAT::11}${MR_GITHUB_PAT:+***}
Sub-commands:
add Add one self-hosted runner on this host
e.g. ${BASH_SOURCE[0]} add --org ORG --repo REPO --labels cloud:ali,region:cn-shanghai
e.g. ${BASH_SOURCE[0]} add --org ORG --count 3
del Delete one self-hosted runner on this host
e.g. ${BASH_SOURCE[0]} del --user runner-1
e.g. ${BASH_SOURCE[0]} del --org ORG --count 3
list List all runners on this host
e.g. ${BASH_SOURCE[0]} list
download Download GitHub Actions Runner release tar to /tmp/
Detect latest on github.com/actions/runner/releases if MR_RELEASE_URL empty
e.g. ${BASH_SOURCE[0]} download
pat2token Get runner registration token from GitHub PAT (MR_GITHUB_PAT)
e.g. ${BASH_SOURCE[0]} pat2token --org SOME_OWNER --repo SOME_REPO
Options:
--enterprise GitHub Cloud Enterprise name, optional
--org GitHub organization name
--repo GitHub repository name, registration on organization-level if empty
--user Linux local username of runner
--labels Extra labels for the runner
--group Runner group for the runner
--token Runner registration token, takes precedence over MR_GITHUB_PAT
--dotenv The lines to set in runner's '.env' files
--count The number to add or del, optional, defaults to 1 for add and all for del
--opts Extra options for 'config.sh', optional, such as '--no-default-labels'
-h --help Show this help.
"
declare -rg HELP
# CLI arguments parser.
# $?: 0 if successful and non-zero otherwise
function mr::main {
local getopt_output='' subCmd=''
local org='' repo='' user='' labels='' token='' group='' dotenv='' count='' opts=''
# parse options into variables
getopt_output="$(getopt -o h -l help,enterprise:,org:,repo:,user:,labels:,token:,group:,dotenv:,count:,opts: -n "$FILE_THIS" -- "$@")"
log::failed $? "getopt failed!" || return $?
eval set -- "$getopt_output"
while true; do
case "$1" in
-h | --help) echo -n "$HELP" && return ;;
--enterprise)
enterprise="$2"
shift 2
;;
--org)
org="$2"
shift 2
;;
--repo)
repo="$2"
shift 2
;;
--user)
user="$2"
shift 2
;;
--labels)
labels="$2"
shift 2
;;
--token)
token="$2"
shift 2
;;
--group)
group="$2"
shift 2
;;
--dotenv)
dotenv+="$2"$'\n'
shift 2
;;
--count)
count="$2"
shift 2
;;
--opts)
opts="$2"
shift 2
;;
--)
shift
break
;;
*)
log::_ ERROR "Invalid option '$1'! See '$FILE_THIS help'."
return 255
;;
esac
done
# parse sub-commands into functions
subCmd="$1"
shift
case "$subCmd" in
add) mr::addRunner "$user" "$enterprise" "$org" "$repo" "$token" "$labels" "$group" "$dotenv" "$count" "$opts" ;;
del) mr::delRunner "$user" "$enterprise" "$org" "$repo" "$token" "$count" "$opts" ;;
list) mr::listRunners ;;
status) mr::statusRunner "$user" ;;
download) mr::downloadRunner ;;
pat2token) mr::pat2token "$enterprise" "$org" "$repo" ;;
help | '') echo -n "$HELP" >&2 ;;
test) mr::test "$@" ;;
*)
log::_ ERROR "Invalid command '$1'! See '$FILE_THIS help'."
return 255
;;
esac
}
mr::main "$@"