-
Notifications
You must be signed in to change notification settings - Fork 0
/
join.go
75 lines (64 loc) · 1.89 KB
/
join.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
package swarm
import (
"fmt"
"strings"
"github.com/docker/docker/api/client"
"github.com/docker/docker/cli"
"github.com/docker/engine-api/types/swarm"
"github.com/spf13/cobra"
"golang.org/x/net/context"
)
type joinOptions struct {
remote string
listenAddr NodeAddrOption
// Not a NodeAddrOption because it has no default port.
advertiseAddr string
token string
}
func newJoinCommand(dockerCli *client.DockerCli) *cobra.Command {
opts := joinOptions{
listenAddr: NewListenAddrOption(),
}
cmd := &cobra.Command{
Use: "join [OPTIONS] HOST:PORT",
Short: "Join a swarm as a node and/or manager",
Args: cli.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.remote = args[0]
return runJoin(dockerCli, opts)
},
}
flags := cmd.Flags()
flags.Var(&opts.listenAddr, flagListenAddr, "Listen address (format: <ip|interface>[:port])")
flags.StringVar(&opts.advertiseAddr, flagAdvertiseAddr, "", "Advertised address (format: <ip|interface>[:port])")
flags.StringVar(&opts.token, flagToken, "", "Token for entry into the swarm")
return cmd
}
func runJoin(dockerCli *client.DockerCli, opts joinOptions) error {
client := dockerCli.Client()
ctx := context.Background()
req := swarm.JoinRequest{
JoinToken: opts.token,
ListenAddr: opts.listenAddr.String(),
AdvertiseAddr: opts.advertiseAddr,
RemoteAddrs: []string{opts.remote},
}
err := client.SwarmJoin(ctx, req)
if err != nil {
return err
}
info, err := client.Info(ctx)
if err != nil {
return err
}
_, _, err = client.NodeInspectWithRaw(ctx, info.Swarm.NodeID)
if err != nil {
// TODO(aaronl): is there a better way to do this?
if strings.Contains(err.Error(), "This node is not a swarm manager.") {
fmt.Fprintln(dockerCli.Out(), "This node joined a swarm as a worker.")
}
} else {
fmt.Fprintln(dockerCli.Out(), "This node joined a swarm as a manager.")
}
return nil
}