-
Notifications
You must be signed in to change notification settings - Fork 14
/
sectService.go
141 lines (134 loc) · 3.73 KB
/
sectService.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package note
import (
"fmt"
"github.com/SUSE/saptune/system"
"strings"
)
// section [service]
// GetServiceVal initialise the systemd service structure with the current
// system settings
func GetServiceVal(key string) string {
var val string
serviceKey := key
keyFields := strings.Split(key, ":")
if len(keyFields) == 2 {
// keyFields[0] = systemd - for further use
serviceKey = keyFields[1]
}
service := system.GetServiceName(serviceKey)
if service == "" {
return "NA"
}
active, _ := system.SystemctlIsRunning(service)
if active {
val = "start"
} else {
val = "stop"
}
enabled, _ := system.SystemctlIsEnabled(service)
if enabled {
val = fmt.Sprintf("%s, enable", val)
} else {
val = fmt.Sprintf("%s, disable", val)
}
return val
}
// OptServiceVal optimises the systemd service structure with the settings
// from the configuration file
func OptServiceVal(key, cfgval string) string {
ssState := false
edState := false
retVal := ""
serviceKey := key
keyFields := strings.Split(key, ":")
if len(keyFields) == 2 {
// keyFields[0] = systemd - for further use
serviceKey = keyFields[1]
}
service := system.GetServiceName(serviceKey)
if service == "" {
return "NA"
}
for _, state := range strings.Split(cfgval, ",") {
sval := strings.ToLower(strings.TrimSpace(state))
if sval != "" && sval != "start" && sval != "stop" && sval != "enable" && sval != "disable" {
system.WarningLog("wrong service state '%s' for '%s'. Skipping...\n", sval, service)
}
setVal := ""
if sval == "start" || sval == "stop" {
if ssState {
system.WarningLog("multiple start/stop entries found, using the first one and skipping '%s'\n", sval)
} else {
// only the first 'start/stop' value is used
ssState = true
setVal = sval
}
// for uuidd.socket we only support 'start' (bsc#1100107)
if service == "uuidd.socket" && setVal != "start" {
system.WarningLog("wrong selection '%s' for '%s'. Now set to 'start' to start the service\n", sval, service)
setVal = "start"
}
}
if sval == "enable" || sval == "disable" {
if edState {
system.WarningLog("multiple enable/disable entries found, using the first one and skipping '%s'\n", sval)
} else {
// only the first 'enable/disable' value is used
edState = true
setVal = sval
}
}
if setVal == "" {
continue
}
if retVal == "" {
retVal = setVal
} else {
retVal = fmt.Sprintf("%s, %s", retVal, setVal)
}
}
if service == "uuidd.socket" {
if retVal == "" {
system.WarningLog("Set missing selection 'start' for '%s' to start the service\n", service)
retVal = "start"
} else if !ssState {
system.WarningLog("Set missing selection 'start' for '%s' to start the service\n", service)
retVal = fmt.Sprintf("%s, start", retVal)
}
}
return retVal
}
// SetServiceVal applies the settings to the system
func SetServiceVal(key, value string) error {
var err error
// for compatibility to saptune v2 (revert!)
// v2 - servicename, v3 - systemd:servicename
serviceKey := key
keyFields := strings.Split(key, ":")
if len(keyFields) == 2 {
// keyFields[0] = systemd - for further use
serviceKey = keyFields[1]
}
service := system.GetServiceName(serviceKey)
if service == "" {
return nil
}
for _, state := range strings.Split(value, ",") {
sval := strings.ToLower(strings.TrimSpace(state))
active, _ := system.SystemctlIsRunning(service)
if sval == "start" && !active {
err = system.SystemctlStart(service)
}
if sval == "stop" && active {
err = system.SystemctlStop(service)
}
enabled, _ := system.SystemctlIsEnabled(service)
if sval == "enable" && !enabled {
err = system.SystemctlEnable(service)
}
if sval == "disable" && enabled {
err = system.SystemctlDisable(service)
}
}
return err
}