2016-10-21 18:07:55 -07:00
|
|
|
package swarm
|
|
|
|
|
|
|
|
import (
|
2016-10-27 18:50:49 -07:00
|
|
|
"bufio"
|
2018-05-03 18:02:44 -07:00
|
|
|
"context"
|
2016-10-27 18:50:49 -07:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
2016-10-21 18:07:55 -07:00
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2022-03-30 15:27:25 +02:00
|
|
|
"github.com/docker/cli/cli/command/completion"
|
2019-01-28 14:30:31 +01:00
|
|
|
"github.com/docker/cli/cli/streams"
|
2016-10-21 18:07:55 -07:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2017-03-27 18:21:59 -07:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/spf13/cobra"
|
2020-11-23 21:51:54 +01:00
|
|
|
"golang.org/x/term"
|
2016-10-21 18:07:55 -07:00
|
|
|
)
|
|
|
|
|
2016-12-25 22:23:35 +01:00
|
|
|
func newUnlockCommand(dockerCli command.Cli) *cobra.Command {
|
2016-10-21 18:07:55 -07:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "unlock",
|
|
|
|
Short: "Unlock swarm",
|
2016-11-21 18:22:22 +08:00
|
|
|
Args: cli.NoArgs,
|
2016-10-21 18:07:55 -07:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2023-09-09 22:27:44 +00:00
|
|
|
return runUnlock(cmd.Context(), dockerCli)
|
2016-11-21 18:22:22 +08:00
|
|
|
},
|
2022-03-29 11:04:50 +02:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"version": "1.24",
|
|
|
|
"swarm": "manager",
|
|
|
|
},
|
2022-03-30 15:27:25 +02:00
|
|
|
ValidArgsFunction: completion.NoComplete,
|
2016-11-21 18:22:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
2016-10-21 18:07:55 -07:00
|
|
|
|
2023-09-09 22:27:44 +00:00
|
|
|
func runUnlock(ctx context.Context, dockerCli command.Cli) error {
|
2016-11-21 18:22:22 +08:00
|
|
|
client := dockerCli.Client()
|
2016-12-15 18:36:37 -08:00
|
|
|
|
2016-12-23 20:48:25 +08:00
|
|
|
// First see if the node is actually part of a swarm, and if it is actually locked first.
|
2016-11-21 18:22:22 +08: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 18:36:37 -08:00
|
|
|
|
2016-11-21 18:22:22 +08:00
|
|
|
switch info.Swarm.LocalNodeState {
|
|
|
|
case swarm.LocalNodeStateInactive:
|
|
|
|
return errors.New("Error: This node is not part of a swarm")
|
|
|
|
case swarm.LocalNodeStateLocked:
|
|
|
|
break
|
2025-05-19 19:10:53 +02:00
|
|
|
case swarm.LocalNodeStatePending, swarm.LocalNodeStateActive, swarm.LocalNodeStateError:
|
2016-11-21 18:22:22 +08:00
|
|
|
return errors.New("Error: swarm is not locked")
|
|
|
|
}
|
2016-10-21 18:07:55 -07:00
|
|
|
|
2024-04-26 13:16:51 -05:00
|
|
|
key, err := readKey(dockerCli.In(), "Enter unlock key: ")
|
2016-11-21 18:22:22 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-21 18:07:55 -07:00
|
|
|
|
2025-05-19 19:10:53 +02:00
|
|
|
return client.SwarmUnlock(ctx, swarm.UnlockRequest{
|
|
|
|
UnlockKey: key,
|
|
|
|
})
|
2016-10-21 18:07:55 -07:00
|
|
|
}
|
2016-10-27 18:50:49 -07:00
|
|
|
|
2019-01-28 14:30:31 +01:00
|
|
|
func readKey(in *streams.In, prompt string) (string, error) {
|
2016-10-27 18:50:49 -07:00
|
|
|
if in.IsTerminal() {
|
|
|
|
fmt.Print(prompt)
|
2020-11-23 21:51:54 +01:00
|
|
|
dt, err := term.ReadPassword(int(in.FD()))
|
2016-10-27 18:50:49 -07: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
|
|
|
|
}
|