This repository has been archived by the owner on Jun 14, 2023. It is now read-only.
/
services.go
56 lines (46 loc) · 1.64 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 types.Phase = services{}
type services struct{}
func (p services) ApplyPhase(sys *types.Config, ctx *types.SystemContext) ([]types.Command, types.Filesystem, error) {
var commands []types.Command
files := types.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] = types.File{Content: svc.Extra.ToUnitFile()}
commands = append(commands, types.Command{Cmd: "systemctl enable " + name})
commands = append(commands, types.Command{Cmd: "systemctl start " + name})
}
return commands, files, nil
}
func (p services) Verify(cfg *types.Config, results *types.VerifyResults, flags ...types.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 *types.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
}