Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport release-1.28] Use the correct template when installing as a SystemV service #5051

Merged
merged 7 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 0 additions & 64 deletions pkg/install/darwin_launchd.go

This file was deleted.

1 change: 0 additions & 1 deletion pkg/install/linux_openrc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const openRCScript = `#!/sbin/openrc-run
{{- if .Option.Environment}}{{range .Option.Environment}}
export {{.}}{{end}}{{- end}}
supervisor=supervise-daemon
name="{{.DisplayName}}"
description="{{.Description}}"
command={{.Path|cmdEscape}}
{{- if .Arguments }}
Expand Down
58 changes: 58 additions & 0 deletions pkg/install/linux_systemd.go
Original file line number Diff line number Diff line change
@@ -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
`
17 changes: 0 additions & 17 deletions pkg/install/process.go

This file was deleted.

95 changes: 1 addition & 94 deletions pkg/install/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package install

import (
"fmt"
"os"
"strings"

"github.com/kardianos/service"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -87,11 +85,6 @@ func EnsureService(args []string, envVars []string, force bool) error {
// fetch service type
svcType := s.Platform()
switch svcType {
case "darwin-launchd":
svcConfig.Option = map[string]interface{}{
"EnvironmentMap": prepareEnvVars(envVars),
"LaunchdConfig": launchdConfig,
}
case "linux-openrc":
deps = []string{"need cgroups", "need net", "use dns", "after firewall"}
svcConfig.Option = map[string]interface{}{
Expand All @@ -103,7 +96,7 @@ func EnsureService(args []string, envVars []string, force bool) error {
}
case "unix-systemv":
svcConfig.Option = map[string]interface{}{
"SystemdScript": sysvScript,
"SysVScript": sysvScript,
}
case "linux-systemd":
deps = []string{"After=network-online.target", "Wants=network-online.target"}
Expand Down Expand Up @@ -151,37 +144,6 @@ func UninstallService(role string) error {
return s.Uninstall()
}

// GetSysInit returns the sys init platform name, and the stub file path for a system
func GetSysInit(role string) (sysInitPlatform string, stubFile string, err error) {
if role == "controller+worker" {
role = "controller"
}
if sysInitPlatform, err = getSysInitPlatform(); err != nil {
return sysInitPlatform, stubFile, err
}
if sysInitPlatform == "linux-systemd" {
stubFile = fmt.Sprintf("/etc/systemd/system/k0s%s.service", role)
if _, err := os.Stat(stubFile); err != nil {
stubFile = ""
}
} else if sysInitPlatform == "linux-openrc" {
stubFile = fmt.Sprintf("/etc/init.d/k0s%s", role)
if _, err := os.Stat(stubFile); err != nil {
stubFile = ""
}
}
return sysInitPlatform, stubFile, err
}

func getSysInitPlatform() (string, error) {
prg := &Program{}
s, err := service.New(prg, &service.Config{Name: "132"})
if err != nil {
return "", err
}
return s.Platform(), nil
}

func GetServiceConfig(role string) *service.Config {
var k0sDisplayName string

Expand All @@ -195,58 +157,3 @@ func GetServiceConfig(role string) *service.Config {
Description: k0sDescription,
}
}

func prepareEnvVars(envVars []string) map[string]string {
result := make(map[string]string)
for _, envVar := range envVars {
parts := strings.SplitN(envVar, "=", 1)
if len(parts) != 2 {
continue
}

result[parts[0]] = parts[1]
}
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
`
Loading