2016-10-21 21:07:55 -04:00
|
|
|
package swarm
|
|
|
|
|
|
|
|
import (
|
2016-10-27 21:50:49 -04:00
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
2016-10-21 21:07:55 -04:00
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2016-10-21 21:07:55 -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"
|
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
2016-11-10 11:23:19 -05:00
|
|
|
"golang.org/x/net/context"
|
2016-10-21 21:07:55 -04:00
|
|
|
)
|
|
|
|
|
2016-11-21 05:22:22 -05:00
|
|
|
type unlockOptions struct{}
|
|
|
|
|
2016-12-25 16:23:35 -05:00
|
|
|
func newUnlockCommand(dockerCli command.Cli) *cobra.Command {
|
2016-11-21 05:22:22 -05:00
|
|
|
opts := unlockOptions{}
|
|
|
|
|
2016-10-21 21:07:55 -04:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "unlock",
|
|
|
|
Short: "Unlock swarm",
|
2016-11-21 05:22:22 -05:00
|
|
|
Args: cli.NoArgs,
|
2016-10-21 21:07:55 -04:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2016-11-21 05:22:22 -05:00
|
|
|
return runUnlock(dockerCli, opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
2016-10-21 21:07:55 -04:00
|
|
|
|
2016-12-25 16:23:35 -05:00
|
|
|
func runUnlock(dockerCli command.Cli, opts unlockOptions) error {
|
2016-11-21 05:22:22 -05:00
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
2016-12-15 21:36:37 -05:00
|
|
|
|
2016-12-23 07:48:25 -05:00
|
|
|
// First see if the node is actually part of a swarm, and if it is actually locked first.
|
2016-11-21 05:22:22 -05:00
|
|
|
// 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
|
|
|
|
}
|
2016-12-15 21:36:37 -05:00
|
|
|
|
2016-11-21 05:22:22 -05:00
|
|
|
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")
|
|
|
|
}
|
2016-10-21 21:07:55 -04:00
|
|
|
|
2016-11-21 05:22:22 -05:00
|
|
|
key, err := readKey(dockerCli.In(), "Please enter unlock key: ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req := swarm.UnlockRequest{
|
|
|
|
UnlockKey: key,
|
2016-10-21 21:07:55 -04:00
|
|
|
}
|
|
|
|
|
2016-11-21 05:22:22 -05:00
|
|
|
return client.SwarmUnlock(ctx, req)
|
2016-10-21 21:07:55 -04:00
|
|
|
}
|
2016-10-27 21:50:49 -04:00
|
|
|
|
|
|
|
func readKey(in *command.InStream, prompt string) (string, error) {
|
|
|
|
if in.IsTerminal() {
|
|
|
|
fmt.Print(prompt)
|
|
|
|
dt, err := terminal.ReadPassword(int(in.FD()))
|
|
|
|
fmt.Println()
|
|
|
|
return string(dt), err
|
|
|
|
}
|
|
|
|
key, err := bufio.NewReader(in).ReadString('\n')
|
|
|
|
if err == io.EOF {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(key), err
|
|
|
|
}
|