diff --git a/base/root/etc/containers/containers.conf b/base/root/etc/containers/containers.conf index 016cace..5661461 100644 --- a/base/root/etc/containers/containers.conf +++ b/base/root/etc/containers/containers.conf @@ -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). diff --git a/lib/common.sh b/lib/common.sh index 11f3cf4..6995d3a 100644 --- a/lib/common.sh +++ b/lib/common.sh @@ -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 } diff --git a/runner/runner.sh b/runner/runner.sh index 8a9d7f3..56b9516 100755 --- a/runner/runner.sh +++ b/runner/runner.sh @@ -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" @@ -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" } @@ -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" } @@ -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" @@ -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 @@ -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 @@ -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 @@ -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