github.com/flanksource/konfigadm@v0.12.0/pkg/phases/services.go (about) 1 package phases 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/flanksource/konfigadm/pkg/types" 8 "github.com/flanksource/konfigadm/pkg/utils" 9 ) 10 11 var Services types.Phase = services{} 12 13 type services struct{} 14 15 func (p services) ApplyPhase(sys *types.Config, ctx *types.SystemContext) ([]types.Command, types.Filesystem, error) { 16 var commands []types.Command 17 files := types.Filesystem{} 18 19 for name, svc := range sys.Services { 20 filename := fmt.Sprintf("/etc/systemd/system/%s.service", name) 21 svc.Extra.Service.ExecStart = svc.ExecStart 22 svc.Extra.Unit.Description = name 23 if svc.Extra.Install.WantedBy == "" && svc.Extra.Install.RequiredBy == "" { 24 svc.Extra.Install.WantedBy = "multi-user.target" 25 } 26 files[filename] = types.File{Content: svc.Extra.ToUnitFile()} 27 commands = append(commands, types.Command{Cmd: "systemctl enable " + name}) 28 commands = append(commands, types.Command{Cmd: "systemctl start " + name}) 29 } 30 return commands, files, nil 31 } 32 33 func (p services) Verify(cfg *types.Config, results *types.VerifyResults, flags ...types.Flag) bool { 34 verify := true 35 for name := range cfg.Services { 36 verify = verify && VerifyService(name, results) 37 38 } 39 return verify 40 } 41 42 //VerifyService checks that the service is enabled and running 43 func VerifyService(name string, results *types.VerifyResults) bool { 44 stdout, ok := utils.SafeExec("systemctl status %s | grep Active", name) 45 stdout = strings.TrimSpace(strings.Split(stdout, "\n")[0]) 46 if !ok { 47 results.Fail("%s is %s", name, stdout) 48 49 } else if strings.Contains(stdout, "active (running)") { 50 results.Pass("%s is %s", name, stdout) 51 return true 52 } else { 53 results.Fail("%s is %s", name, stdout) 54 } 55 return false 56 }