2016-09-08 13:11:39 -04:00
|
|
|
package swarm
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-09-08 13:11:39 -04:00
|
|
|
"fmt"
|
|
|
|
|
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"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2016-10-27 21:50:49 -04:00
|
|
|
"github.com/pkg/errors"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
)
|
|
|
|
|
2016-12-25 16:23:35 -05:00
|
|
|
func newUpdateCommand(dockerCli command.Cli) *cobra.Command {
|
2016-09-08 13:11:39 -04:00
|
|
|
opts := swarmOptions{}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "update [OPTIONS]",
|
|
|
|
Short: "Update the swarm",
|
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2023-09-09 18:27:44 -04:00
|
|
|
return runUpdate(cmd.Context(), dockerCli, cmd.Flags(), opts)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
2016-11-30 16:23:18 -05:00
|
|
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if cmd.Flags().NFlag() == 0 {
|
|
|
|
return pflag.ErrHelp
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
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-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2016-11-10 15:05:19 -05:00
|
|
|
cmd.Flags().BoolVar(&opts.autolock, flagAutolock, false, "Change manager autolocking setting (true|false)")
|
2016-09-08 13:11:39 -04:00
|
|
|
addSwarmFlags(cmd.Flags(), &opts)
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2023-09-09 18:27:44 -04:00
|
|
|
func runUpdate(ctx context.Context, dockerCli command.Cli, flags *pflag.FlagSet, opts swarmOptions) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
|
|
|
|
var updateFlags swarm.UpdateFlags
|
|
|
|
|
2016-12-25 16:23:35 -05:00
|
|
|
swarmInspect, err := client.SwarmInspect(ctx)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-25 16:23:35 -05:00
|
|
|
prevAutoLock := swarmInspect.Spec.EncryptionConfig.AutoLockManagers
|
2016-10-27 21:50:49 -04:00
|
|
|
|
Propagate the provided external CA certificate to the external CA object
in swarm.
Also, fix some CLI command confusions:
1. If the --external-ca flag is provided, require a --ca-cert flag as well, otherwise
the external CA is set but the CA certificate is actually rotated to an internal
cert
2. If a --ca-cert flag is provided, require a --ca-key or --external-ca flag be
provided as well, otherwise either the server will say that the request is
invalid, or if there was previously an external CA corresponding to the cert, it
will succeed. While that works, it's better to require the user to explicitly
set all the parameters of the new desired root CA.
This also changes the `swarm update` function to set the external CA's CACert field,
which while not strictly necessary, makes the CA list more explicit.
Signed-off-by: Ying Li <ying.li@docker.com>
2018-07-02 19:53:23 -04:00
|
|
|
opts.mergeSwarmSpec(&swarmInspect.Spec, flags, swarmInspect.ClusterInfo.TLSInfo.TrustRoot)
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2016-12-25 16:23:35 -05:00
|
|
|
curAutoLock := swarmInspect.Spec.EncryptionConfig.AutoLockManagers
|
2016-10-27 21:50:49 -04:00
|
|
|
|
2016-12-25 16:23:35 -05:00
|
|
|
err = client.SwarmUpdate(ctx, swarmInspect.Version, swarmInspect.Spec, updateFlags)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintln(dockerCli.Out(), "Swarm updated.")
|
|
|
|
|
2016-10-27 21:50:49 -04:00
|
|
|
if curAutoLock && !prevAutoLock {
|
|
|
|
unlockKeyResp, err := client.SwarmGetUnlockKey(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not fetch unlock key")
|
|
|
|
}
|
2017-06-09 17:16:56 -04:00
|
|
|
printUnlockCommand(dockerCli.Out(), unlockKeyResp.UnlockKey)
|
2016-10-27 21:50:49 -04:00
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
return nil
|
|
|
|
}
|