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

@ -5,12 +5,6 @@ import (
"strconv"
"strings"
"time"
log "github.com/sirupsen/logrus"
)
var (
envPrefix = "RUNNER__"
)
// 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,
// without writing to the config file.
// SkipEnv if variable is set
if _, ok := os.LookupEnv("RUNNER__SKIP_ENV"); ok {
return
}
// Log
loadEnvStr(config, envPrefix+"LOG__LEVEL", &config.Log.Level)
loadEnvStr(config, envPrefix+"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, envPrefix+"RUNNER__FILE", &config.Runner.File)
loadEnvInt(config, envPrefix+"RUNNER__CAPACITY", &config.Runner.Capacity)
loadEnvTable(config, envPrefix+"RUNNER__ENVS", &config.Runner.Envs)
loadEnvStr(config, envPrefix+"RUNNER__ENV_FILE", &config.Runner.EnvFile)
loadEnvDuration(config, envPrefix+"RUNNER__SHUTDOWN_TIMEOUT", &config.Runner.ShutdownTimeout)
loadEnvBool(config, envPrefix+"RUNNER__INSECURE", &config.Runner.Insecure)
loadEnvDuration(config, envPrefix+"RUNNER__FETCH_TIMEOUT", &config.Runner.FetchTimeout)
loadEnvDuration(config, envPrefix+"RUNNER__FETCH_INTERVAL", &config.Runner.FetchInterval)
loadEnvDuration(config, envPrefix+"RUNNER__REPORT_INTERVAL", &config.Runner.ReportInterval)
loadEnvList(config, envPrefix+"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, envPrefix+"CACHE__ENABLED", config.Cache.Enabled)
loadEnvStr(config, envPrefix+"CACHE__DIR", &config.Cache.Dir)
loadEnvStr(config, envPrefix+"CACHE__HOST", &config.Cache.Host)
loadEnvUInt16(config, envPrefix+"CACHE__PORT", &config.Cache.Port)
loadEnvStr(config, envPrefix+"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, envPrefix+"CONTAINER__NETWORK", &config.Container.Network)
loadEnvStr(config, envPrefix+"CONTAINER__NETWORK_MODE", &config.Container.NetworkMode)
loadEnvBool(config, envPrefix+"CONTAINER__ENABLE_IPV6", &config.Container.EnableIPv6)
loadEnvBool(config, envPrefix+"CONTAINER__PRIVILEGED", &config.Container.Privileged)
loadEnvStr(config, envPrefix+"CONTAINER__OPTIONS", &config.Container.Options)
loadEnvStr(config, envPrefix+"CONTAINER__WORKDIR_PARENT", &config.Container.WorkdirParent)
loadEnvList(config, envPrefix+"CONTAINER__VALID_VOLUMES", &config.Container.ValidVolumes)
loadEnvStr(config, envPrefix+"CONTAINER__DOCKER_HOST", &config.Container.DockerHost)
loadEnvBool(config, envPrefix+"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, 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.
@ -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.
func loadEnvTable(config *Config, key string, dest *map[string]string) {
// This is a placeholder function.
// This function is not yet implemented.
// This function is meant to be replaced by those more competent than me.
log.Warn("Loading Tables from env is not yet implemented: ", key)
// Example: RUNNER__RUNNER__ENVS = "key1=value1, key2=value2, key3=value3"
if v := os.Getenv(key); v != "" {
*dest = make(map[string]string)
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.