gitlab.com/jfprevost/gitlab-runner-notlscheck@v11.11.4+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/network" 14 ) 15 16 func getDefaultConfigFile() string { 17 return filepath.Join(getDefaultConfigDirectory(), "config.toml") 18 } 19 20 func getDefaultCertificateDirectory() string { 21 return filepath.Join(getDefaultConfigDirectory(), "certs") 22 } 23 24 type configOptions struct { 25 config *common.Config 26 27 ConfigFile string `short:"c" long:"config" env:"CONFIG_FILE" description:"Config file"` 28 } 29 30 func (c *configOptions) saveConfig() error { 31 return c.config.SaveConfig(c.ConfigFile) 32 } 33 34 func (c *configOptions) loadConfig() error { 35 config := common.NewConfig() 36 err := config.LoadConfig(c.ConfigFile) 37 if err != nil { 38 return err 39 } 40 c.config = config 41 return nil 42 } 43 44 func (c *configOptions) touchConfig() error { 45 // try to load existing config 46 err := c.loadConfig() 47 if err != nil { 48 return err 49 } 50 51 // save config for the first time 52 if !c.config.Loaded { 53 return c.saveConfig() 54 } 55 return nil 56 } 57 58 func (c *configOptions) RunnerByName(name string) (*common.RunnerConfig, error) { 59 if c.config == nil { 60 return nil, fmt.Errorf("Config has not been loaded") 61 } 62 63 for _, runner := range c.config.Runners { 64 if runner.Name == name { 65 return runner, nil 66 } 67 } 68 69 return nil, fmt.Errorf("Could not find a runner with the name '%s'", name) 70 } 71 72 type configOptionsWithListenAddress struct { 73 configOptions 74 75 ListenAddress string `long:"listen-address" env:"LISTEN_ADDRESS" description:"Metrics / pprof server listening address"` 76 77 // TODO: Remove in 12.0 78 MetricsServerAddress string `long:"metrics-server" env:"METRICS_SERVER" description:"(DEPRECATED) Metrics / pprof server listening address"` //DEPRECATED 79 } 80 81 func (c *configOptionsWithListenAddress) listenAddress() (string, error) { 82 address := c.listenOrMetricsServerAddress() 83 84 if address == "" { 85 return "", nil 86 } 87 88 _, port, err := net.SplitHostPort(address) 89 if err != nil && !strings.Contains(err.Error(), "missing port in address") { 90 return "", err 91 } 92 93 if len(port) == 0 { 94 return fmt.Sprintf("%s:%d", address, common.DefaultMetricsServerPort), nil 95 } 96 return address, nil 97 } 98 99 func (c *configOptionsWithListenAddress) listenOrMetricsServerAddress() string { 100 if c.ListenAddress != "" { 101 return c.ListenAddress 102 } 103 104 // TODO: Remove in 12.0 105 if c.MetricsServerAddress != "" { 106 logrus.Warnln("'metrics-server' command line option is deprecated and will be removed in one of future releases; please use 'listen-address' instead") 107 108 return c.MetricsServerAddress 109 } 110 111 return c.config.ListenOrServerMetricAddress() 112 } 113 114 func init() { 115 configFile := os.Getenv("CONFIG_FILE") 116 if configFile == "" { 117 os.Setenv("CONFIG_FILE", getDefaultConfigFile()) 118 } 119 120 network.CertificateDirectory = getDefaultCertificateDirectory() 121 }