-
Notifications
You must be signed in to change notification settings - Fork 471
/
service_systemd.go
92 lines (82 loc) · 1.89 KB
/
service_systemd.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
package system
import (
"context"
"fmt"
"strings"
"github.com/goss-org/goss/util"
)
type ServiceSystemd struct {
service string
legacy bool
}
func NewServiceSystemd(_ context.Context, service string, system *System, config util.Config) Service {
return &ServiceSystemd{
service: service,
}
}
func NewServiceSystemdLegacy(_ context.Context, service string, system *System, config util.Config) Service {
return &ServiceSystemd{
service: service,
legacy: true,
}
}
func (s *ServiceSystemd) Service() string {
return s.service
}
func (s *ServiceSystemd) Exists() (bool, error) {
if invalidService(s.service) {
return false, nil
}
cmd := util.NewCommand("systemctl", "-q", "list-unit-files", "--type=service")
cmd.Run()
if strings.Contains(cmd.Stdout.String(), fmt.Sprintf("%s.service", s.service)) {
return true, cmd.Err
}
if s.legacy {
// Fallback on sysv
sysv := &ServiceInit{service: s.service}
if e, err := sysv.Exists(); e && err == nil {
return true, nil
}
}
return false, nil
}
func (s *ServiceSystemd) Enabled() (bool, error) {
if invalidService(s.service) {
return false, nil
}
cmd := util.NewCommand("systemctl", "-q", "is-enabled", s.service)
cmd.Run()
if cmd.Status == 0 {
return true, cmd.Err
}
if s.legacy {
// Fallback on sysv
sysv := &ServiceInit{service: s.service}
if en, err := sysv.Enabled(); en && err == nil {
return true, nil
}
}
return false, nil
}
func (s *ServiceSystemd) Running() (bool, error) {
if invalidService(s.service) {
return false, nil
}
cmd := util.NewCommand("systemctl", "-q", "is-active", s.service)
cmd.Run()
if cmd.Status == 0 {
return true, cmd.Err
}
if s.legacy {
// Fallback on sysv
sysv := &ServiceInit{service: s.service}
if r, err := sysv.Running(); r && err == nil {
return true, nil
}
}
return false, nil
}
func (s *ServiceSystemd) RunLevels() ([]string, error) {
return nil, nil
}