-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
register.go
155 lines (133 loc) · 4.35 KB
/
register.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package register
import (
"flag"
"fmt"
"github.com/hashicorp/consul/api"
"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 {
UI cli.Ui
flags *flag.FlagSet
http *flags.HTTPFlags
help string
// flags
flagKind string
flagId string
flagName string
flagAddress string
flagPort int
flagTags []string
flagMeta map[string]string
flagTaggedAddresses map[string]string
}
func (c *cmd) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
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.")
c.flags.Var((*flags.FlagMapValue)(&c.flagMeta), "meta",
"Metadata to set on the service, formatted as key=value. This flag "+
"may be specified multiple times to set multiple meta fields.")
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.")
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.")
c.flags.StringVar(&c.flagKind, "kind", "", "The services 'kind'")
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags())
flags.Merge(c.flags, c.http.NamespaceFlags())
c.help = flags.Usage(help, c.flags)
}
func (c *cmd) Run(args []string) int {
if err := c.flags.Parse(args); err != nil {
return 1
}
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
}
}
svcs := []*api.AgentServiceRegistration{{
Kind: api.ServiceKind(c.flagKind),
ID: c.flagId,
Name: c.flagName,
Address: c.flagAddress,
Port: c.flagPort,
Tags: c.flagTags,
Meta: c.flagMeta,
TaggedAddresses: taggedAddrs,
}}
// Check for arg validation
args = c.flags.Args()
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.")
return 1
}
if len(args) > 0 {
var err error
svcs, err = services.ServicesFromFiles(c.UI, args)
if err != nil {
c.UI.Error(fmt.Sprintf("Error: %s", err))
return 1
}
}
// 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))
return 1
}
c.UI.Output(fmt.Sprintf("Registered service: %s", svc.Name))
}
return 0
}
func (c *cmd) Synopsis() string {
return synopsis
}
func (c *cmd) Help() string {
return c.help
}
const synopsis = "Register services with the local agent"
const help = `
Usage: consul services register [options] [FILE...]
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.
$ consul services register web.json
Additional flags and more advanced use cases are detailed below.
`