/
initSystem.go
75 lines (66 loc) · 2.15 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package saltboot
import (
"log"
"os"
)
type InitSystem struct {
Start string
Stop string
Restart string
Enable string
Disable string
ActionBin string
StateBin string
CommandOrderASC bool
}
const (
START_ACTION = "start"
STOP_ACTION = "stop"
RESTART_ACTION = "restart"
)
var stat = os.Stat
var (
SYSTEM_D = InitSystem{ActionBin: "/bin/systemctl", StateBin: "/bin/systemctl", Start: START_ACTION, Stop: STOP_ACTION, Restart: RESTART_ACTION, Enable: "enable", Disable: "disable", CommandOrderASC: true}
SYS_V_INIT = InitSystem{ActionBin: "/sbin/service", StateBin: "/sbin/chkconfig", Start: START_ACTION, Stop: STOP_ACTION, Restart: RESTART_ACTION, Enable: "on", Disable: "off", CommandOrderASC: false}
)
func (system InitSystem) ActionCommand(service string, action string) []string {
if action == START_ACTION || action == RESTART_ACTION {
if action == START_ACTION {
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.Restart, service}
}
return []string{system.ActionBin, service, system.Restart}
}
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() (system InitSystem) {
if _, err := stat("/bin/systemctl"); err == nil {
log.Println("[GetInitSystem] /bin/systemctl found, assume systemd")
return SYSTEM_D
}
log.Println("[GetInitSystem] /bin/systemctl not found, assume sysv init")
return SYS_V_INIT
}