-
Notifications
You must be signed in to change notification settings - Fork 950
/
Copy pathupgrade.go
94 lines (78 loc) · 2.6 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
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"context"
"fmt"
"strings"
"github.com/alibaba/pouch/apis/types"
"github.com/spf13/cobra"
)
// upgradeDescription is used to describe upgrade command in detail and auto generate command doc.
var upgradeDescription = "upgrade is a feature to replace a container's image. " +
"You can specify the new Entrypoint and Cmd for the new container. When you want to update " +
"a container's image, but inherit the network and volumes of the old container, then you should " +
"think about the upgrade feature."
// UpgradeCommand use to implement 'upgrade' command, it is used to upgrade a container.
type UpgradeCommand struct {
baseCommand
entrypoint string
image string
}
// Init initialize upgrade command.
func (ug *UpgradeCommand) Init(c *Cli) {
ug.cli = c
ug.cmd = &cobra.Command{
Use: "upgrade [OPTIONS] CONTAINER [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)
flagSet.StringVar(&ug.entrypoint, "entrypoint", "", "Overwrite the default ENTRYPOINT of the image")
flagSet.StringVar(&ug.image, "image", "", "Specify image of the new container")
}
// runUpgrade is the entry of UpgradeCommand command.
func (ug *UpgradeCommand) runUpgrade(args []string) error {
var cmd []string
name := args[0]
if name == "" {
return fmt.Errorf("failed to upgrade container: must specify container name")
}
if len(args) > 1 {
cmd = args[1:]
}
image := ug.image
if image == "" {
return fmt.Errorf("failed to upgrade container: must specify new image")
}
upgradeConfig := &types.ContainerUpgradeConfig{
Image: image,
Cmd: cmd,
Entrypoint: strings.Fields(ug.entrypoint),
}
ctx := context.Background()
apiClient := ug.cli.Client()
if err := pullMissingImage(ctx, apiClient, image, false); err != nil {
return err
}
if err := apiClient.ContainerUpgrade(ctx, name, upgradeConfig); err != nil {
return err
}
fmt.Println(name)
return nil
}
//upgradeExample shows examples in exec command, and is used in auto-generated cli docs.
func upgradeExample() string {
return ` $ pouch run -d -m 20m --name test registry.hub.docker.com/library/busybox:latest
4c58d27f58d38776dda31c01c897bbf554c802a9b80ae4dc20be1337f8a969f2
$ pouch upgrade --image registry.hub.docker.com/library/hello-world:latest test
test`
}