Skip to content

Commit

Permalink
Create and use library of common functions
Browse files Browse the repository at this point in the history
  • Loading branch information
efrecon committed Feb 9, 2024
1 parent d970a56 commit bc164ea
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 145 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ ARG INSTALL_VERSION=latest
ARG INSTALL_NAMESPACE=/opt/gh-runner-krunvm

COPY runner/*.sh ${INSTALL_NAMESPACE}/bin/
# Redundant, but this makes this image more standalone.
COPY lib/*.sh ${INSTALL_NAMESPACE}/lib/
RUN chmod a+x ${INSTALL_NAMESPACE}/bin/*.sh \
&& "${INSTALL_NAMESPACE}/bin/install.sh" -v -l /dev/stdout

Expand Down
1 change: 1 addition & 0 deletions Dockerfile.base
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ FROM fedora:${FEDORA_VERSION}
ARG INSTALL_NAMESPACE=/opt/gh-runner-krunvm

COPY base/*.sh ${INSTALL_NAMESPACE}/bin/
COPY lib/*.sh ${INSTALL_NAMESPACE}/lib/
RUN chmod a+x "${INSTALL_NAMESPACE}/bin/base.sh" \
&& "${INSTALL_NAMESPACE}/bin/base.sh" -dv -l /dev/stdout
COPY base/root/ /
49 changes: 10 additions & 39 deletions base/base.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,20 @@ BASE_UID=${BASE_UID:-1001}
BASE_GROUP=${BASE_GROUP:-runner}
BASE_GID=${BASE_GID:-121}

# Name of the "sudo" group
# Name of the "sudo" group - wheel on Fedora, sudo on Ubuntu
BASE_SUDO=${BASE_SUDO:-"wheel"}

# Resolve the root directory hosting this script to an absolute path, symbolic
# links resolved.
BASE_ROOTDIR=$( cd -P -- "$(dirname -- "$(command -v -- "$(abspath "$0")")")" && pwd -P )

BASE_DOCKER_WRAPPER=${BASE_DOCKER_WRAPPER:-$BASE_ROOTDIR/docker.sh}

usage() {
# This uses the comments behind the options to show the help. Not extremly
# correct, but effective and simple.
echo "$0 installs a base GitHub runner environment in Fedora" && \
grep "[[:space:]].)\ #" "$0" |
sed 's/#//' |
sed -r 's/([a-z-])\)/-\1/'
exit "${1:-0}"
}
# shellcheck source=../lib/common.sh
. "$BASE_ROOTDIR/../lib/common.sh"

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

while getopts "dl:vh-" opt; do
case "$opt" in
Expand All @@ -77,36 +74,10 @@ while getopts "dl:vh-" opt; do
done
shift $((OPTIND-1))

# PML: Poor Man's Logging
_log() {
printf '[%s] [%s] [%s] %s\n' \
"$(basename "$0")" \
"${2:-LOG}" \
"$(date +'%Y%m%d-%H%M%S')" \
"${1:-}" \
>&"$BASE_LOG"
}
trace() { if [ "${BASE_VERBOSE:-0}" -ge "3" ]; then _log "$1" TRC; fi; }
debug() { if [ "${BASE_VERBOSE:-0}" -ge "2" ]; then _log "$1" DBG; fi; }
verbose() { if [ "${BASE_VERBOSE:-0}" -ge "1" ]; then _log "$1" NFO; fi; }
warn() { _log "$1" WRN; }
error() { _log "$1" ERR && exit 1; }

# Find the executable passed as an argument in the PATH variable and print it.
find_exec() {
while IFS= read -r dir; do
if [ -n "${dir}" ] && [ -d "${dir}" ]; then
if [ -x "${dir%/}/$1" ] && [ "${dir%/}/$1" != "$(abspath "$0")" ]; then
printf %s\\n "${dir%/}/$1"
break
fi
fi
done <<EOF
$(printf %s\\n "$PATH"|tr ':' '\n')
EOF
}
# Pass logging configuration and level to imported scripts
KRUNVM_RUNNER_LOG=$BASE_LOG
KRUNVM_RUNNER_VERBOSE=$BASE_VERBOSE

# TODO: one of gosu or su-exec to drop privileges and run as the `runner` user
# TODO: locales?
verbose "Installing base packages"
dnf -y install \
Expand Down
64 changes: 64 additions & 0 deletions lib/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/bin/sh


# Find the executable passed as an argument in the PATH variable and print it.
find_exec() {
while IFS= read -r dir; do
if [ -n "${dir}" ] && [ -d "${dir}" ]; then
if [ -x "${dir%/}/$1" ] && [ "${dir%/}/$1" != "$(abspath "$0")" ]; then
printf %s\\n "${dir%/}/$1"
break
fi
fi
done <<EOF
$(printf %s\\n "$PATH"|tr ':' '\n')
EOF
}

# shellcheck disable=SC2120 # Take none or one argument
to_lower() {
if [ -z "${1:-}" ]; then
tr '[:upper:]' '[:lower:]'
else
printf %s\\n "$1" | to_lower
fi
}

is_true() {
case "$(to_lower "${1:-}")" in
1 | true | yes | y | on | t) return 0;;
*) return 1;;
esac
}

# shellcheck disable=SC2120 # Function has good default.
random_string() {
LC_ALL=C tr -dc 'a-zA-Z0-9' < /dev/urandom | head -c "${1:-12}"
}

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"}" && \
grep "[[:space:]].) #" "$0" |
sed 's/#//' |
sed -r 's/([a-z-])\)/-\1/'
exit "${1:-0}"
}

# PML: Poor Man's Logging
_log() {
printf '[%s] [%s] [%s] %s\n' \
"$(basename "$0")" \
"${2:-LOG}" \
"$(date +'%Y%m%d-%H%M%S')" \
"${1:-}" \
>&"$KRUNVM_RUNNER_LOG"
}
trace() { if [ "${KRUNVM_RUNNER_VERBOSE:-0}" -ge "3" ]; then _log "$1" TRC; fi; }
debug() { if [ "${KRUNVM_RUNNER_VERBOSE:-0}" -ge "2" ]; then _log "$1" DBG; fi; }
verbose() { if [ "${KRUNVM_RUNNER_VERBOSE:-0}" -ge "1" ]; then _log "$1" NFO; fi; }
info() { if [ "${KRUNVM_RUNNER_VERBOSE:-0}" -ge "1" ]; then _log "$1" NFO; fi; }
warn() { _log "$1" WRN; }
error() { _log "$1" ERR && exit 1; }

57 changes: 34 additions & 23 deletions runner/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,28 @@
# Shell sanity. Stop on errors and undefined variables.
set -eu

# This is a readlink -f implementation so this script can (perhaps) run on MacOS
abspath() {
is_abspath() {
case "$1" in
/* | ~*) true;;
*) false;;
esac
}

if [ -d "$1" ]; then
( cd -P -- "$1" && pwd -P )
elif [ -L "$1" ]; then
if is_abspath "$(readlink "$1")"; then
abspath "$(readlink "$1")"
else
abspath "$(dirname "$1")/$(readlink "$1")"
fi
else
printf %s\\n "$(abspath "$(dirname "$1")")/$(basename "$1")"
fi
}

# Level of verbosity, the higher the more verbose. All messages are sent to the
# stderr.
INSTALL_VERBOSE=${INSTALL_VERBOSE:-0}
Expand All @@ -24,15 +46,15 @@ INSTALL_DIRECTORIES=${INSTALL_DIRECTORIES:-"/_work $INSTALL_TOOL_CACHE $INSTALL_
# User to change ownership of directories to
INSTALL_USER=${INSTALL_USER:-runner}

usage() {
# This uses the comments behind the options to show the help. Not extremly
# correct, but effective and simple.
echo "$0 install the GitHub runner" && \
grep "[[:space:]].)\ #" "$0" |
sed 's/#//' |
sed -r 's/([a-z-])\)/-\1/'
exit "${1:-0}"
}
# Resolve the root directory hosting this script to an absolute path, symbolic
# links resolved.
INSTALL_ROOTDIR=$( cd -P -- "$(dirname -- "$(command -v -- "$(abspath "$0")")")" && pwd -P )

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

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

while getopts "l:u:vh-" opt; do
case "$opt" in
Expand All @@ -52,20 +74,9 @@ while getopts "l:u:vh-" opt; do
done
shift $((OPTIND-1))

# PML: Poor Man's Logging
_log() {
printf '[%s] [%s] [%s] %s\n' \
"$(basename "$0")" \
"${2:-LOG}" \
"$(date +'%Y%m%d-%H%M%S')" \
"${1:-}" \
>&"$INSTALL_LOG"
}
trace() { if [ "${INSTALL_VERBOSE:-0}" -ge "3" ]; then _log "$1" TRC; fi; }
debug() { if [ "${INSTALL_VERBOSE:-0}" -ge "2" ]; then _log "$1" DBG; fi; }
verbose() { if [ "${INSTALL_VERBOSE:-0}" -ge "1" ]; then _log "$1" NFO; fi; }
warn() { _log "$1" WRN; }
error() { _log "$1" ERR && exit 1; }
# Pass logging configuration and level to imported scripts
KRUNVM_RUNNER_LOG=$INSTALL_LOG
KRUNVM_RUNNER_VERBOSE=$INSTALL_VERBOSE

# Collect the version to install from the environment or the first argument.
INSTALL_VERSION=${INSTALL_VERSION:-${1:-latest}}
Expand Down
69 changes: 9 additions & 60 deletions runner/runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ abspath() {
fi
}

# shellcheck disable=SC2120 # Function has good default.
random_string() {
LC_ALL=C tr -dc 'a-zA-Z0-9' < /dev/urandom | head -c "${1:-12}"
}

# Resolve the root directory hosting this script to an absolute path, symbolic
# links resolved.
RUNNER_ROOTDIR=$( cd -P -- "$(dirname -- "$(command -v -- "$(abspath "$0")")")" && pwd -P )
Expand All @@ -57,7 +52,7 @@ RUNNER_LABELS=${RUNNER_LABELS:-""}
# Name of the user to run the runner as, defaults to root. User must exist.
RUNNER_USER=${RUNNER_USER:-"root"}

# One of repo, org or enterprise
# Scope of the runner, one of: repo, org or enterprise
RUNNER_SCOPE=${RUNNER_SCOPE:-"repo"}

# Name of organisation, enterprise or repo to attach the runner to, when
Expand All @@ -84,19 +79,14 @@ RUNNER_INSTALL=${RUNNER_INSTALL:-"/opt/actions-runner"}
# Should the runner auto-update
RUNNER_UPDATE=${RUNNER_UPDATE:-"0"}


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"

usage() {
# This uses the comments behind the options to show the help. Not extremly
# correct, but effective and simple.
echo "$0 configure and run the installed GitHub runner" && \
grep "[[:space:]].) #" "$0" |
sed 's/#//' |
sed -r 's/([a-zA-Z-])\)/-\1/'
exit "${1:-0}"
}

while getopts "eg:G:l:L:n:p:s:t:T:u:Uvh-" opt; do
case "$opt" in
Expand Down Expand Up @@ -136,36 +126,9 @@ while getopts "eg:G:l:L:n:p:s:t:T:u:Uvh-" opt; do
done
shift $((OPTIND-1))

# PML: Poor Man's Logging
_log() {
printf '[%s] [%s] [%s] %s\n' \
"$(basename "$0")" \
"${2:-LOG}" \
"$(date +'%Y%m%d-%H%M%S')" \
"${1:-}" \
>&"$RUNNER_LOG"
}
trace() { if [ "${RUNNER_VERBOSE:-0}" -ge "3" ]; then _log "$1" TRC; fi; }
debug() { if [ "${RUNNER_VERBOSE:-0}" -ge "2" ]; then _log "$1" DBG; fi; }
verbose() { if [ "${RUNNER_VERBOSE:-0}" -ge "1" ]; then _log "$1" NFO; fi; }
warn() { _log "$1" WRN; }
error() { _log "$1" ERR && exit 1; }

# shellcheck disable=SC2120 # Take none or one argument
to_lower() {
if [ -z "${1:-}" ]; then
tr '[:upper:]' '[:lower:]'
else
printf %s\\n "$1" | to_lower
fi
}

is_true() {
case "$(to_lower "${1:-}")" in
1 | true | yes | y | on | t) return 0;;
*) return 1;;
esac
}
# Pass logging configuration and level to imported scripts
KRUNVM_RUNNER_LOG=$RUNNER_LOG
KRUNVM_RUNNER_VERBOSE=$RUNNER_VERBOSE

configure() {
verbose "Registering $RUNNER_SCOPE runner '$RUNNER_NAME' for $RUNNER_URL"
Expand Down Expand Up @@ -231,20 +194,6 @@ unregister() {
}


# Find the executable passed as an argument in the PATH variable and print it.
find_exec() {
while IFS= read -r dir; do
if [ -n "${dir}" ] && [ -d "${dir}" ]; then
if [ -x "${dir%/}/$1" ] && [ "${dir%/}/$1" != "$(abspath "$0")" ]; then
printf %s\\n "${dir%/}/$1"
break
fi
fi
done <<EOF
$(printf %s\\n "$PATH"|tr ':' '\n')
EOF
}

runas() {
if [ "$(id -u)" = "0" ]; then
if command -v "runuser" >/dev/null 2>&1; then
Expand Down
Loading

0 comments on commit bc164ea

Please sign in to comment.