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"
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-27 21:21:59 -04:00
"github.com/pkg/errors"
"github.com/spf13/cobra"
2016-09-08 13:11:39 -04:00
)
2016-11-21 05:22:22 -05:00
type joinTokenOptions struct {
role string
rotate bool
quiet bool
}
2016-12-25 16:23:35 -05:00
func newJoinTokenCommand ( dockerCli command . Cli ) * cobra . Command {
2016-11-21 05:22:22 -05:00
opts := joinTokenOptions { }
2016-09-08 13:11:39 -04:00
cmd := & cobra . Command {
Use : "join-token [OPTIONS] (worker|manager)" ,
Short : "Manage join tokens" ,
Args : cli . ExactArgs ( 1 ) ,
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2016-11-21 05:22:22 -05:00
opts . role = args [ 0 ]
2023-09-09 18:27:44 -04:00
return runJoinToken ( cmd . Context ( ) , dockerCli , 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" : "manager" ,
} ,
2016-09-08 13:11:39 -04:00
}
flags := cmd . Flags ( )
2016-11-21 05:22:22 -05:00
flags . BoolVar ( & opts . rotate , flagRotate , false , "Rotate join token" )
flags . BoolVarP ( & opts . quiet , flagQuiet , "q" , false , "Only display token" )
2016-09-08 13:11:39 -04:00
return cmd
}
2023-09-09 18:27:44 -04:00
func runJoinToken ( ctx context . Context , dockerCli command . Cli , opts joinTokenOptions ) error {
2016-11-21 05:22:22 -05:00
worker := opts . role == "worker"
manager := opts . role == "manager"
if ! worker && ! manager {
return errors . New ( "unknown role " + opts . role )
}
2016-09-08 13:11:39 -04:00
client := dockerCli . Client ( )
2016-11-21 05:22:22 -05:00
if opts . rotate {
flags := swarm . UpdateFlags {
RotateWorkerToken : worker ,
RotateManagerToken : manager ,
}
sw , err := client . SwarmInspect ( ctx )
if err != nil {
return err
}
if err := client . SwarmUpdate ( ctx , sw . Version , sw . Spec , flags ) ; err != nil {
return err
}
if ! opts . quiet {
fmt . Fprintf ( dockerCli . Out ( ) , "Successfully rotated %s join token.\n\n" , opts . role )
}
}
// second SwarmInspect in this function,
// this is necessary since SwarmUpdate after first changes the join tokens
sw , err := client . SwarmInspect ( ctx )
if err != nil {
return err
}
if opts . quiet && worker {
fmt . Fprintln ( dockerCli . Out ( ) , sw . JoinTokens . Worker )
return nil
}
if opts . quiet && manager {
fmt . Fprintln ( dockerCli . Out ( ) , sw . JoinTokens . Manager )
return nil
}
2016-09-08 13:11:39 -04:00
2016-11-21 05:22:22 -05:00
info , err := client . Info ( ctx )
2016-09-08 13:11:39 -04:00
if err != nil {
return err
}
2016-11-21 05:22:22 -05:00
return printJoinCommand ( ctx , dockerCli , info . Swarm . NodeID , worker , manager )
}
2016-12-25 16:23:35 -05:00
func printJoinCommand ( ctx context . Context , dockerCli command . Cli , nodeID string , worker bool , manager bool ) error {
2016-11-21 05:22:22 -05:00
client := dockerCli . Client ( )
2016-09-08 13:11:39 -04:00
node , _ , err := client . NodeInspectWithRaw ( ctx , nodeID )
if err != nil {
return err
}
2016-11-21 05:22:22 -05:00
sw , err := client . SwarmInspect ( ctx )
if err != nil {
return err
}
2016-09-08 13:11:39 -04:00
if node . ManagerStatus != nil {
if worker {
2017-04-19 22:35:00 -04:00
fmt . Fprintf ( dockerCli . Out ( ) , "To add a worker to this swarm, run the following command:\n\n docker swarm join --token %s %s\n\n" , sw . JoinTokens . Worker , node . ManagerStatus . Addr )
2016-09-08 13:11:39 -04:00
}
if manager {
2017-04-19 22:35:00 -04:00
fmt . Fprintf ( dockerCli . Out ( ) , "To add a manager to this swarm, run the following command:\n\n docker swarm join --token %s %s\n\n" , sw . JoinTokens . Manager , node . ManagerStatus . Addr )
2016-09-08 13:11:39 -04:00
}
}
return nil
}