diff options
-rw-r--r-- | cmd/podman/generate/systemd.go | 3 | ||||
-rw-r--r-- | completions/bash/podman | 5 | ||||
-rw-r--r-- | docs/source/markdown/podman-generate-systemd.1.md | 12 | ||||
-rw-r--r-- | pkg/domain/entities/generate.go | 6 | ||||
-rw-r--r-- | pkg/domain/infra/abi/generate.go | 6 | ||||
-rw-r--r-- | test/e2e/generate_systemd_test.go | 92 |
6 files changed, 120 insertions, 4 deletions
diff --git a/cmd/podman/generate/systemd.go b/cmd/podman/generate/systemd.go index 20d9748d4..75031e070 100644 --- a/cmd/podman/generate/systemd.go +++ b/cmd/podman/generate/systemd.go @@ -39,6 +39,9 @@ func init() { flags.UintVarP(&systemdTimeout, "time", "t", containerConfig.Engine.StopTimeout, "Stop timeout override") flags.StringVar(&systemdOptions.RestartPolicy, "restart-policy", "on-failure", "Systemd restart-policy") flags.BoolVarP(&systemdOptions.New, "new", "", false, "Create a new container instead of starting an existing one") + flags.StringVar(&systemdOptions.ContainerPrefix, "container-prefix", "container", "Systemd unit name prefix for containers") + flags.StringVar(&systemdOptions.PodPrefix, "pod-prefix", "pod", "Systemd unit name prefix for pods") + flags.StringVar(&systemdOptions.Separator, "separator", "-", "Systemd unit name seperator between name/id and prefix") flags.SetNormalizeFunc(utils.AliasFlags) } diff --git a/completions/bash/podman b/completions/bash/podman index 9baf7901e..8f02a4b36 100644 --- a/completions/bash/podman +++ b/completions/bash/podman @@ -2838,7 +2838,10 @@ _podman_generate_systemd() { local options_with_args=" --restart-policy -t - --time" + --time + --container-prefix + --pod-prefix + --separator" local boolean_options=" -h diff --git a/docs/source/markdown/podman-generate-systemd.1.md b/docs/source/markdown/podman-generate-systemd.1.md index fa04f81f9..72031b19b 100644 --- a/docs/source/markdown/podman-generate-systemd.1.md +++ b/docs/source/markdown/podman-generate-systemd.1.md @@ -40,6 +40,18 @@ Override the default stop timeout for the container with the given value. 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*. +**--container-prefix**=*prefix* + +Set the systemd unit name prefix for containers. The default is *container*. + +**--pod-prefix**=*prefix* + +Set the systemd unit name prefix for pods. The default is *pod*. + +**--separator**=*separator* + +Set the systemd unit name seperator between the name/id of a container/pod and the prefix. The default is *-*. + ## Examples ### Generate and print a systemd unit file for a container diff --git a/pkg/domain/entities/generate.go b/pkg/domain/entities/generate.go index edd217615..68a42d897 100644 --- a/pkg/domain/entities/generate.go +++ b/pkg/domain/entities/generate.go @@ -14,6 +14,12 @@ type GenerateSystemdOptions struct { RestartPolicy string // StopTimeout - time when stopping the container. StopTimeout *uint + // ContainerPrefix - systemd unit name prefix for containers + ContainerPrefix string + // PodPrefix - systemd unit name prefix for pods + PodPrefix string + // Separator - systemd unit name seperator between name/id and prefix + Separator string } // GenerateSystemdReport diff --git a/pkg/domain/infra/abi/generate.go b/pkg/domain/infra/abi/generate.go index be5d452bd..abb5e2911 100644 --- a/pkg/domain/infra/abi/generate.go +++ b/pkg/domain/infra/abi/generate.go @@ -159,14 +159,14 @@ func (ic *ContainerEngine) generateSystemdgenContainerInfo(nameOrID string, pod func generateServiceName(ctr *libpod.Container, pod *libpod.Pod, options entities.GenerateSystemdOptions) (string, string) { var kind, name, ctrName string if pod == nil { - kind = "container" + kind = options.ContainerPrefix //defaults to container name = ctr.ID() if options.Name { name = ctr.Name() } ctrName = name } else { - kind = "pod" + kind = options.PodPrefix //defaults to pod name = pod.ID() ctrName = ctr.ID() if options.Name { @@ -174,7 +174,7 @@ func generateServiceName(ctr *libpod.Container, pod *libpod.Pod, options entitie ctrName = ctr.Name() } } - return ctrName, fmt.Sprintf("%s-%s", kind, name) + return ctrName, fmt.Sprintf("%s%s%s", kind, options.Separator, name) } func (ic *ContainerEngine) GenerateKube(ctx context.Context, nameOrID string, options entities.GenerateKubeOptions) (*entities.GenerateKubeReport, error) { diff --git a/test/e2e/generate_systemd_test.go b/test/e2e/generate_systemd_test.go index abfca4db9..d5ae441e2 100644 --- a/test/e2e/generate_systemd_test.go +++ b/test/e2e/generate_systemd_test.go @@ -233,4 +233,96 @@ var _ = Describe("Podman generate systemd", func() { Expect(session.ExitCode()).To(Equal(125)) }) + It("podman generate systemd --container-prefix con", func() { + n := podmanTest.Podman([]string{"create", "--name", "foo", "alpine", "top"}) + n.WaitWithDefaultTimeout() + Expect(n.ExitCode()).To(Equal(0)) + + session := podmanTest.Podman([]string{"generate", "systemd", "--name", "--container-prefix", "con", "foo"}) + session.WaitWithDefaultTimeout() + Expect(session.ExitCode()).To(Equal(0)) + + // Grepping the output (in addition to unit tests) + found, _ := session.GrepString("# con-foo.service") + Expect(found).To(BeTrue()) + }) + + It("podman generate systemd --separator _", func() { + n := podmanTest.Podman([]string{"create", "--name", "foo", "alpine", "top"}) + n.WaitWithDefaultTimeout() + Expect(n.ExitCode()).To(Equal(0)) + + session := podmanTest.Podman([]string{"generate", "systemd", "--name", "--separator", "_", "foo"}) + session.WaitWithDefaultTimeout() + Expect(session.ExitCode()).To(Equal(0)) + + // Grepping the output (in addition to unit tests) + found, _ := session.GrepString("# container_foo.service") + Expect(found).To(BeTrue()) + }) + + It("podman generate systemd pod --pod-prefix p", func() { + n := podmanTest.Podman([]string{"pod", "create", "--name", "foo"}) + n.WaitWithDefaultTimeout() + Expect(n.ExitCode()).To(Equal(0)) + + n = podmanTest.Podman([]string{"create", "--pod", "foo", "--name", "foo-1", "alpine", "top"}) + n.WaitWithDefaultTimeout() + Expect(n.ExitCode()).To(Equal(0)) + + n = podmanTest.Podman([]string{"create", "--pod", "foo", "--name", "foo-2", "alpine", "top"}) + n.WaitWithDefaultTimeout() + Expect(n.ExitCode()).To(Equal(0)) + + session := podmanTest.Podman([]string{"generate", "systemd", "--pod-prefix", "p", "--name", "foo"}) + session.WaitWithDefaultTimeout() + Expect(session.ExitCode()).To(Equal(0)) + + // Grepping the output (in addition to unit tests) + found, _ := session.GrepString("# p-foo.service") + Expect(found).To(BeTrue()) + + found, _ = session.GrepString("Requires=container-foo-1.service container-foo-2.service") + Expect(found).To(BeTrue()) + + found, _ = session.GrepString("# container-foo-1.service") + Expect(found).To(BeTrue()) + + found, _ = session.GrepString("BindsTo=p-foo.service") + Expect(found).To(BeTrue()) + }) + + It("podman generate systemd pod --pod-prefix p --container-prefix con --separator _ change all prefixes/separator", func() { + n := podmanTest.Podman([]string{"pod", "create", "--name", "foo"}) + n.WaitWithDefaultTimeout() + Expect(n.ExitCode()).To(Equal(0)) + + n = podmanTest.Podman([]string{"create", "--pod", "foo", "--name", "foo-1", "alpine", "top"}) + n.WaitWithDefaultTimeout() + Expect(n.ExitCode()).To(Equal(0)) + + n = podmanTest.Podman([]string{"create", "--pod", "foo", "--name", "foo-2", "alpine", "top"}) + n.WaitWithDefaultTimeout() + Expect(n.ExitCode()).To(Equal(0)) + + session := podmanTest.Podman([]string{"generate", "systemd", "--container-prefix", "con", "--pod-prefix", "p", "--separator", "_", "--name", "foo"}) + session.WaitWithDefaultTimeout() + Expect(session.ExitCode()).To(Equal(0)) + + // Grepping the output (in addition to unit tests) + found, _ := session.GrepString("# p_foo.service") + Expect(found).To(BeTrue()) + + found, _ = session.GrepString("Requires=con_foo-1.service con_foo-2.service") + Expect(found).To(BeTrue()) + + found, _ = session.GrepString("# con_foo-1.service") + Expect(found).To(BeTrue()) + + found, _ = session.GrepString("# con_foo-2.service") + Expect(found).To(BeTrue()) + + found, _ = session.GrepString("BindsTo=p_foo.service") + Expect(found).To(BeTrue()) + }) }) |