unregister.go 2.21 KB
Newer Older
Kamil Trzciński's avatar
Kamil Trzciński committed
1 2 3
package commands

import (
4
	"github.com/urfave/cli"
Kamil Trzciński's avatar
Kamil Trzciński committed
5

6
	log "github.com/sirupsen/logrus"
7 8
	"gitlab.com/gitlab-org/gitlab-runner/common"
	"gitlab.com/gitlab-org/gitlab-runner/network"
Kamil Trzciński's avatar
Kamil Trzciński committed
9 10
)

11 12 13
type UnregisterCommand struct {
	configOptions
	common.RunnerCredentials
14 15
	network    common.Network
	Name       string `toml:"name" json:"name" short:"n" long:"name" description:"Name of the runner you wish to unregister"`
16
	AllRunners bool   `toml:"all_runners" json:"all-runners" long:"all-runners" description:"Unregister all runners"`
17
}
Kamil Trzciński's avatar
Kamil Trzciński committed
18

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
func (c *UnregisterCommand) unregisterAllRunners() (runners []*common.RunnerConfig) {
	log.Warningln("Unregistering all runners")
	for _, r := range c.config.Runners {
		if !c.network.UnregisterRunner(r.RunnerCredentials) {
			log.Errorln("Failed to unregister runner", r.Name)
			//If unregister fails, leave the runner in the config
			runners = append(runners, r)
		}
	}
	return
}

func (c *UnregisterCommand) unregisterSingleRunner() (runners []*common.RunnerConfig) {
	if len(c.Name) > 0 { // Unregister when given a name
		runnerConfig, err := c.RunnerByName(c.Name)
		if err != nil {
			log.Fatalln(err)
		}
		c.RunnerCredentials = runnerConfig.RunnerCredentials
	}

	// Unregister given Token and URL of the runner
	if !c.network.UnregisterRunner(c.RunnerCredentials) {
		log.Fatalln("Failed to unregister runner", c.Name)
	}

	for _, otherRunner := range c.config.Runners {
		if otherRunner.RunnerCredentials == c.RunnerCredentials {
			continue
		}
		runners = append(runners, otherRunner)
	}
	return
}

54
func (c *UnregisterCommand) Execute(context *cli.Context) {
55 56
	userModeWarning(false)

57
	err := c.loadConfig()
Kamil Trzciński's avatar
Kamil Trzciński committed
58
	if err != nil {
59 60 61 62
		log.Fatalln(err)
		return
	}

63
	var runners []*common.RunnerConfig
64
	if c.AllRunners {
65
		runners = c.unregisterAllRunners()
66
	} else {
67
		runners = c.unregisterSingleRunner()
Kamil Trzciński's avatar
Kamil Trzciński committed
68 69 70
	}

	// check if anything changed
71
	if len(c.config.Runners) == len(runners) {
Kamil Trzciński's avatar
Kamil Trzciński committed
72 73 74
		return
	}

75
	c.config.Runners = runners
Kamil Trzciński's avatar
Kamil Trzciński committed
76 77

	// save config file
78
	err = c.saveConfig()
Kamil Trzciński's avatar
Kamil Trzciński committed
79
	if err != nil {
80
		log.Fatalln("Failed to update", c.ConfigFile, err)
Kamil Trzciński's avatar
Kamil Trzciński committed
81
	}
82
	log.Println("Updated", c.ConfigFile)
Kamil Trzciński's avatar
Kamil Trzciński committed
83 84 85
}

func init() {
86
	common.RegisterCommand2("unregister", "unregister specific runner", &UnregisterCommand{
87
		network: network.NewGitLabClient(),
88
	})
Kamil Trzciński's avatar
Kamil Trzciński committed
89
}