forked from AliyunContainerService/pouch
/
upgrade.go
82 lines (67 loc) · 2.08 KB
/
upgrade.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"context"
"fmt"
"github.com/spf13/cobra"
)
// upgradeDescription is used to describe upgrade command in detail and auto generate command doc.
var upgradeDescription = "Upgrade a container with new image and args"
// UpgradeCommand use to implement 'upgrade' command, it is used to upgrade a container.
type UpgradeCommand struct {
baseCommand
*container
}
// Init initialize upgrade command.
func (ug *UpgradeCommand) Init(c *Cli) {
ug.cli = c
ug.cmd = &cobra.Command{
Use: "upgrade [OPTIONS] IMAGE [COMMAND] [ARG...]",
Short: "Upgrade a container with new image and args",
Long: upgradeDescription,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return ug.runUpgrade(args)
},
Example: upgradeExample(),
}
ug.addFlags()
}
// addFlags adds flags for specific command.
func (ug *UpgradeCommand) addFlags() {
flagSet := ug.cmd.Flags()
flagSet.SetInterspersed(false)
c := addCommonFlags(flagSet)
ug.container = c
}
// runUpgrade is the entry of UpgradeCommand command.
func (ug *UpgradeCommand) runUpgrade(args []string) error {
config, err := ug.config()
if err != nil {
return fmt.Errorf("failed to upgrade container: %v", err)
}
config.Image = args[0]
if len(args) > 1 {
config.Cmd = args[1:]
}
containerName := ug.name
if containerName == "" {
return fmt.Errorf("failed to upgrade container: must specify container name")
}
ctx := context.Background()
apiClient := ug.cli.Client()
if err := pullMissingImage(ctx, apiClient, config.Image, false); err != nil {
return err
}
err = apiClient.ContainerUpgrade(ctx, containerName, config.ContainerConfig, config.HostConfig)
if err == nil {
fmt.Println(containerName)
}
return err
}
//upgradeExample shows examples in exec command, and is used in auto-generated cli docs.
func upgradeExample() string {
return ` $ pouch run -d -m 20m --name test1 registry.hub.docker.com/library/busybox:latest
4c58d27f58d38776dda31c01c897bbf554c802a9b80ae4dc20be1337f8a969f2
$ pouch upgrade --name test1 registry.hub.docker.com/library/hello-world:latest
test1`
}