Skip to content

Commit

Permalink
Reroute runner&config's logs as well
Browse files Browse the repository at this point in the history
  • Loading branch information
efrecon committed Mar 8, 2024
1 parent 3bc2121 commit 545216b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
2 changes: 1 addition & 1 deletion base/root/etc/containers/containers.conf
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ netns = "host"
#env = []

# Define where event logs will be stored, when events_logger is "file".
events_logfile_path="/var/log/podman.log"
events_logfile_path = "/var/log/podmand.log"

# Sets the maximum size for events_logfile_path.
# The size can be b (bytes), k (kilobytes), m (megabytes), or g (gigabytes).
Expand Down
12 changes: 7 additions & 5 deletions lib/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,12 @@ sublog() {

# Then reroute its content through our logging printf style
tail -n +0 -f "$1" | while IFS= read -r line; do
printf '[%s] [%s] %s\n' \
"${2-"${KRUNVM_RUNNER_BIN:-$(basename "$0")}"}" \
"$(date +'%Y%m%d-%H%M%S')" \
"$line" \
>&"$KRUNVM_RUNNER_LOG"
if [ -n "$line" ]; then
printf '[%s] [%s] %s\n' \
"${2:-}@${KRUNVM_RUNNER_BIN:-$(basename "$0")}" \
"$(date +'%Y%m%d-%H%M%S')" \
"$line" \
>&"$KRUNVM_RUNNER_LOG"
fi
done
}
62 changes: 54 additions & 8 deletions runner/runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ RUNNER_ID=${RUNNER_ID:-""}
# Location of a file where to store the token
RUNNER_TOKENFILE=${RUNNER_TOKENFILE:-""}

# Location of the podman configuration files. This should match the content of
# the base/root directory of this repository.
RUNNER_CONTAINERS_CONFDIR=${RUNNER_CONTAINERS_CONFDIR:-"/etc/containers"}
RUNNER_CONTAINERS_CONF="${RUNNER_CONTAINERS_CONFDIR%/}/containers.conf"

# Location of the directory where the runner scripts and binaries will log
RUNNER_LOGDIR=${RUNNER_LOGDIR:-"/var/log/runner"}

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

Expand Down Expand Up @@ -265,6 +273,29 @@ runner_unregister() {
if [ "${1:-0}" = 1 ] && [ -n "${RUNNER_TOKENFILE:-}" ] && [ -n "${RUNNER_SECRET:-}" ]; then
printf %s\\n "$RUNNER_SECRET" > "${RUNNER_TOKENFILE%.*}.brk"
fi

# Remove any lingering sublog process, if any
if [ -n "${SUBLOG_PID:-}" ]; then
kill "$SUBLOG_PID" >/dev/null 2>&1 || true
fi
}


runner_log() {
SUBLOG_NAME=$1; shift

# Start the main program in the background, send its output to a log file
"$@" > "$RUNNER_LOGDIR/${SUBLOG_NAME}.log" 2>&1 &
PRG_PID=$!

# Start the sublog redirector in the background, save its PID
sublog "$RUNNER_LOGDIR/${SUBLOG_NAME}.log" "$SUBLOG_NAME" &
SUBLOG_PID=$!

# Wait for the main program to finish, and once it's gone, kill the sublog
# redirector
wait "$PRG_PID"
kill "$SUBLOG_PID"
}


Expand All @@ -275,8 +306,9 @@ runner_control() {
cd "$RUNNER_BINROOT"
script=./${1}; shift
check_command "$script"
script_name=$(basename "$script")
debug "Running $script $*"
RUNNER_ALLOW_RUNASROOT=1 "$script" "$@"
RUNNER_ALLOW_RUNASROOT=1 runner_log "${script_name%.sh}" "$script" "$@"
cd "$cwd"
}

Expand All @@ -297,8 +329,11 @@ docker_daemon() {
# to be able to access the socket.
chgrp "docker" /var/run/docker.sock
chmod g+rw /var/run/docker.sock
# Forwards podman's log file (must be same as in containers.conf)
sublog /var/log/podman.log podman &
# Forwards podman's log file (picked containers.conf)
logpath=$(grep "^events_logfile_path" "$RUNNER_CONTAINERS_CONF"|
cut -d = -f 2|
sed -E -e 's/^\s//g' -e 's/\s$//g' -e 's/^"//g' -e 's/"$//g')
[ -n "$logpath" ] && sublog "$logpath" podmand &
elif [ -n "$dockerd" ]; then
# For docker, the user must be in the docker group to access the daemon.
verbose "Starting $dockerd as a daemon"
Expand Down Expand Up @@ -399,6 +434,11 @@ case "$RUNNER_SCOPE" in
;;
esac

# Make directory for logging
mkdir -p "$RUNNER_LOGDIR"
chown "$RUNNER_USER" "$RUNNER_LOGDIR"
chmod g+rw "$RUNNER_LOGDIR"

# Install runner binaries into SEPARATE directory, then configure from there
runner_install
runner_configure
Expand All @@ -421,13 +461,14 @@ if is_true "$RUNNER_DOCKER"; then
fi

# Start the runner.
# TODO: send logs info files at $RUNNER_LOGDIR and sublog them
verbose "Starting runner as user '$RUNNER_USER' (current user=$(id -un)): $*"
RUNNER_PID=
case "$RUNNER_USER" in
root)
if [ "$(id -u)" = "0" ]; then
"$@" &
RUNNER_PID="$!"
"$@" > "$RUNNER_LOGDIR/runner.log" 2>&1 &
RUNNER_PID=$!
else
error "Cannot start runner as root from non-root user"
fi
Expand All @@ -437,10 +478,11 @@ case "$RUNNER_USER" in
if [ "$(id -u)" = "0" ]; then
verbose "Starting runner as $RUNNER_USER"
chown -R "$RUNNER_USER" "$RUNNER_WORKDIR"
runas "$@" &
RUNNER_PID="$!"
runas "$@" > "$RUNNER_LOGDIR/runner.log" 2>&1 &
RUNNER_PID=$!
elif [ "$(id -un)" = "$RUNNER_USER" ]; then
"$@"
"$@" > "$RUNNER_LOGDIR/runner.log" 2>&1 &
RUNNER_PID=$!
else
error "Cannot start runner as $RUNNER_USER from non-$RUNNER_USER user"
fi
Expand All @@ -451,6 +493,10 @@ case "$RUNNER_USER" in
esac

if [ -n "$RUNNER_PID" ]; then
# Start the sublog redirector in the background, save its PID
sublog "$RUNNER_LOGDIR/runner.log" "runner" &
SUBLOG_PID=$!

while [ -n "$(running "$RUNNER_PID")" ]; do
if [ -n "${RUNNER_TOKENFILE:-}" ] && [ -n "${RUNNER_SECRET:-}" ]; then
if [ -f "${RUNNER_TOKENFILE%.*}.trm" ]; then
Expand Down

0 comments on commit 545216b

Please sign in to comment.