@@ -23,6 +23,9 @@ import (
23
23
)
24
24
25
25
type InitSystem interface {
26
+ // return a string describing how to enable a service
27
+ EnableCommand (service string ) string
28
+
26
29
// ServiceStart tries to start a specific service
27
30
ServiceStart (service string ) error
28
31
@@ -42,8 +45,63 @@ type InitSystem interface {
42
45
ServiceIsActive (service string ) bool
43
46
}
44
47
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
+
45
99
type SystemdInitSystem struct {}
46
100
101
+ func (sysd SystemdInitSystem ) EnableCommand (service string ) string {
102
+ return fmt .Sprintf ("systemctl enable %s.service" , service )
103
+ }
104
+
47
105
func (sysd SystemdInitSystem ) reloadSystemd () error {
48
106
if err := exec .Command ("systemctl" , "daemon-reload" ).Run (); err != nil {
49
107
return fmt .Errorf ("failed to reload systemd: %v" , err )
@@ -110,6 +168,10 @@ func (sysd SystemdInitSystem) ServiceIsActive(service string) bool {
110
168
// WindowsInitSystem is the windows implementation of InitSystem
111
169
type WindowsInitSystem struct {}
112
170
171
+ func (sysd WindowsInitSystem ) EnableCommand (service string ) string {
172
+ return fmt .Sprintf ("Set-Service '%s' -StartupType Automatic" , service )
173
+ }
174
+
113
175
func (sysd WindowsInitSystem ) ServiceStart (service string ) error {
114
176
args := []string {"Start-Service" , service }
115
177
err := exec .Command ("powershell" , args ... ).Run ()
@@ -171,6 +233,10 @@ func GetInitSystem() (InitSystem, error) {
171
233
if err == nil {
172
234
return & SystemdInitSystem {}, nil
173
235
}
236
+ _ , err = exec .LookPath ("openrc" )
237
+ if err == nil {
238
+ return & OpenRCInitSystem {}, nil
239
+ }
174
240
_ , err = exec .LookPath ("wininit.exe" )
175
241
if err == nil {
176
242
return & WindowsInitSystem {}, nil
0 commit comments