Skip to content

Commit

Permalink
Merge pull request containers#9445 from jmguzik/no-header-info-for-sy…
Browse files Browse the repository at this point in the history
…stemd-generation

No header info for systemd generation
  • Loading branch information
openshift-merge-robot authored Feb 22, 2021
2 parents f8ff172 + d2f3098 commit a6e7d19
Show file tree
Hide file tree
Showing 14 changed files with 175 additions and 21 deletions.
1 change: 1 addition & 0 deletions cmd/podman/generate/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func init() {
flags.UintVarP(&systemdTimeout, timeFlagName, "t", containerConfig.Engine.StopTimeout, "Stop timeout override")
_ = systemdCmd.RegisterFlagCompletionFunc(timeFlagName, completion.AutocompleteNone)
flags.BoolVarP(&systemdOptions.New, "new", "", false, "Create a new container instead of starting an existing one")
flags.BoolVarP(&systemdOptions.NoHeader, "no-header", "", false, "Skip header generation")

containerPrefixFlagName := "container-prefix"
flags.StringVar(&systemdOptions.ContainerPrefix, containerPrefixFlagName, "container", "Systemd unit name prefix for containers")
Expand Down
4 changes: 4 additions & 0 deletions docs/source/markdown/podman-generate-systemd.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ Use the name of the container for the start, stop, and description in the unit f

Using this flag will yield unit files that do not expect containers and pods to exist. Instead, new containers and pods are created based on their configuration files. The unit files are created best effort and may need to be further edited; please review the generated files carefully before using them in production.

#### **--no-header**

Do not generate the header including meta data such as the Podman version and the timestamp.

#### **--time**, **-t**=*value*

Override the default stop timeout for the container with the given value.
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/handlers/libpod/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func GenerateSystemd(w http.ResponseWriter, r *http.Request) {
query := struct {
Name bool `schema:"useName"`
New bool `schema:"new"`
NoHeader bool `schema:"noHeader"`
RestartPolicy string `schema:"restartPolicy"`
StopTimeout uint `schema:"stopTimeout"`
ContainerPrefix string `schema:"containerPrefix"`
Expand All @@ -41,6 +42,7 @@ func GenerateSystemd(w http.ResponseWriter, r *http.Request) {
options := entities.GenerateSystemdOptions{
Name: query.Name,
New: query.New,
NoHeader: query.NoHeader,
RestartPolicy: query.RestartPolicy,
StopTimeout: &query.StopTimeout,
ContainerPrefix: query.ContainerPrefix,
Expand Down
5 changes: 5 additions & 0 deletions pkg/api/server/register_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ func (s *APIServer) registerGenerateHandlers(r *mux.Router) error {
// default: false
// description: Create a new container instead of starting an existing one.
// - in: query
// name: noHeader
// type: boolean
// default: false
// description: Do not generate the header including the Podman version and the timestamp.
// - in: query
// name: time
// type: integer
// default: 10
Expand Down
2 changes: 2 additions & 0 deletions pkg/bindings/generate/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type SystemdOptions struct {
UseName *bool
// New - create a new container instead of starting a new one.
New *bool
// NoHeader - Removes autogenerated by Podman and timestamp if set to true
NoHeader *bool
// RestartPolicy - systemd restart policy.
RestartPolicy *string
// StopTimeout - time when stopping the container.
Expand Down
16 changes: 16 additions & 0 deletions pkg/bindings/generate/types_systemd_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ func (o *SystemdOptions) GetNew() bool {
return *o.New
}

// WithNoHeader
func (o *SystemdOptions) WithNoHeader(value bool) *SystemdOptions {
v := &value
o.NoHeader = v
return o
}

// GetNoHeader
func (o *SystemdOptions) GetNoHeader() bool {
var noHeader bool
if o.NoHeader == nil {
return noHeader
}
return *o.NoHeader
}

// WithRestartPolicy
func (o *SystemdOptions) WithRestartPolicy(value string) *SystemdOptions {
v := &value
Expand Down
2 changes: 2 additions & 0 deletions pkg/domain/entities/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type GenerateSystemdOptions struct {
PodPrefix string
// Separator - systemd unit name separator between name/id and prefix
Separator string
// NoHeader - skip header generation
NoHeader bool
}

// GenerateSystemdReport
Expand Down
2 changes: 1 addition & 1 deletion pkg/domain/infra/tunnel/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func (ic *ContainerEngine) GenerateSystemd(ctx context.Context, nameOrID string, opts entities.GenerateSystemdOptions) (*entities.GenerateSystemdReport, error) {
options := new(generate.SystemdOptions).WithUseName(opts.Name).WithContainerPrefix(opts.ContainerPrefix).WithNew(opts.New)
options := new(generate.SystemdOptions).WithUseName(opts.Name).WithContainerPrefix(opts.ContainerPrefix).WithNew(opts.New).WithNoHeader(opts.NoHeader)
options.WithPodPrefix(opts.PodPrefix).WithRestartPolicy(opts.RestartPolicy).WithSeparator(opts.Separator)
if to := opts.StopTimeout; to != nil {
options.WithStopTimeout(*opts.StopTimeout)
Expand Down
2 changes: 2 additions & 0 deletions pkg/systemd/generate/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions pkg/systemd/generate/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ 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
Expand Down Expand Up @@ -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)

Expand Down
59 changes: 50 additions & 9 deletions pkg/systemd/generate/containers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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{
Expand All @@ -413,6 +435,7 @@ WantedBy=multi-user.target default.target
goodName,
false,
false,
false,
},
{"good with name and bound to",
containerInfo{
Expand All @@ -429,6 +452,7 @@ WantedBy=multi-user.target default.target
goodNameBoundTo,
false,
false,
false,
},
{"bad restart policy",
containerInfo{
Expand All @@ -442,6 +466,7 @@ WantedBy=multi-user.target default.target
},
"",
false,
false,
true,
},
{"good with name and generic",
Expand All @@ -459,6 +484,7 @@ WantedBy=multi-user.target default.target
goodWithNameAndGeneric,
true,
false,
false,
},
{"good with explicit short detach param",
containerInfo{
Expand All @@ -475,6 +501,7 @@ WantedBy=multi-user.target default.target
goodWithExplicitShortDetachParam,
true,
false,
false,
},
{"good with explicit short detach param and podInfo",
containerInfo{
Expand All @@ -494,6 +521,7 @@ WantedBy=multi-user.target default.target
goodNameNewWithPodFile,
true,
false,
false,
},
{"good with explicit full detach param",
containerInfo{
Expand All @@ -510,6 +538,7 @@ WantedBy=multi-user.target default.target
goodNameNewDetach,
true,
false,
false,
},
{"good with id and no param",
containerInfo{
Expand All @@ -526,6 +555,7 @@ WantedBy=multi-user.target default.target
goodIDNew,
true,
false,
false,
},
{"good with explicit detach=true param",
containerInfo{
Expand All @@ -542,6 +572,7 @@ WantedBy=multi-user.target default.target
genGoodNewDetach("--detach=true"),
true,
false,
false,
},
{"good with explicit detach=false param",
containerInfo{
Expand All @@ -558,6 +589,7 @@ WantedBy=multi-user.target default.target
genGoodNewDetach("-d"),
true,
false,
false,
},
{"good with explicit detach=false param",
containerInfo{
Expand All @@ -574,6 +606,7 @@ WantedBy=multi-user.target default.target
goodNameNewDetachFalseWithCmd,
true,
false,
false,
},
{"good with multiple detach=false params",
containerInfo{
Expand All @@ -590,6 +623,7 @@ WantedBy=multi-user.target default.target
goodNameNewDetachFalseWithCmd,
true,
false,
false,
},
{"good with multiple shorthand params detach first",
containerInfo{
Expand All @@ -606,6 +640,7 @@ WantedBy=multi-user.target default.target
genGoodNewDetach("-dti"),
true,
false,
false,
},
{"good with multiple shorthand params detach last",
containerInfo{
Expand All @@ -622,6 +657,7 @@ WantedBy=multi-user.target default.target
genGoodNewDetach("-tid"),
true,
false,
false,
},
{"good with root flags",
containerInfo{
Expand All @@ -638,6 +674,7 @@ WantedBy=multi-user.target default.target
goodNewRootFlags,
true,
false,
false,
},
{"good with container create",
containerInfo{
Expand All @@ -654,6 +691,7 @@ WantedBy=multi-user.target default.target
goodContainerCreate,
true,
false,
false,
},
{"good with journald log tag (see #9034)",
containerInfo{
Expand All @@ -670,6 +708,7 @@ WantedBy=multi-user.target default.target
goodNewWithJournaldTag,
true,
false,
false,
},
{"good with special chars",
containerInfo{
Expand All @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions pkg/systemd/generate/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}}}
Expand Down Expand Up @@ -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))
}
Expand Down
Loading

0 comments on commit a6e7d19

Please sign in to comment.