2016-09-08 13:11:39 -04:00
|
|
|
package swarm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
|
|
"github.com/docker/docker/cli"
|
|
|
|
"github.com/docker/docker/cli/command"
|
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 {
|
|
|
|
return runUpdate(dockerCli, cmd.Flags(), opts)
|
|
|
|
},
|
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
|
|
|
|
},
|
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
|
|
|
|
}
|
|
|
|
|
2016-12-25 16:23:35 -05:00
|
|
|
func runUpdate(dockerCli command.Cli, flags *pflag.FlagSet, opts swarmOptions) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-12-25 16:23:35 -05:00
|
|
|
opts.mergeSwarmSpec(&swarmInspect.Spec, flags)
|
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")
|
|
|
|
}
|
|
|
|
printUnlockCommand(ctx, dockerCli, unlockKeyResp.UnlockKey)
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
return nil
|
|
|
|
}
|