diff options
author | Boaz Shuster <boaz.shuster.github@gmail.com> | 2021-11-11 11:45:36 +0200 |
---|---|---|
committer | Boaz Shuster <boaz.shuster.github@gmail.com> | 2021-11-11 11:45:36 +0200 |
commit | 73e1cdfe9e41a8748b75b9461c087e4cee5d2183 (patch) | |
tree | cad9d14e5c451814fcfe035eb52be0f32fb2931a /pkg/systemd/generate/containers.go | |
parent | 6ee3b33d3825177033763e51f756209102e0a309 (diff) | |
download | podman-73e1cdfe9e41a8748b75b9461c087e4cee5d2183.tar.gz podman-73e1cdfe9e41a8748b75b9461c087e4cee5d2183.tar.bz2 podman-73e1cdfe9e41a8748b75b9461c087e4cee5d2183.zip |
export adding id-specifier code to setContainerNameForTemplate
Signed-off-by: Boaz Shuster <boaz.shuster.github@gmail.com>
Diffstat (limited to 'pkg/systemd/generate/containers.go')
-rw-r--r-- | pkg/systemd/generate/containers.go | 69 |
1 files changed, 43 insertions, 26 deletions
diff --git a/pkg/systemd/generate/containers.go b/pkg/systemd/generate/containers.go index a4b18ec88..95ff13371 100644 --- a/pkg/systemd/generate/containers.go +++ b/pkg/systemd/generate/containers.go @@ -206,6 +206,46 @@ func containerServiceName(ctr *libpod.Container, options entities.GenerateSystem return nameOrID, serviceName } +// setContainerNameForTemplate updates startCommand to contain the name argument with +// a value that includes the identify specifier. +// In case startCommand doesn't contain that argument it's added after "run" and its +// value will be set to info.ServiceName concated with the identify specifier %i. +func setContainerNameForTemplate(startCommand []string, info *containerInfo) ([]string, error) { + // find the index of "--name" in the command slice + nameIx := -1 + for argIx, arg := range startCommand { + if arg == "--name" { + nameIx = argIx + 1 + break + } + if strings.HasPrefix(arg, "--name=") { + nameIx = argIx + break + } + } + switch { + case nameIx == -1: + // if not found, add --name argument in the command slice before the "run" argument. + // it's assumed that the command slice contains this argument. + runIx := -1 + for argIx, arg := range startCommand { + if arg == "run" { + runIx = argIx + break + } + } + if runIx == -1 { + return startCommand, fmt.Errorf("\"run\" is missing in the command arguments") + } + startCommand = append(startCommand[:runIx+1], startCommand[runIx:]...) + startCommand[runIx+1] = fmt.Sprintf("--name=%s-%%i", info.ServiceName) + default: + // append the identity specifier (%i) to the end of the --name value + startCommand[nameIx] = fmt.Sprintf("%s-%%i", startCommand[nameIx]) + } + return startCommand, nil +} + // executeContainerTemplate executes the container template on the specified // containerInfo. Note that the containerInfo is also post processed and // completed, which allows for an easier unit testing. @@ -392,32 +432,9 @@ func executeContainerTemplate(info *containerInfo, options entities.GenerateSyst startCommand = escapeSystemdArguments(startCommand) if options.TemplateUnitFile { info.IdentifySpecifier = true - runIx := -1 - nameIx := -1 - // Add systemd identify specifier next to the name value - // to set a name to the container according to the parameters passed to systemd. - // In case no --name set for that container, use ServiceName specified - // in the containerInfo struct. - for argIx, arg := range startCommand { - if arg == "run" { - runIx = argIx - continue - } - if arg == "--name" { - nameIx = argIx + 1 - break - } - if strings.HasPrefix(arg, "--name=") { - nameIx = argIx - break - } - } - if nameIx == -1 { - startCommand = append(startCommand[:runIx+1], startCommand[runIx:]...) - startCommand[runIx+1] = fmt.Sprintf("--name=%s-%%i", info.ServiceName) - fmt.Println(startCommand) - } else { - startCommand[nameIx] = fmt.Sprintf("%s-%%i", startCommand[nameIx]) + startCommand, err = setContainerNameForTemplate(startCommand, info) + if err != nil { + return "", err } } info.ExecStart = strings.Join(startCommand, " ") |