diff --git a/commands/config.go b/commands/config.go index 91d00a5d5c339b5cc9ae90149f12d78741d0e277..d9863f4f107fef3c23b1412159ef6abf64af45fb 100644 --- a/commands/config.go +++ b/commands/config.go @@ -1,6 +1,7 @@ package commands import ( + "fmt" "gitlab.com/gitlab-org/gitlab-ci-multi-runner/common" "gitlab.com/gitlab-org/gitlab-ci-multi-runner/network" "os" @@ -49,6 +50,20 @@ func (c *configOptions) touchConfig() error { return nil } +func (c *configOptions) RunnerByName(name string) (*common.RunnerConfig, error) { + if c.config == nil { + return nil, fmt.Errorf("Config has not been loaded") + } + + for _, runner := range c.config.Runners { + if runner.Name == name { + return runner, nil + } + } + + return nil, fmt.Errorf("Could not find a runner with the name '%s'", name) +} + func init() { configFile := os.Getenv("CONFIG_FILE") if configFile == "" { diff --git a/commands/unregister.go b/commands/unregister.go index 660e63e2c8fbc42eac0e235483b9f771a804b5ab..0487777bb2bf31cc7c141324aa9be523dd77a71b 100644 --- a/commands/unregister.go +++ b/commands/unregister.go @@ -12,18 +12,29 @@ type UnregisterCommand struct { configOptions common.RunnerCredentials network common.Network + Name string `toml:"name" json:"name" short:"n" long:"name" description:"Name of the runner you wish to unregister"` } func (c *UnregisterCommand) Execute(context *cli.Context) { userModeWarning(false) - if !c.network.DeleteRunner(c.RunnerCredentials) { - log.Fatalln("Failed to delete runner") - } - err := c.loadConfig() if err != nil { - log.Warningln(err) + log.Fatalln(err) + return + } + + if len(c.Name) > 0 { + runnerConfig, err := c.RunnerByName(c.Name) + if err != nil { + log.Fatalln(err) + return + } + c.RunnerCredentials = runnerConfig.RunnerCredentials + } + + if !c.network.DeleteRunner(c.RunnerCredentials) { + log.Fatalln("Failed to delete runner", c.Name) return } diff --git a/docs/commands/README.md b/docs/commands/README.md index 99b19745cb60187584502ec7403d350ce7c4dec3..680c3c2f82621b8c16a4fcd0759b05a55837118d 100644 --- a/docs/commands/README.md +++ b/docs/commands/README.md @@ -288,22 +288,32 @@ gitlab-runner verify --delete ### gitlab-runner unregister -This command allows to unregister one of the registered runners. It expects to -enter a full URL and the runner's token. First get the runner's details by +This command allows to unregister one of the registered runners. It expects either +a full URL and the runner's token, or the runner's name. First get the runner's details by executing `gitlab-runner list`: ```bash test-runner Executor=shell Token=t0k3n URL=http://gitlab.example.com/ci/ ``` -Then use this information to unregister it, using the following command. +Then use this information to unregister it, using one of the following commands. >**Warning:** This operation cannot be undone, it will update the configuration file, so make sure to have a backup of `config.toml` before executing it. +#### By URL and token: + +```bash +gitlab-runner unregister --url http://gitlab.example.com/ci/ --token t0k3n +``` + +#### By name: + +> **Note:** If there is more than one runner with the given name, only the first one will be removed + ```bash -gitlab-runner unregister -u http://gitlab.example.com/ci/ -t t0k3n +gitlab-runner unregister --name test-runner ``` ## Service-related commands