2016-09-08 13:11:39 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2016-09-08 13:11:39 -04:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
2017-03-06 16:01:04 -05:00
|
|
|
"github.com/docker/docker/opts"
|
2016-09-08 13:11:39 -04:00
|
|
|
runconfigopts "github.com/docker/docker/runconfig/opts"
|
2017-03-27 21:21:59 -04:00
|
|
|
"github.com/pkg/errors"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
2016-12-24 19:05:37 -05:00
|
|
|
"golang.org/x/net/context"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type updateOptions struct {
|
2016-06-07 15:05:43 -04:00
|
|
|
blkioWeight uint16
|
|
|
|
cpuPeriod int64
|
|
|
|
cpuQuota int64
|
|
|
|
cpuRealtimePeriod int64
|
|
|
|
cpuRealtimeRuntime int64
|
|
|
|
cpusetCpus string
|
|
|
|
cpusetMems string
|
|
|
|
cpuShares int64
|
2017-03-06 16:01:04 -05:00
|
|
|
memory opts.MemBytes
|
|
|
|
memoryReservation opts.MemBytes
|
|
|
|
memorySwap opts.MemSwapBytes
|
|
|
|
kernelMemory opts.MemBytes
|
2016-06-07 15:05:43 -04:00
|
|
|
restartPolicy string
|
2017-02-18 01:04:37 -05:00
|
|
|
cpus opts.NanoCPUs
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
nFlag int
|
|
|
|
|
|
|
|
containers []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewUpdateCommand creates a new cobra.Command for `docker update`
|
|
|
|
func NewUpdateCommand(dockerCli *command.DockerCli) *cobra.Command {
|
|
|
|
var opts updateOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "update [OPTIONS] CONTAINER [CONTAINER...]",
|
|
|
|
Short: "Update configuration of one or more containers",
|
|
|
|
Args: cli.RequiresMinArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
opts.containers = args
|
|
|
|
opts.nFlag = cmd.Flags().NFlag()
|
|
|
|
return runUpdate(dockerCli, &opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2016-11-01 09:09:18 -04:00
|
|
|
flags.Uint16Var(&opts.blkioWeight, "blkio-weight", 0, "Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)")
|
2016-09-08 13:11:39 -04:00
|
|
|
flags.Int64Var(&opts.cpuPeriod, "cpu-period", 0, "Limit CPU CFS (Completely Fair Scheduler) period")
|
|
|
|
flags.Int64Var(&opts.cpuQuota, "cpu-quota", 0, "Limit CPU CFS (Completely Fair Scheduler) quota")
|
2016-06-07 15:05:43 -04:00
|
|
|
flags.Int64Var(&opts.cpuRealtimePeriod, "cpu-rt-period", 0, "Limit the CPU real-time period in microseconds")
|
2017-01-16 11:57:26 -05:00
|
|
|
flags.SetAnnotation("cpu-rt-period", "version", []string{"1.25"})
|
2016-06-07 15:05:43 -04:00
|
|
|
flags.Int64Var(&opts.cpuRealtimeRuntime, "cpu-rt-runtime", 0, "Limit the CPU real-time runtime in microseconds")
|
2017-01-16 11:57:26 -05:00
|
|
|
flags.SetAnnotation("cpu-rt-runtime", "version", []string{"1.25"})
|
2016-09-08 13:11:39 -04:00
|
|
|
flags.StringVar(&opts.cpusetCpus, "cpuset-cpus", "", "CPUs in which to allow execution (0-3, 0,1)")
|
|
|
|
flags.StringVar(&opts.cpusetMems, "cpuset-mems", "", "MEMs in which to allow execution (0-3, 0,1)")
|
|
|
|
flags.Int64VarP(&opts.cpuShares, "cpu-shares", "c", 0, "CPU shares (relative weight)")
|
2017-03-06 16:01:04 -05:00
|
|
|
flags.VarP(&opts.memory, "memory", "m", "Memory limit")
|
|
|
|
flags.Var(&opts.memoryReservation, "memory-reservation", "Memory soft limit")
|
|
|
|
flags.Var(&opts.memorySwap, "memory-swap", "Swap limit equal to memory plus swap: '-1' to enable unlimited swap")
|
|
|
|
flags.Var(&opts.kernelMemory, "kernel-memory", "Kernel memory limit")
|
2016-09-08 13:11:39 -04:00
|
|
|
flags.StringVar(&opts.restartPolicy, "restart", "", "Restart policy to apply when a container exits")
|
|
|
|
|
2017-02-18 01:04:37 -05:00
|
|
|
flags.Var(&opts.cpus, "cpus", "Number of CPUs")
|
|
|
|
flags.SetAnnotation("cpus", "version", []string{"1.29"})
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runUpdate(dockerCli *command.DockerCli, opts *updateOptions) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if opts.nFlag == 0 {
|
2016-12-24 19:05:37 -05:00
|
|
|
return errors.New("You must provide one or more flags when using this command.")
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var restartPolicy containertypes.RestartPolicy
|
|
|
|
if opts.restartPolicy != "" {
|
|
|
|
restartPolicy, err = runconfigopts.ParseRestartPolicy(opts.restartPolicy)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resources := containertypes.Resources{
|
2016-06-07 15:05:43 -04:00
|
|
|
BlkioWeight: opts.blkioWeight,
|
|
|
|
CpusetCpus: opts.cpusetCpus,
|
|
|
|
CpusetMems: opts.cpusetMems,
|
|
|
|
CPUShares: opts.cpuShares,
|
2017-03-06 16:01:04 -05:00
|
|
|
Memory: opts.memory.Value(),
|
|
|
|
MemoryReservation: opts.memoryReservation.Value(),
|
|
|
|
MemorySwap: opts.memorySwap.Value(),
|
|
|
|
KernelMemory: opts.kernelMemory.Value(),
|
2016-06-07 15:05:43 -04:00
|
|
|
CPUPeriod: opts.cpuPeriod,
|
|
|
|
CPUQuota: opts.cpuQuota,
|
|
|
|
CPURealtimePeriod: opts.cpuRealtimePeriod,
|
|
|
|
CPURealtimeRuntime: opts.cpuRealtimeRuntime,
|
2017-02-18 01:04:37 -05:00
|
|
|
NanoCPUs: opts.cpus.Value(),
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
updateConfig := containertypes.UpdateConfig{
|
|
|
|
Resources: resources,
|
|
|
|
RestartPolicy: restartPolicy,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
var (
|
|
|
|
warns []string
|
|
|
|
errs []string
|
|
|
|
)
|
|
|
|
for _, container := range opts.containers {
|
|
|
|
r, err := dockerCli.Client().ContainerUpdate(ctx, container, updateConfig)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err.Error())
|
|
|
|
} else {
|
2016-12-24 19:05:37 -05:00
|
|
|
fmt.Fprintln(dockerCli.Out(), container)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
warns = append(warns, r.Warnings...)
|
|
|
|
}
|
|
|
|
if len(warns) > 0 {
|
2016-12-24 19:05:37 -05:00
|
|
|
fmt.Fprintln(dockerCli.Out(), strings.Join(warns, "\n"))
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
if len(errs) > 0 {
|
2016-12-24 19:05:37 -05:00
|
|
|
return errors.New(strings.Join(errs, "\n"))
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|