github.com/hamo/docker@v1.11.1/api/client/update.go (about)

     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  	names := cmd.Args()
   103  	var errs []string
   104  	for _, name := range names {
   105  		if err := cli.client.ContainerUpdate(context.Background(), name, updateConfig); err != nil {
   106  			errs = append(errs, err.Error())
   107  		} else {
   108  			fmt.Fprintf(cli.out, "%s\n", name)
   109  		}
   110  	}
   111  
   112  	if len(errs) > 0 {
   113  		return fmt.Errorf("%s", strings.Join(errs, "\n"))
   114  	}
   115  
   116  	return nil
   117  }