Skip to content

Commit 0fb1b47

Browse files
authored
Merge pull request #5053 from k0sproject/backport-5044-to-release-1.30
[Backport release-1.30] Use the correct template when installing as a SystemV service
2 parents 4c95dda + 91c1299 commit 0fb1b47

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
@@ -19,8 +19,6 @@ package install
1919
import (
2020
"errors"
2121
"fmt"
22-
"os"
23-
"strings"
2422

2523
"github.com/kardianos/service"
2624
"github.com/sirupsen/logrus"
@@ -88,11 +86,6 @@ func EnsureService(args []string, envVars []string, force bool) error {
8886
// fetch service type
8987
svcType := s.Platform()
9088
switch svcType {
91-
case "darwin-launchd":
92-
svcConfig.Option = map[string]interface{}{
93-
"EnvironmentMap": prepareEnvVars(envVars),
94-
"LaunchdConfig": launchdConfig,
95-
}
9689
case "linux-openrc":
9790
deps = []string{"need cgroups", "need net", "use dns", "after firewall"}
9891
svcConfig.Option = map[string]interface{}{
@@ -104,7 +97,7 @@ func EnsureService(args []string, envVars []string, force bool) error {
10497
}
10598
case "unix-systemv":
10699
svcConfig.Option = map[string]interface{}{
107-
"SystemdScript": sysvScript,
100+
"SysVScript": sysvScript,
108101
}
109102
case "linux-systemd":
110103
deps = []string{"After=network-online.target", "Wants=network-online.target"}
@@ -152,37 +145,6 @@ func UninstallService(role string) error {
152145
return s.Uninstall()
153146
}
154147

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

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

0 commit comments

Comments
 (0)