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