Skip to content

Commit aa84b99

Browse files
authored
Merge pull request kubernetes#73101 from oz123/kubeadm_openrc_support
Add initial support for OpenRC
2 parents 21bec91 + 2a40ef4 commit aa84b99

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

pkg/util/initsystem/initsystem.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ import (
2323
)
2424

2525
type InitSystem interface {
26+
// return a string describing how to enable a service
27+
EnableCommand(service string) string
28+
2629
// ServiceStart tries to start a specific service
2730
ServiceStart(service string) error
2831

@@ -42,8 +45,63 @@ type InitSystem interface {
4245
ServiceIsActive(service string) bool
4346
}
4447

48+
type OpenRCInitSystem struct{}
49+
50+
func (openrc OpenRCInitSystem) ServiceStart(service string) error {
51+
args := []string{service, "start"}
52+
return exec.Command("rc-service", args...).Run()
53+
}
54+
55+
func (openrc OpenRCInitSystem) ServiceStop(service string) error {
56+
args := []string{service, "stop"}
57+
return exec.Command("rc-service", args...).Run()
58+
}
59+
60+
func (openrc OpenRCInitSystem) ServiceRestart(service string) error {
61+
args := []string{service, "restart"}
62+
return exec.Command("rc-service", args...).Run()
63+
}
64+
65+
// openrc writes to stderr if a service is not found or not enabled
66+
// this is in contrast to systemd which only writes to stdout.
67+
// Hence, we use the Combinedoutput, and ignore the error.
68+
func (openrc OpenRCInitSystem) ServiceExists(service string) bool {
69+
args := []string{service, "status"}
70+
outBytes, _ := exec.Command("rc-service", args...).CombinedOutput()
71+
if strings.Contains(string(outBytes), "does not exist") {
72+
return false
73+
}
74+
return true
75+
}
76+
77+
func (openrc OpenRCInitSystem) ServiceIsEnabled(service string) bool {
78+
args := []string{"show", "default"}
79+
outBytes, _ := exec.Command("rc-update", args...).Output()
80+
if strings.Contains(string(outBytes), service) {
81+
return true
82+
}
83+
return false
84+
}
85+
86+
func (openrc OpenRCInitSystem) ServiceIsActive(service string) bool {
87+
args := []string{service, "status"}
88+
outBytes, _ := exec.Command("rc-service", args...).Output()
89+
if strings.Contains(string(outBytes), "stopped") {
90+
return false
91+
}
92+
return true
93+
}
94+
95+
func (openrc OpenRCInitSystem) EnableCommand(service string) string {
96+
return fmt.Sprintf("rc-update add %s default", service)
97+
}
98+
4599
type SystemdInitSystem struct{}
46100

101+
func (sysd SystemdInitSystem) EnableCommand(service string) string {
102+
return fmt.Sprintf("systemctl enable %s.service", service)
103+
}
104+
47105
func (sysd SystemdInitSystem) reloadSystemd() error {
48106
if err := exec.Command("systemctl", "daemon-reload").Run(); err != nil {
49107
return fmt.Errorf("failed to reload systemd: %v", err)
@@ -110,6 +168,10 @@ func (sysd SystemdInitSystem) ServiceIsActive(service string) bool {
110168
// WindowsInitSystem is the windows implementation of InitSystem
111169
type WindowsInitSystem struct{}
112170

171+
func (sysd WindowsInitSystem) EnableCommand(service string) string {
172+
return fmt.Sprintf("Set-Service '%s' -StartupType Automatic", service)
173+
}
174+
113175
func (sysd WindowsInitSystem) ServiceStart(service string) error {
114176
args := []string{"Start-Service", service}
115177
err := exec.Command("powershell", args...).Run()
@@ -171,6 +233,10 @@ func GetInitSystem() (InitSystem, error) {
171233
if err == nil {
172234
return &SystemdInitSystem{}, nil
173235
}
236+
_, err = exec.LookPath("openrc")
237+
if err == nil {
238+
return &OpenRCInitSystem{}, nil
239+
}
174240
_, err = exec.LookPath("wininit.exe")
175241
if err == nil {
176242
return &WindowsInitSystem{}, nil

0 commit comments

Comments
 (0)