2016-06-13 22:56:23 -04:00
|
|
|
package swarm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2016-06-13 22:56:23 -04:00
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
2016-06-13 22:56:23 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
type joinOptions struct {
|
|
|
|
remote string
|
|
|
|
listenAddr NodeAddrOption
|
2016-06-30 21:07:35 -04:00
|
|
|
// Not a NodeAddrOption because it has no default port.
|
|
|
|
advertiseAddr string
|
|
|
|
token string
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func newJoinCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-06-13 22:56:23 -04:00
|
|
|
opts := joinOptions{
|
2016-06-17 15:42:16 -04:00
|
|
|
listenAddr: NewListenAddrOption(),
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "join [OPTIONS] HOST:PORT",
|
2016-07-20 14:15:08 -04:00
|
|
|
Short: "Join a swarm as a node and/or manager",
|
2016-06-13 22:56:23 -04:00
|
|
|
Args: cli.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
opts.remote = args[0]
|
|
|
|
return runJoin(dockerCli, opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2016-07-21 13:40:19 -04:00
|
|
|
flags.Var(&opts.listenAddr, flagListenAddr, "Listen address (format: <ip|interface>[:port])")
|
|
|
|
flags.StringVar(&opts.advertiseAddr, flagAdvertiseAddr, "", "Advertised address (format: <ip|interface>[:port])")
|
2016-07-20 14:15:08 -04:00
|
|
|
flags.StringVar(&opts.token, flagToken, "", "Token for entry into the swarm")
|
2016-06-13 22:56:23 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func runJoin(dockerCli *command.DockerCli, opts joinOptions) error {
|
2016-06-13 22:56:23 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
req := swarm.JoinRequest{
|
2016-06-30 21:07:35 -04:00
|
|
|
JoinToken: opts.token,
|
|
|
|
ListenAddr: opts.listenAddr.String(),
|
|
|
|
AdvertiseAddr: opts.advertiseAddr,
|
|
|
|
RemoteAddrs: []string{opts.remote},
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
err := client.SwarmJoin(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-20 14:15:08 -04:00
|
|
|
|
|
|
|
info, err := client.Info(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-19 02:35:05 -04:00
|
|
|
if info.Swarm.ControlAvailable {
|
2016-07-20 14:15:08 -04:00
|
|
|
fmt.Fprintln(dockerCli.Out(), "This node joined a swarm as a manager.")
|
2016-10-19 02:35:05 -04:00
|
|
|
} else {
|
|
|
|
fmt.Fprintln(dockerCli.Out(), "This node joined a swarm as a worker.")
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|