diff options
Diffstat (limited to 'pkg/systemd')
-rw-r--r-- | pkg/systemd/dbus.go | 2 | ||||
-rw-r--r-- | pkg/systemd/generate/common.go | 2 | ||||
-rw-r--r-- | pkg/systemd/generate/common_test.go | 1 | ||||
-rw-r--r-- | pkg/systemd/generate/containers.go | 22 | ||||
-rw-r--r-- | pkg/systemd/generate/containers_test.go | 67 | ||||
-rw-r--r-- | pkg/systemd/generate/pods.go | 16 | ||||
-rw-r--r-- | pkg/systemd/generate/pods_test.go | 49 |
7 files changed, 122 insertions, 37 deletions
diff --git a/pkg/systemd/dbus.go b/pkg/systemd/dbus.go index 60712bd6f..718082526 100644 --- a/pkg/systemd/dbus.go +++ b/pkg/systemd/dbus.go @@ -6,7 +6,7 @@ import ( "path/filepath" "strconv" - "github.com/containers/podman/v2/pkg/rootless" + "github.com/containers/podman/v3/pkg/rootless" "github.com/coreos/go-systemd/v22/dbus" godbus "github.com/godbus/dbus/v5" ) diff --git a/pkg/systemd/generate/common.go b/pkg/systemd/generate/common.go index e9902319c..bbd1a5f92 100644 --- a/pkg/systemd/generate/common.go +++ b/pkg/systemd/generate/common.go @@ -31,10 +31,12 @@ func validateRestartPolicy(restart string) error { } const headerTemplate = `# {{{{.ServiceName}}}}.service +{{{{- if (eq .GenerateNoHeader false) }}}} # autogenerated by Podman {{{{.PodmanVersion}}}} {{{{- if .TimeStamp}}}} # {{{{.TimeStamp}}}} {{{{- end}}}} +{{{{- end}}}} [Unit] Description=Podman {{{{.ServiceName}}}}.service diff --git a/pkg/systemd/generate/common_test.go b/pkg/systemd/generate/common_test.go index a0691d1ad..3787e461e 100644 --- a/pkg/systemd/generate/common_test.go +++ b/pkg/systemd/generate/common_test.go @@ -8,7 +8,6 @@ import ( ) func TestFilterPodFlags(t *testing.T) { - tests := []struct { input []string }{ diff --git a/pkg/systemd/generate/containers.go b/pkg/systemd/generate/containers.go index abe159812..92c6d8865 100644 --- a/pkg/systemd/generate/containers.go +++ b/pkg/systemd/generate/containers.go @@ -9,9 +9,9 @@ import ( "text/template" "time" - "github.com/containers/podman/v2/libpod" - "github.com/containers/podman/v2/pkg/domain/entities" - "github.com/containers/podman/v2/version" + "github.com/containers/podman/v3/libpod" + "github.com/containers/podman/v3/pkg/domain/entities" + "github.com/containers/podman/v3/version" "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/spf13/pflag" @@ -65,10 +65,11 @@ type containerInfo struct { ExecStop string // ExecStopPost of the unit. ExecStopPost string - + // Removes autogenerated by Podman and timestamp if set to true + GenerateNoHeader bool // If not nil, the container is part of the pod. We can use the // podInfo to extract the relevant data. - pod *podInfo + Pod *podInfo } const containerTemplate = headerTemplate + ` @@ -215,8 +216,8 @@ func executeContainerTemplate(info *containerInfo, options entities.GenerateSyst ) // If the container is in a pod, make sure that the // --pod-id-file is set correctly. - if info.pod != nil { - podFlags := []string{"--pod-id-file", info.pod.PodIDFile} + if info.Pod != nil { + podFlags := []string{"--pod-id-file", "{{{{.Pod.PodIDFile}}}}"} startCommand = append(startCommand, podFlags...) info.CreateCommand = filterPodFlags(info.CreateCommand) } @@ -292,10 +293,15 @@ func executeContainerTemplate(info *containerInfo, options entities.GenerateSyst if info.PodmanVersion == "" { info.PodmanVersion = version.Version.String() } + + if options.NoHeader { + info.GenerateNoHeader = true + info.GenerateTimestamp = false + } + if info.GenerateTimestamp { info.TimeStamp = fmt.Sprintf("%v", time.Now().Format(time.UnixDate)) } - // Sort the slices to assure a deterministic output. sort.Strings(info.BoundToServices) diff --git a/pkg/systemd/generate/containers_test.go b/pkg/systemd/generate/containers_test.go index be14e4c28..747c54699 100644 --- a/pkg/systemd/generate/containers_test.go +++ b/pkg/systemd/generate/containers_test.go @@ -3,7 +3,7 @@ package generate import ( "testing" - "github.com/containers/podman/v2/pkg/domain/entities" + "github.com/containers/podman/v3/pkg/domain/entities" "github.com/stretchr/testify/assert" ) @@ -37,9 +37,11 @@ func TestValidateRestartPolicyContainer(t *testing.T) { } func TestCreateContainerSystemdUnit(t *testing.T) { - goodID := `# container-639c53578af4d84b8800b4635fa4e680ee80fd67e0e6a2d4eea48d1e3230f401.service -# autogenerated by Podman CI - + serviceInfo := `# container-639c53578af4d84b8800b4635fa4e680ee80fd67e0e6a2d4eea48d1e3230f401.service +` + headerInfo := `# autogenerated by Podman CI +` + goodIDContent := ` [Unit] Description=Podman container-639c53578af4d84b8800b4635fa4e680ee80fd67e0e6a2d4eea48d1e3230f401.service Documentation=man:podman-generate-systemd(1) @@ -59,6 +61,8 @@ Type=forking [Install] WantedBy=multi-user.target default.target ` + goodID := serviceInfo + headerInfo + goodIDContent + goodIDNoHeaderInfo := serviceInfo + goodIDContent goodName := `# container-foobar.service # autogenerated by Podman CI @@ -170,7 +174,7 @@ Environment=PODMAN_SYSTEMD_UNIT=%n Restart=always TimeoutStopSec=70 ExecStartPre=/bin/rm -f %t/jadda-jadda.pid %t/jadda-jadda.ctr-id -ExecStart=/usr/bin/podman run --conmon-pidfile %t/jadda-jadda.pid --cidfile %t/jadda-jadda.ctr-id --cgroups=no-conmon --pod-id-file /tmp/pod-foobar.pod-id-file --replace -d --name jadda-jadda --hostname hello-world awesome-image:latest command arg1 ... argN +ExecStart=/usr/bin/podman run --conmon-pidfile %t/jadda-jadda.pid --cidfile %t/jadda-jadda.ctr-id --cgroups=no-conmon --pod-id-file %t/pod-foobar.pod-id-file --replace -d --name jadda-jadda --hostname hello-world awesome-image:latest command arg1 ... argN ExecStop=/usr/bin/podman stop --ignore --cidfile %t/jadda-jadda.ctr-id -t 10 ExecStopPost=/usr/bin/podman rm --ignore -f --cidfile %t/jadda-jadda.ctr-id PIDFile=%t/jadda-jadda.pid @@ -377,11 +381,12 @@ Type=forking WantedBy=multi-user.target default.target ` tests := []struct { - name string - info containerInfo - want string - new bool - wantErr bool + name string + info containerInfo + want string + new bool + noHeader bool + wantErr bool }{ {"good with id", @@ -398,6 +403,23 @@ WantedBy=multi-user.target default.target goodID, false, false, + false, + }, + {"good with noHeader", + containerInfo{ + Executable: "/usr/bin/podman", + ServiceName: "container-639c53578af4d84b8800b4635fa4e680ee80fd67e0e6a2d4eea48d1e3230f401", + ContainerNameOrID: "639c53578af4d84b8800b4635fa4e680ee80fd67e0e6a2d4eea48d1e3230f401", + RestartPolicy: "always", + PIDFile: "/run/containers/storage/overlay-containers/639c53578af4d84b8800b4635fa4e680ee80fd67e0e6a2d4eea48d1e3230f401/userdata/conmon.pid", + StopTimeout: 22, + PodmanVersion: "CI", + EnvVariable: EnvVariable, + }, + goodIDNoHeaderInfo, + false, + true, + false, }, {"good with name", containerInfo{ @@ -413,6 +435,7 @@ WantedBy=multi-user.target default.target goodName, false, false, + false, }, {"good with name and bound to", containerInfo{ @@ -429,6 +452,7 @@ WantedBy=multi-user.target default.target goodNameBoundTo, false, false, + false, }, {"bad restart policy", containerInfo{ @@ -442,6 +466,7 @@ WantedBy=multi-user.target default.target }, "", false, + false, true, }, {"good with name and generic", @@ -459,6 +484,7 @@ WantedBy=multi-user.target default.target goodWithNameAndGeneric, true, false, + false, }, {"good with explicit short detach param", containerInfo{ @@ -475,6 +501,7 @@ WantedBy=multi-user.target default.target goodWithExplicitShortDetachParam, true, false, + false, }, {"good with explicit short detach param and podInfo", containerInfo{ @@ -487,13 +514,14 @@ WantedBy=multi-user.target default.target PodmanVersion: "CI", CreateCommand: []string{"I'll get stripped", "run", "-d", "--name", "jadda-jadda", "--hostname", "hello-world", "awesome-image:latest", "command", "arg1", "...", "argN"}, EnvVariable: EnvVariable, - pod: &podInfo{ - PodIDFile: "/tmp/pod-foobar.pod-id-file", + Pod: &podInfo{ + PodIDFile: "%t/pod-foobar.pod-id-file", }, }, goodNameNewWithPodFile, true, false, + false, }, {"good with explicit full detach param", containerInfo{ @@ -510,6 +538,7 @@ WantedBy=multi-user.target default.target goodNameNewDetach, true, false, + false, }, {"good with id and no param", containerInfo{ @@ -526,6 +555,7 @@ WantedBy=multi-user.target default.target goodIDNew, true, false, + false, }, {"good with explicit detach=true param", containerInfo{ @@ -542,6 +572,7 @@ WantedBy=multi-user.target default.target genGoodNewDetach("--detach=true"), true, false, + false, }, {"good with explicit detach=false param", containerInfo{ @@ -558,6 +589,7 @@ WantedBy=multi-user.target default.target genGoodNewDetach("-d"), true, false, + false, }, {"good with explicit detach=false param", containerInfo{ @@ -574,6 +606,7 @@ WantedBy=multi-user.target default.target goodNameNewDetachFalseWithCmd, true, false, + false, }, {"good with multiple detach=false params", containerInfo{ @@ -590,6 +623,7 @@ WantedBy=multi-user.target default.target goodNameNewDetachFalseWithCmd, true, false, + false, }, {"good with multiple shorthand params detach first", containerInfo{ @@ -606,6 +640,7 @@ WantedBy=multi-user.target default.target genGoodNewDetach("-dti"), true, false, + false, }, {"good with multiple shorthand params detach last", containerInfo{ @@ -622,6 +657,7 @@ WantedBy=multi-user.target default.target genGoodNewDetach("-tid"), true, false, + false, }, {"good with root flags", containerInfo{ @@ -638,6 +674,7 @@ WantedBy=multi-user.target default.target goodNewRootFlags, true, false, + false, }, {"good with container create", containerInfo{ @@ -654,6 +691,7 @@ WantedBy=multi-user.target default.target goodContainerCreate, true, false, + false, }, {"good with journald log tag (see #9034)", containerInfo{ @@ -670,6 +708,7 @@ WantedBy=multi-user.target default.target goodNewWithJournaldTag, true, false, + false, }, {"good with special chars", containerInfo{ @@ -686,13 +725,15 @@ WantedBy=multi-user.target default.target goodNewWithSpecialChars, true, false, + false, }, } for _, tt := range tests { test := tt t.Run(tt.name, func(t *testing.T) { opts := entities.GenerateSystemdOptions{ - New: test.new, + New: test.new, + NoHeader: test.noHeader, } got, err := executeContainerTemplate(&test.info, opts) if (err != nil) != test.wantErr { diff --git a/pkg/systemd/generate/pods.go b/pkg/systemd/generate/pods.go index d6ede19af..8c0401278 100644 --- a/pkg/systemd/generate/pods.go +++ b/pkg/systemd/generate/pods.go @@ -9,9 +9,9 @@ import ( "text/template" "time" - "github.com/containers/podman/v2/libpod" - "github.com/containers/podman/v2/pkg/domain/entities" - "github.com/containers/podman/v2/version" + "github.com/containers/podman/v3/libpod" + "github.com/containers/podman/v3/pkg/domain/entities" + "github.com/containers/podman/v3/version" "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/spf13/pflag" @@ -70,6 +70,8 @@ type podInfo struct { ExecStop string // ExecStopPost of the unit. ExecStopPost string + // Removes autogenerated by Podman and timestamp if set to true + GenerateNoHeader bool } const podTemplate = headerTemplate + `Requires={{{{- range $index, $value := .RequiredServices -}}}}{{{{if $index}}}} {{{{end}}}}{{{{ $value }}}}.service{{{{end}}}} @@ -162,7 +164,7 @@ func PodUnits(pod *libpod.Pod, options entities.GenerateSystemdOptions) (map[str } units[podInfo.ServiceName] = out for _, info := range containerInfos { - info.pod = podInfo + info.Pod = podInfo out, err := executeContainerTemplate(info, options) if err != nil { return nil, err @@ -319,6 +321,12 @@ func executePodTemplate(info *podInfo, options entities.GenerateSystemdOptions) if info.PodmanVersion == "" { info.PodmanVersion = version.Version.String() } + + if options.NoHeader { + info.GenerateNoHeader = true + info.GenerateTimestamp = false + } + if info.GenerateTimestamp { info.TimeStamp = fmt.Sprintf("%v", time.Now().Format(time.UnixDate)) } diff --git a/pkg/systemd/generate/pods_test.go b/pkg/systemd/generate/pods_test.go index 2b430226b..50c8d4556 100644 --- a/pkg/systemd/generate/pods_test.go +++ b/pkg/systemd/generate/pods_test.go @@ -3,7 +3,7 @@ package generate import ( "testing" - "github.com/containers/podman/v2/pkg/domain/entities" + "github.com/containers/podman/v3/pkg/domain/entities" "github.com/stretchr/testify/assert" ) @@ -37,9 +37,11 @@ func TestValidateRestartPolicyPod(t *testing.T) { } func TestCreatePodSystemdUnit(t *testing.T) { - podGood := `# pod-123abc.service -# autogenerated by Podman CI - + serviceInfo := `# pod-123abc.service +` + headerInfo := `# autogenerated by Podman CI +` + podContent := ` [Unit] Description=Podman pod-123abc.service Documentation=man:podman-generate-systemd(1) @@ -61,6 +63,8 @@ Type=forking [Install] WantedBy=multi-user.target default.target ` + podGood := serviceInfo + headerInfo + podContent + podGoodNoHeaderInfo := serviceInfo + podContent podGoodNamedNew := `# pod-123abc.service # autogenerated by Podman CI @@ -171,11 +175,12 @@ WantedBy=multi-user.target default.target ` tests := []struct { - name string - info podInfo - want string - new bool - wantErr bool + name string + info podInfo + want string + new bool + noHeader bool + wantErr bool }{ {"pod", podInfo{ @@ -192,6 +197,24 @@ WantedBy=multi-user.target default.target podGood, false, false, + false, + }, + {"pod noHeader", + podInfo{ + Executable: "/usr/bin/podman", + ServiceName: "pod-123abc", + InfraNameOrID: "jadda-jadda-infra", + RestartPolicy: "always", + PIDFile: "/run/containers/storage/overlay-containers/639c53578af4d84b8800b4635fa4e680ee80fd67e0e6a2d4eea48d1e3230f401/userdata/conmon.pid", + StopTimeout: 42, + PodmanVersion: "CI", + RequiredServices: []string{"container-1", "container-2"}, + CreateCommand: []string{"podman", "pod", "create", "--name", "foo", "bar=arg with space"}, + }, + podGoodNoHeaderInfo, + false, + true, + false, }, {"pod with root args", podInfo{ @@ -208,6 +231,7 @@ WantedBy=multi-user.target default.target podGood, false, false, + false, }, {"pod --new", podInfo{ @@ -224,6 +248,7 @@ WantedBy=multi-user.target default.target podGoodNamedNew, true, false, + false, }, {"pod --new with root args", podInfo{ @@ -240,6 +265,7 @@ WantedBy=multi-user.target default.target podGoodNamedNewWithRootArgs, true, false, + false, }, {"pod --new with --replace=false", podInfo{ @@ -256,6 +282,7 @@ WantedBy=multi-user.target default.target podGoodNamedNewWithReplaceFalse, true, false, + false, }, {"pod --new with double curly braces", podInfo{ @@ -272,6 +299,7 @@ WantedBy=multi-user.target default.target podNewLabelWithCurlyBraces, true, false, + false, }, } @@ -279,7 +307,8 @@ WantedBy=multi-user.target default.target test := tt t.Run(tt.name, func(t *testing.T) { opts := entities.GenerateSystemdOptions{ - New: test.new, + New: test.new, + NoHeader: test.noHeader, } got, err := executePodTemplate(&test.info, opts) if (err != nil) != test.wantErr { |