github.com/secure-build/gitlab-runner@v12.5.0+incompatible/commands/verify.go (about) 1 package commands 2 3 import ( 4 "errors" 5 6 "github.com/sirupsen/logrus" 7 "github.com/urfave/cli" 8 9 "gitlab.com/gitlab-org/gitlab-runner/common" 10 "gitlab.com/gitlab-org/gitlab-runner/network" 11 ) 12 13 type VerifyCommand struct { 14 configOptions 15 common.RunnerCredentials 16 network common.Network 17 Name string `toml:"name" json:"name" short:"n" long:"name" description:"Name of the runner you wish to verify"` 18 DeleteNonExisting bool `long:"delete" description:"Delete no longer existing runners?"` 19 } 20 21 func (c *VerifyCommand) Execute(context *cli.Context) { 22 userModeWarning(true) 23 24 err := c.loadConfig() 25 if err != nil { 26 logrus.Fatalln(err) 27 return 28 } 29 30 // check if there's something to verify 31 toVerify, okRunners, err := c.selectRunners() 32 if err != nil { 33 logrus.Fatalln(err) 34 return 35 } 36 37 // verify if runner exist 38 for _, runner := range toVerify { 39 if c.network.VerifyRunner(runner.RunnerCredentials) { 40 okRunners = append(okRunners, runner) 41 } 42 } 43 44 // check if anything changed 45 if len(c.config.Runners) == len(okRunners) { 46 return 47 } 48 49 if !c.DeleteNonExisting { 50 logrus.Fatalln("Failed to verify runners") 51 return 52 } 53 54 c.config.Runners = okRunners 55 56 // save config file 57 err = c.saveConfig() 58 if err != nil { 59 logrus.Fatalln("Failed to update", c.ConfigFile, err) 60 } 61 logrus.Println("Updated", c.ConfigFile) 62 } 63 64 func (c *VerifyCommand) selectRunners() (toVerify []*common.RunnerConfig, okRunners []*common.RunnerConfig, err error) { 65 var selectorPresent = c.Name != "" || c.RunnerCredentials.URL != "" || c.RunnerCredentials.Token != "" 66 67 for _, runner := range c.config.Runners { 68 selected := !selectorPresent || runner.Name == c.Name || runner.RunnerCredentials.SameAs(&c.RunnerCredentials) 69 70 if selected { 71 toVerify = append(toVerify, runner) 72 } else { 73 okRunners = append(okRunners, runner) 74 } 75 } 76 77 if selectorPresent && len(toVerify) == 0 { 78 err = errors.New("No runner matches the filtering parameters") 79 } 80 return 81 } 82 83 func init() { 84 common.RegisterCommand2("verify", "verify all registered runners", &VerifyCommand{ 85 network: network.NewGitLabClient(), 86 }) 87 }