github.com/fabiokung/docker@v0.11.2-0.20170222101415-4534dcd49497/cli/command/container/update.go (about)

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