Skip to content

Commit

Permalink
Add --init option to docker service create
Browse files Browse the repository at this point in the history
Signed-off-by: Timothy Higinbottom <[email protected]>
Signed-off-by: Vincent Demeester <[email protected]>
  • Loading branch information
vdemeester committed Jun 8, 2018
1 parent 001c8f2 commit a782c83
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cli/command/service/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func newCreateCommand(dockerCli command.Cli) *cobra.Command {
flags.SetAnnotation(flagDNSSearch, "version", []string{"1.25"})
flags.Var(&opts.hosts, flagHost, "Set one or more custom host-to-IP mappings (host:ip)")
flags.SetAnnotation(flagHost, "version", []string{"1.25"})
flags.BoolVar(&opts.init, flagInit, false, "Use an init inside each service container to forward signals and reap processes")
flags.SetAnnotation(flagInit, "version", []string{"1.37"})

flags.Var(cliopts.NewListOptsRef(&opts.resources.resGenericResources, ValidateSingleGenericResource), "generic-resource", "User defined resources")
flags.SetAnnotation(flagHostAdd, "version", []string{"1.32"})
Expand Down
3 changes: 3 additions & 0 deletions cli/command/service/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ type serviceOptions struct {
user string
groups opts.ListOpts
credentialSpec credentialSpecOpt
init bool
stopSignal string
tty bool
readOnly bool
Expand Down Expand Up @@ -624,6 +625,7 @@ func (options *serviceOptions) ToService(ctx context.Context, apiClient client.N
TTY: options.tty,
ReadOnly: options.readOnly,
Mounts: options.mounts.Value(),
Init: options.init,
DNSConfig: &swarm.DNSConfig{
Nameservers: options.dns.GetAll(),
Search: options.dnsSearch.GetAll(),
Expand Down Expand Up @@ -875,6 +877,7 @@ const (
flagRollbackMonitor = "rollback-monitor"
flagRollbackOrder = "rollback-order"
flagRollbackParallelism = "rollback-parallelism"
flagInit = "init"
flagStopGracePeriod = "stop-grace-period"
flagStopSignal = "stop-signal"
flagTTY = "tty"
Expand Down
8 changes: 8 additions & 0 deletions cli/command/service/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ func newUpdateCommand(dockerCli command.Cli) *cobra.Command {
flags.SetAnnotation(flagDNSSearchAdd, "version", []string{"1.25"})
flags.Var(&options.hosts, flagHostAdd, "Add a custom host-to-IP mapping (host:ip)")
flags.SetAnnotation(flagHostAdd, "version", []string{"1.25"})
flags.BoolVar(&options.init, flagInit, false, "Use an init inside each service container to forward signals and reap processes")
flags.SetAnnotation(flagInit, "version", []string{"1.37"})

// Add needs parsing, Remove only needs the key
flags.Var(newListOptsVar(), flagGenericResourcesRemove, "Remove a Generic resource")
Expand Down Expand Up @@ -235,6 +237,11 @@ func runUpdate(dockerCli command.Cli, flags *pflag.FlagSet, options *serviceOpti

// nolint: gocyclo
func updateService(ctx context.Context, apiClient client.NetworkAPIClient, flags *pflag.FlagSet, spec *swarm.ServiceSpec) error {
updateBool := func(flag string, field *bool) {
if flags.Changed(flag) {
*field, _ = flags.GetBool(flag)
}
}
updateString := func(flag string, field *string) {
if flags.Changed(flag) {
*field, _ = flags.GetString(flag)
Expand Down Expand Up @@ -306,6 +313,7 @@ func updateService(ctx context.Context, apiClient client.NetworkAPIClient, flags
updateString(flagWorkdir, &cspec.Dir)
updateString(flagUser, &cspec.User)
updateString(flagHostname, &cspec.Hostname)
updateBool(flagInit, &cspec.Init)
if err := updateIsolation(flagIsolation, &cspec.Isolation); err != nil {
return err
}
Expand Down
26 changes: 26 additions & 0 deletions cli/command/service/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,32 @@ func TestUpdateReadOnly(t *testing.T) {
assert.Check(t, !cspec.ReadOnly)
}

func TestUpdateInit(t *testing.T) {
spec := &swarm.ServiceSpec{
TaskTemplate: swarm.TaskSpec{
ContainerSpec: &swarm.ContainerSpec{},
},
}
cspec := spec.TaskTemplate.ContainerSpec

// Update with --stop-signal=SIGUSR1
flags := newUpdateCommand(nil).Flags()
flags.Set("init", "true")
updateService(nil, nil, flags, spec)
assert.Check(t, is.Equal(true, cspec.Init))

// Update without --stop-signal, no change
flags = newUpdateCommand(nil).Flags()
updateService(nil, nil, flags, spec)
assert.Check(t, is.Equal(true, cspec.Init))

// Update with --stop-signal=SIGWINCH
flags = newUpdateCommand(nil).Flags()
flags.Set("init", "false")
updateService(nil, nil, flags, spec)
assert.Check(t, is.Equal(false, cspec.Init))
}

func TestUpdateStopSignal(t *testing.T) {
spec := &swarm.ServiceSpec{
TaskTemplate: swarm.TaskSpec{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a782c83

Please sign in to comment.