2018-09-28 06:52:17 +00:00
|
|
|
package register
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2018-10-01 15:05:57 +00:00
|
|
|
"fmt"
|
2018-09-28 06:52:17 +00:00
|
|
|
|
2018-10-01 16:16:14 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2018-09-28 06:52:17 +00:00
|
|
|
"github.com/hashicorp/consul/command/flags"
|
2018-10-01 15:27:59 +00:00
|
|
|
"github.com/hashicorp/consul/command/services"
|
2018-09-28 06:52:17 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func New(ui cli.Ui) *cmd {
|
|
|
|
c := &cmd{UI: ui}
|
|
|
|
c.init()
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
type cmd struct {
|
|
|
|
UI cli.Ui
|
|
|
|
flags *flag.FlagSet
|
|
|
|
http *flags.HTTPFlags
|
|
|
|
help string
|
|
|
|
|
|
|
|
// flags
|
2019-06-18 00:52:01 +00:00
|
|
|
flagKind string
|
2019-06-17 14:51:50 +00:00
|
|
|
flagId string
|
|
|
|
flagName string
|
|
|
|
flagAddress string
|
|
|
|
flagPort int
|
|
|
|
flagTags []string
|
|
|
|
flagMeta map[string]string
|
|
|
|
flagTaggedAddresses map[string]string
|
2018-09-28 06:52:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) init() {
|
|
|
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
2018-10-01 16:16:14 +00:00
|
|
|
c.flags.StringVar(&c.flagId, "id", "",
|
|
|
|
"ID of the service to register for arg-based registration. If this "+
|
|
|
|
"isn't set, it will default to the -name value.")
|
|
|
|
c.flags.StringVar(&c.flagName, "name", "",
|
|
|
|
"Name of the service to register for arg-based registration.")
|
|
|
|
c.flags.StringVar(&c.flagAddress, "address", "",
|
|
|
|
"Address of the service to register for arg-based registration.")
|
|
|
|
c.flags.IntVar(&c.flagPort, "port", 0,
|
|
|
|
"Port of the service to register for arg-based registration.")
|
2018-09-28 06:52:17 +00:00
|
|
|
c.flags.Var((*flags.FlagMapValue)(&c.flagMeta), "meta",
|
2019-06-17 14:51:50 +00:00
|
|
|
"Metadata to set on the service, formatted as key=value. This flag "+
|
2018-09-28 06:52:17 +00:00
|
|
|
"may be specified multiple times to set multiple meta fields.")
|
2018-10-01 16:16:14 +00:00
|
|
|
c.flags.Var((*flags.AppendSliceValue)(&c.flagTags), "tag",
|
|
|
|
"Tag to add to the service. This flag can be specified multiple "+
|
|
|
|
"times to set multiple tags.")
|
2019-06-17 14:51:50 +00:00
|
|
|
c.flags.Var((*flags.FlagMapValue)(&c.flagTaggedAddresses), "tagged-address",
|
|
|
|
"Tagged address to set on the service, formatted as key=value. This flag "+
|
|
|
|
"may be specified multiple times to set multiple addresses.")
|
2019-06-18 00:52:01 +00:00
|
|
|
c.flags.StringVar(&c.flagKind, "kind", "", "The services 'kind'")
|
2018-09-28 06:52:17 +00:00
|
|
|
|
|
|
|
c.http = &flags.HTTPFlags{}
|
|
|
|
flags.Merge(c.flags, c.http.ClientFlags())
|
|
|
|
flags.Merge(c.flags, c.http.ServerFlags())
|
2019-12-10 02:26:41 +00:00
|
|
|
flags.Merge(c.flags, c.http.NamespaceFlags())
|
2018-09-28 06:52:17 +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
|
|
|
|
}
|
|
|
|
|
2019-06-17 14:51:50 +00:00
|
|
|
var taggedAddrs map[string]api.ServiceAddress
|
|
|
|
if len(c.flagTaggedAddresses) > 0 {
|
|
|
|
taggedAddrs = make(map[string]api.ServiceAddress)
|
|
|
|
for k, v := range c.flagTaggedAddresses {
|
|
|
|
addr, err := api.ParseServiceAddr(v)
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Invalid Tagged Address: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
taggedAddrs[k] = addr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-01 16:16:14 +00:00
|
|
|
svcs := []*api.AgentServiceRegistration{&api.AgentServiceRegistration{
|
2019-06-18 00:52:01 +00:00
|
|
|
Kind: api.ServiceKind(c.flagKind),
|
2019-06-17 14:51:50 +00:00
|
|
|
ID: c.flagId,
|
|
|
|
Name: c.flagName,
|
|
|
|
Address: c.flagAddress,
|
|
|
|
Port: c.flagPort,
|
|
|
|
Tags: c.flagTags,
|
|
|
|
Meta: c.flagMeta,
|
|
|
|
TaggedAddresses: taggedAddrs,
|
2018-10-01 16:16:14 +00:00
|
|
|
}}
|
|
|
|
|
2018-09-28 06:52:17 +00:00
|
|
|
// Check for arg validation
|
|
|
|
args = c.flags.Args()
|
2018-10-01 16:16:14 +00:00
|
|
|
if len(args) == 0 && c.flagName == "" {
|
|
|
|
c.UI.Error("Service registration requires at least one argument or flags.")
|
|
|
|
return 1
|
|
|
|
} else if len(args) > 0 && c.flagName != "" {
|
|
|
|
c.UI.Error("Service registration requires arguments or -id, not both.")
|
2018-09-28 06:52:17 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2018-10-01 16:16:14 +00:00
|
|
|
if len(args) > 0 {
|
|
|
|
var err error
|
|
|
|
svcs, err = services.ServicesFromFiles(args)
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
2018-10-01 15:05:57 +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 {
|
|
|
|
if err := client.Agent().ServiceRegister(svc); err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error registering service %q: %s",
|
|
|
|
svc.Name, err))
|
2018-09-28 06:52:17 +00:00
|
|
|
return 1
|
|
|
|
}
|
2018-10-02 19:48:46 +00:00
|
|
|
|
|
|
|
c.UI.Output(fmt.Sprintf("Registered service: %s", svc.Name))
|
2018-10-01 15:05:57 +00:00
|
|
|
}
|
2018-09-28 06:52:17 +00:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Synopsis() string {
|
|
|
|
return synopsis
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Help() string {
|
|
|
|
return c.help
|
|
|
|
}
|
|
|
|
|
2018-10-01 15:27:59 +00:00
|
|
|
const synopsis = "Register services with the local agent"
|
2018-09-28 06:52:17 +00:00
|
|
|
const help = `
|
2018-10-01 15:27:59 +00:00
|
|
|
Usage: consul services register [options] [FILE...]
|
2018-09-28 06:52:17 +00:00
|
|
|
|
2018-10-01 15:27:59 +00:00
|
|
|
Register one or more services using the local agent API. Services can
|
|
|
|
be registered from standard Consul configuration files (HCL or JSON) or
|
|
|
|
using flags. The service is registered and the command returns. The caller
|
|
|
|
must remember to call "consul services deregister" or a similar API to
|
|
|
|
deregister the service when complete.
|
2018-09-28 06:52:17 +00:00
|
|
|
|
2018-10-01 15:27:59 +00:00
|
|
|
$ consul services register web.json
|
2018-09-28 06:52:17 +00:00
|
|
|
|
|
|
|
Additional flags and more advanced use cases are detailed below.
|
|
|
|
`
|