Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate systemd: add --start-timeout flag #12380

Merged
merged 1 commit into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions cmd/podman/generate/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).`
Expand Down Expand Up @@ -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")
vrothberg marked this conversation as resolved.
Show resolved Hide resolved
_ = 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")

Expand All @@ -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
}
Expand All @@ -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
Expand Down
8 changes: 6 additions & 2 deletions docs/source/markdown/podman-generate-systemd.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ Note that `--new` only works on containers and pods created directly via Podman

Do not generate the header including meta data such as the Podman version and the timestamp.

#### **--time**, **-t**=*value*
#### **--start-timeout** =*value*

Override the default stop timeout for the container with the given value.
Override the default start timeout for the container with the given value in seconds.

#### **--stop-timeout** =*value*

Override the default stop timeout for the container with the given value in seconds.

#### **--restart-policy**=*policy*

Expand Down
3 changes: 3 additions & 0 deletions pkg/api/handlers/libpod/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ func GenerateSystemd(w http.ResponseWriter, r *http.Request) {
TemplateUnitFile bool `schema:"templateUnitFile"`
RestartPolicy *string `schema:"restartPolicy"`
StopTimeout uint `schema:"stopTimeout"`
StartTimeout uint `schema:"startTimeout"`
ContainerPrefix string `schema:"containerPrefix"`
PodPrefix string `schema:"podPrefix"`
Separator string `schema:"separator"`
}{
StartTimeout: 0,
StopTimeout: util.DefaultContainerConfig().Engine.StopTimeout,
ContainerPrefix: "container",
PodPrefix: "pod",
Expand All @@ -46,6 +48,7 @@ func GenerateSystemd(w http.ResponseWriter, r *http.Request) {
NoHeader: query.NoHeader,
TemplateUnitFile: query.TemplateUnitFile,
RestartPolicy: query.RestartPolicy,
StartTimeout: &query.StartTimeout,
StopTimeout: &query.StopTimeout,
ContainerPrefix: query.ContainerPrefix,
PodPrefix: query.PodPrefix,
Expand Down
9 changes: 7 additions & 2 deletions pkg/api/server/register_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ func (s *APIServer) registerGenerateHandlers(r *mux.Router) error {
// default: false
// description: Do not generate the header including the Podman version and the timestamp.
// - in: query
// name: time
// name: startTimeout
// type: integer
// default: 0
// description: Start timeout in seconds.
// - in: query
// name: stopTimeout
// type: integer
// default: 10
// description: Stop timeout override.
// description: Stop timeout in seconds.
// - in: query
// name: restartPolicy
// default: on-failure
Expand Down
2 changes: 2 additions & 0 deletions pkg/bindings/generate/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type SystemdOptions struct {
TemplateUnitFile *bool
// RestartPolicy - systemd restart policy.
RestartPolicy *string
// StartTimeout - time when starting the container.
StartTimeout *uint
// StopTimeout - time when stopping the container.
StopTimeout *uint
// ContainerPrefix - systemd unit name prefix for containers
Expand Down
15 changes: 15 additions & 0 deletions pkg/bindings/generate/types_systemd_options.go

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

2 changes: 2 additions & 0 deletions pkg/domain/entities/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ type GenerateSystemdOptions struct {
New bool
// RestartPolicy - systemd restart policy.
RestartPolicy *string
// StartTimeout - time when starting the container.
StartTimeout *uint
// StopTimeout - time when stopping the container.
StopTimeout *uint
// ContainerPrefix - systemd unit name prefix for containers
Expand Down
14 changes: 9 additions & 5 deletions pkg/domain/infra/tunnel/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ import (
)

func (ic *ContainerEngine) GenerateSystemd(ctx context.Context, nameOrID string, opts entities.GenerateSystemdOptions) (*entities.GenerateSystemdReport, error) {
options := new(generate.SystemdOptions).WithUseName(opts.Name).WithContainerPrefix(opts.ContainerPrefix).WithNew(opts.New).WithNoHeader(opts.NoHeader).WithTemplateUnitFile(opts.TemplateUnitFile)
options.WithPodPrefix(opts.PodPrefix).WithSeparator(opts.Separator)
if opts.RestartPolicy != nil {
options.WithRestartPolicy(*opts.RestartPolicy)
options := new(generate.SystemdOptions).WithUseName(opts.Name).WithContainerPrefix(opts.ContainerPrefix).WithNew(opts.New).WithNoHeader(opts.NoHeader).WithTemplateUnitFile(opts.TemplateUnitFile).WithPodPrefix(opts.PodPrefix).WithSeparator(opts.Separator)

if opts.StartTimeout != nil {
options.WithStartTimeout(*opts.StartTimeout)
}
if to := opts.StopTimeout; to != nil {
if opts.StopTimeout != nil {
options.WithStopTimeout(*opts.StopTimeout)
}
if opts.RestartPolicy != nil {
options.WithRestartPolicy(*opts.RestartPolicy)
}

return generate.Systemd(ic.ClientCtx, nameOrID, options)
}

Expand Down
17 changes: 14 additions & 3 deletions pkg/systemd/generate/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ type containerInfo struct {
ExecStartPre string
// ExecStart of the unit.
ExecStart string
// TimeoutStartSec of the unit.
TimeoutStartSec uint
// TimeoutStopSec of the unit.
TimeoutStopSec uint
// ExecStop of the unit.
Expand Down Expand Up @@ -109,6 +111,9 @@ Restart={{{{.RestartPolicy}}}}
{{{{- if .StartLimitBurst}}}}
StartLimitBurst={{{{.StartLimitBurst}}}}
{{{{- end}}}}
{{{{- if ne .TimeoutStartSec 0}}}}
TimeoutStartSec={{{{.TimeoutStartSec}}}}
{{{{- end}}}}
TimeoutStopSec={{{{.TimeoutStopSec}}}}
{{{{- if .ExecStartPre}}}}
ExecStartPre={{{{.ExecStartPre}}}}
Expand Down Expand Up @@ -148,9 +153,14 @@ func ContainerUnit(ctr *libpod.Container, options entities.GenerateSystemdOption
}

func generateContainerInfo(ctr *libpod.Container, options entities.GenerateSystemdOptions) (*containerInfo, error) {
timeout := ctr.StopTimeout()
stopTimeout := ctr.StopTimeout()
if options.StopTimeout != nil {
timeout = *options.StopTimeout
stopTimeout = *options.StopTimeout
}

startTimeout := uint(0)
if options.StartTimeout != nil {
startTimeout = *options.StartTimeout
}

config := ctr.Config()
Expand Down Expand Up @@ -185,7 +195,8 @@ func generateContainerInfo(ctr *libpod.Container, options entities.GenerateSyste
ContainerNameOrID: nameOrID,
RestartPolicy: define.DefaultRestartPolicy,
PIDFile: conmonPidFile,
StopTimeout: timeout,
TimeoutStartSec: startTimeout,
StopTimeout: stopTimeout,
GenerateTimestamp: true,
CreateCommand: createCommand,
RunRoot: runRoot,
Expand Down
6 changes: 3 additions & 3 deletions pkg/systemd/generate/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ func generatePodInfo(pod *libpod.Pod, options entities.GenerateSystemdOptions) (
return nil, errors.Wrap(err, "could not find infra container")
}

timeout := infraCtr.StopTimeout()
stopTimeout := infraCtr.StopTimeout()
if options.StopTimeout != nil {
timeout = *options.StopTimeout
stopTimeout = *options.StopTimeout
}

config := infraCtr.Config()
Expand All @@ -223,7 +223,7 @@ func generatePodInfo(pod *libpod.Pod, options entities.GenerateSystemdOptions) (
ServiceName: serviceName,
InfraNameOrID: ctrNameOrID,
PIDFile: conmonPidFile,
StopTimeout: timeout,
StopTimeout: stopTimeout,
GenerateTimestamp: true,
CreateCommand: createCommand,
}
Expand Down
9 changes: 9 additions & 0 deletions test/e2e/generate_systemd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ var _ = Describe("Podman generate systemd", func() {
session := podmanTest.Podman([]string{"generate", "systemd", "--time", "5", "nginx"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
Expect(session.OutputToString()).To(ContainSubstring("TimeoutStopSec=65"))
Expect(session.OutputToString()).ToNot(ContainSubstring("TimeoutStartSec="))
Expect(session.OutputToString()).To(ContainSubstring("podman stop -t 5"))

session = podmanTest.Podman([]string{"generate", "systemd", "--stop-timeout", "5", "--start-timeout", "123", "nginx"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
Expect(session.OutputToString()).To(ContainSubstring("TimeoutStartSec=123"))
Expect(session.OutputToString()).To(ContainSubstring("TimeoutStopSec=65"))
Expect(session.OutputToString()).To(ContainSubstring("podman stop -t 5"))
})

Expand Down
2 changes: 1 addition & 1 deletion test/system/250-systemd.bats
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function service_cleanup() {
}

# Regression test for #11438
@test "podman generate systemd - restart policy" {
@test "podman generate systemd - restart policy & timeouts" {
cname=$(random_string)
run_podman create --restart=always --name $cname $IMAGE
run_podman generate systemd --new $cname
Expand Down