diff --git a/entrypoint.sh b/entrypoint.sh index 492ce47..4a3425f 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -30,21 +30,42 @@ if [ "$#" -gt 0 ]; then exit fi +# Set default values (if needed) +DEFAULT_DOCKER_HOST="tcp://docker:2376" +DEFAULT_DOCKER_TLS_VERIFY="1" +DEFAULT_DOCKER_CERT_PATH="/certs/client" + +# Ensure the variables are not empty by using explicit checks +DOCKER_HOST="${RUNNER__DOCKER_HOST:-${DOCKER_HOST:-${DEFAULT_DOCKER_HOST}}}" +DOCKER_TLS_VERIFY="${RUNNER__DOCKER_TLS_VERIFY:-${DOCKER_TLS_VERIFY:-${DEFAULT_DOCKER_TLS_VERIFY}}}" +DOCKER_CERT_PATH="${DOCKER_CERT_PATH:-${DEFAULT_DOCKER_CERT_PATH}}" + +RUNNER__container__DOCKER_HOST="${RUNNER__DOCKER_HOST:-${DOCKER_HOST:-${DEFAULT_DOCKER_HOST}}}" +RUNNER__runner__INSECURE="${RUNNER__DOCKER_TLS_VERIFY:-${DOCKER_TLS_VERIFY:-${DEFAULT_DOCKER_TLS_VERIFY}}}" + +RUNNER__container__NETWORK="${RUNNER__container__NETWORK:-host}" +RUNNER__container__OPTIONS="${RUNNER__container__OPTIONS:-} -v ${DOCKER_CERT_PATH}:${DOCKER_CERT_PATH}:ro" +RUNNER__container__VALID_VOLUMES="${RUNNER__container__VALID_VOLUMES:-} ${DOCKER_CERT_PATH}" +RUNNER__container__PRIVILEGED="${RUNNER__container__PRIVILEGED:-true}" + +RUNNER__runner__FILE="${RUNNER__runner__FILE:-/data/runner.json}" + +decho "DOCKER_HOST: ${DOCKER_HOST}" +decho "DOCKER_TLS_VERIFY: ${DOCKER_TLS_VERIFY}" +decho "DOCKER_CERT_PATH: ${DOCKER_CERT_PATH}" +decho "RUNNER__container__DOCKER_HOST: ${RUNNER__container__DOCKER_HOST}" +decho "RUNNER__runner__INSECURE: ${RUNNER__runner__INSECURE}" +decho "RUNNER__container__NETWORK: ${RUNNER__container__NETWORK}" +decho "RUNNER__container__OPTIONS: ${RUNNER__container__OPTIONS}" +decho "RUNNER__container__VALID_VOLUMES: ${RUNNER__container__VALID_VOLUMES}" +decho "RUNNER__container__PRIVILEGED: ${RUNNER__container__PRIVILEGED}" +decho "RUNNER__runner__FILE: ${RUNNER__runner__FILE}" + # Use environment variables directly, with fallback defaults if not set RUNNER__CONFIG_FILE="${RUNNER__CONFIG_FILE:-/data/config.yml}" ENV_FILE="${ENV_FILE:-/data/.env}" -RUNNER__RUNNER__FILE="${RUNNER__RUNNER__FILE:-/data/runner.json}" - # 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}" @@ -60,8 +81,8 @@ decho "Using environment file: ${ENV_FILE}" # Set extra arguments from environment variables EXTRA_ARGS="" -if [[ -n "${RUNNER__CONTAINER__LABELS}" ]]; then - EXTRA_ARGS="${EXTRA_ARGS} --labels ${RUNNER__CONTAINER__LABELS}" +if [[ -n "${RUNNER__container__LABELS}" ]]; then + EXTRA_ARGS="${EXTRA_ARGS} --labels ${RUNNER__container__LABELS}" fi decho "EXTRA_ARGS: ${EXTRA_ARGS}" @@ -71,8 +92,8 @@ if [[ "${SKIP_WAIT}" != "true" ]]; then fi # Try to register the runner -if [[ ! -s "${RUNNER__RUNNER__FILE}" ]]; then - touch ${RUNNER__RUNNER__FILE} +if [[ ! -s "${RUNNER__runner__FILE}" ]]; then + touch ${RUNNER__runner__FILE} try=$((try + 1)) success=0 decho "try: ${try}, success: ${success}" diff --git a/examples/docker-compose/compose-forgejo-and-runner.yml b/examples/docker-compose/compose-forgejo-and-runner.yml index 7ae268d..00a2c2f 100644 --- a/examples/docker-compose/compose-forgejo-and-runner.yml +++ b/examples/docker-compose/compose-forgejo-and-runner.yml @@ -78,20 +78,15 @@ services: FORGEJO_SECRET: "02f8e8ed1bd08d55338026d04b5513684ff23c1f" # shared secret, must match Forgejo's, overrides RUNNER_TOKEN RUNNER_TOKEN: ${RUNNER_TOKEN} # token obtained from Forgejo web interface - RUNNER__CONFIG_FILE: config.yml # defaults to /data/config.yml - RUNNER__NAME: forgejo-runner # defaults to forgejo-runner - RUNNER__RUNNER__FILE: .runner # defaults to /data/runner.json - - RUNNER__CONTAINER__PRIVILEGED: "true" # defaults to false for security reasons - RUNNER__CONTAINER_OPTIONS: -v /certs/client:/certs/client:ro # needed for TLS - RUNNER__CONTAINER__DOCKER_HOST: "tcp://docker:2376" # defaults to "-", an available docker host will automatically be found - RUNNER__RUNNER__INSECURE: false - RUNNER__RUNNER__ENVS: | - DOCKER_HOST=tcp://docker:2376 - DOCKER_TLS_VERIFY=1 - DOCKER_CERT_PATH=/certs/client # defaults to empty - RUNNER__RUNNER__LABELS: | - "docker:docker://code.forgejo.org/oci/node:20-bookworm" - - DEBUG: "true" # defaults to false, set to true to enable debug logging in the entrypoint - SKIP_WAIT: "false" # defaults to false, set to true to skip the 10 second wait to allow for forgejo and docker-in-docker to start + DOCKER_HOST: tcp://docker:2376 + DOCKER_TLS_VERIFY: 1 + DOCKER_CERT_PATH: /certs/client + + RUNNER__log__LEVEL: "debug" + RUNNER__container__PRIVILEGED: "true" + RUNNER__runner__LABELS: | + docker:docker://code.forgejo.org/oci/node:20-bookworm + + DEBUG: "true" + SKIP_WAIT: "false" + SLEEP_DEBUG: "false" # toggles wether to sleep indefinitely after starting the runner diff --git a/internal/pkg/config/config-env.go b/internal/pkg/config/config-env.go index 0664df2..3937a62 100644 --- a/internal/pkg/config/config-env.go +++ b/internal/pkg/config/config-env.go @@ -5,6 +5,8 @@ import ( "strconv" "strings" "time" + + log "github.com/sirupsen/logrus" ) // loadEnvVars loads configuration settings from environment variables @@ -14,53 +16,59 @@ func loadEnvVars(config *Config) { // This implementation causes env-vars to override config file settings, // without writing to the config file. - // SkipEnv if variable is set - if _, ok := os.LookupEnv("RUNNER__SKIP_ENV"); ok { - return + if debug, ok := os.LookupEnv("DEBUG"); ok && debug == "true" { + log.SetLevel(log.DebugLevel) + log.Debug("Debug logging enabled") } // Log - loadEnvStr(config, "RUNNER__LOG__LEVEL", &config.Log.Level) - loadEnvStr(config, "RUNNER__LOG__JOB_LEVEL", &config.Log.JobLevel) + loadEnvStr(config, "RUNNER__log__LEVEL", &config.Log.Level) + loadEnvStr(config, "RUNNER__log__JOB_LEVEL", &config.Log.JobLevel) // Runner - loadEnvStr(config, "RUNNER__RUNNER__FILE", &config.Runner.File) - loadEnvInt(config, "RUNNER__RUNNER__CAPACITY", &config.Runner.Capacity) - loadEnvTable(config, "RUNNER__RUNNER__ENVS", &config.Runner.Envs) - loadEnvStr(config, "RUNNER__RUNNER__ENV_FILE", &config.Runner.EnvFile) - loadEnvDuration(config, "RUNNER__RUNNER__SHUTDOWN_TIMEOUT", &config.Runner.ShutdownTimeout) - loadEnvBool(config, "RUNNER__RUNNER__INSECURE", &config.Runner.Insecure) - loadEnvDuration(config, "RUNNER__RUNNER__FETCH_TIMEOUT", &config.Runner.FetchTimeout) - loadEnvDuration(config, "RUNNER__RUNNER__FETCH_INTERVAL", &config.Runner.FetchInterval) - loadEnvDuration(config, "RUNNER__RUNNER__REPORT_INTERVAL", &config.Runner.ReportInterval) - loadEnvList(config, "RUNNER__RUNNER__LABELS", &config.Runner.Labels) + loadEnvStr(config, "RUNNER__runner__FILE", &config.Runner.File) + loadEnvInt(config, "RUNNER__runner__CAPACITY", &config.Runner.Capacity) + loadEnvTable(config, "RUNNER__runner__ENVS", &config.Runner.Envs) + loadEnvStr(config, "RUNNER__runner__ENV_FILE", &config.Runner.EnvFile) + loadEnvDuration(config, "RUNNER__runner__SHUTDOWN_TIMEOUT", &config.Runner.ShutdownTimeout) + loadEnvBool(config, "RUNNER__runner__INSECURE", &config.Runner.Insecure) + loadEnvDuration(config, "RUNNER__runner__FETCH_TIMEOUT", &config.Runner.FetchTimeout) + loadEnvDuration(config, "RUNNER__runner__FETCH_INTERVAL", &config.Runner.FetchInterval) + loadEnvDuration(config, "RUNNER__runner__REPORT_INTERVAL", &config.Runner.ReportInterval) + loadEnvList(config, "RUNNER__runner__LABELS", &config.Runner.Labels) // Cache - loadEnvBool(config, "RUNNER__CACHE__ENABLED", config.Cache.Enabled) - loadEnvStr(config, "RUNNER__CACHE__DIR", &config.Cache.Dir) - loadEnvStr(config, "RUNNER__CACHE__HOST", &config.Cache.Host) - loadEnvUInt16(config, "RUNNER__CACHE__PORT", &config.Cache.Port) - loadEnvStr(config, "RUNNER__CACHE__EXTERNAL_SERVER", &config.Cache.ExternalServer) + loadEnvBool(config, "RUNNER__cache__ENABLED", config.Cache.Enabled) + loadEnvStr(config, "RUNNER__cache__DIR", &config.Cache.Dir) + loadEnvStr(config, "RUNNER__cache__HOST", &config.Cache.Host) + loadEnvUInt16(config, "RUNNER__cache__PORT", &config.Cache.Port) + loadEnvStr(config, "RUNNER__cache__EXTERNAL_SERVER", &config.Cache.ExternalServer) // Container - loadEnvStr(config, "RUNNER__CONTAINER__NETWORK", &config.Container.Network) - loadEnvStr(config, "RUNNER__CONTAINER__NETWORK_MODE", &config.Container.NetworkMode) - loadEnvBool(config, "RUNNER__CONTAINER__ENABLE_IPV6", &config.Container.EnableIPv6) - loadEnvBool(config, "RUNNER__CONTAINER__PRIVILEGED", &config.Container.Privileged) - loadEnvStr(config, "RUNNER__CONTAINER__OPTIONS", &config.Container.Options) - loadEnvStr(config, "RUNNER__CONTAINER__WORKDIR_PARENT", &config.Container.WorkdirParent) - loadEnvList(config, "RUNNER__CONTAINER__VALID_VOLUMES", &config.Container.ValidVolumes) - loadEnvStr(config, "RUNNER__CONTAINER__DOCKER_HOST", &config.Container.DockerHost) - loadEnvBool(config, "RUNNER__CONTAINER__FORCE_PULL", &config.Container.ForcePull) + loadEnvStr(config, "RUNNER__container__NETWORK", &config.Container.Network) + loadEnvStr(config, "RUNNER__container__NETWORK_MODE", &config.Container.NetworkMode) + loadEnvBool(config, "RUNNER__container__ENABLE_IPV6", &config.Container.EnableIPv6) + loadEnvBool(config, "RUNNER__container__PRIVILEGED", &config.Container.Privileged) + loadEnvStr(config, "RUNNER__container__OPTIONS", &config.Container.Options) + loadEnvStr(config, "RUNNER__container__WORKDIR_PARENT", &config.Container.WorkdirParent) + loadEnvList(config, "RUNNER__container__VALID_VOLUMES", &config.Container.ValidVolumes) + loadEnvStr(config, "RUNNER__container__DOCKER_HOST", &config.Container.DockerHost) + loadEnvBool(config, "RUNNER__container__FORCE_PULL", &config.Container.ForcePull) // Host - loadEnvStr(config, "RUNNER__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. +// General Coverage Docs for below: +// loadEnvType, where Type is the type of the variable being loaded, loads an environment variable into the provided pointer if the environment variable exists and is not empty. +// The key parameter is the environment variable key to look for. +// The dest parameter is a pointer to the variable to load the environment variable into. +// Config is present but unused, it remains for future use to prevent the need to redo the above functions. + func loadEnvStr(config *Config, key string, dest *string) { // Example: RUNNER__LOG__LEVEL = "info" if v := os.Getenv(key); v != "" { + log.Debug("Loading env var: ", key, "=", v) *dest = v } } @@ -70,6 +78,7 @@ func loadEnvInt(config *Config, key string, dest *int) { // Example: RUNNER__RUNNER__CAPACITY = "1" if v := os.Getenv(key); v != "" { if intValue, err := strconv.Atoi(v); err == nil { + log.Debug("Loading env var: ", key, "=", v) *dest = intValue } } @@ -80,6 +89,7 @@ func loadEnvUInt16(config *Config, key string, dest *uint16) { // Example: RUNNER__CACHE__PORT = "8080" if v := os.Getenv(key); v != "" { if uint16Value, err := strconv.ParseUint(v, 10, 16); err == nil { + log.Debug("Loading env var: ", key, "=", v) *dest = uint16(uint16Value) } } @@ -90,6 +100,7 @@ func loadEnvDuration(config *Config, key string, dest *time.Duration) { // Example: RUNNER__RUNNER__SHUTDOWN_TIMEOUT = "3h" if v := os.Getenv(key); v != "" { if durationValue, err := time.ParseDuration(v); err == nil { + log.Debug("Loading env var: ", key, "=", v) *dest = durationValue } } @@ -100,6 +111,7 @@ func loadEnvBool(config *Config, key string, dest *bool) { // Example: RUNNER__RUNNER__INSECURE = "false" if v := os.Getenv(key); v != "" { if boolValue, err := strconv.ParseBool(v); err == nil { + log.Debug("Loading env var: ", key, "=", v) *dest = boolValue } } @@ -123,6 +135,7 @@ func loadEnvTable(config *Config, key string, dest *map[string]string) { func loadEnvList(config *Config, key string, dest *[]string) { // Example: RUNNER__RUNNER__LABELS = "label1, label2, label3" if v := os.Getenv(key); v != "" { + log.Debug("Loading env var: ", key, "=", v) *dest = splitAndTrim(v) } } diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index 0a4b548..80508de 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -173,5 +173,15 @@ func LoadDefault(file string) (*Config, error) { // Load environment variables at the end to allow for overriding the default values. loadEnvVars(cfg) + // Write the state of the config, including default values, to a debug file. + debugFile := file + ".debug.yml" + content, err := yaml.Marshal(cfg) + if err != nil { + return nil, fmt.Errorf("marshal config to debug file %q: %w", debugFile, err) + } + if err := os.WriteFile(debugFile, content, 0644); err != nil { + return nil, fmt.Errorf("write debug config file %q: %w", debugFile, err) + } + return cfg, nil }