-
Notifications
You must be signed in to change notification settings - Fork 3
/
initSystem.go
60 lines (53 loc) · 1.75 KB
/
initSystem.go
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
package saltboot
import (
"log"
"os"
)
type InitSystem struct {
Start string
Stop string
Enable string
Disable string
ActionBin string
StateBin string
CommandOrderASC bool
}
var (
SYSYEM_D = InitSystem{ActionBin: "/bin/systemctl", StateBin: "/bin/systemctl", Start: "start", Stop: "stop", Enable: "enable", Disable: "disable", CommandOrderASC: true}
SYS_V_INIT = InitSystem{ActionBin: "/sbin/service", StateBin: "/sbin/chkconfig", Start: "start", Stop: "stop", Enable: "on", Disable: "off", CommandOrderASC: false}
)
func (system InitSystem) ActionCommand(service string, run bool) []string {
if run {
if system.CommandOrderASC {
return []string{system.ActionBin, system.Start, service}
}
return []string{system.ActionBin, service, system.Start}
}
if system.CommandOrderASC {
return []string{system.ActionBin, system.Stop, service}
}
return []string{system.ActionBin, service, system.Stop}
}
func (system InitSystem) StateCommand(service string, enable bool) []string {
if enable {
if system.CommandOrderASC {
return []string{system.StateBin, system.Enable, service}
}
return []string{system.StateBin, service, system.Enable}
}
if system.CommandOrderASC {
return []string{system.StateBin, system.Disable, service}
}
return []string{system.StateBin, service, system.Disable}
}
func (system InitSystem) Error() string {
return "Failed to determine init system"
}
func GetInitSystem(stat func(name string) (os.FileInfo, error)) (system InitSystem) {
if _, err := stat("/bin/systemctl"); err == nil {
log.Printf("[GetInitSystem] /bin/systemctl found, assume systemd")
return SYSYEM_D
}
log.Printf("[GetInitSystem] /bin/systemctl not found, assume sysv init")
return SYS_V_INIT
}