update.go 4.7 KB

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