diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2021-05-10 11:43:40 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-10 11:43:40 -0400 |
commit | fbc128ee93553d7f9fa9f665d6a3fe5b0b596e63 (patch) | |
tree | 74fb3dde23d26d19e676ebbf639d8b26ebf747a4 /pkg/systemd/generate/containers.go | |
parent | 446e5b300611ce576d5d3980fbf57fe3653b04a1 (diff) | |
parent | 77e6ae24369e6c7bed85141ae6f7d0c7b0e26c0b (diff) | |
download | podman-fbc128ee93553d7f9fa9f665d6a3fe5b0b596e63.tar.gz podman-fbc128ee93553d7f9fa9f665d6a3fe5b0b596e63.tar.bz2 podman-fbc128ee93553d7f9fa9f665d6a3fe5b0b596e63.zip |
Merge pull request #10236 from Luap99/generate-systemd-env
Add envars to the generated systemd unit
Diffstat (limited to 'pkg/systemd/generate/containers.go')
-rw-r--r-- | pkg/systemd/generate/containers.go | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/pkg/systemd/generate/containers.go b/pkg/systemd/generate/containers.go index e06655a8d..eb1fb67ff 100644 --- a/pkg/systemd/generate/containers.go +++ b/pkg/systemd/generate/containers.go @@ -54,6 +54,12 @@ type containerInfo struct { // CreateCommand is the full command plus arguments of the process the // container has been created with. CreateCommand []string + // containerEnv stores the container environment variables + containerEnv []string + // ExtraEnvs contains the container environment variables referenced + // by only the key in the container create command, e.g. --env FOO. + // This is only used with --new + ExtraEnvs []string // EnvVariable is generate.EnvVariable and must not be set. EnvVariable string // ExecStartPre of the unit. @@ -87,6 +93,9 @@ After={{{{- range $index, $value := .BoundToServices -}}}}{{{{if $index}}}} {{{{ [Service] Environment={{{{.EnvVariable}}}}=%n +{{{{- if .ExtraEnvs}}}} +Environment={{{{- range $index, $value := .ExtraEnvs -}}}}{{{{if $index}}}} {{{{end}}}}{{{{ $value }}}}{{{{end}}}} +{{{{- end}}}} Restart={{{{.RestartPolicy}}}} TimeoutStopSec={{{{.TimeoutStopSec}}}} {{{{- if .ExecStartPre}}}} @@ -153,6 +162,8 @@ func generateContainerInfo(ctr *libpod.Container, options entities.GenerateSyste return nil, errors.Errorf("could not lookup container's runroot: got empty string") } + envs := config.Spec.Process.Env + info := containerInfo{ ServiceName: serviceName, ContainerNameOrID: nameOrID, @@ -163,6 +174,7 @@ func generateContainerInfo(ctr *libpod.Container, options entities.GenerateSyste CreateCommand: createCommand, GraphRoot: graphRoot, RunRoot: runRoot, + containerEnv: envs, } return &info, nil @@ -248,6 +260,7 @@ func executeContainerTemplate(info *containerInfo, options entities.GenerateSyst fs.BoolP("detach", "d", false, "") fs.String("name", "", "") fs.Bool("replace", false, "") + fs.StringArrayP("env", "e", nil, "") fs.Parse(remainingCmd) remainingCmd = filterCommonContainerFlags(remainingCmd, fs.NArg()) @@ -304,6 +317,24 @@ func executeContainerTemplate(info *containerInfo, options entities.GenerateSyst remainingCmd = removeReplaceArg(remainingCmd, fs.NArg()) } } + + envs, err := fs.GetStringArray("env") + if err != nil { + return "", err + } + for _, env := range envs { + // if env arg does not contain a equal sign we have to add the envar to the unit + // because it does try to red the value from the environment + if !strings.Contains(env, "=") { + for _, containerEnv := range info.containerEnv { + split := strings.SplitN(containerEnv, "=", 2) + if split[0] == env { + info.ExtraEnvs = append(info.ExtraEnvs, escapeSystemdArg(containerEnv)) + } + } + } + } + startCommand = append(startCommand, remainingCmd...) startCommand = escapeSystemdArguments(startCommand) |