2022-10-01 23:33:17 -05:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2022-10-15 03:40:35 -05:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2022-10-01 23:33:17 -05:00
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
|
2022-10-15 03:40:35 -05:00
|
|
|
"gitea.com/gitea/act_runner/core"
|
2022-11-23 21:55:52 -06:00
|
|
|
|
2022-10-01 23:33:17 -05:00
|
|
|
"github.com/joho/godotenv"
|
|
|
|
"github.com/kelseyhightower/envconfig"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Config provides the system configuration.
|
|
|
|
Config struct {
|
2022-11-16 04:08:12 -06:00
|
|
|
Debug bool `envconfig:"GITEA_DEBUG"`
|
|
|
|
Trace bool `envconfig:"GITEA_TRACE"`
|
|
|
|
Client Client
|
|
|
|
Runner Runner
|
|
|
|
Platform Platform
|
2022-10-01 23:33:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Client struct {
|
|
|
|
Address string `ignored:"true"`
|
|
|
|
SkipVerify bool `envconfig:"GITEA_RPC_SKIP_VERIFY"`
|
|
|
|
GRPC bool `envconfig:"GITEA_RPC_GRPC" default:"true"`
|
|
|
|
GRPCWeb bool `envconfig:"GITEA_RPC_GRPC_WEB"`
|
|
|
|
}
|
|
|
|
|
|
|
|
Runner struct {
|
2022-10-15 07:03:33 -05:00
|
|
|
UUID string `ignored:"true"`
|
2022-10-01 23:33:17 -05:00
|
|
|
Name string `envconfig:"GITEA_RUNNER_NAME"`
|
2022-11-15 08:42:41 -06:00
|
|
|
Token string `ignored:"true"`
|
2022-10-01 23:33:17 -05:00
|
|
|
Capacity int `envconfig:"GITEA_RUNNER_CAPACITY" default:"1"`
|
2022-10-15 03:12:32 -05:00
|
|
|
File string `envconfig:"GITEA_RUNNER_FILE" default:".runner"`
|
2022-10-01 23:33:17 -05:00
|
|
|
Environ map[string]string `envconfig:"GITEA_RUNNER_ENVIRON"`
|
|
|
|
EnvFile string `envconfig:"GITEA_RUNNER_ENV_FILE"`
|
|
|
|
Labels []string `envconfig:"GITEA_RUNNER_LABELS"`
|
|
|
|
}
|
|
|
|
|
|
|
|
Platform struct {
|
2022-10-15 03:12:32 -05:00
|
|
|
OS string `envconfig:"GITEA_PLATFORM_OS"`
|
|
|
|
Arch string `envconfig:"GITEA_PLATFORM_ARCH"`
|
2022-10-01 23:33:17 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// FromEnviron returns the settings from the environment.
|
|
|
|
func FromEnviron() (Config, error) {
|
|
|
|
cfg := Config{}
|
|
|
|
if err := envconfig.Process("", &cfg); err != nil {
|
|
|
|
return cfg, err
|
|
|
|
}
|
|
|
|
|
2022-10-15 03:40:35 -05:00
|
|
|
// check runner config exist
|
2022-11-23 21:55:52 -06:00
|
|
|
if f, err := os.Stat(cfg.Runner.File); err == nil && !f.IsDir() {
|
2022-10-15 03:40:35 -05:00
|
|
|
jsonFile, _ := os.Open(cfg.Runner.File)
|
|
|
|
defer jsonFile.Close()
|
|
|
|
byteValue, _ := io.ReadAll(jsonFile)
|
|
|
|
var runner core.Runner
|
|
|
|
if err := json.Unmarshal(byteValue, &runner); err != nil {
|
|
|
|
return cfg, err
|
|
|
|
}
|
|
|
|
if runner.UUID != "" {
|
2022-10-15 07:03:33 -05:00
|
|
|
cfg.Runner.UUID = runner.UUID
|
2022-10-15 03:40:35 -05:00
|
|
|
}
|
2022-10-28 10:23:19 -05:00
|
|
|
if runner.Token != "" {
|
|
|
|
cfg.Runner.Token = runner.Token
|
|
|
|
}
|
2022-11-23 21:55:52 -06:00
|
|
|
if len(runner.Labels) != 0 {
|
|
|
|
cfg.Runner.Labels = runner.Labels
|
|
|
|
}
|
2022-11-15 08:42:41 -06:00
|
|
|
if runner.Address != "" {
|
|
|
|
cfg.Client.Address = runner.Address
|
|
|
|
}
|
2022-10-15 03:40:35 -05:00
|
|
|
}
|
2022-10-01 23:33:17 -05:00
|
|
|
|
|
|
|
// runner config
|
|
|
|
if cfg.Runner.Environ == nil {
|
2022-11-04 04:23:59 -05:00
|
|
|
cfg.Runner.Environ = map[string]string{
|
|
|
|
"GITHUB_API_URL": cfg.Client.Address + "/api/v1",
|
|
|
|
"GITHUB_SERVER_URL": cfg.Client.Address,
|
|
|
|
}
|
2022-10-01 23:33:17 -05:00
|
|
|
}
|
|
|
|
if cfg.Runner.Name == "" {
|
|
|
|
cfg.Runner.Name, _ = os.Hostname()
|
|
|
|
}
|
|
|
|
|
|
|
|
// platform config
|
|
|
|
if cfg.Platform.OS == "" {
|
|
|
|
cfg.Platform.OS = runtime.GOOS
|
|
|
|
}
|
|
|
|
if cfg.Platform.Arch == "" {
|
|
|
|
cfg.Platform.Arch = runtime.GOARCH
|
|
|
|
}
|
|
|
|
|
|
|
|
if file := cfg.Runner.EnvFile; file != "" {
|
|
|
|
envs, err := godotenv.Read(file)
|
|
|
|
if err != nil {
|
|
|
|
return cfg, err
|
|
|
|
}
|
|
|
|
for k, v := range envs {
|
|
|
|
cfg.Runner.Environ[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-15 03:12:32 -05:00
|
|
|
return cfg, nil
|
2022-10-01 23:33:17 -05:00
|
|
|
}
|