Skip to content

Commit

Permalink
Merge pull request #6321 from Luap99/podman-generate-systemd-unit-prefix
Browse files Browse the repository at this point in the history
Allow to change the generated systemd unit name prefix
  • Loading branch information
openshift-merge-robot authored May 25, 2020
2 parents 3fec749 + e704f13 commit 1077d2d
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 4 deletions.
3 changes: 3 additions & 0 deletions cmd/podman/generate/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
5 changes: 4 additions & 1 deletion completions/bash/podman
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions docs/source/markdown/podman-generate-systemd.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions pkg/domain/entities/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pkg/domain/infra/abi/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,22 +159,22 @@ 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 {
name = pod.Name()
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) {
Expand Down
92 changes: 92 additions & 0 deletions test/e2e/generate_systemd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
})

0 comments on commit 1077d2d

Please sign in to comment.