summaryrefslogtreecommitdiff
path: root/pkg/systemd/generate/containers.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/systemd/generate/containers.go')
-rw-r--r--pkg/systemd/generate/containers.go69
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, " ")