-
Notifications
You must be signed in to change notification settings - Fork 3k
Generate Systemd #2985
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 #2985
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/containers/libpod/cmd/podman/cliconfig" | ||
| "github.com/containers/libpod/pkg/adapter" | ||
| "github.com/containers/libpod/pkg/systemdgen" | ||
| "github.com/pkg/errors" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var ( | ||
| containerSystemdCommand cliconfig.GenerateSystemdValues | ||
| containerSystemdDescription = `Command generates a systemd unit file for a Podman container | ||
| ` | ||
| _containerSystemdCommand = &cobra.Command{ | ||
| Use: "systemd [flags] CONTAINER | POD", | ||
| Short: "Generate a systemd unit file for a Podman container", | ||
| Long: containerSystemdDescription, | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| containerSystemdCommand.InputArgs = args | ||
| containerSystemdCommand.GlobalFlags = MainGlobalOpts | ||
| containerSystemdCommand.Remote = remoteclient | ||
| return generateSystemdCmd(&containerSystemdCommand) | ||
| }, | ||
| Args: func(cmd *cobra.Command, args []string) error { | ||
| if len(args) > 1 || len(args) < 1 { | ||
| return errors.New("provide only one container name or ID") | ||
| } | ||
| return nil | ||
| }, | ||
| Example: `podman generate kube ctrID | ||
| `, | ||
| } | ||
| ) | ||
|
|
||
| func init() { | ||
| containerSystemdCommand.Command = _containerSystemdCommand | ||
| containerSystemdCommand.SetHelpTemplate(HelpTemplate()) | ||
| containerSystemdCommand.SetUsageTemplate(UsageTemplate()) | ||
| flags := containerSystemdCommand.Flags() | ||
| flags.BoolVarP(&containerSystemdCommand.Name, "name", "n", false, "use the container name instead of ID") | ||
| flags.IntVarP(&containerSystemdCommand.StopTimeout, "timeout", "t", -1, "stop timeout override") | ||
| flags.StringVar(&containerSystemdCommand.RestartPolicy, "restart-policy", "on-failure", "applicable systemd restart-policy") | ||
| } | ||
|
|
||
| func generateSystemdCmd(c *cliconfig.GenerateSystemdValues) error { | ||
| runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand) | ||
| if err != nil { | ||
| return errors.Wrapf(err, "could not get runtime") | ||
| } | ||
| defer runtime.Shutdown(false) | ||
|
|
||
| // User input stop timeout must be 0 or greater | ||
| if c.Flag("timeout").Changed && c.StopTimeout < 0 { | ||
| return errors.New("timeout value must be 0 or greater") | ||
| } | ||
| // Make sure the input restart policy is valid | ||
| if err := systemdgen.ValidateRestartPolicy(c.RestartPolicy); err != nil { | ||
| return err | ||
| } | ||
|
|
||
baude marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| unit, err := runtime.GenerateSystemd(c) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| fmt.Println(unit) | ||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| % podman-generate Podman Man Pages | ||
| % Brent Baude | ||
| % April 2019 | ||
| # NAME | ||
| podman-generate-systemd- Generate Systemd Unit file | ||
|
|
||
| # SYNOPSIS | ||
| **podman generate systemd** [*-n*|*--name*] [*-t*|*--timeout*] [*--restart-policy*] *container* | ||
|
|
||
| # DESCRIPTION | ||
| **podman generate systemd** will create a Systemd unit file that can be used to control a container. The | ||
| command will dynamically create the unit file and output it to stdout where it can be piped by the user | ||
| to a file. The options can be used to influence the results of the output as well. | ||
|
|
||
|
|
||
| # OPTIONS: | ||
|
|
||
| **--name** **-n** | ||
|
|
||
| Use the name of the container for the start, stop, and description in the unit file | ||
|
|
||
| **--timeout** **-t** | ||
|
|
||
| Override the default stop timeout for the container with the given value. | ||
|
|
||
| **--restart-policy** | ||
| 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*. | ||
|
|
||
| ## Examples ## | ||
|
|
||
| Create a systemd unit file for a container running nginx: | ||
|
|
||
| ``` | ||
| $ sudo podman generate systemd nginx | ||
| [Unit] | ||
| Description=c21da63c4783be2ac2cd3487ef8d2ec15ee2a28f63dd8f145e3b05607f31cffc Podman Container | ||
| [Service] | ||
| Restart=on-failure | ||
| ExecStart=/usr/bin/podman start c21da63c4783be2ac2cd3487ef8d2ec15ee2a28f63dd8f145e3b05607f31cffc | ||
| ExecStop=/usr/bin/podman stop -t 10 c21da63c4783be2ac2cd3487ef8d2ec15ee2a28f63dd8f145e3b05607f31cffc | ||
| KillMode=none | ||
| Type=forking | ||
| PIDFile=/var/lib/containers/storage/overlay-containers/c21da63c4783be2ac2cd3487ef8d2ec15ee2a28f63dd8f145e3b05607f31cffc/userdata/c21da63c4783be2ac2cd3487ef8d2ec15ee2a28f63dd8f145e3b05607f31cffc.pid | ||
| [Install] | ||
| WantedBy=multi-user.target | ||
| ``` | ||
|
|
||
| Create a systemd unit file for a container running nginx with an *always* restart policy and 1-second timeout. | ||
| ``` | ||
| $ sudo podman generate systemd --restart-policy=always -t 1 nginx | ||
| [Unit] | ||
| Description=c21da63c4783be2ac2cd3487ef8d2ec15ee2a28f63dd8f145e3b05607f31cffc Podman Container | ||
| [Service] | ||
| Restart=always | ||
| ExecStart=/usr/bin/podman start c21da63c4783be2ac2cd3487ef8d2ec15ee2a28f63dd8f145e3b05607f31cffc | ||
| ExecStop=/usr/bin/podman stop -t 1 c21da63c4783be2ac2cd3487ef8d2ec15ee2a28f63dd8f145e3b05607f31cffc | ||
| KillMode=none | ||
| Type=forking | ||
| PIDFile=/var/lib/containers/storage/overlay-containers/c21da63c4783be2ac2cd3487ef8d2ec15ee2a28f63dd8f145e3b05607f31cffc/userdata/c21da63c4783be2ac2cd3487ef8d2ec15ee2a28f63dd8f145e3b05607f31cffc.pid | ||
|
||
| [Install] | ||
| WantedBy=multi-user.target | ||
| ``` | ||
|
|
||
| ## SEE ALSO | ||
| podman(1), podman-container(1) | ||
|
|
||
| # HISTORY | ||
| April 2019, Originally compiled by Brent Baude (bbaude at redhat dot com) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package systemdgen | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "path/filepath" | ||
|
|
||
| "github.com/pkg/errors" | ||
| ) | ||
|
|
||
| var template = `[Unit] | ||
| Description=%s Podman Container | ||
| [Service] | ||
| Restart=%s | ||
| ExecStart=/usr/bin/podman start %s | ||
| ExecStop=/usr/bin/podman stop -t %d %s | ||
| KillMode=none | ||
| Type=forking | ||
| PIDFile=%s | ||
|
||
| [Install] | ||
| WantedBy=multi-user.target` | ||
|
|
||
| var restartPolicies = []string{"no", "on-success", "on-failure", "on-abnormal", "on-watchdog", "on-abort", "always"} | ||
|
|
||
| // ValidateRestartPolicy checks that the user-provided policy is valid | ||
| func ValidateRestartPolicy(restart string) error { | ||
| for _, i := range restartPolicies { | ||
| if i == restart { | ||
| return nil | ||
| } | ||
| } | ||
| return errors.Errorf("%s is not a valid restart policy", restart) | ||
| } | ||
|
|
||
| // CreateSystemdUnitAsString takes variables to create a systemd unit file used to control | ||
| // a libpod container | ||
| func CreateSystemdUnitAsString(name, cid, restart, pidPath string, stopTimeout int) (string, error) { | ||
| if err := ValidateRestartPolicy(restart); err != nil { | ||
| return "", err | ||
| } | ||
| pidFile := filepath.Join(pidPath, fmt.Sprintf("%s.pid", cid)) | ||
| unit := fmt.Sprintf(template, name, restart, name, stopTimeout, name, pidFile) | ||
| return unit, nil | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.