2016-06-13 22:56:23 -04:00
|
|
|
package swarm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2016-06-13 22:56:23 -04:00
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
2016-10-27 21:50:49 -04:00
|
|
|
"github.com/pkg/errors"
|
2016-06-13 22:56:23 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
)
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func newUpdateCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-07-20 14:15:08 -04:00
|
|
|
opts := swarmOptions{}
|
2016-06-13 22:56:23 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2016-07-16 10:44:10 -04:00
|
|
|
Use: "update [OPTIONS]",
|
2016-07-20 14:15:08 -04:00
|
|
|
Short: "Update the swarm",
|
2016-06-13 22:56:23 -04:00
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2016-06-17 11:19:51 -04:00
|
|
|
return runUpdate(dockerCli, cmd.Flags(), opts)
|
2016-06-13 22:56:23 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-11-10 15:05:19 -05:00
|
|
|
cmd.Flags().BoolVar(&opts.autolock, flagAutolock, false, "Change manager autolocking setting (true|false)")
|
2016-06-21 17:27:04 -04:00
|
|
|
addSwarmFlags(cmd.Flags(), &opts)
|
2016-06-13 22:56:23 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func runUpdate(dockerCli *command.DockerCli, flags *pflag.FlagSet, opts swarmOptions) error {
|
2016-06-13 22:56:23 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2016-07-20 14:15:08 -04:00
|
|
|
var updateFlags swarm.UpdateFlags
|
|
|
|
|
2016-06-13 22:56:23 -04:00
|
|
|
swarm, err := client.SwarmInspect(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-27 21:50:49 -04:00
|
|
|
prevAutoLock := swarm.Spec.EncryptionConfig.AutoLockManagers
|
|
|
|
|
2016-11-02 15:29:51 -04:00
|
|
|
opts.mergeSwarmSpec(&swarm.Spec, flags)
|
2016-06-15 17:30:54 -04:00
|
|
|
|
2016-10-27 21:50:49 -04:00
|
|
|
curAutoLock := swarm.Spec.EncryptionConfig.AutoLockManagers
|
|
|
|
|
2016-07-20 14:15:08 -04:00
|
|
|
err = client.SwarmUpdate(ctx, swarm.Version, swarm.Spec, updateFlags)
|
2016-06-13 22:56:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-04 00:02:56 -04:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
printUnlockCommand(ctx, dockerCli, unlockKeyResp.UnlockKey)
|
|
|
|
}
|
|
|
|
|
2016-06-13 22:56:23 -04:00
|
|
|
return nil
|
|
|
|
}
|