(ctx context.Context, dockerCLI command.Cli)
| 37 | } |
| 38 | |
| 39 | func runUnlock(ctx context.Context, dockerCLI command.Cli) error { |
| 40 | apiClient := dockerCLI.Client() |
| 41 | |
| 42 | // First see if the node is actually part of a swarm, and if it is actually locked first. |
| 43 | // If it's in any other state than locked, don't ask for the key. |
| 44 | res, err := apiClient.Info(ctx, client.InfoOptions{}) |
| 45 | if err != nil { |
| 46 | return err |
| 47 | } |
| 48 | |
| 49 | switch res.Info.Swarm.LocalNodeState { |
| 50 | case swarm.LocalNodeStateInactive: |
| 51 | return errors.New("error: this node is not part of a swarm") |
| 52 | case swarm.LocalNodeStateLocked: |
| 53 | break |
| 54 | case swarm.LocalNodeStatePending, swarm.LocalNodeStateActive, swarm.LocalNodeStateError: |
| 55 | return errors.New("error: swarm is not locked") |
| 56 | } |
| 57 | |
| 58 | key, err := readKey(dockerCLI.In(), "Enter unlock key: ") |
| 59 | if err != nil { |
| 60 | return err |
| 61 | } |
| 62 | |
| 63 | _, err = apiClient.SwarmUnlock(ctx, client.SwarmUnlockOptions{ |
| 64 | Key: key, |
| 65 | }) |
| 66 | return err |
| 67 | } |
| 68 | |
| 69 | func readKey(in *streams.In, prompt string) (string, error) { |
| 70 | if in.IsTerminal() { |
no test coverage detected
searching dependent graphs…