Skip to content

Commit 9b07834

Browse files
authored
Merge pull request #5051 from k0sproject/backport-5044-to-release-1.28
[Backport release-1.28] Use the correct template when installing as a SystemV service
2 parents 72b856f + 506325e commit 9b07834

File tree

5 files changed

+59
-176
lines changed

5 files changed

+59
-176
lines changed

pkg/install/darwin_launchd.go

-64
This file was deleted.

pkg/install/linux_openrc.go

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ const openRCScript = `#!/sbin/openrc-run
2020
{{- if .Option.Environment}}{{range .Option.Environment}}
2121
export {{.}}{{end}}{{- end}}
2222
supervisor=supervise-daemon
23-
name="{{.DisplayName}}"
2423
description="{{.Description}}"
2524
command={{.Path|cmdEscape}}
2625
{{- if .Arguments }}

pkg/install/linux_systemd.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Copyright 2024 k0s authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package install
18+
19+
// Upstream kardianos/service does not support all the options we want to set to the systemd unit, hence we override the template
20+
// Currently mostly for KillMode=process so we get systemd to only send the sigterm to the main process
21+
const systemdScript = `[Unit]
22+
Description={{.Description}}
23+
Documentation=https://docs.k0sproject.io
24+
ConditionFileIsExecutable={{.Path|cmdEscape}}
25+
{{range $i, $dep := .Dependencies}}
26+
{{$dep}} {{end}}
27+
28+
[Service]
29+
StartLimitInterval=5
30+
StartLimitBurst=10
31+
ExecStart={{.Path|cmdEscape}}{{range .Arguments}} {{.|cmdEscape}}{{end}}
32+
Environment="{{- range $key, $value := .EnvVars}}{{$key}}={{$value}} {{- end}}"
33+
34+
RestartSec=10
35+
Delegate=yes
36+
KillMode=process
37+
LimitCORE=infinity
38+
TasksMax=infinity
39+
TimeoutStartSec=0
40+
41+
{{- if .ChRoot}}RootDirectory={{.ChRoot|cmd}}{{- end}}
42+
43+
{{- if .WorkingDirectory}}WorkingDirectory={{.WorkingDirectory|cmdEscape}}{{- end}}
44+
{{- if .UserName}}User={{.UserName}}{{end}}
45+
{{- if .ReloadSignal}}ExecReload=/bin/kill -{{.ReloadSignal}} "$MAINPID"{{- end}}
46+
{{- if .PIDFile}}PIDFile={{.PIDFile|cmd}}{{- end}}
47+
{{- if and .LogOutput .HasOutputFileSupport -}}
48+
StandardOutput=file:/var/log/{{.Name}}.out
49+
StandardError=file:/var/log/{{.Name}}.err
50+
{{- end}}
51+
52+
{{- if .SuccessExitStatus}}SuccessExitStatus={{.SuccessExitStatus}}{{- end}}
53+
{{ if gt .LimitNOFILE -1 }}LimitNOFILE={{.LimitNOFILE}}{{- end}}
54+
{{ if .Restart}}Restart={{.Restart}}{{- end}}
55+
56+
[Install]
57+
WantedBy=multi-user.target
58+
`

pkg/install/process.go

-17
This file was deleted.

pkg/install/service.go

+1-94
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ package install
1818

1919
import (
2020
"fmt"
21-
"os"
22-
"strings"
2321

2422
"github.com/kardianos/service"
2523
"github.com/sirupsen/logrus"
@@ -87,11 +85,6 @@ func EnsureService(args []string, envVars []string, force bool) error {
8785
// fetch service type
8886
svcType := s.Platform()
8987
switch svcType {
90-
case "darwin-launchd":
91-
svcConfig.Option = map[string]interface{}{
92-
"EnvironmentMap": prepareEnvVars(envVars),
93-
"LaunchdConfig": launchdConfig,
94-
}
9588
case "linux-openrc":
9689
deps = []string{"need cgroups", "need net", "use dns", "after firewall"}
9790
svcConfig.Option = map[string]interface{}{
@@ -103,7 +96,7 @@ func EnsureService(args []string, envVars []string, force bool) error {
10396
}
10497
case "unix-systemv":
10598
svcConfig.Option = map[string]interface{}{
106-
"SystemdScript": sysvScript,
99+
"SysVScript": sysvScript,
107100
}
108101
case "linux-systemd":
109102
deps = []string{"After=network-online.target", "Wants=network-online.target"}
@@ -151,37 +144,6 @@ func UninstallService(role string) error {
151144
return s.Uninstall()
152145
}
153146

154-
// GetSysInit returns the sys init platform name, and the stub file path for a system
155-
func GetSysInit(role string) (sysInitPlatform string, stubFile string, err error) {
156-
if role == "controller+worker" {
157-
role = "controller"
158-
}
159-
if sysInitPlatform, err = getSysInitPlatform(); err != nil {
160-
return sysInitPlatform, stubFile, err
161-
}
162-
if sysInitPlatform == "linux-systemd" {
163-
stubFile = fmt.Sprintf("/etc/systemd/system/k0s%s.service", role)
164-
if _, err := os.Stat(stubFile); err != nil {
165-
stubFile = ""
166-
}
167-
} else if sysInitPlatform == "linux-openrc" {
168-
stubFile = fmt.Sprintf("/etc/init.d/k0s%s", role)
169-
if _, err := os.Stat(stubFile); err != nil {
170-
stubFile = ""
171-
}
172-
}
173-
return sysInitPlatform, stubFile, err
174-
}
175-
176-
func getSysInitPlatform() (string, error) {
177-
prg := &Program{}
178-
s, err := service.New(prg, &service.Config{Name: "132"})
179-
if err != nil {
180-
return "", err
181-
}
182-
return s.Platform(), nil
183-
}
184-
185147
func GetServiceConfig(role string) *service.Config {
186148
var k0sDisplayName string
187149

@@ -195,58 +157,3 @@ func GetServiceConfig(role string) *service.Config {
195157
Description: k0sDescription,
196158
}
197159
}
198-
199-
func prepareEnvVars(envVars []string) map[string]string {
200-
result := make(map[string]string)
201-
for _, envVar := range envVars {
202-
parts := strings.SplitN(envVar, "=", 1)
203-
if len(parts) != 2 {
204-
continue
205-
}
206-
207-
result[parts[0]] = parts[1]
208-
}
209-
return result
210-
}
211-
212-
// Upstream kardianos/service does not support all the options we want to set to the systemd unit, hence we override the template
213-
// Currently mostly for KillMode=process so we get systemd to only send the sigterm to the main process
214-
const systemdScript = `[Unit]
215-
Description={{.Description}}
216-
Documentation=https://docs.k0sproject.io
217-
ConditionFileIsExecutable={{.Path|cmdEscape}}
218-
{{range $i, $dep := .Dependencies}}
219-
{{$dep}} {{end}}
220-
221-
[Service]
222-
StartLimitInterval=5
223-
StartLimitBurst=10
224-
ExecStart={{.Path|cmdEscape}}{{range .Arguments}} {{.|cmdEscape}}{{end}}
225-
{{- if .Option.Environment}}{{range .Option.Environment}}
226-
Environment="{{.}}"{{end}}{{- end}}
227-
228-
RestartSec=10
229-
Delegate=yes
230-
KillMode=process
231-
LimitCORE=infinity
232-
TasksMax=infinity
233-
TimeoutStartSec=0
234-
235-
{{- if .ChRoot}}RootDirectory={{.ChRoot|cmd}}{{- end}}
236-
237-
{{- if .WorkingDirectory}}WorkingDirectory={{.WorkingDirectory|cmdEscape}}{{- end}}
238-
{{- if .UserName}}User={{.UserName}}{{end}}
239-
{{- if .ReloadSignal}}ExecReload=/bin/kill -{{.ReloadSignal}} "$MAINPID"{{- end}}
240-
{{- if .PIDFile}}PIDFile={{.PIDFile|cmd}}{{- end}}
241-
{{- if and .LogOutput .HasOutputFileSupport -}}
242-
StandardOutput=file:/var/log/{{.Name}}.out
243-
StandardError=file:/var/log/{{.Name}}.err
244-
{{- end}}
245-
246-
{{- if .SuccessExitStatus}}SuccessExitStatus={{.SuccessExitStatus}}{{- end}}
247-
{{ if gt .LimitNOFILE -1 }}LimitNOFILE={{.LimitNOFILE}}{{- end}}
248-
{{ if .Restart}}Restart={{.Restart}}{{- end}}
249-
250-
[Install]
251-
WantedBy=multi-user.target
252-
`

0 commit comments

Comments
 (0)