github.com/vieux/docker@v0.6.3-0.20161004191708-e097c2a938c7/cli/command/container/update.go (about)

     1  package container
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"golang.org/x/net/context"
     8  
     9  	containertypes "github.com/docker/docker/api/types/container"
    10  	"github.com/docker/docker/cli"
    11  	"github.com/docker/docker/cli/command"
    12  	runconfigopts "github.com/docker/docker/runconfig/opts"
    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 *command.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  
    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 *command.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 (
   138  		warns []string
   139  		errs  []string
   140  	)
   141  	for _, container := range opts.containers {
   142  		r, err := dockerCli.Client().ContainerUpdate(ctx, container, updateConfig)
   143  		if err != nil {
   144  			errs = append(errs, err.Error())
   145  		} else {
   146  			fmt.Fprintf(dockerCli.Out(), "%s\n", container)
   147  		}
   148  		warns = append(warns, r.Warnings...)
   149  	}
   150  	if len(warns) > 0 {
   151  		fmt.Fprintf(dockerCli.Out(), "%s", strings.Join(warns, "\n"))
   152  	}
   153  	if len(errs) > 0 {
   154  		return fmt.Errorf("%s", strings.Join(errs, "\n"))
   155  	}
   156  	return nil
   157  }