Before asking a user for the unlock key when they run docker swarm unlock, actually

check to see if the node is part of a swarm, and if so, if it is unlocked first.
If neither of these are true, abort the command.

Signed-off-by: Ying Li <ying.li@docker.com>
This commit is contained in:
Ying Li
2016-12-15 18:36:37 -08:00
parent 15bcbad07a
commit e4102ce61e

View File

@ -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