Skip to content

Commit e956ef8

Browse files
committed
Use Consul Catalog, instead of consul agent to lookup services
Fixes: #1
1 parent 0de521a commit e956ef8

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

run.go

+22-19
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ func run(c *cli.Context) error {
3838
return cli.ShowAppHelp(c)
3939
}
4040

41-
// Create a consul client
42-
client, err := consulClient(consulServer)
41+
// Create a consul catalog client
42+
catalog, err := consulCatalog(consulServer)
4343
if err != nil {
4444
return cli.Exit(fmt.Sprintf(
4545
"Error creating consul agent: %s\n", err,
@@ -50,7 +50,7 @@ func run(c *cli.Context) error {
5050
service := c.String("service")
5151
if service == "" {
5252
fmt.Printf("No service specified. Available services:\n")
53-
avail, err := consulServices(client)
53+
avail, err := consulServices(catalog)
5454
if err != nil {
5555
return cli.Exit(fmt.Sprintf(
5656
"Error querying Consul services: %s\n", err,
@@ -63,7 +63,7 @@ func run(c *cli.Context) error {
6363
}
6464

6565
// Add consul services to linked list
66-
machineList, err := populateList(client, service, c.String("user"))
66+
machineList, err := populateList(catalog, service, c.String("user"))
6767
if err != nil {
6868
return cli.Exit(fmt.Sprintf(
6969
"Error populating DSH machine list: %s\n", err,
@@ -81,6 +81,11 @@ func run(c *cli.Context) error {
8181
opts.RemoteCommand = fmt.Sprintf("%s %s", opts.RemoteCommand, v)
8282
}
8383

84+
if machineList.Len() < 1 {
85+
return cli.Exit(fmt.Sprintf(
86+
"No servers found for service %s", service), 1)
87+
}
88+
8489
// Execute DSH!
8590
if err := opts.Execute(); err != nil {
8691
return cli.Exit(fmt.Sprintf("Error executing: %s", err), 1)
@@ -100,9 +105,7 @@ func defaultDSHConfig() dsh.ExecOpts {
100105
}
101106

102107
// Returns all available consul services
103-
func consulServices(client *consul.Client) (map[string][]string, error) {
104-
// Create catalog
105-
catalog := client.Catalog()
108+
func consulServices(catalog *consul.Catalog) (map[string][]string, error) {
106109
services, _, err := catalog.Services(nil)
107110
if err != nil {
108111
return nil, err
@@ -111,31 +114,31 @@ func consulServices(client *consul.Client) (map[string][]string, error) {
111114
}
112115

113116
// Returns a Consul Client
114-
func consulClient(server string) (*consul.Client, error) {
117+
func consulCatalog(server string) (*consul.Catalog, error) {
115118
// Create Consul Client
116119
config := consul.DefaultConfig()
117120
config.Address = server
118-
return consul.NewClient(config)
121+
client, err := consul.NewClient(config)
122+
if err != nil {
123+
return nil, err
124+
}
125+
return client.Catalog(), nil
119126
}
120127

121128
// Populates doubly linked machine list, with a list of requested consul services's addresses
122-
func populateList(client *consul.Client, service string, user string) (*list.List, error) {
123-
// Create consul agent
124-
agent := client.Agent()
125-
services, err := agent.Services()
129+
func populateList(catalog *consul.Catalog, service string, user string) (*list.List, error) {
130+
services, _, err := catalog.Service(service, "", nil)
126131
if err != nil {
127132
return nil, fmt.Errorf("Error querying consul services: %s", err)
128133
}
129134

130135
serviceList := list.New()
131136
for _, v := range services {
132-
if v.Service == service {
133-
remoteAddr := v.Address
134-
if user != "" {
135-
remoteAddr = fmt.Sprintf("%s@%s", user, remoteAddr)
136-
}
137-
addList(serviceList, remoteAddr)
137+
remoteAddr := v.Address
138+
if user != "" {
139+
remoteAddr = fmt.Sprintf("%s@%s", user, remoteAddr)
138140
}
141+
addList(serviceList, remoteAddr)
139142
}
140143

141144
return serviceList, nil

0 commit comments

Comments
 (0)