github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/generate_systemd.go (about) 1 package main 2 3 import ( 4 "fmt" 5 6 "github.com/containers/libpod/cmd/podman/cliconfig" 7 "github.com/containers/libpod/pkg/adapter" 8 "github.com/pkg/errors" 9 "github.com/spf13/cobra" 10 ) 11 12 var ( 13 containerSystemdCommand cliconfig.GenerateSystemdValues 14 containerSystemdDescription = `Command generates a systemd unit file for a Podman container 15 ` 16 _containerSystemdCommand = &cobra.Command{ 17 Use: "systemd [flags] CONTAINER | POD", 18 Short: "Generate a systemd unit file for a Podman container", 19 Long: containerSystemdDescription, 20 RunE: func(cmd *cobra.Command, args []string) error { 21 containerSystemdCommand.InputArgs = args 22 containerSystemdCommand.GlobalFlags = MainGlobalOpts 23 containerSystemdCommand.Remote = remoteclient 24 return generateSystemdCmd(&containerSystemdCommand) 25 }, 26 Args: func(cmd *cobra.Command, args []string) error { 27 if len(args) > 1 || len(args) < 1 { 28 return errors.New("provide only one container name or ID") 29 } 30 return nil 31 }, 32 Example: `podman generate systemd ctrID 33 `, 34 } 35 ) 36 37 func init() { 38 containerSystemdCommand.Command = _containerSystemdCommand 39 containerSystemdCommand.SetHelpTemplate(HelpTemplate()) 40 containerSystemdCommand.SetUsageTemplate(UsageTemplate()) 41 flags := containerSystemdCommand.Flags() 42 flags.BoolVarP(&containerSystemdCommand.Name, "name", "n", false, "use the container/pod name instead of ID") 43 if !remoteclient { 44 flags.BoolVarP(&containerSystemdCommand.Files, "files", "f", false, "generate files instead of printing to stdout") 45 } 46 flags.UintVarP(&containerSystemdCommand.StopTimeout, "time", "t", defaultContainerConfig.Engine.StopTimeout, "stop timeout override") 47 flags.StringVar(&containerSystemdCommand.RestartPolicy, "restart-policy", "on-failure", "applicable systemd restart-policy") 48 flags.BoolVarP(&containerSystemdCommand.New, "new", "", false, "create a new container instead of starting an existing one") 49 flags.SetNormalizeFunc(aliasFlags) 50 } 51 52 func generateSystemdCmd(c *cliconfig.GenerateSystemdValues) error { 53 runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand) 54 if err != nil { 55 return errors.Wrapf(err, "could not get runtime") 56 } 57 defer runtime.DeferredShutdown(false) 58 59 unit, err := runtime.GenerateSystemd(c) 60 if err != nil { 61 return err 62 } 63 fmt.Println(unit) 64 return nil 65 }