github.1git.de/docker/cli@v26.1.3+incompatible/cli/command/plugin/upgrade.go (about) 1 package plugin 2 3 import ( 4 "context" 5 "fmt" 6 "strings" 7 8 "github.com/distribution/reference" 9 "github.com/docker/cli/cli" 10 "github.com/docker/cli/cli/command" 11 "github.com/docker/docker/errdefs" 12 "github.com/docker/docker/pkg/jsonmessage" 13 "github.com/pkg/errors" 14 "github.com/spf13/cobra" 15 ) 16 17 func newUpgradeCommand(dockerCli command.Cli) *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(cmd.Context(), dockerCli, options) 29 }, 30 Annotations: map[string]string{"version": "1.26"}, 31 } 32 33 flags := cmd.Flags() 34 loadPullFlags(dockerCli, &options, flags) 35 flags.BoolVar(&options.skipRemoteCheck, "skip-remote-check", false, "Do not check if specified remote plugin matches existing plugin image") 36 return cmd 37 } 38 39 func runUpgrade(ctx context.Context, dockerCli command.Cli, opts pluginOptions) error { 40 p, _, err := dockerCli.Client().PluginInspectWithRaw(ctx, opts.localName) 41 if err != nil { 42 return errors.Errorf("error reading plugin data: %v", err) 43 } 44 45 if p.Enabled { 46 return errors.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.ParseNormalizedNamed(opts.remote) 54 if err != nil { 55 return errors.Wrap(err, "error parsing remote upgrade image reference") 56 } 57 remote = reference.TagNameOnly(remote) 58 59 old, err := reference.ParseNormalizedNamed(p.PluginReference) 60 if err != nil { 61 return errors.Wrap(err, "error parsing current image reference") 62 } 63 old = reference.TagNameOnly(old) 64 65 fmt.Fprintf(dockerCli.Out(), "Upgrading plugin %s from %s to %s\n", p.Name, reference.FamiliarString(old), reference.FamiliarString(remote)) 66 if !opts.skipRemoteCheck && remote.String() != old.String() { 67 r, err := command.PromptForConfirmation(ctx, dockerCli.In(), dockerCli.Out(), "Plugin images do not match, are you sure?") 68 if err != nil { 69 return err 70 } 71 if !r { 72 return errdefs.Cancelled(errors.New("plugin upgrade has been cancelled")) 73 } 74 } 75 76 options, err := buildPullConfig(ctx, dockerCli, opts, "plugin upgrade") 77 if err != nil { 78 return err 79 } 80 81 responseBody, err := dockerCli.Client().PluginUpgrade(ctx, opts.localName, options) 82 if err != nil { 83 if strings.Contains(err.Error(), "target is image") { 84 return errors.New(err.Error() + " - Use `docker image pull`") 85 } 86 return err 87 } 88 defer responseBody.Close() 89 if err := jsonmessage.DisplayJSONMessagesToStream(responseBody, dockerCli.Out(), nil); err != nil { 90 return err 91 } 92 fmt.Fprintf(dockerCli.Out(), "Upgraded plugin %s to %s\n", opts.localName, opts.remote) // todo: return proper values from the API for this result 93 return nil 94 }