github.com/secure-build/gitlab-runner@v12.5.0+incompatible/commands/config.go (about) 1 package commands 2 3 import ( 4 "fmt" 5 "net" 6 "os" 7 "path/filepath" 8 "strings" 9 10 "github.com/sirupsen/logrus" 11 12 "gitlab.com/gitlab-org/gitlab-runner/common" 13 "gitlab.com/gitlab-org/gitlab-runner/helpers/fslocker" 14 "gitlab.com/gitlab-org/gitlab-runner/network" 15 ) 16 17 func getDefaultConfigFile() string { 18 return filepath.Join(getDefaultConfigDirectory(), "config.toml") 19 } 20 21 func getDefaultCertificateDirectory() string { 22 return filepath.Join(getDefaultConfigDirectory(), "certs") 23 } 24 25 type configOptions struct { 26 config *common.Config 27 28 ConfigFile string `short:"c" long:"config" env:"CONFIG_FILE" description:"Config file"` 29 } 30 31 func (c *configOptions) saveConfig() error { 32 return c.config.SaveConfig(c.ConfigFile) 33 } 34 35 func (c *configOptions) loadConfig() error { 36 config := common.NewConfig() 37 err := config.LoadConfig(c.ConfigFile) 38 if err != nil { 39 return err 40 } 41 c.config = config 42 return nil 43 } 44 45 func (c *configOptions) inLock(fn func()) error { 46 lockFile := fmt.Sprintf("%s.lock", c.ConfigFile) 47 48 return fslocker.InLock(lockFile, fn) 49 } 50 51 func (c *configOptions) RunnerByName(name string) (*common.RunnerConfig, error) { 52 if c.config == nil { 53 return nil, fmt.Errorf("config has not been loaded") 54 } 55 56 for _, runner := range c.config.Runners { 57 if runner.Name == name { 58 return runner, nil 59 } 60 } 61 62 return nil, fmt.Errorf("could not find a runner with the name '%s'", name) 63 } 64 65 type configOptionsWithListenAddress struct { 66 configOptions 67 68 ListenAddress string `long:"listen-address" env:"LISTEN_ADDRESS" description:"Metrics / pprof server listening address"` 69 } 70 71 func (c *configOptionsWithListenAddress) listenAddress() (string, error) { 72 address := c.config.ListenAddress 73 if c.ListenAddress != "" { 74 address = c.ListenAddress 75 } 76 77 if address == "" { 78 return "", nil 79 } 80 81 _, port, err := net.SplitHostPort(address) 82 if err != nil && !strings.Contains(err.Error(), "missing port in address") { 83 return "", err 84 } 85 86 if len(port) == 0 { 87 return fmt.Sprintf("%s:%d", address, common.DefaultMetricsServerPort), nil 88 } 89 return address, nil 90 } 91 92 func init() { 93 configFile := os.Getenv("CONFIG_FILE") 94 if configFile == "" { 95 err := os.Setenv("CONFIG_FILE", getDefaultConfigFile()) 96 if err != nil { 97 logrus.WithError(err).Fatal("Couldn't set CONFIG_FILE environment variable") 98 } 99 } 100 101 network.CertificateDirectory = getDefaultCertificateDirectory() 102 }