github.com/brahmaroutu/docker@v1.2.1-0.20160809185609-eb28dde01f16/api/client/container/update.go (about) 1 package container 2 3 import ( 4 "fmt" 5 "strings" 6 7 "golang.org/x/net/context" 8 9 "github.com/docker/docker/api/client" 10 "github.com/docker/docker/cli" 11 runconfigopts "github.com/docker/docker/runconfig/opts" 12 containertypes "github.com/docker/engine-api/types/container" 13 "github.com/docker/go-units" 14 "github.com/spf13/cobra" 15 ) 16 17 type updateOptions struct { 18 blkioWeight uint16 19 cpuPeriod int64 20 cpuQuota int64 21 cpusetCpus string 22 cpusetMems string 23 cpuShares int64 24 memoryString string 25 memoryReservation string 26 memorySwap string 27 kernelMemory string 28 restartPolicy string 29 30 nFlag int 31 32 containers []string 33 } 34 35 // NewUpdateCommand creates a new cobra.Command for `docker update` 36 func NewUpdateCommand(dockerCli *client.DockerCli) *cobra.Command { 37 var opts updateOptions 38 39 cmd := &cobra.Command{ 40 Use: "update [OPTIONS] CONTAINER [CONTAINER...]", 41 Short: "Update configuration of one or more containers", 42 Args: cli.RequiresMinArgs(1), 43 RunE: func(cmd *cobra.Command, args []string) error { 44 opts.containers = args 45 opts.nFlag = cmd.Flags().NFlag() 46 return runUpdate(dockerCli, &opts) 47 }, 48 } 49 cmd.SetFlagErrorFunc(flagErrorFunc) 50 51 flags := cmd.Flags() 52 flags.Uint16Var(&opts.blkioWeight, "blkio-weight", 0, "Block IO (relative weight), between 10 and 1000") 53 flags.Int64Var(&opts.cpuPeriod, "cpu-period", 0, "Limit CPU CFS (Completely Fair Scheduler) period") 54 flags.Int64Var(&opts.cpuQuota, "cpu-quota", 0, "Limit CPU CFS (Completely Fair Scheduler) quota") 55 flags.StringVar(&opts.cpusetCpus, "cpuset-cpus", "", "CPUs in which to allow execution (0-3, 0,1)") 56 flags.StringVar(&opts.cpusetMems, "cpuset-mems", "", "MEMs in which to allow execution (0-3, 0,1)") 57 flags.Int64VarP(&opts.cpuShares, "cpu-shares", "c", 0, "CPU shares (relative weight)") 58 flags.StringVarP(&opts.memoryString, "memory", "m", "", "Memory limit") 59 flags.StringVar(&opts.memoryReservation, "memory-reservation", "", "Memory soft limit") 60 flags.StringVar(&opts.memorySwap, "memory-swap", "", "Swap limit equal to memory plus swap: '-1' to enable unlimited swap") 61 flags.StringVar(&opts.kernelMemory, "kernel-memory", "", "Kernel memory limit") 62 flags.StringVar(&opts.restartPolicy, "restart", "", "Restart policy to apply when a container exits") 63 64 return cmd 65 } 66 67 func runUpdate(dockerCli *client.DockerCli, opts *updateOptions) error { 68 var err error 69 70 if opts.nFlag == 0 { 71 return fmt.Errorf("You must provide one or more flags when using this command.") 72 } 73 74 var memory int64 75 if opts.memoryString != "" { 76 memory, err = units.RAMInBytes(opts.memoryString) 77 if err != nil { 78 return err 79 } 80 } 81 82 var memoryReservation int64 83 if opts.memoryReservation != "" { 84 memoryReservation, err = units.RAMInBytes(opts.memoryReservation) 85 if err != nil { 86 return err 87 } 88 } 89 90 var memorySwap int64 91 if opts.memorySwap != "" { 92 if opts.memorySwap == "-1" { 93 memorySwap = -1 94 } else { 95 memorySwap, err = units.RAMInBytes(opts.memorySwap) 96 if err != nil { 97 return err 98 } 99 } 100 } 101 102 var kernelMemory int64 103 if opts.kernelMemory != "" { 104 kernelMemory, err = units.RAMInBytes(opts.kernelMemory) 105 if err != nil { 106 return err 107 } 108 } 109 110 var restartPolicy containertypes.RestartPolicy 111 if opts.restartPolicy != "" { 112 restartPolicy, err = runconfigopts.ParseRestartPolicy(opts.restartPolicy) 113 if err != nil { 114 return err 115 } 116 } 117 118 resources := containertypes.Resources{ 119 BlkioWeight: opts.blkioWeight, 120 CpusetCpus: opts.cpusetCpus, 121 CpusetMems: opts.cpusetMems, 122 CPUShares: opts.cpuShares, 123 Memory: memory, 124 MemoryReservation: memoryReservation, 125 MemorySwap: memorySwap, 126 KernelMemory: kernelMemory, 127 CPUPeriod: opts.cpuPeriod, 128 CPUQuota: opts.cpuQuota, 129 } 130 131 updateConfig := containertypes.UpdateConfig{ 132 Resources: resources, 133 RestartPolicy: restartPolicy, 134 } 135 136 ctx := context.Background() 137 138 var errs []string 139 for _, container := range opts.containers { 140 if err := dockerCli.Client().ContainerUpdate(ctx, container, updateConfig); err != nil { 141 errs = append(errs, err.Error()) 142 } else { 143 fmt.Fprintf(dockerCli.Out(), "%s\n", container) 144 } 145 } 146 if len(errs) > 0 { 147 return fmt.Errorf("%s", strings.Join(errs, "\n")) 148 } 149 return nil 150 }