2016-07-20 14:15:08 -04:00
package swarm
import (
"errors"
"fmt"
"github.com/spf13/cobra"
2016-09-06 14:18:12 -04:00
"github.com/docker/docker/api/types/swarm"
2016-07-20 14:15:08 -04:00
"github.com/docker/docker/cli"
2016-09-08 13:11:39 -04:00
"github.com/docker/docker/cli/command"
2016-07-20 14:15:08 -04:00
"golang.org/x/net/context"
)
2016-09-08 13:11:39 -04:00
func newJoinTokenCommand ( dockerCli * command . DockerCli ) * cobra . Command {
2016-07-20 14:15:08 -04:00
var rotate , quiet bool
cmd := & cobra . Command {
2016-08-17 03:45:04 -04:00
Use : "join-token [OPTIONS] (worker|manager)" ,
2016-07-20 14:15:08 -04:00
Short : "Manage join tokens" ,
Args : cli . ExactArgs ( 1 ) ,
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2016-07-27 10:14:29 -04:00
worker := args [ 0 ] == "worker"
manager := args [ 0 ] == "manager"
if ! worker && ! manager {
2016-07-20 14:15:08 -04:00
return errors . New ( "unknown role " + args [ 0 ] )
}
client := dockerCli . Client ( )
ctx := context . Background ( )
if rotate {
var flags swarm . UpdateFlags
swarm , err := client . SwarmInspect ( ctx )
if err != nil {
return err
}
2016-07-27 10:14:29 -04:00
flags . RotateWorkerToken = worker
flags . RotateManagerToken = manager
2016-07-20 14:15:08 -04:00
err = client . SwarmUpdate ( ctx , swarm . Version , swarm . Spec , flags )
if err != nil {
return err
}
2016-07-27 10:14:29 -04:00
if ! quiet {
2016-10-29 03:03:26 -04:00
fmt . Fprintf ( dockerCli . Out ( ) , "Successfully rotated %s join token.\n\n" , args [ 0 ] )
2016-07-27 10:14:29 -04:00
}
2016-07-20 14:15:08 -04:00
}
swarm , err := client . SwarmInspect ( ctx )
if err != nil {
return err
}
if quiet {
2016-07-27 10:14:29 -04:00
if worker {
2016-07-20 14:15:08 -04:00
fmt . Fprintln ( dockerCli . Out ( ) , swarm . JoinTokens . Worker )
2016-07-27 10:14:29 -04:00
} else {
2016-07-20 14:15:08 -04:00
fmt . Fprintln ( dockerCli . Out ( ) , swarm . JoinTokens . Manager )
}
} else {
info , err := client . Info ( ctx )
if err != nil {
return err
}
2016-07-27 10:14:29 -04:00
return printJoinCommand ( ctx , dockerCli , info . Swarm . NodeID , worker , manager )
2016-07-20 14:15:08 -04:00
}
return nil
} ,
}
flags := cmd . Flags ( )
flags . BoolVar ( & rotate , flagRotate , false , "Rotate join token" )
flags . BoolVarP ( & quiet , flagQuiet , "q" , false , "Only display token" )
return cmd
}
2016-09-08 13:11:39 -04:00
func printJoinCommand ( ctx context . Context , dockerCli * command . DockerCli , nodeID string , worker bool , manager bool ) error {
2016-07-20 14:15:08 -04:00
client := dockerCli . Client ( )
swarm , err := client . SwarmInspect ( ctx )
if err != nil {
return err
}
node , _ , err := client . NodeInspectWithRaw ( ctx , nodeID )
if err != nil {
return err
}
if node . ManagerStatus != nil {
if worker {
2016-07-27 10:14:29 -04:00
fmt . Fprintf ( dockerCli . Out ( ) , "To add a worker to this swarm, run the following command:\n\n docker swarm join \\\n --token %s \\\n %s\n\n" , swarm . JoinTokens . Worker , node . ManagerStatus . Addr )
2016-07-20 14:15:08 -04:00
}
if manager {
2016-07-27 10:14:29 -04:00
fmt . Fprintf ( dockerCli . Out ( ) , "To add a manager to this swarm, run the following command:\n\n docker swarm join \\\n --token %s \\\n %s\n\n" , swarm . JoinTokens . Manager , node . ManagerStatus . Addr )
2016-07-20 14:15:08 -04:00
}
}
return nil
}