Added new flags to 'podman generate systemd' to change the unit name prefix

--container-prefix <string> - default 'container'
Systemd unit name prefix for containers

--pod-prefix <string> - default 'pod'
Systemd unit name prefix for pods

--separator <string> - default '-'
Systemd unit name seperator between name/id and prefix

Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
This commit is contained in:
Luap99
2020-05-21 17:36:41 +02:00
parent 72e880351a
commit e704f1362a
6 changed files with 120 additions and 4 deletions

View File

@ -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)
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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) {

View File

@ -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())
})
})