1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
Needed for OpenRC support until https://github.com/kubernetes/kubernetes/pull/73101 is merged.
Brought to attention by https://bugs.alpinelinux.org/issues/10179
---------------------------------
--- a/pkg/util/initsystem/initsystem.go
+++ b/pkg/util/initsystem/initsystem.go
@@ -1,5 +1,5 @@
/*
-Copyright 2016 The Kubernetes Authors.
+Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -23,6 +23,9 @@
)
type InitSystem interface {
+ // return a string describing how to enable a service
+ EnableCommand(service string) string
+
// ServiceStart tries to start a specific service
ServiceStart(service string) error
@@ -42,8 +45,63 @@
ServiceIsActive(service string) bool
}
+type OpenRCInitSystem struct{}
+
+func (openrc OpenRCInitSystem) ServiceStart(service string) error {
+ args := []string{service, "start"}
+ return exec.Command("rc-service", args...).Run()
+}
+
+func (openrc OpenRCInitSystem) ServiceStop(service string) error {
+ args := []string{service, "stop"}
+ return exec.Command("rc-service", args...).Run()
+}
+
+func (openrc OpenRCInitSystem) ServiceRestart(service string) error {
+ args := []string{service, "restart"}
+ return exec.Command("rc-service", args...).Run()
+}
+
+// openrc writes to stderr if a service is not found or not enabled
+// this is in contrast to systemd which only writes to stdout.
+// Hence, we use the Combinedoutput, and ignore the error.
+func (openrc OpenRCInitSystem) ServiceExists(service string) bool {
+ args := []string{service, "status"}
+ outBytes, _ := exec.Command("rc-service", args...).CombinedOutput()
+ if strings.Contains(string(outBytes), "does not exist") {
+ return false
+ }
+ return true
+}
+
+func (openrc OpenRCInitSystem) ServiceIsEnabled(service string) bool {
+ args := []string{"show", "default"}
+ outBytes, _ := exec.Command("rc-update", args...).Output()
+ if strings.Contains(string(outBytes), service) {
+ return true
+ }
+ return false
+}
+
+func (openrc OpenRCInitSystem) ServiceIsActive(service string) bool {
+ args := []string{service, "status"}
+ outBytes, _ := exec.Command("rc-service", args...).Output()
+ if strings.Contains(string(outBytes), "stopped") {
+ return false
+ }
+ return true
+}
+
+func (openrc OpenRCInitSystem) EnableCommand(service string) string {
+ return fmt.Sprintf("rc-update add %s default", service)
+}
+
type SystemdInitSystem struct{}
+func (sysd SystemdInitSystem) EnableCommand(service string) string {
+ return fmt.Sprintf("systemctl enable %s.service", service)
+}
+
func (sysd SystemdInitSystem) reloadSystemd() error {
if err := exec.Command("systemctl", "daemon-reload").Run(); err != nil {
return fmt.Errorf("failed to reload systemd: %v", err)
@@ -110,6 +168,10 @@
// WindowsInitSystem is the windows implementation of InitSystem
type WindowsInitSystem struct{}
+func (sysd WindowsInitSystem) EnableCommand(service string) string {
+ return fmt.Sprintf("Set-Service '%s' -StartupType Automatic", service)
+}
+
func (sysd WindowsInitSystem) ServiceStart(service string) error {
args := []string{"Start-Service", service}
err := exec.Command("powershell", args...).Run()
@@ -170,6 +232,10 @@
_, err := exec.LookPath("systemctl")
if err == nil {
return &SystemdInitSystem{}, nil
+ }
+ _, err = exec.LookPath("openrc")
+ if err == nil {
+ return &OpenRCInitSystem{}, nil
}
_, err = exec.LookPath("wininit.exe")
if err == nil {
|