github.com/kunnos/engine@v1.13.1/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  	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.Int64Var(&opts.cpuRealtimeRuntime, "cpu-rt-runtime", 0, "Limit the CPU real-time runtime in microseconds")
    58  	flags.StringVar(&opts.cpusetCpus, "cpuset-cpus", "", "CPUs in which to allow execution (0-3, 0,1)")
    59  	flags.StringVar(&opts.cpusetMems, "cpuset-mems", "", "MEMs in which to allow execution (0-3, 0,1)")
    60  	flags.Int64VarP(&opts.cpuShares, "cpu-shares", "c", 0, "CPU shares (relative weight)")
    61  	flags.StringVarP(&opts.memoryString, "memory", "m", "", "Memory limit")
    62  	flags.StringVar(&opts.memoryReservation, "memory-reservation", "", "Memory soft limit")
    63  	flags.StringVar(&opts.memorySwap, "memory-swap", "", "Swap limit equal to memory plus swap: '-1' to enable unlimited swap")
    64  	flags.StringVar(&opts.kernelMemory, "kernel-memory", "", "Kernel memory limit")
    65  	flags.StringVar(&opts.restartPolicy, "restart", "", "Restart policy to apply when a container exits")
    66  
    67  	return cmd
    68  }
    69  
    70  func runUpdate(dockerCli *command.DockerCli, opts *updateOptions) error {
    71  	var err error
    72  
    73  	if opts.nFlag == 0 {
    74  		return fmt.Errorf("You must provide one or more flags when using this command.")
    75  	}
    76  
    77  	var memory int64
    78  	if opts.memoryString != "" {
    79  		memory, err = units.RAMInBytes(opts.memoryString)
    80  		if err != nil {
    81  			return err
    82  		}
    83  	}
    84  
    85  	var memoryReservation int64
    86  	if opts.memoryReservation != "" {
    87  		memoryReservation, err = units.RAMInBytes(opts.memoryReservation)
    88  		if err != nil {
    89  			return err
    90  		}
    91  	}
    92  
    93  	var memorySwap int64
    94  	if opts.memorySwap != "" {
    95  		if opts.memorySwap == "-1" {
    96  			memorySwap = -1
    97  		} else {
    98  			memorySwap, err = units.RAMInBytes(opts.memorySwap)
    99  			if err != nil {
   100  				return err
   101  			}
   102  		}
   103  	}
   104  
   105  	var kernelMemory int64
   106  	if opts.kernelMemory != "" {
   107  		kernelMemory, err = units.RAMInBytes(opts.kernelMemory)
   108  		if err != nil {
   109  			return err
   110  		}
   111  	}
   112  
   113  	var restartPolicy containertypes.RestartPolicy
   114  	if opts.restartPolicy != "" {
   115  		restartPolicy, err = runconfigopts.ParseRestartPolicy(opts.restartPolicy)
   116  		if err != nil {
   117  			return err
   118  		}
   119  	}
   120  
   121  	resources := containertypes.Resources{
   122  		BlkioWeight:        opts.blkioWeight,
   123  		CpusetCpus:         opts.cpusetCpus,
   124  		CpusetMems:         opts.cpusetMems,
   125  		CPUShares:          opts.cpuShares,
   126  		Memory:             memory,
   127  		MemoryReservation:  memoryReservation,
   128  		MemorySwap:         memorySwap,
   129  		KernelMemory:       kernelMemory,
   130  		CPUPeriod:          opts.cpuPeriod,
   131  		CPUQuota:           opts.cpuQuota,
   132  		CPURealtimePeriod:  opts.cpuRealtimePeriod,
   133  		CPURealtimeRuntime: opts.cpuRealtimeRuntime,
   134  	}
   135  
   136  	updateConfig := containertypes.UpdateConfig{
   137  		Resources:     resources,
   138  		RestartPolicy: restartPolicy,
   139  	}
   140  
   141  	ctx := context.Background()
   142  
   143  	var (
   144  		warns []string
   145  		errs  []string
   146  	)
   147  	for _, container := range opts.containers {
   148  		r, err := dockerCli.Client().ContainerUpdate(ctx, container, updateConfig)
   149  		if err != nil {
   150  			errs = append(errs, err.Error())
   151  		} else {
   152  			fmt.Fprintf(dockerCli.Out(), "%s\n", container)
   153  		}
   154  		warns = append(warns, r.Warnings...)
   155  	}
   156  	if len(warns) > 0 {
   157  		fmt.Fprintf(dockerCli.Out(), "%s", strings.Join(warns, "\n"))
   158  	}
   159  	if len(errs) > 0 {
   160  		return fmt.Errorf("%s", strings.Join(errs, "\n"))
   161  	}
   162  	return nil
   163  }