From 9037f77d315acf2c794bdef337564aa713620aed Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Sun, 5 Feb 2017 21:22:57 -0800 Subject: [PATCH] Add `--stop-signal` for `service create` and `service update` This fix tries to address the issue raised in 25696 where it was not possible to specify `--stop-signal` for `docker service create` and `docker service update`, in order to use special signal to stop the container. This fix adds `--stop-signal` and update the `StopSignal` in `Config` through `service create` and `service update`. Related docs has been updated. Integration test has been added. This fix fixes 25696. Signed-off-by: Yong Tang --- command/service/opts.go | 28 +++++++++++++++++----------- command/service/update.go | 2 ++ command/service/update_test.go | 22 ++++++++++++++++++++++ 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/command/service/opts.go b/command/service/opts.go index d8618e73ca..adab9b3658 100644 --- a/command/service/opts.go +++ b/command/service/opts.go @@ -269,6 +269,7 @@ type serviceOptions struct { workdir string user string groups opts.ListOpts + stopSignal string tty bool readOnly bool mounts opts.MountOpt @@ -372,17 +373,18 @@ func (opts *serviceOptions) ToService() (swarm.ServiceSpec, error) { }, TaskTemplate: swarm.TaskSpec{ ContainerSpec: swarm.ContainerSpec{ - Image: opts.image, - Args: opts.args, - Env: currentEnv, - Hostname: opts.hostname, - Labels: runconfigopts.ConvertKVStringsToMap(opts.containerLabels.GetAll()), - Dir: opts.workdir, - User: opts.user, - Groups: opts.groups.GetAll(), - TTY: opts.tty, - ReadOnly: opts.readOnly, - Mounts: opts.mounts.Value(), + Image: opts.image, + Args: opts.args, + Env: currentEnv, + Hostname: opts.hostname, + Labels: runconfigopts.ConvertKVStringsToMap(opts.containerLabels.GetAll()), + Dir: opts.workdir, + User: opts.user, + Groups: opts.groups.GetAll(), + StopSignal: opts.stopSignal, + TTY: opts.tty, + ReadOnly: opts.readOnly, + Mounts: opts.mounts.Value(), DNSConfig: &swarm.DNSConfig{ Nameservers: opts.dns.GetAll(), Search: opts.dnsSearch.GetAll(), @@ -470,6 +472,9 @@ func addServiceFlags(cmd *cobra.Command, opts *serviceOptions) { flags.BoolVar(&opts.readOnly, flagReadOnly, false, "Mount the container's root filesystem as read only") flags.SetAnnotation(flagReadOnly, "version", []string{"1.27"}) + + flags.StringVar(&opts.stopSignal, flagStopSignal, "", "Signal to stop the container") + flags.SetAnnotation(flagStopSignal, "version", []string{"1.27"}) } const ( @@ -523,6 +528,7 @@ const ( flagRestartMaxAttempts = "restart-max-attempts" flagRestartWindow = "restart-window" flagStopGracePeriod = "stop-grace-period" + flagStopSignal = "stop-signal" flagTTY = "tty" flagUpdateDelay = "update-delay" flagUpdateFailureAction = "update-failure-action" diff --git a/command/service/update.go b/command/service/update.go index 7f461c90a9..770a5bd26f 100644 --- a/command/service/update.go +++ b/command/service/update.go @@ -349,6 +349,8 @@ func updateService(flags *pflag.FlagSet, spec *swarm.ServiceSpec) error { cspec.ReadOnly = readOnly } + updateString(flagStopSignal, &cspec.StopSignal) + return nil } diff --git a/command/service/update_test.go b/command/service/update_test.go index f2887e229d..c43e596136 100644 --- a/command/service/update_test.go +++ b/command/service/update_test.go @@ -441,3 +441,25 @@ func TestUpdateReadOnly(t *testing.T) { updateService(flags, spec) assert.Equal(t, cspec.ReadOnly, false) } + +func TestUpdateStopSignal(t *testing.T) { + spec := &swarm.ServiceSpec{} + cspec := &spec.TaskTemplate.ContainerSpec + + // Update with --stop-signal=SIGUSR1 + flags := newUpdateCommand(nil).Flags() + flags.Set("stop-signal", "SIGUSR1") + updateService(flags, spec) + assert.Equal(t, cspec.StopSignal, "SIGUSR1") + + // Update without --stop-signal, no change + flags = newUpdateCommand(nil).Flags() + updateService(flags, spec) + assert.Equal(t, cspec.StopSignal, "SIGUSR1") + + // Update with --stop-signal=SIGWINCH + flags = newUpdateCommand(nil).Flags() + flags.Set("stop-signal", "SIGWINCH") + updateService(flags, spec) + assert.Equal(t, cspec.StopSignal, "SIGWINCH") +}