-
Notifications
You must be signed in to change notification settings - Fork 471
/
service_upstart.go
84 lines (74 loc) · 2 KB
/
service_upstart.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
package system
import (
"bufio"
"context"
"fmt"
"os"
"regexp"
"strings"
"github.com/goss-org/goss/util"
)
type ServiceUpstart struct {
service string
}
var upstartEnabled = regexp.MustCompile(`^\s*start on`)
var upstartDisabled = regexp.MustCompile(`^manual`)
func NewServiceUpstart(_ context.Context, service string, system *System, config util.Config) Service {
return &ServiceUpstart{service: service}
}
func (s *ServiceUpstart) Service() string {
return s.service
}
func (s *ServiceUpstart) Exists() (bool, error) {
// upstart
if _, err := os.Stat(fmt.Sprintf("/etc/init/%s.conf", s.service)); err == nil {
return true, nil
}
// Fallback on sysv
sysv := &ServiceInit{service: s.service}
if e, err := sysv.Exists(); e && err == nil {
return true, nil
}
return false, nil
}
func (s *ServiceUpstart) Enabled() (bool, error) {
if fh, err := os.Open(fmt.Sprintf("/etc/init/%s.override", s.service)); err == nil {
scanner := bufio.NewScanner(fh)
for scanner.Scan() {
line := scanner.Text()
if upstartDisabled.MatchString(line) {
return false, nil
}
}
}
// If no /etc/init/<service>.override with `manual` keyword in it has been found
// Check the contents of the upstart manifest.
if fh, err := os.Open(fmt.Sprintf("/etc/init/%s.conf", s.service)); err == nil {
scanner := bufio.NewScanner(fh)
for scanner.Scan() {
line := scanner.Text()
if upstartEnabled.MatchString(line) {
return true, nil
}
}
}
// Fallback on sysv
sysv := &ServiceInit{service: s.service}
if en, err := sysv.Enabled(); en && err == nil {
return true, nil
}
return false, nil
}
func (s *ServiceUpstart) Running() (bool, error) {
cmd := util.NewCommand("service", s.service, "status")
cmd.Run()
out := cmd.Stdout.String()
if cmd.Status == 0 && (strings.Contains(out, "running") || strings.Contains(out, "online")) {
return true, cmd.Err
}
return false, nil
}
func (s *ServiceUpstart) RunLevels() ([]string, error) {
sysv := &ServiceInit{service: s.service}
return sysv.RunLevels()
}