From ae615adaa006ea65eafb57e699109403f40f480e Mon Sep 17 00:00:00 2001 From: Kimmo Lehto Date: Thu, 19 Sep 2024 11:10:39 +0300 Subject: [PATCH] Extract systemd script template to pkg/install/linux_systemd.go Signed-off-by: Kimmo Lehto --- pkg/install/linux_systemd.go | 58 ++++++++++++++++++++++++++++++++++++ pkg/install/service.go | 42 -------------------------- 2 files changed, 58 insertions(+), 42 deletions(-) create mode 100644 pkg/install/linux_systemd.go diff --git a/pkg/install/linux_systemd.go b/pkg/install/linux_systemd.go new file mode 100644 index 000000000000..b546e104cac8 --- /dev/null +++ b/pkg/install/linux_systemd.go @@ -0,0 +1,58 @@ +/* +Copyright 2024 k0s authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package install + +// Upstream kardianos/service does not support all the options we want to set to the systemd unit, hence we override the template +// Currently mostly for KillMode=process so we get systemd to only send the sigterm to the main process +const systemdScript = `[Unit] +Description={{.Description}} +Documentation=https://docs.k0sproject.io +ConditionFileIsExecutable={{.Path|cmdEscape}} +{{range $i, $dep := .Dependencies}} +{{$dep}} {{end}} + +[Service] +StartLimitInterval=5 +StartLimitBurst=10 +ExecStart={{.Path|cmdEscape}}{{range .Arguments}} {{.|cmdEscape}}{{end}} +Environment="{{- range $key, $value := .EnvVars}}{{$key}}={{$value}} {{- end}}" + +RestartSec=10 +Delegate=yes +KillMode=process +LimitCORE=infinity +TasksMax=infinity +TimeoutStartSec=0 + +{{- if .ChRoot}}RootDirectory={{.ChRoot|cmd}}{{- end}} + +{{- if .WorkingDirectory}}WorkingDirectory={{.WorkingDirectory|cmdEscape}}{{- end}} +{{- if .UserName}}User={{.UserName}}{{end}} +{{- if .ReloadSignal}}ExecReload=/bin/kill -{{.ReloadSignal}} "$MAINPID"{{- end}} +{{- if .PIDFile}}PIDFile={{.PIDFile|cmd}}{{- end}} +{{- if and .LogOutput .HasOutputFileSupport -}} +StandardOutput=file:/var/log/{{.Name}}.out +StandardError=file:/var/log/{{.Name}}.err +{{- end}} + +{{- if .SuccessExitStatus}}SuccessExitStatus={{.SuccessExitStatus}}{{- end}} +{{ if gt .LimitNOFILE -1 }}LimitNOFILE={{.LimitNOFILE}}{{- end}} +{{ if .Restart}}Restart={{.Restart}}{{- end}} + +[Install] +WantedBy=multi-user.target +` diff --git a/pkg/install/service.go b/pkg/install/service.go index f9d35a324743..8e25b1b50589 100644 --- a/pkg/install/service.go +++ b/pkg/install/service.go @@ -172,45 +172,3 @@ func prepareEnvVars(envVars []string) map[string]string { } return result } - -// Upstream kardianos/service does not support all the options we want to set to the systemd unit, hence we override the template -// Currently mostly for KillMode=process so we get systemd to only send the sigterm to the main process -const systemdScript = `[Unit] -Description={{.Description}} -Documentation=https://docs.k0sproject.io -ConditionFileIsExecutable={{.Path|cmdEscape}} -{{range $i, $dep := .Dependencies}} -{{$dep}} {{end}} - -[Service] -StartLimitInterval=5 -StartLimitBurst=10 -ExecStart={{.Path|cmdEscape}}{{range .Arguments}} {{.|cmdEscape}}{{end}} -{{- if .Option.Environment}}{{range .Option.Environment}} -Environment="{{.}}"{{end}}{{- end}} - -RestartSec=10 -Delegate=yes -KillMode=process -LimitCORE=infinity -TasksMax=infinity -TimeoutStartSec=0 - -{{- if .ChRoot}}RootDirectory={{.ChRoot|cmd}}{{- end}} - -{{- if .WorkingDirectory}}WorkingDirectory={{.WorkingDirectory|cmdEscape}}{{- end}} -{{- if .UserName}}User={{.UserName}}{{end}} -{{- if .ReloadSignal}}ExecReload=/bin/kill -{{.ReloadSignal}} "$MAINPID"{{- end}} -{{- if .PIDFile}}PIDFile={{.PIDFile|cmd}}{{- end}} -{{- if and .LogOutput .HasOutputFileSupport -}} -StandardOutput=file:/var/log/{{.Name}}.out -StandardError=file:/var/log/{{.Name}}.err -{{- end}} - -{{- if .SuccessExitStatus}}SuccessExitStatus={{.SuccessExitStatus}}{{- end}} -{{ if gt .LimitNOFILE -1 }}LimitNOFILE={{.LimitNOFILE}}{{- end}} -{{ if .Restart}}Restart={{.Restart}}{{- end}} - -[Install] -WantedBy=multi-user.target -`