This fix is part of the effort to convert commands to spf13/cobra #23211.
Thif fix coverted command `docker update` to use spf13/cobra
Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
| 11 | 10 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,150 @@ |
| 0 |
+package container |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "fmt" |
|
| 4 |
+ "strings" |
|
| 5 |
+ |
|
| 6 |
+ "golang.org/x/net/context" |
|
| 7 |
+ |
|
| 8 |
+ "github.com/docker/docker/api/client" |
|
| 9 |
+ "github.com/docker/docker/cli" |
|
| 10 |
+ runconfigopts "github.com/docker/docker/runconfig/opts" |
|
| 11 |
+ containertypes "github.com/docker/engine-api/types/container" |
|
| 12 |
+ "github.com/docker/go-units" |
|
| 13 |
+ "github.com/spf13/cobra" |
|
| 14 |
+) |
|
| 15 |
+ |
|
| 16 |
+type updateOptions struct {
|
|
| 17 |
+ blkioWeight uint16 |
|
| 18 |
+ cpuPeriod int64 |
|
| 19 |
+ cpuQuota int64 |
|
| 20 |
+ cpusetCpus string |
|
| 21 |
+ cpusetMems string |
|
| 22 |
+ cpuShares int64 |
|
| 23 |
+ memoryString string |
|
| 24 |
+ memoryReservation string |
|
| 25 |
+ memorySwap string |
|
| 26 |
+ kernelMemory string |
|
| 27 |
+ restartPolicy string |
|
| 28 |
+ |
|
| 29 |
+ nFlag int |
|
| 30 |
+ |
|
| 31 |
+ containers []string |
|
| 32 |
+} |
|
| 33 |
+ |
|
| 34 |
+// NewUpdateCommand creats a new cobra.Command for `docker update` |
|
| 35 |
+func NewUpdateCommand(dockerCli *client.DockerCli) *cobra.Command {
|
|
| 36 |
+ var opts updateOptions |
|
| 37 |
+ |
|
| 38 |
+ cmd := &cobra.Command{
|
|
| 39 |
+ Use: "update [OPTIONS] CONTAINER [CONTAINER...]", |
|
| 40 |
+ Short: "Update configuration of one or more containers", |
|
| 41 |
+ Args: cli.RequiresMinArgs(1), |
|
| 42 |
+ RunE: func(cmd *cobra.Command, args []string) error {
|
|
| 43 |
+ opts.containers = args |
|
| 44 |
+ opts.nFlag = cmd.Flags().NFlag() |
|
| 45 |
+ return runUpdate(dockerCli, &opts) |
|
| 46 |
+ }, |
|
| 47 |
+ } |
|
| 48 |
+ cmd.SetFlagErrorFunc(flagErrorFunc) |
|
| 49 |
+ |
|
| 50 |
+ flags := cmd.Flags() |
|
| 51 |
+ flags.Uint16Var(&opts.blkioWeight, "blkio-weight", 0, "Block IO (relative weight), between 10 and 1000") |
|
| 52 |
+ flags.Int64Var(&opts.cpuPeriod, "cpu-period", 0, "Limit CPU CFS (Completely Fair Scheduler) period") |
|
| 53 |
+ flags.Int64Var(&opts.cpuQuota, "cpu-quota", 0, "Limit CPU CFS (Completely Fair Scheduler) quota") |
|
| 54 |
+ flags.StringVar(&opts.cpusetCpus, "cpuset-cpus", "", "CPUs in which to allow execution (0-3, 0,1)") |
|
| 55 |
+ flags.StringVar(&opts.cpusetMems, "cpuset-mems", "", "MEMs in which to allow execution (0-3, 0,1)") |
|
| 56 |
+ flags.Int64VarP(&opts.cpuShares, "cpu-shares", "c", 0, "CPU shares (relative weight)") |
|
| 57 |
+ flags.StringVarP(&opts.memoryString, "memory", "m", "", "Memory limit") |
|
| 58 |
+ flags.StringVar(&opts.memoryReservation, "memory-reservation", "", "Memory soft limit") |
|
| 59 |
+ flags.StringVar(&opts.memorySwap, "memory-swap", "", "Swap limit equal to memory plus swap: '-1' to enable unlimited swap") |
|
| 60 |
+ flags.StringVar(&opts.kernelMemory, "kernel-memory", "", "Kernel memory limit") |
|
| 61 |
+ flags.StringVar(&opts.restartPolicy, "restart", "", "Restart policy to apply when a container exits") |
|
| 62 |
+ |
|
| 63 |
+ return cmd |
|
| 64 |
+} |
|
| 65 |
+ |
|
| 66 |
+func runUpdate(dockerCli *client.DockerCli, opts *updateOptions) error {
|
|
| 67 |
+ var err error |
|
| 68 |
+ |
|
| 69 |
+ if opts.nFlag == 0 {
|
|
| 70 |
+ return fmt.Errorf("You must provide one or more flags when using this command.")
|
|
| 71 |
+ } |
|
| 72 |
+ |
|
| 73 |
+ var memory int64 |
|
| 74 |
+ if opts.memoryString != "" {
|
|
| 75 |
+ memory, err = units.RAMInBytes(opts.memoryString) |
|
| 76 |
+ if err != nil {
|
|
| 77 |
+ return err |
|
| 78 |
+ } |
|
| 79 |
+ } |
|
| 80 |
+ |
|
| 81 |
+ var memoryReservation int64 |
|
| 82 |
+ if opts.memoryReservation != "" {
|
|
| 83 |
+ memoryReservation, err = units.RAMInBytes(opts.memoryReservation) |
|
| 84 |
+ if err != nil {
|
|
| 85 |
+ return err |
|
| 86 |
+ } |
|
| 87 |
+ } |
|
| 88 |
+ |
|
| 89 |
+ var memorySwap int64 |
|
| 90 |
+ if opts.memorySwap != "" {
|
|
| 91 |
+ if opts.memorySwap == "-1" {
|
|
| 92 |
+ memorySwap = -1 |
|
| 93 |
+ } else {
|
|
| 94 |
+ memorySwap, err = units.RAMInBytes(opts.memorySwap) |
|
| 95 |
+ if err != nil {
|
|
| 96 |
+ return err |
|
| 97 |
+ } |
|
| 98 |
+ } |
|
| 99 |
+ } |
|
| 100 |
+ |
|
| 101 |
+ var kernelMemory int64 |
|
| 102 |
+ if opts.kernelMemory != "" {
|
|
| 103 |
+ kernelMemory, err = units.RAMInBytes(opts.kernelMemory) |
|
| 104 |
+ if err != nil {
|
|
| 105 |
+ return err |
|
| 106 |
+ } |
|
| 107 |
+ } |
|
| 108 |
+ |
|
| 109 |
+ var restartPolicy containertypes.RestartPolicy |
|
| 110 |
+ if opts.restartPolicy != "" {
|
|
| 111 |
+ restartPolicy, err = runconfigopts.ParseRestartPolicy(opts.restartPolicy) |
|
| 112 |
+ if err != nil {
|
|
| 113 |
+ return err |
|
| 114 |
+ } |
|
| 115 |
+ } |
|
| 116 |
+ |
|
| 117 |
+ resources := containertypes.Resources{
|
|
| 118 |
+ BlkioWeight: opts.blkioWeight, |
|
| 119 |
+ CpusetCpus: opts.cpusetCpus, |
|
| 120 |
+ CpusetMems: opts.cpusetMems, |
|
| 121 |
+ CPUShares: opts.cpuShares, |
|
| 122 |
+ Memory: memory, |
|
| 123 |
+ MemoryReservation: memoryReservation, |
|
| 124 |
+ MemorySwap: memorySwap, |
|
| 125 |
+ KernelMemory: kernelMemory, |
|
| 126 |
+ CPUPeriod: opts.cpuPeriod, |
|
| 127 |
+ CPUQuota: opts.cpuQuota, |
|
| 128 |
+ } |
|
| 129 |
+ |
|
| 130 |
+ updateConfig := containertypes.UpdateConfig{
|
|
| 131 |
+ Resources: resources, |
|
| 132 |
+ RestartPolicy: restartPolicy, |
|
| 133 |
+ } |
|
| 134 |
+ |
|
| 135 |
+ ctx := context.Background() |
|
| 136 |
+ |
|
| 137 |
+ var errs []string |
|
| 138 |
+ for _, container := range opts.containers {
|
|
| 139 |
+ if err := dockerCli.Client().ContainerUpdate(ctx, container, updateConfig); err != nil {
|
|
| 140 |
+ errs = append(errs, err.Error()) |
|
| 141 |
+ } else {
|
|
| 142 |
+ fmt.Fprintf(dockerCli.Out(), "%s\n", container) |
|
| 143 |
+ } |
|
| 144 |
+ } |
|
| 145 |
+ if len(errs) > 0 {
|
|
| 146 |
+ return fmt.Errorf("%s", strings.Join(errs, "\n"))
|
|
| 147 |
+ } |
|
| 148 |
+ return nil |
|
| 149 |
+} |
| 0 | 150 |
deleted file mode 100644 |
| ... | ... |
@@ -1,120 +0,0 @@ |
| 1 |
-package client |
|
| 2 |
- |
|
| 3 |
-import ( |
|
| 4 |
- "fmt" |
|
| 5 |
- "strings" |
|
| 6 |
- |
|
| 7 |
- "golang.org/x/net/context" |
|
| 8 |
- |
|
| 9 |
- Cli "github.com/docker/docker/cli" |
|
| 10 |
- flag "github.com/docker/docker/pkg/mflag" |
|
| 11 |
- "github.com/docker/docker/runconfig/opts" |
|
| 12 |
- "github.com/docker/engine-api/types/container" |
|
| 13 |
- "github.com/docker/go-units" |
|
| 14 |
-) |
|
| 15 |
- |
|
| 16 |
-// CmdUpdate updates resources of one or more containers. |
|
| 17 |
-// |
|
| 18 |
-// Usage: docker update [OPTIONS] CONTAINER [CONTAINER...] |
|
| 19 |
-func (cli *DockerCli) CmdUpdate(args ...string) error {
|
|
| 20 |
- cmd := Cli.Subcmd("update", []string{"CONTAINER [CONTAINER...]"}, Cli.DockerCommands["update"].Description, true)
|
|
| 21 |
- flBlkioWeight := cmd.Uint16([]string{"-blkio-weight"}, 0, "Block IO (relative weight), between 10 and 1000")
|
|
| 22 |
- flCPUPeriod := cmd.Int64([]string{"-cpu-period"}, 0, "Limit CPU CFS (Completely Fair Scheduler) period")
|
|
| 23 |
- flCPUQuota := cmd.Int64([]string{"-cpu-quota"}, 0, "Limit CPU CFS (Completely Fair Scheduler) quota")
|
|
| 24 |
- flCpusetCpus := cmd.String([]string{"-cpuset-cpus"}, "", "CPUs in which to allow execution (0-3, 0,1)")
|
|
| 25 |
- flCpusetMems := cmd.String([]string{"-cpuset-mems"}, "", "MEMs in which to allow execution (0-3, 0,1)")
|
|
| 26 |
- flCPUShares := cmd.Int64([]string{"c", "-cpu-shares"}, 0, "CPU shares (relative weight)")
|
|
| 27 |
- flMemoryString := cmd.String([]string{"m", "-memory"}, "", "Memory limit")
|
|
| 28 |
- flMemoryReservation := cmd.String([]string{"-memory-reservation"}, "", "Memory soft limit")
|
|
| 29 |
- flMemorySwap := cmd.String([]string{"-memory-swap"}, "", "Swap limit equal to memory plus swap: '-1' to enable unlimited swap")
|
|
| 30 |
- flKernelMemory := cmd.String([]string{"-kernel-memory"}, "", "Kernel memory limit")
|
|
| 31 |
- flRestartPolicy := cmd.String([]string{"-restart"}, "", "Restart policy to apply when a container exits")
|
|
| 32 |
- |
|
| 33 |
- cmd.Require(flag.Min, 1) |
|
| 34 |
- cmd.ParseFlags(args, true) |
|
| 35 |
- if cmd.NFlag() == 0 {
|
|
| 36 |
- return fmt.Errorf("You must provide one or more flags when using this command.")
|
|
| 37 |
- } |
|
| 38 |
- |
|
| 39 |
- var err error |
|
| 40 |
- var flMemory int64 |
|
| 41 |
- if *flMemoryString != "" {
|
|
| 42 |
- flMemory, err = units.RAMInBytes(*flMemoryString) |
|
| 43 |
- if err != nil {
|
|
| 44 |
- return err |
|
| 45 |
- } |
|
| 46 |
- } |
|
| 47 |
- |
|
| 48 |
- var memoryReservation int64 |
|
| 49 |
- if *flMemoryReservation != "" {
|
|
| 50 |
- memoryReservation, err = units.RAMInBytes(*flMemoryReservation) |
|
| 51 |
- if err != nil {
|
|
| 52 |
- return err |
|
| 53 |
- } |
|
| 54 |
- } |
|
| 55 |
- |
|
| 56 |
- var memorySwap int64 |
|
| 57 |
- if *flMemorySwap != "" {
|
|
| 58 |
- if *flMemorySwap == "-1" {
|
|
| 59 |
- memorySwap = -1 |
|
| 60 |
- } else {
|
|
| 61 |
- memorySwap, err = units.RAMInBytes(*flMemorySwap) |
|
| 62 |
- if err != nil {
|
|
| 63 |
- return err |
|
| 64 |
- } |
|
| 65 |
- } |
|
| 66 |
- } |
|
| 67 |
- |
|
| 68 |
- var kernelMemory int64 |
|
| 69 |
- if *flKernelMemory != "" {
|
|
| 70 |
- kernelMemory, err = units.RAMInBytes(*flKernelMemory) |
|
| 71 |
- if err != nil {
|
|
| 72 |
- return err |
|
| 73 |
- } |
|
| 74 |
- } |
|
| 75 |
- |
|
| 76 |
- var restartPolicy container.RestartPolicy |
|
| 77 |
- if *flRestartPolicy != "" {
|
|
| 78 |
- restartPolicy, err = opts.ParseRestartPolicy(*flRestartPolicy) |
|
| 79 |
- if err != nil {
|
|
| 80 |
- return err |
|
| 81 |
- } |
|
| 82 |
- } |
|
| 83 |
- |
|
| 84 |
- resources := container.Resources{
|
|
| 85 |
- BlkioWeight: *flBlkioWeight, |
|
| 86 |
- CpusetCpus: *flCpusetCpus, |
|
| 87 |
- CpusetMems: *flCpusetMems, |
|
| 88 |
- CPUShares: *flCPUShares, |
|
| 89 |
- Memory: flMemory, |
|
| 90 |
- MemoryReservation: memoryReservation, |
|
| 91 |
- MemorySwap: memorySwap, |
|
| 92 |
- KernelMemory: kernelMemory, |
|
| 93 |
- CPUPeriod: *flCPUPeriod, |
|
| 94 |
- CPUQuota: *flCPUQuota, |
|
| 95 |
- } |
|
| 96 |
- |
|
| 97 |
- updateConfig := container.UpdateConfig{
|
|
| 98 |
- Resources: resources, |
|
| 99 |
- RestartPolicy: restartPolicy, |
|
| 100 |
- } |
|
| 101 |
- |
|
| 102 |
- ctx := context.Background() |
|
| 103 |
- |
|
| 104 |
- names := cmd.Args() |
|
| 105 |
- var errs []string |
|
| 106 |
- |
|
| 107 |
- for _, name := range names {
|
|
| 108 |
- if err := cli.client.ContainerUpdate(ctx, name, updateConfig); err != nil {
|
|
| 109 |
- errs = append(errs, err.Error()) |
|
| 110 |
- } else {
|
|
| 111 |
- fmt.Fprintf(cli.out, "%s\n", name) |
|
| 112 |
- } |
|
| 113 |
- } |
|
| 114 |
- |
|
| 115 |
- if len(errs) > 0 {
|
|
| 116 |
- return fmt.Errorf("%s", strings.Join(errs, "\n"))
|
|
| 117 |
- } |
|
| 118 |
- |
|
| 119 |
- return nil |
|
| 120 |
-} |
| ... | ... |
@@ -66,6 +66,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor {
|
| 66 | 66 |
container.NewStopCommand(dockerCli), |
| 67 | 67 |
container.NewTopCommand(dockerCli), |
| 68 | 68 |
container.NewUnpauseCommand(dockerCli), |
| 69 |
+ container.NewUpdateCommand(dockerCli), |
|
| 69 | 70 |
container.NewWaitCommand(dockerCli), |
| 70 | 71 |
image.NewBuildCommand(dockerCli), |
| 71 | 72 |
image.NewHistoryCommand(dockerCli), |
| ... | ... |
@@ -10,7 +10,6 @@ type Command struct {
|
| 10 | 10 |
var DockerCommandUsage = []Command{
|
| 11 | 11 |
{"exec", "Run a command in a running container"},
|
| 12 | 12 |
{"inspect", "Return low-level information on a container, image or task"},
|
| 13 |
- {"update", "Update configuration of one or more containers"},
|
|
| 14 | 13 |
} |
| 15 | 14 |
|
| 16 | 15 |
// DockerCommands stores all the docker command |