diff --git a/pkg/install/darwin_launchd.go b/pkg/install/darwin_launchd.go deleted file mode 100644 index 828073a6d642..000000000000 --- a/pkg/install/darwin_launchd.go +++ /dev/null @@ -1,64 +0,0 @@ -/* -Copyright 2022 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 - -const launchdConfig = ` - - - - {{- if .Option.Environment}} - EnvironmentVariables - - {{range $k, $v := .Option.EnvironmentMap -}} - {{html $k}} - {{html $v}} - {{end -}} - {{end -}} - - Label - {{html .Name}} - ProgramArguments - - {{html .Path}} - {{range .Config.Arguments}} - {{html .}} - {{end}} - - {{if .UserName}}UserName - {{html .UserName}}{{end}} - {{if .ChRoot}}RootDirectory - {{html .ChRoot}}{{end}} - {{if .WorkingDirectory}}WorkingDirectory - {{html .WorkingDirectory}}{{end}} - SessionCreate - <{{bool .SessionCreate}}/> - KeepAlive - <{{bool .KeepAlive}}/> - RunAtLoad - <{{bool .RunAtLoad}}/> - Disabled - - - StandardOutPath - /usr/local/var/log/{{html .Name}}.out.log - StandardErrorPath - /usr/local/var/log/{{html .Name}}.err.log - - - -` diff --git a/pkg/install/linux_openrc.go b/pkg/install/linux_openrc.go index b7d4c8c194f6..4f3461a8a310 100644 --- a/pkg/install/linux_openrc.go +++ b/pkg/install/linux_openrc.go @@ -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 }} 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/process.go b/pkg/install/process.go deleted file mode 100644 index bc10785c2842..000000000000 --- a/pkg/install/process.go +++ /dev/null @@ -1,17 +0,0 @@ -/* -Copyright 2021 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 diff --git a/pkg/install/service.go b/pkg/install/service.go index 6540cc2cc5df..d98eb722de7e 100644 --- a/pkg/install/service.go +++ b/pkg/install/service.go @@ -18,8 +18,6 @@ package install import ( "fmt" - "os" - "strings" "github.com/kardianos/service" "github.com/sirupsen/logrus" @@ -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{}{ @@ -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"} @@ -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 @@ -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 -`