mirror of
https://github.com/containers/podman.git
synced 2025-06-21 01:19:15 +08:00
Add restart-sec option to systemd generate
Signed-off-by: Ondra Machacek <omachace@redhat.com>
This commit is contained in:
@ -23,6 +23,7 @@ const (
|
|||||||
stopTimeoutFlagName = "stop-timeout"
|
stopTimeoutFlagName = "stop-timeout"
|
||||||
stopTimeoutCompatFlagName = "time"
|
stopTimeoutCompatFlagName = "time"
|
||||||
restartPolicyFlagName = "restart-policy"
|
restartPolicyFlagName = "restart-policy"
|
||||||
|
restartSecFlagName = "restart-sec"
|
||||||
newFlagName = "new"
|
newFlagName = "new"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -30,6 +31,7 @@ var (
|
|||||||
files bool
|
files bool
|
||||||
format string
|
format string
|
||||||
systemdRestart string
|
systemdRestart string
|
||||||
|
systemdRestartSec uint
|
||||||
startTimeout uint
|
startTimeout uint
|
||||||
stopTimeout uint
|
stopTimeout uint
|
||||||
systemdOptions = entities.GenerateSystemdOptions{}
|
systemdOptions = entities.GenerateSystemdOptions{}
|
||||||
@ -88,6 +90,9 @@ func init() {
|
|||||||
flags.StringVar(&systemdRestart, restartPolicyFlagName, systemDefine.DefaultRestartPolicy, "Systemd restart-policy")
|
flags.StringVar(&systemdRestart, restartPolicyFlagName, systemDefine.DefaultRestartPolicy, "Systemd restart-policy")
|
||||||
_ = systemdCmd.RegisterFlagCompletionFunc(restartPolicyFlagName, common.AutocompleteSystemdRestartOptions)
|
_ = systemdCmd.RegisterFlagCompletionFunc(restartPolicyFlagName, common.AutocompleteSystemdRestartOptions)
|
||||||
|
|
||||||
|
flags.UintVarP(&systemdRestartSec, restartSecFlagName, "", 0, "Systemd restart-sec")
|
||||||
|
_ = systemdCmd.RegisterFlagCompletionFunc(restartSecFlagName, completion.AutocompleteNone)
|
||||||
|
|
||||||
formatFlagName := "format"
|
formatFlagName := "format"
|
||||||
flags.StringVar(&format, formatFlagName, "", "Print the created units in specified format (json)")
|
flags.StringVar(&format, formatFlagName, "", "Print the created units in specified format (json)")
|
||||||
_ = systemdCmd.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(nil))
|
_ = systemdCmd.RegisterFlagCompletionFunc(formatFlagName, common.AutocompleteFormat(nil))
|
||||||
@ -111,6 +116,9 @@ func systemd(cmd *cobra.Command, args []string) error {
|
|||||||
systemdOptions.New = true
|
systemdOptions.New = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cmd.Flags().Changed(restartSecFlagName) {
|
||||||
|
systemdOptions.RestartSec = &systemdRestartSec
|
||||||
|
}
|
||||||
if cmd.Flags().Changed(startTimeoutFlagName) {
|
if cmd.Flags().Changed(startTimeoutFlagName) {
|
||||||
systemdOptions.StartTimeout = &startTimeout
|
systemdOptions.StartTimeout = &startTimeout
|
||||||
}
|
}
|
||||||
|
@ -51,6 +51,11 @@ Override the default stop timeout for the container with the given value in seco
|
|||||||
Set the systemd restart policy. The restart-policy must be one of: "no", "on-success", "on-failure", "on-abnormal",
|
Set the systemd restart policy. The restart-policy must be one of: "no", "on-success", "on-failure", "on-abnormal",
|
||||||
"on-watchdog", "on-abort", or "always". The default policy is *on-failure*.
|
"on-watchdog", "on-abort", or "always". The default policy is *on-failure*.
|
||||||
|
|
||||||
|
#### **--restart-sec**=*time*
|
||||||
|
|
||||||
|
Set the systemd service restartsec value. Configures the time to sleep before restarting a service (as configured with restart-policy).
|
||||||
|
Takes a value in seconds.
|
||||||
|
|
||||||
#### **--container-prefix**=*prefix*
|
#### **--container-prefix**=*prefix*
|
||||||
|
|
||||||
Set the systemd unit name prefix for containers. The default is *container*.
|
Set the systemd unit name prefix for containers. The default is *container*.
|
||||||
|
@ -22,6 +22,7 @@ func GenerateSystemd(w http.ResponseWriter, r *http.Request) {
|
|||||||
NoHeader bool `schema:"noHeader"`
|
NoHeader bool `schema:"noHeader"`
|
||||||
TemplateUnitFile bool `schema:"templateUnitFile"`
|
TemplateUnitFile bool `schema:"templateUnitFile"`
|
||||||
RestartPolicy *string `schema:"restartPolicy"`
|
RestartPolicy *string `schema:"restartPolicy"`
|
||||||
|
RestartSec uint `schema:"restartSec"`
|
||||||
StopTimeout uint `schema:"stopTimeout"`
|
StopTimeout uint `schema:"stopTimeout"`
|
||||||
StartTimeout uint `schema:"startTimeout"`
|
StartTimeout uint `schema:"startTimeout"`
|
||||||
ContainerPrefix string `schema:"containerPrefix"`
|
ContainerPrefix string `schema:"containerPrefix"`
|
||||||
@ -53,6 +54,7 @@ func GenerateSystemd(w http.ResponseWriter, r *http.Request) {
|
|||||||
ContainerPrefix: query.ContainerPrefix,
|
ContainerPrefix: query.ContainerPrefix,
|
||||||
PodPrefix: query.PodPrefix,
|
PodPrefix: query.PodPrefix,
|
||||||
Separator: query.Separator,
|
Separator: query.Separator,
|
||||||
|
RestartSec: &query.RestartSec,
|
||||||
}
|
}
|
||||||
|
|
||||||
report, err := containerEngine.GenerateSystemd(r.Context(), utils.GetName(r), options)
|
report, err := containerEngine.GenerateSystemd(r.Context(), utils.GetName(r), options)
|
||||||
|
@ -67,6 +67,11 @@ func (s *APIServer) registerGenerateHandlers(r *mux.Router) error {
|
|||||||
// type: string
|
// type: string
|
||||||
// default: "-"
|
// default: "-"
|
||||||
// description: Systemd unit name separator between name/id and prefix.
|
// description: Systemd unit name separator between name/id and prefix.
|
||||||
|
// - in: query
|
||||||
|
// name: restartSec
|
||||||
|
// type: integer
|
||||||
|
// default: 0
|
||||||
|
// description: Configures the time to sleep before restarting a service.
|
||||||
// produces:
|
// produces:
|
||||||
// - application/json
|
// - application/json
|
||||||
// responses:
|
// responses:
|
||||||
|
@ -20,6 +20,8 @@ type SystemdOptions struct {
|
|||||||
TemplateUnitFile *bool
|
TemplateUnitFile *bool
|
||||||
// RestartPolicy - systemd restart policy.
|
// RestartPolicy - systemd restart policy.
|
||||||
RestartPolicy *string
|
RestartPolicy *string
|
||||||
|
// RestartSec - systemd service restartsec. Configures the time to sleep before restarting a service.
|
||||||
|
RestartSec *uint
|
||||||
// StartTimeout - time when starting the container.
|
// StartTimeout - time when starting the container.
|
||||||
StartTimeout *uint
|
StartTimeout *uint
|
||||||
// StopTimeout - time when stopping the container.
|
// StopTimeout - time when stopping the container.
|
||||||
|
@ -92,6 +92,21 @@ func (o *SystemdOptions) GetRestartPolicy() string {
|
|||||||
return *o.RestartPolicy
|
return *o.RestartPolicy
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithRestartSec set field RestartSec to given value
|
||||||
|
func (o *SystemdOptions) WithRestartSec(value uint) *SystemdOptions {
|
||||||
|
o.RestartSec = &value
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetRestartSec returns value of field RestartSec
|
||||||
|
func (o *SystemdOptions) GetRestartSec() uint {
|
||||||
|
if o.RestartSec == nil {
|
||||||
|
var z uint
|
||||||
|
return z
|
||||||
|
}
|
||||||
|
return *o.RestartSec
|
||||||
|
}
|
||||||
|
|
||||||
// WithStartTimeout set field StartTimeout to given value
|
// WithStartTimeout set field StartTimeout to given value
|
||||||
func (o *SystemdOptions) WithStartTimeout(value uint) *SystemdOptions {
|
func (o *SystemdOptions) WithStartTimeout(value uint) *SystemdOptions {
|
||||||
o.StartTimeout = &value
|
o.StartTimeout = &value
|
||||||
|
@ -10,6 +10,8 @@ type GenerateSystemdOptions struct {
|
|||||||
New bool
|
New bool
|
||||||
// RestartPolicy - systemd restart policy.
|
// RestartPolicy - systemd restart policy.
|
||||||
RestartPolicy *string
|
RestartPolicy *string
|
||||||
|
// RestartSec - systemd service restartsec. Configures the time to sleep before restarting a service.
|
||||||
|
RestartSec *uint
|
||||||
// StartTimeout - time when starting the container.
|
// StartTimeout - time when starting the container.
|
||||||
StartTimeout *uint
|
StartTimeout *uint
|
||||||
// StopTimeout - time when stopping the container.
|
// StopTimeout - time when stopping the container.
|
||||||
|
@ -19,6 +19,9 @@ func (ic *ContainerEngine) GenerateSystemd(ctx context.Context, nameOrID string,
|
|||||||
if opts.RestartPolicy != nil {
|
if opts.RestartPolicy != nil {
|
||||||
options.WithRestartPolicy(*opts.RestartPolicy)
|
options.WithRestartPolicy(*opts.RestartPolicy)
|
||||||
}
|
}
|
||||||
|
if opts.RestartSec != nil {
|
||||||
|
options.WithRestartSec(*opts.RestartSec)
|
||||||
|
}
|
||||||
|
|
||||||
return generate.Systemd(ic.ClientCtx, nameOrID, options)
|
return generate.Systemd(ic.ClientCtx, nameOrID, options)
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,8 @@ type podInfo struct {
|
|||||||
StopTimeout uint
|
StopTimeout uint
|
||||||
// RestartPolicy of the systemd unit (e.g., no, on-failure, always).
|
// RestartPolicy of the systemd unit (e.g., no, on-failure, always).
|
||||||
RestartPolicy string
|
RestartPolicy string
|
||||||
|
// RestartSec of the systemd unit. Configures the time to sleep before restarting a service.
|
||||||
|
RestartSec uint
|
||||||
// PIDFile of the service. Required for forking services. Must point to the
|
// PIDFile of the service. Required for forking services. Must point to the
|
||||||
// PID of the associated conmon process.
|
// PID of the associated conmon process.
|
||||||
PIDFile string
|
PIDFile string
|
||||||
@ -89,6 +91,9 @@ Before={{{{- range $index, $value := .RequiredServices -}}}}{{{{if $index}}}} {{
|
|||||||
[Service]
|
[Service]
|
||||||
Environment={{{{.EnvVariable}}}}=%n
|
Environment={{{{.EnvVariable}}}}=%n
|
||||||
Restart={{{{.RestartPolicy}}}}
|
Restart={{{{.RestartPolicy}}}}
|
||||||
|
{{{{- if .RestartSec}}}}
|
||||||
|
RestartSec={{{{.RestartSec}}}}
|
||||||
|
{{{{- end}}}}
|
||||||
TimeoutStopSec={{{{.TimeoutStopSec}}}}
|
TimeoutStopSec={{{{.TimeoutStopSec}}}}
|
||||||
{{{{- if .ExecStartPre1}}}}
|
{{{{- if .ExecStartPre1}}}}
|
||||||
ExecStartPre={{{{.ExecStartPre1}}}}
|
ExecStartPre={{{{.ExecStartPre1}}}}
|
||||||
@ -242,6 +247,10 @@ func executePodTemplate(info *podInfo, options entities.GenerateSystemdOptions)
|
|||||||
info.RestartPolicy = *options.RestartPolicy
|
info.RestartPolicy = *options.RestartPolicy
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if options.RestartSec != nil {
|
||||||
|
info.RestartSec = *options.RestartSec
|
||||||
|
}
|
||||||
|
|
||||||
// Make sure the executable is set.
|
// Make sure the executable is set.
|
||||||
if info.Executable == "" {
|
if info.Executable == "" {
|
||||||
executable, err := os.Executable()
|
executable, err := os.Executable()
|
||||||
|
@ -67,6 +67,33 @@ WantedBy=default.target
|
|||||||
podGood := serviceInfo + headerInfo + podContent
|
podGood := serviceInfo + headerInfo + podContent
|
||||||
podGoodNoHeaderInfo := serviceInfo + podContent
|
podGoodNoHeaderInfo := serviceInfo + podContent
|
||||||
|
|
||||||
|
podGoodRestartSec := `# pod-123abc.service
|
||||||
|
# autogenerated by Podman CI
|
||||||
|
|
||||||
|
[Unit]
|
||||||
|
Description=Podman pod-123abc.service
|
||||||
|
Documentation=man:podman-generate-systemd(1)
|
||||||
|
Wants=network-online.target
|
||||||
|
After=network-online.target
|
||||||
|
RequiresMountsFor=/var/run/containers/storage
|
||||||
|
Requires=container-1.service container-2.service
|
||||||
|
Before=container-1.service container-2.service
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Environment=PODMAN_SYSTEMD_UNIT=%n
|
||||||
|
Restart=on-failure
|
||||||
|
RestartSec=15
|
||||||
|
TimeoutStopSec=102
|
||||||
|
ExecStart=/usr/bin/podman start jadda-jadda-infra
|
||||||
|
ExecStop=/usr/bin/podman stop -t 42 jadda-jadda-infra
|
||||||
|
ExecStopPost=/usr/bin/podman stop -t 42 jadda-jadda-infra
|
||||||
|
PIDFile=/run/containers/storage/overlay-containers/639c53578af4d84b8800b4635fa4e680ee80fd67e0e6a2d4eea48d1e3230f401/userdata/conmon.pid
|
||||||
|
Type=forking
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=default.target
|
||||||
|
`
|
||||||
|
|
||||||
podGoodNamedNew := `# pod-123abc.service
|
podGoodNamedNew := `# pod-123abc.service
|
||||||
# autogenerated by Podman CI
|
# autogenerated by Podman CI
|
||||||
|
|
||||||
@ -205,6 +232,25 @@ WantedBy=default.target
|
|||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
},
|
},
|
||||||
|
{"pod restartSec",
|
||||||
|
podInfo{
|
||||||
|
Executable: "/usr/bin/podman",
|
||||||
|
ServiceName: "pod-123abc",
|
||||||
|
InfraNameOrID: "jadda-jadda-infra",
|
||||||
|
PIDFile: "/run/containers/storage/overlay-containers/639c53578af4d84b8800b4635fa4e680ee80fd67e0e6a2d4eea48d1e3230f401/userdata/conmon.pid",
|
||||||
|
StopTimeout: 42,
|
||||||
|
PodmanVersion: "CI",
|
||||||
|
GraphRoot: "/var/lib/containers/storage",
|
||||||
|
RunRoot: "/var/run/containers/storage",
|
||||||
|
RequiredServices: []string{"container-1", "container-2"},
|
||||||
|
CreateCommand: []string{"podman", "pod", "create", "--name", "foo", "bar=arg with space"},
|
||||||
|
RestartSec: 15,
|
||||||
|
},
|
||||||
|
podGoodRestartSec,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
},
|
||||||
{"pod noHeader",
|
{"pod noHeader",
|
||||||
podInfo{
|
podInfo{
|
||||||
Executable: "/usr/bin/podman",
|
Executable: "/usr/bin/podman",
|
||||||
|
@ -282,6 +282,19 @@ var _ = Describe("Podman generate systemd", func() {
|
|||||||
Expect(session.OutputToString()).To(ContainSubstring(" pod create "))
|
Expect(session.OutputToString()).To(ContainSubstring(" pod create "))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
It("podman generate systemd --restart-sec 15 --name foo", func() {
|
||||||
|
n := podmanTest.Podman([]string{"pod", "create", "--name", "foo"})
|
||||||
|
n.WaitWithDefaultTimeout()
|
||||||
|
Expect(n).Should(Exit(0))
|
||||||
|
|
||||||
|
session := podmanTest.Podman([]string{"generate", "systemd", "--restart-sec", "15", "--name", "foo"})
|
||||||
|
session.WaitWithDefaultTimeout()
|
||||||
|
Expect(session).Should(Exit(0))
|
||||||
|
|
||||||
|
// Grepping the output (in addition to unit tests)
|
||||||
|
Expect(session.OutputToString()).To(ContainSubstring("RestartSec=15"))
|
||||||
|
})
|
||||||
|
|
||||||
It("podman generate systemd --new=false pod", func() {
|
It("podman generate systemd --new=false pod", func() {
|
||||||
n := podmanTest.Podman([]string{"pod", "create", "--name", "foo"})
|
n := podmanTest.Podman([]string{"pod", "create", "--name", "foo"})
|
||||||
n.WaitWithDefaultTimeout()
|
n.WaitWithDefaultTimeout()
|
||||||
|
Reference in New Issue
Block a user