-
Notifications
You must be signed in to change notification settings - Fork 72
/
types.go
123 lines (99 loc) · 2.44 KB
/
types.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package services
import (
"errors"
"fmt"
"github.com/mperham/inspeqtor/util"
)
type ProcessStatus struct {
Pid int
Status
}
func NewStatus() *ProcessStatus {
return &ProcessStatus{0, Unknown}
}
func (s *ProcessStatus) String() string {
return fmt.Sprintf("%s/%d", s.Status, s.Pid)
}
type Status string
func (s Status) String() string {
return string(s)
}
const (
Unknown Status = "Unknown"
Down = "Down"
Starting = "Starting"
Up = "Up"
)
type ServiceError struct {
Init string
Name string
Err error
}
func (e *ServiceError) Error() string { return e.Init + "/" + e.Name + ": " + e.Err.Error() }
var ErrServiceNotFound = errors.New("No such service")
// Your init system(s) manages services. We use
// the init system to:
// 1. find the associated process
// 2. restart the service
type InitSystem interface {
// Name of the init system: "upstart", "runit", etc.
Name() string
// Find the process info for a given service name. All errors
// returned must be of type ServiceError.
LookupService(name string) (*ProcessStatus, error)
// Restart the process associated with the given service name. All errors
// returned must be of type ServiceError.
Restart(name string) error
}
var (
SupportedInits = []func() (InitSystem, error){
func() (InitSystem, error) {
return detectLaunchd("/")
},
func() (InitSystem, error) {
return detectUpstart("/etc/init")
},
func() (InitSystem, error) {
return detectRunit("/")
},
func() (InitSystem, error) {
return detectSystemd("/etc/systemd")
},
}
)
func Detect() []InitSystem {
inits := make([]InitSystem, 0)
for name, funk := range SupportedInits {
sm, err := funk()
if err != nil {
util.Warn("Couldn't detect %s: %s", name, err.Error())
continue
}
if sm != nil {
inits = append(inits, sm)
}
}
if len(inits) == 0 {
util.Warn("No init system detected. Inspeqtor cannot control any services!")
}
return inits
}
func MockInit() *MockInitSystem {
return &MockInitSystem{}
}
type MockInitSystem struct {
Actions []string
CurrentStatus *ProcessStatus
}
func (m *MockInitSystem) Name() string { return "mock" }
func (m *MockInitSystem) Restart(name string) error {
m.Actions = append(m.Actions, "restart "+name)
return nil
}
func (m *MockInitSystem) LookupService(name string) (*ProcessStatus, error) {
if m.CurrentStatus != nil {
return m.CurrentStatus, nil
} else {
return &ProcessStatus{123, Up}, nil
}
}