diff --git a/cmd/osbuild-worker/config.go b/cmd/osbuild-worker/config.go new file mode 100644 index 000000000..60613ff3a --- /dev/null +++ b/cmd/osbuild-worker/config.go @@ -0,0 +1,67 @@ +package main + +import ( + "os" + + "github.com/BurntSushi/toml" + "github.com/sirupsen/logrus" +) + +type workerConfig struct { + Composer *struct { + Proxy string `toml:"proxy"` + } `toml:"composer"` + Koji map[string]struct { + Kerberos *struct { + Principal string `toml:"principal"` + KeyTab string `toml:"keytab"` + } `toml:"kerberos,omitempty"` + RelaxTimeoutFactor uint `toml:"relax_timeout_factor"` + } `toml:"koji"` + GCP *struct { + Credentials string `toml:"credentials"` + } `toml:"gcp"` + Azure *struct { + Credentials string `toml:"credentials"` + } `toml:"azure"` + AWS *struct { + Credentials string `toml:"credentials"` + Bucket string `toml:"bucket"` + } `toml:"aws"` + GenericS3 *struct { + Credentials string `toml:"credentials"` + Endpoint string `toml:"endpoint"` + Region string `toml:"region"` + Bucket string `toml:"bucket"` + CABundle string `toml:"ca_bundle"` + SkipSSLVerification bool `toml:"skip_ssl_verification"` + } `toml:"generic_s3"` + Authentication *struct { + OAuthURL string `toml:"oauth_url"` + OfflineTokenPath string `toml:"offline_token"` + ClientId string `toml:"client_id"` + ClientSecretPath string `toml:"client_secret"` + } `toml:"authentication"` + // default value: /api/worker/v1 + BasePath string `toml:"base_path"` + DNFJson string `toml:"dnf-json"` +} + +func parseConfig(file string) (*workerConfig, error) { + var config workerConfig + _, err := toml.DecodeFile(file, &config) + if err != nil { + // Return error only when we failed to decode the file. + // A non-existing config isn't an error, use defaults in this case. + if !os.IsNotExist(err) { + return nil, err + } + + logrus.Info("Configuration file not found, using defaults") + } + if config.BasePath == "" { + config.BasePath = "/api/worker/v1" + } + + return &config, nil +} diff --git a/cmd/osbuild-worker/main.go b/cmd/osbuild-worker/main.go index 3a8cc00d9..20c8af017 100644 --- a/cmd/osbuild-worker/main.go +++ b/cmd/osbuild-worker/main.go @@ -198,44 +198,6 @@ func RequestAndRunJob(client *worker.Client, acceptedJobTypes []string, jobImpls } func main() { - var config struct { - Composer *struct { - Proxy string `toml:"proxy"` - } `toml:"composer"` - Koji map[string]struct { - Kerberos *struct { - Principal string `toml:"principal"` - KeyTab string `toml:"keytab"` - } `toml:"kerberos,omitempty"` - RelaxTimeoutFactor uint `toml:"relax_timeout_factor"` - } `toml:"koji"` - GCP *struct { - Credentials string `toml:"credentials"` - } `toml:"gcp"` - Azure *struct { - Credentials string `toml:"credentials"` - } `toml:"azure"` - AWS *struct { - Credentials string `toml:"credentials"` - Bucket string `toml:"bucket"` - } `toml:"aws"` - GenericS3 *struct { - Credentials string `toml:"credentials"` - Endpoint string `toml:"endpoint"` - Region string `toml:"region"` - Bucket string `toml:"bucket"` - CABundle string `toml:"ca_bundle"` - SkipSSLVerification bool `toml:"skip_ssl_verification"` - } `toml:"generic_s3"` - Authentication *struct { - OAuthURL string `toml:"oauth_url"` - OfflineTokenPath string `toml:"offline_token"` - ClientId string `toml:"client_id"` - ClientSecretPath string `toml:"client_secret"` - } `toml:"authentication"` - BasePath string `toml:"base_path"` - DNFJson string `toml:"dnf-json"` - } var unix bool flag.BoolVar(&unix, "unix", false, "Interpret 'address' as a path to a unix domain socket instead of a network address") @@ -252,7 +214,7 @@ func main() { os.Exit(2) } - _, err := toml.DecodeFile(configFile, &config) + config, err := parseConfig(configFile) if err == nil { logrus.Info("Composer configuration:") encoder := toml.NewEncoder(logrus.StandardLogger().WriterLevel(logrus.InfoLevel)) @@ -260,14 +222,10 @@ func main() { if err != nil { logrus.Fatalf("Could not print config: %v", err) } - } else if !os.IsNotExist(err) { + } else { logrus.Fatalf("Could not load config file '%s': %v", configFile, err) } - if config.BasePath == "" { - config.BasePath = "/api/worker/v1" - } - cacheDirectory, ok := os.LookupEnv("CACHE_DIRECTORY") if !ok { logrus.Fatal("CACHE_DIRECTORY is not set. Is the service file missing CacheDirectory=?")