From 566b78dd02e91f376e9f219cb96ddba153671eb5 Mon Sep 17 00:00:00 2001 From: Valentin Rothberg Date: Mon, 22 Nov 2021 11:05:59 +0100 Subject: generate systemd: add --start-timeout flag Add a new flag to set the start timeout for a generated systemd unit. To make naming consistent, add a new --stop-timeout flag as well and let the previous --time map to it. Fixes: #11618 Signed-off-by: Valentin Rothberg --- cmd/podman/generate/systemd.go | 44 +++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) (limited to 'cmd/podman') diff --git a/cmd/podman/generate/systemd.go b/cmd/podman/generate/systemd.go index cdc103865..173b3656b 100644 --- a/cmd/podman/generate/systemd.go +++ b/cmd/podman/generate/systemd.go @@ -19,16 +19,19 @@ import ( ) const ( - restartPolicyFlagName = "restart-policy" - timeFlagName = "time" - newFlagName = "new" + startTimeoutFlagName = "start-timeout" + stopTimeoutFlagName = "stop-timeout" + stopTimeoutCompatFlagName = "time" + restartPolicyFlagName = "restart-policy" + newFlagName = "new" ) var ( files bool format string - systemdTimeout uint systemdRestart string + startTimeout uint + stopTimeout uint systemdOptions = entities.GenerateSystemdOptions{} systemdDescription = `Generate systemd units for a pod or container. The generated units can later be controlled via systemctl(1).` @@ -56,8 +59,17 @@ func init() { flags.BoolVarP(&files, "files", "f", false, "Generate .service files instead of printing to stdout") flags.BoolVar(&systemdOptions.TemplateUnitFile, "template", false, "Make it a template file and use %i and %I specifiers. Working only for containers") - flags.UintVarP(&systemdTimeout, timeFlagName, "t", containerConfig.Engine.StopTimeout, "Stop timeout override") - _ = systemdCmd.RegisterFlagCompletionFunc(timeFlagName, completion.AutocompleteNone) + flags.UintVarP(&startTimeout, startTimeoutFlagName, "", 0, "Start timeout override") + _ = systemdCmd.RegisterFlagCompletionFunc(startTimeoutFlagName, completion.AutocompleteNone) + + // NOTE: initially, there was only a --time/-t flag which mapped to + // stop-timeout. To remain backwards compatible create a hidden flag + // that maps to StopTimeout. + flags.UintVarP(&stopTimeout, stopTimeoutFlagName, "", containerConfig.Engine.StopTimeout, "Stop timeout override") + _ = systemdCmd.RegisterFlagCompletionFunc(stopTimeoutFlagName, completion.AutocompleteNone) + flags.UintVarP(&stopTimeout, stopTimeoutCompatFlagName, "t", containerConfig.Engine.StopTimeout, "Backwards alias for --stop-timeout") + _ = flags.MarkHidden("time") + flags.BoolVar(&systemdOptions.New, newFlagName, false, "Create a new container or pod instead of starting an existing one") flags.BoolVarP(&systemdOptions.NoHeader, "no-header", "", false, "Skip header generation") @@ -84,9 +96,6 @@ func init() { } func systemd(cmd *cobra.Command, args []string) error { - if cmd.Flags().Changed(timeFlagName) { - systemdOptions.StopTimeout = &systemdTimeout - } if cmd.Flags().Changed(restartPolicyFlagName) { systemdOptions.RestartPolicy = &systemdRestart } @@ -102,6 +111,23 @@ func systemd(cmd *cobra.Command, args []string) error { systemdOptions.New = true } + if cmd.Flags().Changed(startTimeoutFlagName) { + systemdOptions.StartTimeout = &startTimeout + } + setStopTimeout := 0 + if cmd.Flags().Changed(stopTimeoutFlagName) { + setStopTimeout++ + } + if cmd.Flags().Changed(stopTimeoutCompatFlagName) { + setStopTimeout++ + } + switch setStopTimeout { + case 1: + systemdOptions.StopTimeout = &stopTimeout + case 2: + return fmt.Errorf("%s and %s are redundant and cannot be used together", stopTimeoutFlagName, stopTimeoutCompatFlagName) + } + reports, err := registry.ContainerEngine().GenerateSystemd(registry.GetContext(), args[0], systemdOptions) if err != nil { return err -- cgit v1.2.3-54-g00ecf