2023-02-28 10:44:46 +00:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2022-10-02 04:33:17 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2023-04-02 14:41:48 +00:00
|
|
|
"fmt"
|
2022-10-02 04:33:17 +00:00
|
|
|
"os"
|
2023-04-02 14:41:48 +00:00
|
|
|
"path/filepath"
|
|
|
|
"time"
|
2022-11-24 03:55:52 +00:00
|
|
|
|
2022-10-02 04:33:17 +00:00
|
|
|
"github.com/joho/godotenv"
|
2023-04-02 14:41:48 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2022-10-02 04:33:17 +00:00
|
|
|
)
|
|
|
|
|
2023-04-02 14:41:48 +00:00
|
|
|
type Config struct {
|
|
|
|
Log struct {
|
|
|
|
Level string `yaml:"level"`
|
|
|
|
} `yaml:"log"`
|
2022-10-02 04:33:17 +00:00
|
|
|
Runner struct {
|
2023-04-06 02:57:36 +00:00
|
|
|
File string `yaml:"file"`
|
|
|
|
Capacity int `yaml:"capacity"`
|
|
|
|
Envs map[string]string `yaml:"envs"`
|
|
|
|
EnvFile string `yaml:"env_file"`
|
|
|
|
Timeout time.Duration `yaml:"timeout"`
|
|
|
|
Insecure bool `yaml:"insecure"`
|
|
|
|
FetchTimeout time.Duration `yaml:"fetch_timeout"`
|
|
|
|
FetchInterval time.Duration `yaml:"fetch_interval"`
|
2023-04-02 14:41:48 +00:00
|
|
|
} `yaml:"runner"`
|
|
|
|
Cache struct {
|
|
|
|
Enabled *bool `yaml:"enabled"` // pointer to distinguish between false and not set, and it will be true if not set
|
|
|
|
Dir string `yaml:"dir"`
|
|
|
|
Host string `yaml:"host"`
|
|
|
|
Port uint16 `yaml:"port"`
|
|
|
|
} `yaml:"cache"`
|
2023-04-04 06:32:01 +00:00
|
|
|
Container struct {
|
2023-04-28 14:03:52 +00:00
|
|
|
NetworkMode string `yaml:"network_mode"`
|
|
|
|
Privileged bool `yaml:"privileged"`
|
|
|
|
Options string `yaml:"options"`
|
|
|
|
WorkdirParent string `yaml:"workdir_parent"`
|
2023-04-11 02:58:12 +00:00
|
|
|
} `yaml:"container"`
|
2023-04-02 14:41:48 +00:00
|
|
|
}
|
2022-10-02 04:33:17 +00:00
|
|
|
|
2023-04-02 14:41:48 +00:00
|
|
|
// LoadDefault returns the default configuration.
|
|
|
|
// If file is not empty, it will be used to load the configuration.
|
|
|
|
func LoadDefault(file string) (*Config, error) {
|
|
|
|
cfg := &Config{}
|
|
|
|
if file != "" {
|
|
|
|
f, err := os.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-11-15 14:42:41 +00:00
|
|
|
}
|
2023-04-02 14:41:48 +00:00
|
|
|
defer f.Close()
|
|
|
|
decoder := yaml.NewDecoder(f)
|
|
|
|
if err := decoder.Decode(&cfg); err != nil {
|
|
|
|
return nil, err
|
2023-02-15 08:51:14 +00:00
|
|
|
}
|
2022-10-15 08:40:35 +00:00
|
|
|
}
|
2023-04-02 14:41:48 +00:00
|
|
|
compatibleWithOldEnvs(file != "", cfg)
|
2022-10-02 04:33:17 +00:00
|
|
|
|
2023-04-02 14:41:48 +00:00
|
|
|
if cfg.Runner.EnvFile != "" {
|
|
|
|
if stat, err := os.Stat(cfg.Runner.EnvFile); err == nil && !stat.IsDir() {
|
|
|
|
envs, err := godotenv.Read(cfg.Runner.EnvFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("read env file %q: %w", cfg.Runner.EnvFile, err)
|
|
|
|
}
|
|
|
|
for k, v := range envs {
|
|
|
|
cfg.Runner.Envs[k] = v
|
|
|
|
}
|
2022-11-04 09:23:59 +00:00
|
|
|
}
|
2022-10-02 04:33:17 +00:00
|
|
|
}
|
|
|
|
|
2023-04-02 14:41:48 +00:00
|
|
|
if cfg.Log.Level == "" {
|
|
|
|
cfg.Log.Level = "info"
|
2022-10-02 04:33:17 +00:00
|
|
|
}
|
2023-04-02 14:41:48 +00:00
|
|
|
if cfg.Runner.File == "" {
|
|
|
|
cfg.Runner.File = ".runner"
|
2022-10-02 04:33:17 +00:00
|
|
|
}
|
2023-04-02 14:41:48 +00:00
|
|
|
if cfg.Runner.Capacity <= 0 {
|
|
|
|
cfg.Runner.Capacity = 1
|
|
|
|
}
|
|
|
|
if cfg.Runner.Timeout <= 0 {
|
|
|
|
cfg.Runner.Timeout = 3 * time.Hour
|
|
|
|
}
|
|
|
|
if cfg.Cache.Enabled == nil {
|
|
|
|
b := true
|
|
|
|
cfg.Cache.Enabled = &b
|
|
|
|
}
|
|
|
|
if *cfg.Cache.Enabled {
|
|
|
|
if cfg.Cache.Dir == "" {
|
|
|
|
home, _ := os.UserHomeDir()
|
|
|
|
cfg.Cache.Dir = filepath.Join(home, ".cache", "actcache")
|
2022-10-02 04:33:17 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-04 13:32:04 +00:00
|
|
|
if cfg.Container.NetworkMode == "" {
|
|
|
|
cfg.Container.NetworkMode = "bridge"
|
2023-04-04 06:32:01 +00:00
|
|
|
}
|
2023-04-28 14:03:52 +00:00
|
|
|
if cfg.Container.WorkdirParent == "" {
|
|
|
|
cfg.Container.WorkdirParent = "workspace"
|
|
|
|
}
|
2023-04-06 02:57:36 +00:00
|
|
|
if cfg.Runner.FetchTimeout <= 0 {
|
|
|
|
cfg.Runner.FetchTimeout = 5 * time.Second
|
|
|
|
}
|
|
|
|
if cfg.Runner.FetchInterval <= 0 {
|
|
|
|
cfg.Runner.FetchInterval = 2 * time.Second
|
|
|
|
}
|
2022-10-02 04:33:17 +00:00
|
|
|
|
2022-10-15 08:12:32 +00:00
|
|
|
return cfg, nil
|
2022-10-02 04:33:17 +00:00
|
|
|
}
|