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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newUpdateCommand(dockerCli *command.DockerCli) *cobra.Command {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func runUpdate(dockerCli *command.DockerCli, flags *pflag.FlagSet, opts swarmOptions) error {
|
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
var updateFlags swarm.UpdateFlags
|
|
|
|
|
|
|
|
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-09-08 13:11:39 -04:00
|
|
|
|
2016-10-27 21:50:49 -04:00
|
|
|
curAutoLock := swarm.Spec.EncryptionConfig.AutoLockManagers
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
err = client.SwarmUpdate(ctx, swarm.Version, swarm.Spec, updateFlags)
|
|
|
|
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
|
|
|
|
}
|