-
Notifications
You must be signed in to change notification settings - Fork 3
/
admin.go
337 lines (260 loc) · 6.82 KB
/
admin.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package main
import (
"fmt"
"github.com/kballard/go-shellquote"
"github.com/skynetservices/skynet"
"github.com/skynetservices/skynet/daemon"
"github.com/skynetservices/skynet/log"
"os"
"sync"
"text/template"
)
var startTemplate = template.Must(template.New("").Parse(
`Started service with UUID {{.UUID}}.
`))
func Start(criteria *skynet.Criteria, args []string) {
if len(args) < 1 {
fmt.Println("Please provide a service name 'sky start binaryName'")
return
}
hosts, err := skynet.GetServiceManager().ListHosts(criteria)
if err != nil {
log.Fatal(err)
}
var wait sync.WaitGroup
for _, host := range hosts {
wait.Add(1)
go func(host string) {
fmt.Println("Starting on host: " + host)
d := daemon.GetDaemonForHost(host)
in := daemon.StartSubServiceRequest{
BinaryName: args[0],
Args: shellquote.Join(args[1:]...),
// TODO: maybe an optional flag to change this?
Registered: true,
}
out, err := d.StartSubService(in)
if err != nil {
fmt.Println("Returned Error: " + err.Error())
wait.Done()
return
}
startTemplate.Execute(os.Stdout, out)
wait.Done()
}(host)
}
wait.Wait()
}
var stopTemplate = template.Must(template.New("").Parse(
`{{if .Ok}}Stopped service with UUID {{.UUID}}.
{{else}}Service with UUID {{.UUID}} is already stopped.
{{end}}`))
func Stop(criteria *skynet.Criteria) {
instances, err := skynet.GetServiceManager().ListInstances(criteria)
if err != nil {
log.Fatal(err)
}
var wait sync.WaitGroup
for _, instance := range filterDaemon(instances) {
wait.Add(1)
go func(instance skynet.ServiceInfo) {
fmt.Println("Stopping: " + instance.UUID)
d := daemon.GetDaemonForService(&instance)
in := daemon.StopSubServiceRequest{
UUID: instance.UUID,
}
out, err := d.StopSubService(in)
if err != nil {
fmt.Println("Returned Error: " + err.Error())
wait.Done()
return
}
stopTemplate.Execute(os.Stdout, out)
wait.Done()
}(instance)
}
wait.Wait()
}
var restartTemplate = template.Must(template.New("").Parse(
`{{if .Ok}}Restarted service with UUID {{.UUID}}.
{{else}}Service with UUID {{.UUID}} is not running.
{{end}}`))
func Restart(criteria *skynet.Criteria) {
instances, err := skynet.GetServiceManager().ListInstances(criteria)
if err != nil {
log.Fatal(err)
}
var wait sync.WaitGroup
for _, instance := range filterDaemon(instances) {
wait.Add(1)
go func(instance skynet.ServiceInfo) {
fmt.Println("Restarting: " + instance.UUID)
d := daemon.GetDaemonForService(&instance)
in := daemon.RestartSubServiceRequest{
UUID: instance.UUID,
}
out, err := d.RestartSubService(in)
if err != nil {
fmt.Println("Returned Error: " + err.Error())
wait.Done()
return
}
restartTemplate.Execute(os.Stdout, out)
wait.Done()
}(instance)
}
wait.Wait()
}
var registerTemplate = template.Must(template.New("").Parse(
`Registered service with UUID {{.UUID}}.
`))
func Register(criteria *skynet.Criteria) {
instances, err := skynet.GetServiceManager().ListInstances(criteria)
if err != nil {
log.Fatal(err)
}
var wait sync.WaitGroup
for _, instance := range filterDaemon(instances) {
wait.Add(1)
go func(instance skynet.ServiceInfo) {
fmt.Println("Registering: " + instance.UUID)
d := daemon.GetDaemonForService(&instance)
in := daemon.RegisterSubServiceRequest{
UUID: instance.UUID,
}
out, err := d.RegisterSubService(in)
if err != nil {
fmt.Println("Returned Error: " + err.Error())
wait.Done()
return
}
registerTemplate.Execute(os.Stdout, out)
wait.Done()
}(instance)
}
wait.Wait()
}
var unregisterTemplate = template.Must(template.New("").Parse(
`Unregistered service with UUID {{.UUID}}.
`))
func Unregister(criteria *skynet.Criteria) {
instances, err := skynet.GetServiceManager().ListInstances(criteria)
if err != nil {
log.Fatal(err)
}
var wait sync.WaitGroup
for _, instance := range filterDaemon(instances) {
wait.Add(1)
go func(instance skynet.ServiceInfo) {
fmt.Println("Unregistering: " + instance.UUID)
d := daemon.GetDaemonForService(&instance)
in := daemon.UnregisterSubServiceRequest{
UUID: instance.UUID,
}
out, err := d.UnregisterSubService(in)
if err != nil {
fmt.Println("Returned Error: " + err.Error())
wait.Done()
return
}
unregisterTemplate.Execute(os.Stdout, out)
wait.Done()
}(instance)
}
wait.Wait()
}
var logLevelTemplate = template.Must(template.New("").Parse(
`Set LogLevel to {{.Level}} for UUID {{.UUID}}.
`))
func SetLogLevel(criteria *skynet.Criteria, level string) {
instances, err := skynet.GetServiceManager().ListInstances(criteria)
if err != nil {
log.Fatal(err)
}
var wait sync.WaitGroup
for _, instance := range filterDaemon(instances) {
wait.Add(1)
go func(instance skynet.ServiceInfo) {
fmt.Println("Setting LogLevel to " + level + " for: " + instance.UUID)
d := daemon.GetDaemonForService(&instance)
in := daemon.SubServiceLogLevelRequest{
UUID: instance.UUID,
Level: level,
}
out, err := d.SubServiceLogLevel(in)
if err != nil {
fmt.Println("Returned Error: " + err.Error())
wait.Done()
return
}
logLevelTemplate.Execute(os.Stdout, out)
wait.Done()
}(instance)
}
wait.Wait()
}
func SetDaemonLogLevel(criteria *skynet.Criteria, level string) {
hosts, err := skynet.GetServiceManager().ListHosts(criteria)
if err != nil {
log.Fatal(err)
}
var wait sync.WaitGroup
for _, host := range hosts {
wait.Add(1)
go func(host string) {
d := daemon.GetDaemonForHost(host)
in := daemon.LogLevelRequest{
Level: level,
}
out, err := d.LogLevel(in)
if err != nil {
fmt.Println("Returned Error: " + err.Error())
wait.Done()
return
}
if out.Ok {
fmt.Printf("Set daemon log level to %v on host: %v\n", level, host)
} else {
fmt.Printf("Failed to set daemon log level to %v on host: %v\n", level, host)
}
wait.Done()
}(host)
}
wait.Wait()
}
func StopDaemon(criteria *skynet.Criteria) {
hosts, err := skynet.GetServiceManager().ListHosts(criteria)
if err != nil {
log.Fatal(err)
}
var wait sync.WaitGroup
for _, host := range hosts {
wait.Add(1)
go func(host string) {
d := daemon.GetDaemonForHost(host)
in := daemon.StopRequest{}
out, err := d.Stop(in)
if err != nil {
fmt.Println("Returned Error: " + err.Error())
wait.Done()
return
}
if out.Ok {
fmt.Printf("Daemon stopped on host: %v\n", host)
} else {
fmt.Printf("Failed to stop daemon on host: %v\n", host)
}
wait.Done()
}(host)
}
wait.Wait()
}
func filterDaemon(instances []skynet.ServiceInfo) []skynet.ServiceInfo {
filteredInstances := make([]skynet.ServiceInfo, 0)
for _, i := range instances {
if i.Name != "SkynetDaemon" {
filteredInstances = append(filteredInstances, i)
}
}
return filteredInstances
}