mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
add more flag constants to service update.
Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
parent
e070503511
commit
9d1f3373b3
2 changed files with 38 additions and 25 deletions
|
@ -438,14 +438,14 @@ func addServiceFlags(cmd *cobra.Command, opts *serviceOptions) {
|
|||
|
||||
flags.VarP(&opts.env, "env", "e", "Set environment variables")
|
||||
flags.StringVarP(&opts.workdir, "workdir", "w", "", "Working directory inside the container")
|
||||
flags.StringVarP(&opts.user, "user", "u", "", "Username or UID")
|
||||
flags.StringVarP(&opts.user, flagUser, "u", "", "Username or UID")
|
||||
flags.VarP(&opts.mounts, flagMount, "m", "Attach a mount to the service")
|
||||
|
||||
flags.Var(&opts.resources.limitCPU, flagLimitCPU, "Limit CPUs")
|
||||
flags.Var(&opts.resources.limitMemBytes, flagLimitMemory, "Limit Memory")
|
||||
flags.Var(&opts.resources.resCPU, flagReserveCPU, "Reserve CPUs")
|
||||
flags.Var(&opts.resources.resMemBytes, flagReserveMemory, "Reserve Memory")
|
||||
flags.Var(&opts.stopGrace, "stop-grace-period", "Time to wait before force killing a container")
|
||||
flags.Var(&opts.stopGrace, flagStopGracePeriod, "Time to wait before force killing a container")
|
||||
|
||||
flags.StringVar(&opts.mode, flagMode, "replicated", "Service mode (replicated or global)")
|
||||
flags.Var(&opts.replicas, flagReplicas, "Number of tasks")
|
||||
|
@ -467,22 +467,24 @@ func addServiceFlags(cmd *cobra.Command, opts *serviceOptions) {
|
|||
|
||||
const (
|
||||
flagConstraint = "constraint"
|
||||
flagName = "name"
|
||||
flagEndpointMode = "endpoint-mode"
|
||||
flagLabel = "label"
|
||||
flagLimitCPU = "limit-cpu"
|
||||
flagLimitMemory = "limit-memory"
|
||||
flagMode = "mode"
|
||||
flagMount = "mount"
|
||||
flagName = "name"
|
||||
flagNetwork = "network"
|
||||
flagPublish = "publish"
|
||||
flagReplicas = "replicas"
|
||||
flagReserveCPU = "reserve-cpu"
|
||||
flagReserveMemory = "reserve-memory"
|
||||
flagMount = "mount"
|
||||
flagMode = "mode"
|
||||
flagReplicas = "replicas"
|
||||
flagPublish = "publish"
|
||||
flagNetwork = "network"
|
||||
flagRestartCondition = "restart-condition"
|
||||
flagRestartDelay = "restart-delay"
|
||||
flagRestartMaxAttempts = "restart-max-attempts"
|
||||
flagRestartWindow = "restart-window"
|
||||
flagEndpointMode = "endpoint-mode"
|
||||
flagUpdateParallelism = "update-parallelism"
|
||||
flagStopGracePeriod = "stop-grace-period"
|
||||
flagUpdateDelay = "update-delay"
|
||||
flagUpdateParallelism = "update-parallelism"
|
||||
flagUser = "user"
|
||||
)
|
||||
|
|
|
@ -18,18 +18,17 @@ import (
|
|||
|
||||
func newUpdateCommand(dockerCli *client.DockerCli) *cobra.Command {
|
||||
opts := newServiceOptions()
|
||||
var flags *pflag.FlagSet
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "update [OPTIONS] SERVICE",
|
||||
Short: "Update a service",
|
||||
Args: cli.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return runUpdate(dockerCli, flags, args[0])
|
||||
return runUpdate(dockerCli, cmd.Flags(), args[0])
|
||||
},
|
||||
}
|
||||
|
||||
flags = cmd.Flags()
|
||||
flags := cmd.Flags()
|
||||
flags.String("image", "", "Service image tag")
|
||||
flags.StringSlice("command", []string{}, "Service command")
|
||||
flags.StringSlice("arg", []string{}, "Service command args")
|
||||
|
@ -112,6 +111,14 @@ func updateService(spec *swarm.ServiceSpec, flags *pflag.FlagSet) error {
|
|||
|
||||
cspec := &spec.TaskTemplate.ContainerSpec
|
||||
task := &spec.TaskTemplate
|
||||
|
||||
taskResources := func() *swarm.ResourceRequirements {
|
||||
if task.Resources == nil {
|
||||
task.Resources = &swarm.ResourceRequirements{}
|
||||
}
|
||||
return task.Resources
|
||||
}
|
||||
|
||||
updateString(flagName, &spec.Name)
|
||||
updateLabels(flags, &spec.Labels)
|
||||
updateString("image", &cspec.Image)
|
||||
|
@ -119,30 +126,24 @@ func updateService(spec *swarm.ServiceSpec, flags *pflag.FlagSet) error {
|
|||
updateSlice("arg", &cspec.Command)
|
||||
updateListOpts("env", &cspec.Env)
|
||||
updateString("workdir", &cspec.Dir)
|
||||
updateString("user", &cspec.User)
|
||||
updateString(flagUser, &cspec.User)
|
||||
updateMounts(flags, &cspec.Mounts)
|
||||
|
||||
if flags.Changed(flagLimitCPU) || flags.Changed(flagLimitMemory) {
|
||||
if task.Resources == nil {
|
||||
task.Resources = &swarm.ResourceRequirements{}
|
||||
}
|
||||
task.Resources.Limits = &swarm.Resources{}
|
||||
taskResources().Limits = &swarm.Resources{}
|
||||
updateInt64Value(flagLimitCPU, &task.Resources.Limits.NanoCPUs)
|
||||
updateInt64Value(flagLimitMemory, &task.Resources.Limits.MemoryBytes)
|
||||
|
||||
}
|
||||
if flags.Changed(flagReserveCPU) || flags.Changed(flagReserveMemory) {
|
||||
if task.Resources == nil {
|
||||
task.Resources = &swarm.ResourceRequirements{}
|
||||
}
|
||||
task.Resources.Reservations = &swarm.Resources{}
|
||||
taskResources().Reservations = &swarm.Resources{}
|
||||
updateInt64Value(flagReserveCPU, &task.Resources.Reservations.NanoCPUs)
|
||||
updateInt64Value(flagReserveMemory, &task.Resources.Reservations.MemoryBytes)
|
||||
}
|
||||
|
||||
updateDurationOpt("stop-grace-period", cspec.StopGracePeriod)
|
||||
updateDurationOpt(flagStopGracePeriod, cspec.StopGracePeriod)
|
||||
|
||||
if flags.Changed(flagRestartCondition) || flags.Changed(flagRestartDelay) || flags.Changed(flagRestartMaxAttempts) || flags.Changed(flagRestartWindow) {
|
||||
if anyChanged(flags, flagRestartCondition, flagRestartDelay, flagRestartMaxAttempts, flagRestartWindow) {
|
||||
if task.RestartPolicy == nil {
|
||||
task.RestartPolicy = &swarm.RestartPolicy{}
|
||||
}
|
||||
|
@ -165,7 +166,7 @@ func updateService(spec *swarm.ServiceSpec, flags *pflag.FlagSet) error {
|
|||
return err
|
||||
}
|
||||
|
||||
if flags.Changed(flagUpdateParallelism) || flags.Changed(flagUpdateDelay) {
|
||||
if anyChanged(flags, flagUpdateParallelism, flagUpdateDelay) {
|
||||
if spec.UpdateConfig == nil {
|
||||
spec.UpdateConfig = &swarm.UpdateConfig{}
|
||||
}
|
||||
|
@ -202,6 +203,16 @@ func updateLabels(flags *pflag.FlagSet, field *map[string]string) {
|
|||
*field = localLabels
|
||||
}
|
||||
|
||||
func anyChanged(flags *pflag.FlagSet, fields ...string) bool {
|
||||
for _, flag := range fields {
|
||||
if flags.Changed(flag) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
// TODO: should this override by destination path, or does swarm handle that?
|
||||
func updateMounts(flags *pflag.FlagSet, mounts *[]swarm.Mount) {
|
||||
if !flags.Changed(flagMount) {
|
||||
|
|
Loading…
Reference in a new issue