Env-to-config works

This commit is contained in:
Merith-TK 2024-12-25 23:46:40 +00:00
parent 676f16b16a
commit e9634ef5fb
2 changed files with 86 additions and 100 deletions

View file

@ -2,7 +2,7 @@
set -e set -e
# Technically not nessecary but it cleans up the logs from having token/secret values # Technically not necessary, but it cleans up the logs from having token/secret values
run_command() { run_command() {
local cmd="$@" local cmd="$@"
# Replace any --token <value> or --secret <value> with [REDACTED] # Replace any --token <value> or --secret <value> with [REDACTED]
@ -26,92 +26,73 @@ fi
# Handle if `command` is passed, as command appends arguments to the entrypoint # Handle if `command` is passed, as command appends arguments to the entrypoint
if [ "$#" -gt 0 ]; then if [ "$#" -gt 0 ]; then
run_command $@ run_command $@
exit exit
fi fi
# Handle and alter the config file # Use environment variables directly, with fallback defaults if not set
if [[ -z "${CONFIG_FILE}" ]]; then RUNNER__CONFIG_FILE="${RUNNER__CONFIG_FILE:-/data/config.yml}"
echo "CONFIG_FILE is not set" ENV_FILE="${ENV_FILE:-/data/.env}"
CONFIG_FILE="/data/config.yml" RUNNER__RUNNER__FILE="${RUNNER__RUNNER__FILE:-/data/runner.json}"
fi
CONFIG_ARG="--config ${CONFIG_FILE}" # Set config arguments
CONFIG_ARG="--config ${RUNNER__CONFIG_FILE}"
# Container-specific environment variables
RUNNER__CONTAINER__NETWORK="${RUNNER__CONTAINER__NETWORK:-host}"
RUNNER__CONTAINER__ENABLE_IPV6="${RUNNER__CONTAINER__ENABLE_IPV6:-false}"
RUNNER__CONTAINER__PRIVILEGED="${RUNNER__CONTAINER__PRIVILEGED:-false}"
RUNNER__CONTAINER__LABELS="${RUNNER__CONTAINER__LABELS:-}"
RUNNER__CONTAINER__VALID_VOLUMES="${RUNNER__CONTAINER__VALID_VOLUMES:-}"
# Show config variables
decho "CONFIG: ${CONFIG_ARG}" decho "CONFIG: ${CONFIG_ARG}"
DOCKER_HOST=${DOCKER_HOST:-"tcp://docker:2367"} # Generate config if not found
DOCKER_CERT_PATH=${DOCKER_CERT_PATH:-"/certs/client"} if [[ ! -f "${RUNNER__CONFIG_FILE}" ]]; then
DOCKER_TLS_VERIFY=${DOCKER_TLS_VERIFY:-1} echo "Creating ${RUNNER__CONFIG_FILE}"
decho "DOCKER_HOST: ${DOCKER_HOST}" run_command "forgejo-runner generate-config > ${RUNNER__CONFIG_FILE}"
decho "DOCKER_CERT_PATH: ${DOCKER_CERT_PATH}"
decho "DOCKER_TLS_VERIFY: ${DOCKER_TLS_VERIFY}"
if [[ ! -f "${CONFIG_FILE}" ]]; then
echo "Creating ${CONFIG_FILE}"
run_command "forgejo-runner generate-config > ${CONFIG_FILE}"
# Remove test environment variables if they exist in the config file
sed -i "/^ A_TEST_ENV_NAME_1:/d" ${CONFIG_FILE}
sed -i "/^ A_TEST_ENV_NAME_2:/d" ${CONFIG_FILE}
# Apply default values for docker
sed -i "/^ labels:/c\ labels: [\"docker:docker://code.forgejo.org/oci/node:20-bookworm\", \"ubuntu-22.04:docker://catthehacker/ubuntu:act-22.04\"]" ${CONFIG_FILE}
sed -i "/^ network:/c\ network: host" ${CONFIG_FILE}
if [[ "${DOCKER_PRIVILEGED}" == "true" ]]; then
sed -i "/^ privileged:/c\ privileged: true" ${CONFIG_FILE}
sed -i "/^ options:/c\ options: -v /certs/client:/certs/client:ro" ${CONFIG_FILE}
sed -i "/^ valid_volumes:/c\ valid_volumes:\n - /certs/client" ${CONFIG_FILE}
sed -i "/^ envs:/c\ envs:\n DOCKER_HOST: ${DOCKER_HOST}\n DOCKER_TLS_VERIFY: ${DOCKER_TLS_VERIFY}\n DOCKER_CERT_PATH: ${DOCKER_CERT_PATH}" ${CONFIG_FILE}
fi
fi fi
ENV_FILE=${ENV_FILE:-"/data/.env"} # Use environment variables directly in the config, no need for sed edits
decho "ENV_FILE: ${ENV_FILE}" decho "Using config from: ${RUNNER__CONFIG_FILE}"
sed -i "/^ env_file:/c\ env_file: ${ENV_FILE}" ${CONFIG_FILE} decho "Using environment file: ${ENV_FILE}"
# Set extra arguments from environment variables
EXTRA_ARGS="" EXTRA_ARGS=""
if [[ ! -z "${RUNNER_LABELS}" ]]; then if [[ -n "${RUNNER__CONTAINER__LABELS}" ]]; then
EXTRA_ARGS="${EXTRA_ARGS} --labels ${RUNNER_LABELS}" EXTRA_ARGS="${EXTRA_ARGS} --labels ${RUNNER__CONTAINER__LABELS}"
fi fi
decho "EXTRA_ARGS: ${EXTRA_ARGS}" decho "EXTRA_ARGS: ${EXTRA_ARGS}"
# Set the runner file
RUNNER_FILE=${RUNNER_FILE:-"runner.json"} # use json so editors know how to highlight
decho "RUNNER_FILE: ${RUNNER_FILE}"
sed -i "/^ file:/c\ file: ${RUNNER_FILE}" ${CONFIG_FILE}
if [[ "${SKIP_WAIT}" != "true" ]]; then if [[ "${SKIP_WAIT}" != "true" ]]; then
echo "Waiting 10s to allow other services to start up..." echo "Waiting 10s to allow other services to start up..."
sleep 10 sleep 10
fi fi
if [[ ! -s "${RUNNER_FILE}" ]]; then # Try to register the runner
touch ${RUNNER_FILE} if [[ ! -s "${RUNNER__RUNNER__FILE}" ]]; then
touch ${RUNNER__RUNNER__FILE}
try=$((try + 1)) try=$((try + 1))
success=0 success=0
decho "try: ${try}, success: ${success}" decho "try: ${try}, success: ${success}"
# The point of this loop is to make it simple, when running both forgejo-runner and gitea in docker,
# for the forgejo-runner to wait a moment for gitea to become available before erroring out. Within
# the context of a single docker-compose, something similar could be done via healthchecks, but
# this is more flexible.
while [[ $success -eq 0 ]] && [[ $try -lt ${MAX_REG_ATTEMPTS:-10} ]]; do while [[ $success -eq 0 ]] && [[ $try -lt ${MAX_REG_ATTEMPTS:-10} ]]; do
if [[ ! -z "${FORGEJO_SECRET}" ]]; then if [[ -n "${FORGEJO_SECRET}" ]]; then
run_command forgejo-runner create-runner-file --connect \ run_command forgejo-runner create-runner-file --connect \
--instance "${FORGEJO_URL:-http://forgejo:3000}" \ --instance "${FORGEJO_URL:-http://forgejo:3000}" \
--name "${RUNNER_NAME:-$(hostname)}" \ --name "${RUNNER_NAME:-$(hostname)}" \
--secret "${FORGEJO_SECRET}" \ --secret "${FORGEJO_SECRET}" \
${CONFIG_ARG}\ ${CONFIG_ARG} \
${EXTRA_ARGS} 2>&1 | tee /tmp/reg.log ${EXTRA_ARGS} 2>&1 | tee /tmp/reg.log
else else
run_command forgejo-runner register \ run_command forgejo-runner register \
--instance "${FORGEJO_URL:-http://forgejo:3000}" \ --instance "${FORGEJO_URL:-http://forgejo:3000}" \
--name "${RUNNER_NAME:-$(hostname)}" \ --name "${RUNNER_NAME:-$(hostname)}" \
--token "${RUNNER_TOKEN}" \ --token "${RUNNER_TOKEN}" \
--no-interactive \ --no-interactive \
${CONFIG_ARG}\ ${CONFIG_ARG} \
${EXTRA_ARGS} 2>&1 | tee /tmp/reg.log ${EXTRA_ARGS} 2>&1 | tee /tmp/reg.log
fi fi
cat /tmp/reg.log | grep -E 'connection successful|registered successfully' >/dev/null cat /tmp/reg.log | grep -E 'connection successful|registered successfully' >/dev/null
if [[ $? -eq 0 ]]; then if [[ $? -eq 0 ]]; then

View file

@ -5,12 +5,6 @@ import (
"strconv" "strconv"
"strings" "strings"
"time" "time"
log "github.com/sirupsen/logrus"
)
var (
envPrefix = "RUNNER__"
) )
// loadEnvVars loads configuration settings from environment variables // loadEnvVars loads configuration settings from environment variables
@ -20,42 +14,47 @@ func loadEnvVars(config *Config) {
// This implementation causes env-vars to override config file settings, // This implementation causes env-vars to override config file settings,
// without writing to the config file. // without writing to the config file.
// SkipEnv if variable is set
if _, ok := os.LookupEnv("RUNNER__SKIP_ENV"); ok {
return
}
// Log // Log
loadEnvStr(config, envPrefix+"LOG__LEVEL", &config.Log.Level) loadEnvStr(config, "RUNNER__LOG__LEVEL", &config.Log.Level)
loadEnvStr(config, envPrefix+"LOG__JOB_LEVEL", &config.Log.JobLevel) loadEnvStr(config, "RUNNER__LOG__JOB_LEVEL", &config.Log.JobLevel)
// Runner // Runner
loadEnvStr(config, envPrefix+"RUNNER__FILE", &config.Runner.File) loadEnvStr(config, "RUNNER__RUNNER__FILE", &config.Runner.File)
loadEnvInt(config, envPrefix+"RUNNER__CAPACITY", &config.Runner.Capacity) loadEnvInt(config, "RUNNER__RUNNER__CAPACITY", &config.Runner.Capacity)
loadEnvTable(config, envPrefix+"RUNNER__ENVS", &config.Runner.Envs) loadEnvTable(config, "RUNNER__RUNNER__ENVS", &config.Runner.Envs)
loadEnvStr(config, envPrefix+"RUNNER__ENV_FILE", &config.Runner.EnvFile) loadEnvStr(config, "RUNNER__RUNNER__ENV_FILE", &config.Runner.EnvFile)
loadEnvDuration(config, envPrefix+"RUNNER__SHUTDOWN_TIMEOUT", &config.Runner.ShutdownTimeout) loadEnvDuration(config, "RUNNER__RUNNER__SHUTDOWN_TIMEOUT", &config.Runner.ShutdownTimeout)
loadEnvBool(config, envPrefix+"RUNNER__INSECURE", &config.Runner.Insecure) loadEnvBool(config, "RUNNER__RUNNER__INSECURE", &config.Runner.Insecure)
loadEnvDuration(config, envPrefix+"RUNNER__FETCH_TIMEOUT", &config.Runner.FetchTimeout) loadEnvDuration(config, "RUNNER__RUNNER__FETCH_TIMEOUT", &config.Runner.FetchTimeout)
loadEnvDuration(config, envPrefix+"RUNNER__FETCH_INTERVAL", &config.Runner.FetchInterval) loadEnvDuration(config, "RUNNER__RUNNER__FETCH_INTERVAL", &config.Runner.FetchInterval)
loadEnvDuration(config, envPrefix+"RUNNER__REPORT_INTERVAL", &config.Runner.ReportInterval) loadEnvDuration(config, "RUNNER__RUNNER__REPORT_INTERVAL", &config.Runner.ReportInterval)
loadEnvList(config, envPrefix+"RUNNER__LABELS", &config.Runner.Labels) loadEnvList(config, "RUNNER__RUNNER__LABELS", &config.Runner.Labels)
// Cache // Cache
loadEnvBool(config, envPrefix+"CACHE__ENABLED", config.Cache.Enabled) loadEnvBool(config, "RUNNER__CACHE__ENABLED", config.Cache.Enabled)
loadEnvStr(config, envPrefix+"CACHE__DIR", &config.Cache.Dir) loadEnvStr(config, "RUNNER__CACHE__DIR", &config.Cache.Dir)
loadEnvStr(config, envPrefix+"CACHE__HOST", &config.Cache.Host) loadEnvStr(config, "RUNNER__CACHE__HOST", &config.Cache.Host)
loadEnvUInt16(config, envPrefix+"CACHE__PORT", &config.Cache.Port) loadEnvUInt16(config, "RUNNER__CACHE__PORT", &config.Cache.Port)
loadEnvStr(config, envPrefix+"CACHE__EXTERNAL_SERVER", &config.Cache.ExternalServer) loadEnvStr(config, "RUNNER__CACHE__EXTERNAL_SERVER", &config.Cache.ExternalServer)
// Container // Container
loadEnvStr(config, envPrefix+"CONTAINER__NETWORK", &config.Container.Network) loadEnvStr(config, "RUNNER__CONTAINER__NETWORK", &config.Container.Network)
loadEnvStr(config, envPrefix+"CONTAINER__NETWORK_MODE", &config.Container.NetworkMode) loadEnvStr(config, "RUNNER__CONTAINER__NETWORK_MODE", &config.Container.NetworkMode)
loadEnvBool(config, envPrefix+"CONTAINER__ENABLE_IPV6", &config.Container.EnableIPv6) loadEnvBool(config, "RUNNER__CONTAINER__ENABLE_IPV6", &config.Container.EnableIPv6)
loadEnvBool(config, envPrefix+"CONTAINER__PRIVILEGED", &config.Container.Privileged) loadEnvBool(config, "RUNNER__CONTAINER__PRIVILEGED", &config.Container.Privileged)
loadEnvStr(config, envPrefix+"CONTAINER__OPTIONS", &config.Container.Options) loadEnvStr(config, "RUNNER__CONTAINER__OPTIONS", &config.Container.Options)
loadEnvStr(config, envPrefix+"CONTAINER__WORKDIR_PARENT", &config.Container.WorkdirParent) loadEnvStr(config, "RUNNER__CONTAINER__WORKDIR_PARENT", &config.Container.WorkdirParent)
loadEnvList(config, envPrefix+"CONTAINER__VALID_VOLUMES", &config.Container.ValidVolumes) loadEnvList(config, "RUNNER__CONTAINER__VALID_VOLUMES", &config.Container.ValidVolumes)
loadEnvStr(config, envPrefix+"CONTAINER__DOCKER_HOST", &config.Container.DockerHost) loadEnvStr(config, "RUNNER__CONTAINER__DOCKER_HOST", &config.Container.DockerHost)
loadEnvBool(config, envPrefix+"CONTAINER__FORCE_PULL", &config.Container.ForcePull) loadEnvBool(config, "RUNNER__CONTAINER__FORCE_PULL", &config.Container.ForcePull)
// Host // Host
loadEnvStr(config, envPrefix+"HOST__WORKDIR_PARENT", &config.Host.WorkdirParent) loadEnvStr(config, "RUNNER__HOST__WORKDIR_PARENT", &config.Host.WorkdirParent)
} }
// loadEnvStr loads an environment variable into the provided string pointer if it exists. // loadEnvStr loads an environment variable into the provided string pointer if it exists.
@ -108,10 +107,16 @@ func loadEnvBool(config *Config, key string, dest *bool) {
// loadEnvTable loads an environment variable into the provided map[string]string pointer if it exists. // loadEnvTable loads an environment variable into the provided map[string]string pointer if it exists.
func loadEnvTable(config *Config, key string, dest *map[string]string) { func loadEnvTable(config *Config, key string, dest *map[string]string) {
// This is a placeholder function. // Example: RUNNER__RUNNER__ENVS = "key1=value1, key2=value2, key3=value3"
// This function is not yet implemented. if v := os.Getenv(key); v != "" {
// This function is meant to be replaced by those more competent than me. *dest = make(map[string]string)
log.Warn("Loading Tables from env is not yet implemented: ", key) for _, pair := range splitAndTrim(v) {
kv := strings.SplitN(pair, "=", 2)
if len(kv) == 2 {
(*dest)[kv[0]] = kv[1]
}
}
}
} }
// loadEnvList loads an environment variable into the provided []string pointer if it exists. // loadEnvList loads an environment variable into the provided []string pointer if it exists.