github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/api/client/update.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	Cli "github.com/docker/docker/cli"
     8  	flag "github.com/docker/docker/pkg/mflag"
     9  	"github.com/docker/engine-api/types/container"
    10  	"github.com/docker/go-units"
    11  )
    12  
    13  // CmdUpdate updates resources of one or more containers.
    14  //
    15  // Usage: docker update [OPTIONS] CONTAINER [CONTAINER...]
    16  func (cli *DockerCli) CmdUpdate(args ...string) error {
    17  	cmd := Cli.Subcmd("update", []string{"CONTAINER [CONTAINER...]"}, Cli.DockerCommands["update"].Description, true)
    18  	flBlkioWeight := cmd.Uint16([]string{"-blkio-weight"}, 0, "Block IO (relative weight), between 10 and 1000")
    19  	flCPUPeriod := cmd.Int64([]string{"-cpu-period"}, 0, "Limit CPU CFS (Completely Fair Scheduler) period")
    20  	flCPUQuota := cmd.Int64([]string{"-cpu-quota"}, 0, "Limit CPU CFS (Completely Fair Scheduler) quota")
    21  	flCpusetCpus := cmd.String([]string{"-cpuset-cpus"}, "", "CPUs in which to allow execution (0-3, 0,1)")
    22  	flCpusetMems := cmd.String([]string{"-cpuset-mems"}, "", "MEMs in which to allow execution (0-3, 0,1)")
    23  	flCPUShares := cmd.Int64([]string{"#c", "-cpu-shares"}, 0, "CPU shares (relative weight)")
    24  	flMemoryString := cmd.String([]string{"m", "-memory"}, "", "Memory limit")
    25  	flMemoryReservation := cmd.String([]string{"-memory-reservation"}, "", "Memory soft limit")
    26  	flMemorySwap := cmd.String([]string{"-memory-swap"}, "", "Swap limit equal to memory plus swap: '-1' to enable unlimited swap")
    27  	flKernelMemory := cmd.String([]string{"-kernel-memory"}, "", "Kernel memory limit")
    28  
    29  	cmd.Require(flag.Min, 1)
    30  	cmd.ParseFlags(args, true)
    31  	if cmd.NFlag() == 0 {
    32  		return fmt.Errorf("You must provide one or more flags when using this command.")
    33  	}
    34  
    35  	var err error
    36  	var flMemory int64
    37  	if *flMemoryString != "" {
    38  		flMemory, err = units.RAMInBytes(*flMemoryString)
    39  		if err != nil {
    40  			return err
    41  		}
    42  	}
    43  
    44  	var memoryReservation int64
    45  	if *flMemoryReservation != "" {
    46  		memoryReservation, err = units.RAMInBytes(*flMemoryReservation)
    47  		if err != nil {
    48  			return err
    49  		}
    50  	}
    51  
    52  	var memorySwap int64
    53  	if *flMemorySwap != "" {
    54  		if *flMemorySwap == "-1" {
    55  			memorySwap = -1
    56  		} else {
    57  			memorySwap, err = units.RAMInBytes(*flMemorySwap)
    58  			if err != nil {
    59  				return err
    60  			}
    61  		}
    62  	}
    63  
    64  	var kernelMemory int64
    65  	if *flKernelMemory != "" {
    66  		kernelMemory, err = units.RAMInBytes(*flKernelMemory)
    67  		if err != nil {
    68  			return err
    69  		}
    70  	}
    71  
    72  	resources := container.Resources{
    73  		BlkioWeight:       *flBlkioWeight,
    74  		CpusetCpus:        *flCpusetCpus,
    75  		CpusetMems:        *flCpusetMems,
    76  		CPUShares:         *flCPUShares,
    77  		Memory:            flMemory,
    78  		MemoryReservation: memoryReservation,
    79  		MemorySwap:        memorySwap,
    80  		KernelMemory:      kernelMemory,
    81  		CPUPeriod:         *flCPUPeriod,
    82  		CPUQuota:          *flCPUQuota,
    83  	}
    84  
    85  	updateConfig := container.UpdateConfig{
    86  		Resources: resources,
    87  	}
    88  
    89  	names := cmd.Args()
    90  	var errs []string
    91  	for _, name := range names {
    92  		if err := cli.client.ContainerUpdate(name, updateConfig); err != nil {
    93  			errs = append(errs, fmt.Sprintf("Failed to update container (%s): %s", name, err))
    94  		} else {
    95  			fmt.Fprintf(cli.out, "%s\n", name)
    96  		}
    97  	}
    98  
    99  	if len(errs) > 0 {
   100  		return fmt.Errorf("%s", strings.Join(errs, "\n"))
   101  	}
   102  
   103  	return nil
   104  }