This repository has been archived by the owner on Jun 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
services.go
56 lines (46 loc) · 1.56 KB
/
services.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
package phases
import (
"fmt"
"strings"
. "github.com/flanksource/konfigadm/pkg/types"
"github.com/flanksource/konfigadm/pkg/utils"
)
var Services Phase = services{}
type services struct{}
func (p services) ApplyPhase(sys *Config, ctx *SystemContext) ([]Command, Filesystem, error) {
var commands []Command
files := Filesystem{}
for name, svc := range sys.Services {
filename := fmt.Sprintf("/etc/systemd/system/%s.service", name)
svc.Extra.Service.ExecStart = svc.ExecStart
svc.Extra.Unit.Description = name
if svc.Extra.Install.WantedBy == "" && svc.Extra.Install.RequiredBy == "" {
svc.Extra.Install.WantedBy = "multi-user.target"
}
files[filename] = File{Content: svc.Extra.ToUnitFile()}
commands = append(commands, Command{Cmd: "systemctl enable " + name})
commands = append(commands, Command{Cmd: "systemctl start " + name})
}
return commands, files, nil
}
func (p services) Verify(cfg *Config, results *VerifyResults, flags ...Flag) bool {
verify := true
for name := range cfg.Services {
verify = verify && VerifyService(name, results)
}
return verify
}
//VerifyService checks that the service is enabled and running
func VerifyService(name string, results *VerifyResults) bool {
stdout, ok := utils.SafeExec("systemctl status %s | grep Active", name)
stdout = strings.TrimSpace(strings.Split(stdout, "\n")[0])
if !ok {
results.Fail("%s is %s", name, stdout)
} else if strings.Contains(stdout, "active (running)") {
results.Pass("%s is %s", name, stdout)
return true
} else {
results.Fail("%s is %s", name, stdout)
}
return false
}