update.go 4.2 KB

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