@@ -21,9 +21,9 @@ declare -rg MR_GITHUB_PAT
21
21
# download URL of actions runner release, defaults to latest release on GitHub.com
22
22
declare -rg MR_RELEASE_URL
23
23
# baseurl of GitHub API, defaults to https://api.github.com
24
- declare -rg MR_GIHUB_API_BASEURL =" ${MR_GIHUB_API_BASEURL :- https:// api.github.com} "
24
+ declare -rg MR_GITHUB_API_BASEURL =" ${MR_GITHUB_API_BASEURL :- https:// api.github.com} "
25
25
# baseurl of GitHub service, defaults to https://github.com
26
- declare -rg MR_GIHUB_BASEURL =" ${MR_GIHUB_BASEURL :- https:// github.com} "
26
+ declare -rg MR_GITHUB_BASEURL =" ${MR_GITHUB_BASEURL :- https:// github.com} "
27
27
# runners' local username prefix, defaults to `runner-`
28
28
declare -rg MR_USER_PREFIX=" ${MR_USER_PREFIX:- runner-} "
29
29
# runners' local users base directory, overrides the `HOME` setting in `/etc/default/useradd`
@@ -53,12 +53,12 @@ function log::_ {
53
53
&& pos=" /$each$pos "
54
54
done
55
55
case " $1 " in
56
- FATAL) color=" 5;1;91" ;;
57
- ERR* ) color=" 1;91" ;;
58
- WARN* ) color=" 95" ;;
59
- INFO* | NOTICE) color=" 92" ;;
60
- DEBUG) color=" 94" ;;
61
- * ) color=" 96" ;;
56
+ FATAL) color=" 5;1;91" ;;
57
+ ERR* ) color=" 1;91" ;;
58
+ WARN* ) color=" 95" ;;
59
+ INFO* | NOTICE) color=" 92" ;;
60
+ DEBUG) color=" 94" ;;
61
+ * ) color=" 96" ;;
62
62
esac
63
63
datetime=" \e[3;2;90m$( date -Isecond) \e[0m"
64
64
pos=" \e[3;90m${pos: 1} \e[0m"
@@ -110,40 +110,40 @@ function run::exists {
110
110
done
111
111
}
112
112
113
- # Check if varible values of given varible names are not empty
114
- # $@: varible names
113
+ # Check if any variable value of given variable names is not empty
114
+ # $@: variable names
115
115
# $?: 0 if non-empty and non-zero otherwise
116
- function str::varNotEmpty {
116
+ function str::anyVarNotEmpty {
117
117
local each=' '
118
118
for each in " $@ " ; do
119
119
[[ -n " ${! each} " ]]
120
120
log::failed $? " Var '$each ' is empty!" || return $?
121
121
done
122
122
}
123
123
124
- # Check if any varible value of given varible names are not empty
125
- # $@: varible names
124
+ # Check if any variable value of given variable names are not empty
125
+ # $@: variable names
126
126
# $?: 0 if non-empty and non-zero otherwise
127
- function str::varsNotEmpty {
127
+ function str::allVarsNotEmpty {
128
128
local each=' '
129
129
for each in " $@ " ; do
130
130
[[ -n " ${! each} " ]] && return
131
131
done
132
- log::failed 1 " Vars ' $@ ' are all empty!" || return $?
132
+ log::failed 1 " Vars $* are all empty!" || return $?
133
133
}
134
134
135
- # Check if varible value of given varible name is IN subsequent arguments
136
- # $1: varible name
135
+ # Check if variable value of given variable name is IN subsequent arguments
136
+ # $1: variable name
137
137
# $N: arguments as candidate set
138
138
# $?: 0 if it's in and non-zero otherwise
139
139
function str::varIn {
140
140
local varName=" $1 " varVal=" ${! 1} " each=' '
141
141
shift
142
- str::varNotEmpty varName || return $?
142
+ str::anyVarNotEmpty varName || return $?
143
143
for each in " $@ " ; do
144
144
[[ " $each " == " $varVal " ]] && return
145
145
done
146
- log::_ ERROR " Invalid value '$varVal ' for varible '$varName '!"
146
+ log::_ ERROR " Invalid value '$varVal ' for variable '$varName '!"
147
147
return 1
148
148
}
149
149
@@ -184,13 +184,16 @@ function mr::nproc {
184
184
# 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
185
185
# https://docs.github.com/en/actions/hosting-your-own-runners/managing-self-hosted-runners/autoscaling-with-self-hosted-runners#authentication-requirements
186
186
# https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens
187
- # $1: organization
188
- # $2: repository, registration on organization if empty
187
+ # $1: enterprise
188
+ # $2: organization
189
+ # $3: repository, registration on organization if empty
190
+ # $?: 0 if successful and non-zero otherwise
191
+ # stdout: registration token
189
192
function mr::pat2token {
190
193
run::exists jq || return $?
191
194
local enterprise=" $1 " org=" $2 " repo=" $3 " api=' ' middle=' ' res=' '
192
- str::varNotEmpty MR_GITHUB_PAT || return $?
193
- str::varsNotEmpty org enterprise || return $?
195
+ str::anyVarNotEmpty MR_GITHUB_PAT || return $?
196
+ str::allVarsNotEmpty org enterprise || return $?
194
197
195
198
if [[ -n " $enterprise " ]]; then
196
199
middle=" enterprises/$enterprise "
@@ -199,7 +202,7 @@ function mr::pat2token {
199
202
else
200
203
middle=" repos/$org /$repo "
201
204
fi
202
- api=" $MR_GIHUB_API_BASEURL /$middle /actions/runners/registration-token"
205
+ api=" $MR_GITHUB_API_BASEURL /$middle /actions/runners/registration-token"
203
206
204
207
log::_ DEBUG " Calling API: $api "
205
208
res=" $( curl -Lsm 3 --retry 1 \
@@ -246,17 +249,18 @@ function mr::downloadRunner {
246
249
247
250
# Add GitHub Actions Runner by local username
248
251
# $1: username, optional
249
- # $2: organization
250
- # $3: repository, optional
251
- # $4: runner registration token, optional
252
- # $5: extra labels, optional
253
- # $6: group, defaults to `default`
254
- # $7: lines to set in runner's '.env' files, optional
252
+ # $2: enterprise
253
+ # $3: organization
254
+ # $4: repository, optional
255
+ # $5: runner registration token, optional
256
+ # $6: extra labels, optional
257
+ # $7: group, defaults to `default`
258
+ # $8: lines to set in runner's '.env' files, optional
255
259
# $?: 0 if successful and non-zero otherwise
256
260
function mr::addRunner {
257
261
local user=" $1 " enterprise=" $2 " org=" $3 " repo=" $4 " token=" $5 " extraLabels=" $6 " group=" ${7:- default} " dotenv=" $8 " tarpath=' '
262
+ str::allVarsNotEmpty enterprise org || return $?
258
263
259
- str::varsNotEmpty enterprise org || return $?
260
264
tarpath=" $( mr::downloadRunner) " || return $?
261
265
[[ -z " $token " ]] && { token=" $( mr::pat2token " $enterprise " " $org " " $repo " ) " || return $? ; }
262
266
user=" $( mr::addUser " $user " ) " || return $?
@@ -265,20 +269,20 @@ function mr::addRunner {
265
269
266
270
local labels=" controller:${MR_URL# https:// } ,username:$user ,hostname:$HOSTNAME "
267
271
[[ -r /etc/os-release ]] && labels=" $labels ,os:$( source /etc/os-release && echo " $ID -$VERSION_ID " ) "
268
- [[ -n " $repo " ]] && labels=" $labels ,$org /$repo " || labels=" $labels ,$org "
269
272
[[ -n " $extraLabels " ]] && labels=" $labels ,$extraLabels "
270
273
274
+ local url=' '
271
275
if [[ -n " $enterprise " ]]; then
272
- middle=" enterprises/$enterprise "
276
+ url=" $MR_GITHUB_BASEURL /enterprises/$enterprise "
277
+ labels=" $labels ,$enterprise "
273
278
elif [[ -z " $repo " ]]; then
274
- middle=" orgs/$org "
279
+ url=" $MR_GITHUB_BASEURL /orgs/$org "
280
+ labels=" $labels ,$org "
275
281
else
276
- middle=" repos/$org /$repo "
282
+ url=" $MR_GITHUB_BASEURL /repos/$org /$repo "
283
+ labels=" $labels ,$org /$repo "
277
284
fi
278
285
279
- local url=' '
280
- url=" $MR_GIHUB_BASEURL /$middle "
281
-
282
286
log::_ INFO " Adding runner into local user '$user ' for $url "
283
287
run::logFailed sudo su --login " $user " -- -eo pipefail << -__
284
288
mkdir -p runner/mr.d && cd runner/mr.d
297
301
298
302
# Delete GitHub Actions Runner by local username
299
303
# $1: username
300
- # $2: organization, optional
301
- # $3: repository, optional
302
- # $4: runner registration token, optional
304
+ # $2: enterprise, optional
305
+ # $3: organization, optional
306
+ # $4: repository, optional
307
+ # $5: runner registration token, optional
303
308
# $?: 0 if successful and non-zero otherwise
304
309
function mr::delRunner {
305
310
local user=" $1 " enterprise=" $2 " org=" $3 " repo=" $4 " token=" $5 "
306
- str::varNotEmpty user || return $?
311
+ str::anyVarNotEmpty user || return $?
307
312
308
313
if [[ -z " $token " ]]; then
309
- [[ -z " $enterprise " ]] && enterprise=" $( run::logFailed sudo -Hiu " $user " -- cat runner/mr.d/org ) "
314
+ [[ -z " $enterprise " ]] && enterprise=" $( run::logFailed sudo -Hiu " $user " -- cat runner/mr.d/enterprise ) "
310
315
[[ -z " $org " ]] && org=" $( run::logFailed sudo -Hiu " $user " -- cat runner/mr.d/org) "
311
316
[[ -z " $repo " ]] && repo=" $( run::logFailed sudo -Hiu " $user " -- cat runner/mr.d/repo) "
312
- token=" $( mr::pat2token " $org " " $repo " ) "
317
+ token=" $( mr::pat2token " $enterprise " " $ org" " $repo " ) "
313
318
fi
314
319
315
320
log::_ INFO " Deleting runner and local user '$user '"
@@ -350,8 +355,8 @@ function mr::test {
350
355
HELP=" $FILE_THIS - $MR_URL
351
356
352
357
Environment variables:
353
- MR_GIHUB_BASEURL= $MR_GIHUB_BASEURL
354
- MR_GIHUB_API_BASEURL= $MR_GIHUB_API_BASEURL
358
+ MR_GITHUB_BASEURL= $MR_GITHUB_BASEURL
359
+ MR_GITHUB_API_BASEURL= $MR_GITHUB_API_BASEURL
355
360
MR_RELEASE_URL=${MR_RELEASE_URL:- <latest on github.com/ actions/ runner/ releases>}
356
361
MR_USER_BASE=${MR_USER_BASE:- <default in / etc/ default/ useradd>}
357
362
MR_GITHUB_PAT=${MR_GITHUB_PAT:: 11}${MR_GITHUB_PAT: +*** }
@@ -370,7 +375,7 @@ Sub-commands:
370
375
e.g. ${BASH_SOURCE[0]} pat2token --org SOME_OWNER --repo SOME_REPO
371
376
372
377
Options:
373
- --enterprise GitHub Enterprise name
378
+ --enterprise GitHub Cloud Enterprise name, optional
374
379
--org GitHub organization name
375
380
--repo GitHub repository name, registration on organization-level if empty
376
381
--user Linux local username of runner
@@ -395,66 +400,66 @@ function mr::main {
395
400
396
401
while true ; do
397
402
case " $1 " in
398
- -h | --help) echo -n " $HELP " && return ;;
399
- --enterprise)
400
- enterprise=" $2 "
401
- shift 2
402
- ;;
403
- --org)
404
- org=" $2 "
405
- shift 2
406
- ;;
407
- --repo)
408
- repo=" $2 "
409
- shift 2
410
- ;;
411
- --user)
412
- user=" $2 "
413
- shift 2
414
- ;;
415
- --labels)
416
- labels=" $2 "
417
- shift 2
418
- ;;
419
- --token)
420
- token=" $2 "
421
- shift 2
422
- ;;
423
- --group)
424
- group=" $2 "
425
- shift 2
426
- ;;
427
- --dotenv)
428
- dotenv+=" $2 " $' \n '
429
- shift 2
430
- ;;
431
- --)
432
- shift
433
- break
434
- ;;
435
- * )
436
- log::_ ERROR " Invalid option '$1 '! See '$FILE_THIS help'."
437
- return 255
438
- ;;
403
+ -h | --help) echo -n " $HELP " && return ;;
404
+ --enterprise)
405
+ enterprise=" $2 "
406
+ shift 2
407
+ ;;
408
+ --org)
409
+ org=" $2 "
410
+ shift 2
411
+ ;;
412
+ --repo)
413
+ repo=" $2 "
414
+ shift 2
415
+ ;;
416
+ --user)
417
+ user=" $2 "
418
+ shift 2
419
+ ;;
420
+ --labels)
421
+ labels=" $2 "
422
+ shift 2
423
+ ;;
424
+ --token)
425
+ token=" $2 "
426
+ shift 2
427
+ ;;
428
+ --group)
429
+ group=" $2 "
430
+ shift 2
431
+ ;;
432
+ --dotenv)
433
+ dotenv+=" $2 " $' \n '
434
+ shift 2
435
+ ;;
436
+ --)
437
+ shift
438
+ break
439
+ ;;
440
+ * )
441
+ log::_ ERROR " Invalid option '$1 '! See '$FILE_THIS help'."
442
+ return 255
443
+ ;;
439
444
esac
440
445
done
441
446
442
447
# parse sub-commands into functions
443
448
subCmd=" $1 "
444
449
shift
445
450
case " $subCmd " in
446
- add) mr::addRunner " $user " " $enterprise " " $org " " $repo " " $token " " $labels " " $group " " $dotenv " ;;
447
- del) mr::delRunner " $user " " $enterprise " " $org " " $repo " " $token " ;;
448
- list) mr::listRunners ;;
449
- status) mr::statusRunner " $user " ;;
450
- download) mr::downloadRunner ;;
451
- pat2token) mr::pat2token " $enterprise " " $org " " $repo " ;;
452
- help | ' ' ) echo -n " $HELP " >&2 ;;
453
- test) mr::test " $@ " ;;
454
- * )
455
- log::_ ERROR " Invalid command '$1 '! See '$FILE_THIS help'."
456
- return 255
457
- ;;
451
+ add) mr::addRunner " $user " " $enterprise " " $org " " $repo " " $token " " $labels " " $group " " $dotenv " ;;
452
+ del) mr::delRunner " $user " " $enterprise " " $org " " $repo " " $token " ;;
453
+ list) mr::listRunners ;;
454
+ status) mr::statusRunner " $user " ;;
455
+ download) mr::downloadRunner ;;
456
+ pat2token) mr::pat2token " $enterprise " " $org " " $repo " ;;
457
+ help | ' ' ) echo -n " $HELP " >&2 ;;
458
+ test) mr::test " $@ " ;;
459
+ * )
460
+ log::_ ERROR " Invalid command '$1 '! See '$FILE_THIS help'."
461
+ return 255
462
+ ;;
458
463
esac
459
464
}
460
465
0 commit comments