2016-09-08 13:11:39 -04:00
|
|
|
package swarm
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-09-08 13:11:39 -04:00
|
|
|
"fmt"
|
2016-12-21 21:06:16 -05:00
|
|
|
"strings"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
2016-12-21 21:06:16 -05:00
|
|
|
"github.com/spf13/pflag"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type joinOptions struct {
|
|
|
|
remote string
|
|
|
|
listenAddr NodeAddrOption
|
|
|
|
// Not a NodeAddrOption because it has no default port.
|
|
|
|
advertiseAddr string
|
2017-04-14 19:54:17 -04:00
|
|
|
dataPathAddr string
|
2016-09-08 13:11:39 -04:00
|
|
|
token string
|
2016-12-21 21:06:16 -05:00
|
|
|
availability string
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2016-12-25 16:23:35 -05:00
|
|
|
func newJoinCommand(dockerCli command.Cli) *cobra.Command {
|
2016-09-08 13:11:39 -04:00
|
|
|
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]
|
2016-12-21 21:06:16 -05:00
|
|
|
return runJoin(dockerCli, cmd.Flags(), opts)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
2022-03-29 05:04:50 -04:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"version": "1.24",
|
|
|
|
"swarm": "", // swarm join does not require swarm to be active, and is always available on API 1.24 and up
|
|
|
|
},
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2023-01-03 05:12:24 -05:00
|
|
|
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.dataPathAddr, flagDataPathAddr, "", `Address or interface to use for data path traffic (format: "<ip|interface>")`)
|
2018-07-30 04:54:05 -04:00
|
|
|
flags.SetAnnotation(flagDataPathAddr, "version", []string{"1.31"})
|
2016-09-08 13:11:39 -04:00
|
|
|
flags.StringVar(&opts.token, flagToken, "", "Token for entry into the swarm")
|
2023-01-03 05:12:24 -05:00
|
|
|
flags.StringVar(&opts.availability, flagAvailability, "active", `Availability of the node ("active", "pause", "drain")`)
|
2016-09-08 13:11:39 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-12-21 21:06:16 -05:00
|
|
|
func runJoin(dockerCli command.Cli, flags *pflag.FlagSet, opts joinOptions) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
req := swarm.JoinRequest{
|
|
|
|
JoinToken: opts.token,
|
|
|
|
ListenAddr: opts.listenAddr.String(),
|
|
|
|
AdvertiseAddr: opts.advertiseAddr,
|
2017-04-14 19:54:17 -04:00
|
|
|
DataPathAddr: opts.dataPathAddr,
|
2016-09-08 13:11:39 -04:00
|
|
|
RemoteAddrs: []string{opts.remote},
|
|
|
|
}
|
2016-12-21 21:06:16 -05:00
|
|
|
if flags.Changed(flagAvailability) {
|
|
|
|
availability := swarm.NodeAvailability(strings.ToLower(opts.availability))
|
|
|
|
switch availability {
|
|
|
|
case swarm.NodeAvailabilityActive, swarm.NodeAvailabilityPause, swarm.NodeAvailabilityDrain:
|
|
|
|
req.Availability = availability
|
|
|
|
default:
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf("invalid availability %q, only active, pause and drain are supported", opts.availability)
|
2016-12-21 21:06:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
err := client.SwarmJoin(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := client.Info(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-19 02:35:05 -04:00
|
|
|
if info.Swarm.ControlAvailable {
|
2016-09-08 13:11:39 -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-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|