diff --git a/components/cli/command/swarm/unlock.go b/components/cli/command/swarm/unlock.go index 048fb56e3d..abb9e89fe7 100644 --- a/components/cli/command/swarm/unlock.go +++ b/components/cli/command/swarm/unlock.go @@ -2,6 +2,7 @@ package swarm import ( "bufio" + "errors" "fmt" "io" "strings" @@ -24,6 +25,22 @@ func newUnlockCommand(dockerCli *command.DockerCli) *cobra.Command { client := dockerCli.Client() ctx := context.Background() + // First see if the node is actually part of a swarm, and if it's is actually locked first. + // If it's in any other state than locked, don't ask for the key. + info, err := client.Info(ctx) + if err != nil { + return err + } + + switch info.Swarm.LocalNodeState { + case swarm.LocalNodeStateInactive: + return errors.New("Error: This node is not part of a swarm") + case swarm.LocalNodeStateLocked: + break + default: + return errors.New("Error: swarm is not locked") + } + key, err := readKey(dockerCli.In(), "Please enter unlock key: ") if err != nil { return err