2018-10-01 15:39:27 +00:00
|
|
|
package deregister
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
|
2018-10-01 15:53:30 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2018-10-01 15:39:27 +00:00
|
|
|
"github.com/hashicorp/consul/command/flags"
|
|
|
|
"github.com/hashicorp/consul/command/services"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func New(ui cli.Ui) *cmd {
|
|
|
|
c := &cmd{UI: ui}
|
|
|
|
c.init()
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
type cmd struct {
|
2018-10-01 15:53:30 +00:00
|
|
|
UI cli.Ui
|
|
|
|
flags *flag.FlagSet
|
|
|
|
http *flags.HTTPFlags
|
|
|
|
help string
|
|
|
|
flagId string
|
2018-10-01 15:39:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) init() {
|
|
|
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
2018-10-01 15:53:30 +00:00
|
|
|
c.flags.StringVar(&c.flagId, "id", "",
|
|
|
|
"ID to delete. This must not be set if arguments are given.")
|
2018-10-01 15:39:27 +00:00
|
|
|
|
|
|
|
c.http = &flags.HTTPFlags{}
|
|
|
|
flags.Merge(c.flags, c.http.ClientFlags())
|
|
|
|
flags.Merge(c.flags, c.http.ServerFlags())
|
2021-07-21 19:45:24 +00:00
|
|
|
flags.Merge(c.flags, c.http.MultiTenancyFlags())
|
2018-10-01 15:39:27 +00:00
|
|
|
c.help = flags.Usage(help, c.flags)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Run(args []string) int {
|
|
|
|
if err := c.flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for arg validation
|
|
|
|
args = c.flags.Args()
|
2018-10-01 15:53:30 +00:00
|
|
|
if len(args) == 0 && c.flagId == "" {
|
|
|
|
c.UI.Error("Service deregistration requires at least one argument or -id.")
|
2018-10-01 15:39:27 +00:00
|
|
|
return 1
|
2018-10-01 15:55:32 +00:00
|
|
|
} else if len(args) > 0 && c.flagId != "" {
|
|
|
|
c.UI.Error("Service deregistration requires arguments or -id, not both.")
|
|
|
|
return 1
|
2018-10-01 15:39:27 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 17:19:31 +00:00
|
|
|
svcs := []*api.AgentServiceRegistration{{
|
2021-07-21 19:45:24 +00:00
|
|
|
ID: c.flagId,
|
|
|
|
}}
|
2018-10-01 15:53:30 +00:00
|
|
|
if len(args) > 0 {
|
|
|
|
var err error
|
2020-05-02 00:17:27 +00:00
|
|
|
svcs, err = services.ServicesFromFiles(c.UI, args)
|
2018-10-01 15:53:30 +00:00
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
2018-10-01 15:39:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create and test the HTTP client
|
|
|
|
client, err := c.http.APIClient()
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create all the services
|
|
|
|
for _, svc := range svcs {
|
|
|
|
id := svc.ID
|
|
|
|
if id == "" {
|
|
|
|
id = svc.Name
|
|
|
|
}
|
|
|
|
if id == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := client.Agent().ServiceDeregister(id); err != nil {
|
2022-08-04 15:07:40 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error deregistering service %q: %s",
|
2018-10-01 15:39:27 +00:00
|
|
|
svc.Name, err))
|
|
|
|
return 1
|
|
|
|
}
|
2018-10-02 19:48:46 +00:00
|
|
|
|
|
|
|
c.UI.Output(fmt.Sprintf("Deregistered service: %s", id))
|
2018-10-01 15:39:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Synopsis() string {
|
|
|
|
return synopsis
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Help() string {
|
|
|
|
return c.help
|
|
|
|
}
|
|
|
|
|
2021-07-21 19:45:24 +00:00
|
|
|
const (
|
|
|
|
synopsis = "Deregister services with the local agent"
|
|
|
|
help = `
|
2018-10-01 15:39:27 +00:00
|
|
|
Usage: consul services deregister [options] [FILE...]
|
|
|
|
|
|
|
|
Deregister one or more services that were previously registered with
|
|
|
|
the local agent.
|
|
|
|
|
|
|
|
$ consul services deregister web.json db.json
|
|
|
|
|
|
|
|
The -id flag may be used to deregister a single service by ID:
|
|
|
|
|
|
|
|
$ consul services deregister -id=web
|
|
|
|
|
|
|
|
Services are deregistered from the local agent catalog. This command must
|
|
|
|
be run against the same agent where the service was registered.
|
|
|
|
`
|
2021-07-21 19:45:24 +00:00
|
|
|
)
|