Skip to content

Commit

Permalink
feat: Add MR_USER_BASE environment variable to override system-wide d…
Browse files Browse the repository at this point in the history
…efault for runners' local users base directory
  • Loading branch information
vbem committed Jun 7, 2024
1 parent d8b4693 commit 3394733
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Environment variables:
MR_GIHUB_BASEURL=https://github.com
MR_GIHUB_API_BASEURL=https://api.github.com
MR_RELEASE_URL=<latest on github.com/actions/runner/releases>
MR_USER_BASE=/home
MR_GITHUB_PAT=github_pat_***
Sub-commands:
Expand All @@ -45,7 +46,6 @@ Options:
--org GitHub organization name
--repo GitHub repository name, registration on organization-level if empty
--user Linux local username of runner
--base Base directory for user home directories
--labels Extra labels for the runner
--group Runner group for the runner
--token Runner registration token, takes precedence over MR_GITHUB_PAT
Expand Down Expand Up @@ -118,6 +118,8 @@ To setup multi-runners, you can simplify run following command multi times:
./mr.bash add --org <ORG-NAME-2>
```

This application will create one Linux local user for one runner via `useradd` command. The *Base Directory* of these users is read from `HOME` setting in your `/etc/default/useradd` file by default (typically `/home`). You can also set it in environment variable `MR_USER_BASE` to override system-wide default.

### List all runners on current host

This application also integrated status check of runners.
Expand Down
12 changes: 5 additions & 7 deletions mr.bash
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ declare -rg MR_GIHUB_API_BASEURL="${MR_GIHUB_API_BASEURL:-https://api.github.com
declare -rg MR_GIHUB_BASEURL="${MR_GIHUB_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="${MR_USER_BASE:-$(useradd -D | grep '^HOME=' | cut -d= -f2-)}"
# URL of this application
declare -rg MR_URL='https://github.com/vbem/multi-runners'

Expand Down Expand Up @@ -153,7 +155,7 @@ function mr::addUser {
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 -b "${base:-/home}" -m -s /bin/bash -G 'runners,docker' "$user" >&2 || return $?
&& run::log sudo useradd -b "$MR_USER_BASE" -m -s /bin/bash -G 'runners,docker' "$user" >&2 || return $?
echo "$user"
}

Expand Down Expand Up @@ -318,6 +320,7 @@ Environment variables:
MR_GIHUB_BASEURL=$MR_GIHUB_BASEURL
MR_GIHUB_API_BASEURL=$MR_GIHUB_API_BASEURL
MR_RELEASE_URL=${MR_RELEASE_URL:-<latest on github.com/actions/runner/releases>}
MR_USER_BASE=$MR_USER_BASE
MR_GITHUB_PAT=${MR_GITHUB_PAT::11}${MR_GITHUB_PAT:+***}
Sub-commands:
Expand All @@ -337,7 +340,6 @@ Options:
--org GitHub organization name
--repo GitHub repository name, registration on organization-level if empty
--user Linux local username of runner
--base Base directory for user home directories
--labels Extra labels for the runner
--group Runner group for the runner
--token Runner registration token, takes precedence over MR_GITHUB_PAT
Expand All @@ -353,7 +355,7 @@ function mr::main {
local org='' repo='' user='' labels='' token='' group='' dotenv=''

# parse options into variables
getopt_output="$(getopt -o h -l help,org:,repo:,base:,user:,labels:,token:,group:,dotenv: -n "$FILE_THIS" -- "$@")"
getopt_output="$(getopt -o h -l help,org:,repo:,user:,labels:,token:,group:,dotenv: -n "$FILE_THIS" -- "$@")"
log::failed $? "getopt failed!" || return $?
eval set -- "$getopt_output"

Expand All @@ -372,10 +374,6 @@ function mr::main {
user="$2"
shift 2
;;
--base)
base="$2"
shift 2
;;
--labels)
labels="$2"
shift 2
Expand Down

2 comments on commit 3394733

@rrauenza
Copy link
Contributor

@rrauenza rrauenza commented on 3394733 Jun 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! FYI, alternative approach I've been doing recently is using bash arrays to conditionally add more args...

if [ -n "$MR_USER_BASE" ]
then
SET_HOME_ARGS=(-b "$MR_USER_BASE")
else
SET_HOME_ARGS=()
fi

useradd "$SET_HOME_ARGS[@]" ...

Then you don't have to parse the useradd defaults file and can just conditionally add in arguments.

@vbem
Copy link
Owner Author

@vbem vbem commented on 3394733 Jun 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! FYI, alternative approach I've been doing recently is using bash arrays to conditionally add more args...

if [ -n "$MR_USER_BASE" ] then SET_HOME_ARGS=(-b "$MR_USER_BASE") else SET_HOME_ARGS=() fi

useradd "$SET_HOME_ARGS[@]" ...

Then you don't have to parse the useradd defaults file and can just conditionally add in arguments.

Cool! I've added this approach in 95b39d0.

Please sign in to comment.