github.com/kunnos/engine@v1.13.1/cli/command/plugin/upgrade.go (about)

     1  package plugin
     2  
     3  import (
     4  	"bufio"
     5  	"context"
     6  	"fmt"
     7  	"strings"
     8  
     9  	"github.com/docker/docker/cli"
    10  	"github.com/docker/docker/cli/command"
    11  	"github.com/docker/docker/pkg/jsonmessage"
    12  	"github.com/docker/docker/reference"
    13  	"github.com/pkg/errors"
    14  	"github.com/spf13/cobra"
    15  )
    16  
    17  func newUpgradeCommand(dockerCli *command.DockerCli) *cobra.Command {
    18  	var options pluginOptions
    19  	cmd := &cobra.Command{
    20  		Use:   "upgrade [OPTIONS] PLUGIN [REMOTE]",
    21  		Short: "Upgrade an existing plugin",
    22  		Args:  cli.RequiresRangeArgs(1, 2),
    23  		RunE: func(cmd *cobra.Command, args []string) error {
    24  			options.localName = args[0]
    25  			if len(args) == 2 {
    26  				options.remote = args[1]
    27  			}
    28  			return runUpgrade(dockerCli, options)
    29  		},
    30  	}
    31  
    32  	flags := cmd.Flags()
    33  	loadPullFlags(&options, flags)
    34  	flags.BoolVar(&options.skipRemoteCheck, "skip-remote-check", false, "Do not check if specified remote plugin matches existing plugin image")
    35  	return cmd
    36  }
    37  
    38  func runUpgrade(dockerCli *command.DockerCli, opts pluginOptions) error {
    39  	ctx := context.Background()
    40  	p, _, err := dockerCli.Client().PluginInspectWithRaw(ctx, opts.localName)
    41  	if err != nil {
    42  		return fmt.Errorf("error reading plugin data: %v", err)
    43  	}
    44  
    45  	if p.Enabled {
    46  		return fmt.Errorf("the plugin must be disabled before upgrading")
    47  	}
    48  
    49  	opts.localName = p.Name
    50  	if opts.remote == "" {
    51  		opts.remote = p.PluginReference
    52  	}
    53  	remote, err := reference.ParseNamed(opts.remote)
    54  	if err != nil {
    55  		return errors.Wrap(err, "error parsing remote upgrade image reference")
    56  	}
    57  	remote = reference.WithDefaultTag(remote)
    58  
    59  	old, err := reference.ParseNamed(p.PluginReference)
    60  	if err != nil {
    61  		return errors.Wrap(err, "error parsing current image reference")
    62  	}
    63  	old = reference.WithDefaultTag(old)
    64  
    65  	fmt.Fprintf(dockerCli.Out(), "Upgrading plugin %s from %s to %s\n", p.Name, old, remote)
    66  	if !opts.skipRemoteCheck && remote.String() != old.String() {
    67  		_, err := fmt.Fprint(dockerCli.Out(), "Plugin images do not match, are you sure? ")
    68  		if err != nil {
    69  			return errors.Wrap(err, "error writing to stdout")
    70  		}
    71  
    72  		rdr := bufio.NewReader(dockerCli.In())
    73  		line, _, err := rdr.ReadLine()
    74  		if err != nil {
    75  			return errors.Wrap(err, "error reading from stdin")
    76  		}
    77  		if strings.ToLower(string(line)) != "y" {
    78  			return errors.New("canceling upgrade request")
    79  		}
    80  	}
    81  
    82  	options, err := buildPullConfig(ctx, dockerCli, opts, "plugin upgrade")
    83  	if err != nil {
    84  		return err
    85  	}
    86  
    87  	responseBody, err := dockerCli.Client().PluginUpgrade(ctx, opts.localName, options)
    88  	if err != nil {
    89  		if strings.Contains(err.Error(), "target is image") {
    90  			return errors.New(err.Error() + " - Use `docker image pull`")
    91  		}
    92  		return err
    93  	}
    94  	defer responseBody.Close()
    95  	if err := jsonmessage.DisplayJSONMessagesToStream(responseBody, dockerCli.Out(), nil); err != nil {
    96  		return err
    97  	}
    98  	fmt.Fprintf(dockerCli.Out(), "Upgraded plugin %s to %s\n", opts.localName, opts.remote) // todo: return proper values from the API for this result
    99  	return nil
   100  }