2016-10-21 21:07:55 -04:00
|
|
|
package swarm
|
|
|
|
|
|
|
|
import (
|
2016-10-27 21:50:49 -04:00
|
|
|
"bufio"
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-10-27 21:50:49 -04:00
|
|
|
"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"
|
2022-03-30 09:27:25 -04:00
|
|
|
"github.com/docker/cli/cli/command/completion"
|
2019-01-28 08:30:31 -05:00
|
|
|
"github.com/docker/cli/cli/streams"
|
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"
|
2020-11-23 15:51:54 -05:00
|
|
|
"golang.org/x/term"
|
2016-10-21 21:07:55 -04:00
|
|
|
)
|
|
|
|
|
2016-12-25 16:23:35 -05:00
|
|
|
func newUnlockCommand(dockerCli command.Cli) *cobra.Command {
|
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 {
|
2017-09-26 12:33:35 -04:00
|
|
|
return runUnlock(dockerCli)
|
2016-11-21 05:22:22 -05:00
|
|
|
},
|
2022-03-29 05:04:50 -04:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"version": "1.24",
|
|
|
|
"swarm": "manager",
|
|
|
|
},
|
2022-03-30 09:27:25 -04:00
|
|
|
ValidArgsFunction: completion.NoComplete,
|
2016-11-21 05:22:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
2016-10-21 21:07:55 -04:00
|
|
|
|
2017-09-26 12:33:35 -04:00
|
|
|
func runUnlock(dockerCli command.Cli) 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
|
|
|
|
2019-01-28 08:30:31 -05:00
|
|
|
func readKey(in *streams.In, prompt string) (string, error) {
|
2016-10-27 21:50:49 -04:00
|
|
|
if in.IsTerminal() {
|
|
|
|
fmt.Print(prompt)
|
2020-11-23 15:51:54 -05:00
|
|
|
dt, err := term.ReadPassword(int(in.FD()))
|
2016-10-27 21:50:49 -04:00
|
|
|
fmt.Println()
|
|
|
|
return string(dt), err
|
|
|
|
}
|
|
|
|
key, err := bufio.NewReader(in).ReadString('\n')
|
|
|
|
if err == io.EOF {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(key), err
|
|
|
|
}
|