github.com/kunnos/engine@v1.13.1/cli/command/swarm/update.go (about)

     1  package swarm
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"golang.org/x/net/context"
     7  
     8  	"github.com/docker/docker/api/types/swarm"
     9  	"github.com/docker/docker/cli"
    10  	"github.com/docker/docker/cli/command"
    11  	"github.com/pkg/errors"
    12  	"github.com/spf13/cobra"
    13  	"github.com/spf13/pflag"
    14  )
    15  
    16  func newUpdateCommand(dockerCli *command.DockerCli) *cobra.Command {
    17  	opts := swarmOptions{}
    18  
    19  	cmd := &cobra.Command{
    20  		Use:   "update [OPTIONS]",
    21  		Short: "Update the swarm",
    22  		Args:  cli.NoArgs,
    23  		RunE: func(cmd *cobra.Command, args []string) error {
    24  			return runUpdate(dockerCli, cmd.Flags(), opts)
    25  		},
    26  		PreRunE: func(cmd *cobra.Command, args []string) error {
    27  			if cmd.Flags().NFlag() == 0 {
    28  				return pflag.ErrHelp
    29  			}
    30  			return nil
    31  		},
    32  	}
    33  
    34  	cmd.Flags().BoolVar(&opts.autolock, flagAutolock, false, "Change manager autolocking setting (true|false)")
    35  	addSwarmFlags(cmd.Flags(), &opts)
    36  	return cmd
    37  }
    38  
    39  func runUpdate(dockerCli *command.DockerCli, flags *pflag.FlagSet, opts swarmOptions) error {
    40  	client := dockerCli.Client()
    41  	ctx := context.Background()
    42  
    43  	var updateFlags swarm.UpdateFlags
    44  
    45  	swarm, err := client.SwarmInspect(ctx)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	prevAutoLock := swarm.Spec.EncryptionConfig.AutoLockManagers
    51  
    52  	opts.mergeSwarmSpec(&swarm.Spec, flags)
    53  
    54  	curAutoLock := swarm.Spec.EncryptionConfig.AutoLockManagers
    55  
    56  	err = client.SwarmUpdate(ctx, swarm.Version, swarm.Spec, updateFlags)
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	fmt.Fprintln(dockerCli.Out(), "Swarm updated.")
    62  
    63  	if curAutoLock && !prevAutoLock {
    64  		unlockKeyResp, err := client.SwarmGetUnlockKey(ctx)
    65  		if err != nil {
    66  			return errors.Wrap(err, "could not fetch unlock key")
    67  		}
    68  		printUnlockCommand(ctx, dockerCli, unlockKeyResp.UnlockKey)
    69  	}
    70  
    71  	return nil
    72  }