Skip to content

Commit

Permalink
Improve logging: use identifier
Browse files Browse the repository at this point in the history
Improve logging so the identifier of the runner (locally and at GitHub)
is present in each log message to ease identification.
  • Loading branch information
efrecon committed Feb 11, 2024
1 parent 6d7b4fc commit a0a7d7d
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 18 deletions.
2 changes: 1 addition & 1 deletion base/base.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ BASE_DOCKER_WRAPPER=${BASE_DOCKER_WRAPPER:-$BASE_ROOTDIR/docker.sh}
. "$BASE_ROOTDIR/../lib/common.sh"

# shellcheck disable=SC2034 # Used in sourced scripts
KRUNVM_RUNNER_MAIN="Install a base GitHub runner environment in Fedora"
KRUNVM_RUNNER_DESCR="Install a base GitHub runner environment in Fedora"

while getopts "dl:vh-" opt; do
case "$opt" in
Expand Down
8 changes: 6 additions & 2 deletions lib/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ random_string() {
usage() {
# This uses the comments behind the options to show the help. Not extremly
# correct, but effective and simple.
echo "$0 -- ${KRUNVM_RUNNER_MAIN:-"Part of the gh-krunvm-runner project"}" && \
echo "$0 -- ${KRUNVM_RUNNER_DESCR:-"Part of the gh-krunvm-runner project"}" && \
grep "[[:space:]].) #" "$0" |
sed 's/#//' |
sed -r 's/([a-zA-Z-])\)/-\1/'
Expand Down Expand Up @@ -75,9 +75,13 @@ run_krunvm() {
_log() {
# Capture level and shift it away, rest will be passed blindly to printf
_lvl=${1:-LOG}; shift
if [ -z "${KRUNVM_RUNNER_BIN:-}" ]; then
KRUNVM_RUNNER_BIN=$(basename "$0")
KRUNVM_RUNNER_BIN=${KRUNVM_RUNNER_BIN%.sh}
fi
# shellcheck disable=SC2059 # We want to expand the format string
printf '[%s] [%s] [%s] %s\n' \
"$(basename "$0")" \
"${KRUNVM_RUNNER_BIN:-$(basename "$0")}" \
"$_lvl" \
"$(date +'%Y%m%d-%H%M%S')" \
"$(printf "$@")" \
Expand Down
5 changes: 3 additions & 2 deletions orchestrator.sh
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ RUNNER_UPDATE=${RUNNER_UPDATE:-"0"}
. "$ORCHESTRATOR_ROOTDIR/lib/common.sh"

# shellcheck disable=SC2034 # Used in sourced scripts
KRUNVM_RUNNER_MAIN="Run several krunvm-based GitHub runners on a single host"
KRUNVM_RUNNER_DESCR="Run several krunvm-based GitHub runners on a single host"


while getopts "c:d:D:g:G:i:Il:L:m:M:n:p:s:t:T:u:Uvh-" opt; do
Expand Down Expand Up @@ -238,7 +238,8 @@ for i in $(seq 1 "$runners"); do
"$ORCHESTRATOR_ROOTDIR/runner.sh" \
-n "$ORCHESTRATOR_NAME" \
-D "$ORCHESTRATOR_DIR" \
-E "${ORCHESTRATOR_ENVIRONMENT:-}" &
-E "${ORCHESTRATOR_ENVIRONMENT:-}" \
-- "$i" &
set -- "$@" "$!"
fi
done
Expand Down
21 changes: 15 additions & 6 deletions runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ RUNNER_EPHEMERAL=${RUNNER_EPHEMERAL:-"1"}
. "$RUNNER_ROOTDIR/lib/common.sh"

# shellcheck disable=SC2034 # Used in sourced scripts
KRUNVM_RUNNER_MAIN="Create runners forever using krunvm"
KRUNVM_RUNNER_DESCR="Create runners forever using krunvm"


while getopts "D:E:g:G:l:L:M:n:p:s:T:u:Uvh-" opt; do
Expand Down Expand Up @@ -118,6 +118,8 @@ while getopts "D:E:g:G:l:L:M:n:p:s:T:u:Uvh-" opt; do
RUNNER_VERBOSE=$((RUNNER_VERBOSE+1));;
h) # Print help and exit
usage;;
-) # End of options, follows the identifier of the runner, if any
break;;
?)
usage 1;;
esac
Expand All @@ -127,6 +129,11 @@ shift $((OPTIND-1))
# Pass logging configuration and level to imported scripts
KRUNVM_RUNNER_LOG=$RUNNER_LOG
KRUNVM_RUNNER_VERBOSE=$RUNNER_VERBOSE
loop=${1:-}
if [ -n "${loop:-}" ]; then
KRUNVM_RUNNER_BIN=$(basename "$0")
KRUNVM_RUNNER_BIN="${KRUNVM_RUNNER_BIN%.sh}-$loop"
fi

# Decide which runner.sh implementation (this is the "entrypoint" of the
# microVM) to use: the one from the mount point, or the built-in one.
Expand All @@ -138,25 +145,27 @@ else
fi

while true; do
verbose "Starting microVM $RUNNER_NAME to run an ephemeral GitHub runner"
id=$(random_string)
RUNNER_ID=${loop}-${id}
verbose "Starting microVM $RUNNER_NAME to run ephemeral GitHub runner $RUNNER_ID"
if [ -n "$RUNNER_ENVIRONMENT" ]; then
# Create an env file with most of the RUNNER_ variables. This works because
# the `runner.sh` script that will be called uses the same set of variables.
id=$(random_string)
verbose "Creating isolation environment ${RUNNER_ENVIRONMENT}/${id}.env"
verbose "Creating isolation environment ${RUNNER_ENVIRONMENT}/${RUNNER_ID}.env"
while IFS= read -r varset; do
# shellcheck disable=SC2163 # We want to expand the variable
printf '%s\n' "$varset" >> "${RUNNER_ENVIRONMENT}/${id}.env"
printf '%s\n' "$varset" >> "${RUNNER_ENVIRONMENT}/${RUNNER_ID}.env"
done <<EOF
$(set | grep '^RUNNER_' | grep -vE '(ROOTDIR|ENVIRONMENT|NAME|MOUNT)')
EOF

set -- -E "/_environment/${id}.env"
set -- -E "/_environment/${RUNNER_ID}.env"
else
set -- \
-e \
-g "$RUNNER_GITHUB" \
-G "$RUNNER_GROUP" \
-i "$RUNNER_ID" \
-l "$RUNNER_LOG" \
-L "$RUNNER_LABELS" \
-p "$RUNNER_PRINCIPAL" \
Expand Down
2 changes: 1 addition & 1 deletion runner/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ INSTALL_ROOTDIR=$( cd -P -- "$(dirname -- "$(command -v -- "$(abspath "$0")")")"
. "$INSTALL_ROOTDIR/../lib/common.sh"

# shellcheck disable=SC2034 # Used in sourced scripts
KRUNVM_RUNNER_MAIN="Install the GitHub runner"
KRUNVM_RUNNER_DESCR="Install the GitHub runner"

while getopts "l:u:vh-" opt; do
case "$opt" in
Expand Down
22 changes: 17 additions & 5 deletions runner/runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,19 @@ RUNNER_UPDATE=${RUNNER_UPDATE:-"0"}
# options!). The environment file is automatically removed after reading.
RUNNER_ENVFILE=${RUNNER_ENVFILE:-""}

# Identifier of the runner (used in logs)
RUNNER_ID=${RUNNER_ID:-""}

RUNNER_TOOL_CACHE=${RUNNER_TOOL_CACHE:-"${AGENT_TOOLSDIRECTORY:-"/opt/hostedtoolcache"}"}

# shellcheck source=../lib/common.sh
. "$RUNNER_ROOTDIR/../lib/common.sh"

# shellcheck disable=SC2034 # Used in sourced scripts
KRUNVM_RUNNER_MAIN="Configure and run the installed GitHub runner"
KRUNVM_RUNNER_DESCR="Configure and run the installed GitHub runner"


while getopts "eE:g:G:l:L:n:p:s:t:T:u:Uvh-" opt; do
while getopts "eE:g:G:i:l:L:n:p:s:t:T:u:Uvh-" opt; do
case "$opt" in
e) # Ephemeral runner
RUNNER_EPHEMERAL=1;;
Expand All @@ -102,11 +105,13 @@ while getopts "eE:g:G:l:L:n:p:s:t:T:u:Uvh-" opt; do
RUNNER_GITHUB="$OPTARG";;
G) # Group to attach the runner to
RUNNER_GROUP="$OPTARG";;
i) # Identifier of the runner (used in logs and to name the runner)
RUNNER_ID="$OPTARG";;
l) # Where to send logs
RUNNER_LOG="$OPTARG";;
L) # Comma separated list of labels to attach to the runner
RUNNER_LABELS="$OPTARG";;
n) # Name of the runner to register (random prefixed name will be used if not set)
n) # Name of the runner to register (id or random prefixed name will be used if not set)
RUNNER_NAME="$OPTARG";;
p) # Principal to authorise the runner for, name of repo, org or enterprise
RUNNER_PRINCIPAL="$OPTARG";;
Expand All @@ -124,7 +129,7 @@ while getopts "eE:g:G:l:L:n:p:s:t:T:u:Uvh-" opt; do
RUNNER_VERBOSE=$((RUNNER_VERBOSE+1));;
h) # Print help and exit
usage;;
-) # End of options, everything is executed as a command, as relevant user
-) # End of options, everything after is executed as a command, as relevant user
break;;
?)
usage 1;;
Expand All @@ -135,6 +140,11 @@ shift $((OPTIND-1))
# Pass logging configuration and level to imported scripts
KRUNVM_RUNNER_LOG=$RUNNER_LOG
KRUNVM_RUNNER_VERBOSE=$RUNNER_VERBOSE
if [ -z "$RUNNER_ID" ]; then
RUNNER_ID=$(random_string)
fi
KRUNVM_RUNNER_BIN=$(basename "$0")
KRUNVM_RUNNER_BIN="${KRUNVM_RUNNER_BIN%.sh}-$RUNNER_ID"

configure() {
verbose "Registering $RUNNER_SCOPE runner '$RUNNER_NAME' for $RUNNER_URL"
Expand Down Expand Up @@ -227,6 +237,8 @@ if [ -n "$RUNNER_ENVFILE" ]; then
# might have modified in the .env file.
KRUNVM_RUNNER_LOG=$RUNNER_LOG
KRUNVM_RUNNER_VERBOSE=$RUNNER_VERBOSE
KRUNVM_RUNNER_BIN=$(basename "$0")
KRUNVM_RUNNER_BIN="${KRUNVM_RUNNER_BIN%.sh}-$RUNNER_ID"

# Remove the environment file, as it is not needed anymore and might
# contains secrets.
Expand Down Expand Up @@ -254,7 +266,7 @@ debug "Setting up missing defaults"
distro=$(get_env "/etc/os-release" "ID")
RUNNER_DISTRO=${RUNNER_DISTRO:-"${distro:-"unknown}"}"}
RUNNER_NAME_PREFIX=${RUNNER_NAME_PREFIX:-"${RUNNER_DISTRO}-krunvm"}
RUNNER_NAME=${RUNNER_NAME:-"${RUNNER_NAME_PREFIX}-$(random_string)"}
RUNNER_NAME=${RUNNER_NAME:-"${RUNNER_NAME_PREFIX}-$RUNNER_ID"}

RUNNER_WORKDIR=${RUNNER_WORKDIR:-"/_work/${RUNNER_NAME}"}
if [ -n "${distro:-}" ]; then
Expand Down
2 changes: 1 addition & 1 deletion runner/token.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ TOKEN_PRINCIPAL=${TOKEN_PRINCIPAL:-""}
. "$TOKEN_ROOTDIR/../lib/common.sh"

# shellcheck disable=SC2034 # Used in sourced scripts
KRUNVM_RUNNER_MAIN="Acquire a runner token from GitHub API"
KRUNVM_RUNNER_DESCR="Acquire a runner token from GitHub API"

while getopts "g:l:p:s:T:vh-" opt; do
case "$opt" in
Expand Down

0 comments on commit a0a7d7d

Please sign in to comment.