2016-10-27 21:50:49 -04:00
package swarm
import (
"fmt"
"github.com/spf13/cobra"
2016-10-28 19:35:49 -04:00
"github.com/docker/docker/api/types/swarm"
2016-10-27 21:50:49 -04:00
"github.com/docker/docker/cli"
"github.com/docker/docker/cli/command"
"github.com/pkg/errors"
"golang.org/x/net/context"
)
func newUnlockKeyCommand ( dockerCli * command . DockerCli ) * cobra . Command {
var rotate , quiet bool
cmd := & cobra . Command {
Use : "unlock-key [OPTIONS]" ,
Short : "Manage the unlock key" ,
Args : cli . NoArgs ,
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
client := dockerCli . Client ( )
ctx := context . Background ( )
if rotate {
2016-10-28 19:35:49 -04:00
flags := swarm . UpdateFlags { RotateManagerUnlockKey : true }
swarm , err := client . SwarmInspect ( ctx )
if err != nil {
return err
}
if ! swarm . Spec . EncryptionConfig . AutoLockManagers {
return errors . New ( "cannot rotate because autolock is not turned on" )
}
err = client . SwarmUpdate ( ctx , swarm . Version , swarm . Spec , flags )
if err != nil {
return err
}
if ! quiet {
fmt . Fprintf ( dockerCli . Out ( ) , "Successfully rotated manager unlock key.\n\n" )
}
2016-10-27 21:50:49 -04:00
}
unlockKeyResp , err := client . SwarmGetUnlockKey ( ctx )
if err != nil {
return errors . Wrap ( err , "could not fetch unlock key" )
}
2016-10-28 19:35:49 -04:00
if unlockKeyResp . UnlockKey == "" {
return errors . New ( "no unlock key is set" )
}
2016-10-27 21:50:49 -04:00
if quiet {
fmt . Fprintln ( dockerCli . Out ( ) , unlockKeyResp . UnlockKey )
} else {
printUnlockCommand ( ctx , dockerCli , unlockKeyResp . UnlockKey )
}
return nil
} ,
}
flags := cmd . Flags ( )
flags . BoolVar ( & rotate , flagRotate , false , "Rotate unlock key" )
flags . BoolVarP ( & quiet , flagQuiet , "q" , false , "Only display token" )
return cmd
}
func printUnlockCommand ( ctx context . Context , dockerCli * command . DockerCli , unlockKey string ) {
if len ( unlockKey ) == 0 {
return
}
fmt . Fprintf ( dockerCli . Out ( ) , "To unlock a swarm manager after it restarts, run the `docker swarm unlock`\ncommand and provide the following key:\n\n %s\n\nPlease remember to store this key in a password manager, since without it you\nwill not be able to restart the manager.\n" , unlockKey )
return
}