-
Notifications
You must be signed in to change notification settings - Fork 1
/
services.go
248 lines (225 loc) · 4.29 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"os"
"os/exec"
)
var servs map[string]*service
var procs map[string]process
type service struct {
Enabled bool
Start []string
}
type process struct {
file *os.File
cmd *exec.Cmd
}
func init() {
servs = make(map[string]*service)
procs = make(map[string]process)
}
func serving(name string) (ok bool) {
_, ok = servs[name]
return
}
func running(name string) (ok bool) {
_, ok = procs[name]
return
}
func create(path string) (f *os.File, err error) {
_, err = os.Stat(path)
if err == nil {
err = os.Remove(path)
if err != nil {
return nil, errors.New("failed to remove file")
}
}
f, err = os.Create(path)
return
}
func command(args []string) (*os.File, *exec.Cmd, error) {
f, err := create(path + args[0])
if err != nil {
return nil, nil, err
}
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdout, cmd.Stderr = f, f
return f, cmd, nil
}
func add(args []string) error {
if !serving(args[0]) && !running(args[0]) {
servs[args[0]] = &service{Enabled: true, Start: args}
err := save(path + "services")
if err != nil {
return err
}
return nil
}
return errors.New("service already exists")
}
func remove(name string) error {
if serving(name) {
if running(name) {
return errors.New("service is currently running")
}
delete(servs, name)
delete(procs, name)
err := save(path + "services")
if err != nil {
return err
}
return nil
}
return errors.New("service does not exist")
}
func enable(name string) error {
if serving(name) {
if servs[name].Enabled {
return errors.New("service already enabled")
}
servs[name].Enabled = true
err := save(path + "services")
if err != nil {
return err
}
return nil
}
return errors.New("service does not exist")
}
func disable(name string) error {
if serving(name) {
if !servs[name].Enabled {
return errors.New("service is already disabled")
}
servs[name].Enabled = false
err := save(path + "services")
if err != nil {
return err
}
return nil
} else {
return errors.New("service does not exist")
}
}
func start(name string) error {
if serving(name) {
if !running(name) {
if _, ok := procs[name]; !ok {
f, cmd, err := command(servs[name].Start)
if err != nil {
return err
}
procs[name] = process{file: f, cmd: cmd}
err = procs[name].cmd.Start()
if err != nil {
return errors.New("failed to start process")
}
go procs[name].cmd.Wait()
return nil
} else {
return errors.New("process already exists")
}
} else {
return errors.New("service is already running")
}
} else {
return errors.New("service does not exist")
}
}
func stop(name string) error {
if serving(name) {
if running(name) {
procs[name].cmd.Process.Signal(os.Interrupt)
delete(procs, name)
return nil
} else {
return errors.New("service is not running")
}
} else {
return errors.New("service does not exist")
}
}
func load(path string) error {
if len(servs) > 0 || len(procs) > 0 {
return errors.New("services already loaded")
}
b, err := ioutil.ReadFile(path)
if err == nil {
var data map[string]*service
err = json.Unmarshal(b, &data)
if err == nil {
servs = data
}
}
return err
}
func reload() error {
err := unload()
if err == nil {
err = load(path + "services")
}
return err
}
func unload() error {
interrupt()
for key, _ := range procs {
delete(procs, key)
}
for key, _ := range servs {
delete(servs, key)
}
if len(procs) == 0 {
if len(servs) == 0 {
return nil
} else {
return errors.New("failed to unload all services")
}
} else {
return errors.New("failed to unload all processes")
}
}
func save(path string) error {
f, err := create(path)
if err == nil {
js, err := json.MarshalIndent(servs, "", "\t")
if err == nil {
_, err = f.Write(js)
}
}
return err
}
func interrupt() {
for _, val := range procs {
err := val.cmd.Process.Signal(os.Interrupt)
if err != nil {
log.Println(err)
}
}
}
func boot() error {
for key, val := range servs {
if val.Enabled {
err := start(key)
if err != nil {
return err
}
}
}
return nil
}
func reboot() error {
interrupt()
for key, _ := range procs {
delete(procs, key)
}
err := boot()
return err
}
func shutdown() {
interrupt()
os.Remove(usock)
os.Exit(0)
}