-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
336 lines (285 loc) · 6.86 KB
/
main.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
package main
import (
"flag"
"fmt"
"net"
"net/rpc/jsonrpc"
"os"
"strings"
"github.com/abiosoft/ishell"
"github.com/sirupsen/logrus"
"github.com/ttacon/glorious/agent"
"github.com/ttacon/glorious/config"
"github.com/ttacon/glorious/context"
)
var (
configFileLocation = flag.String("config", "glorious.glorious", "config file location")
debugMode = flag.Bool("debug", false, "run in debug mode")
daemonMode = flag.Bool("daemon", false, "run as daemon")
addr = flag.String("addr", ":7777", "The address to connect to")
contex context.Context
MAGIC_COOKIE_V1 = []byte{0x00, 0x00, 0x01, 0x00}
MAGIC_COOKIE_V1_LOGS = []byte{0x00, 0x00, 0x01, 0x01}
)
func init() {
contex = context.NewContext()
}
func main() {
flag.Parse()
if *debugMode {
contex.Logger().SetLevel(logrus.DebugLevel)
}
lgr := contex.Logger()
lgr.Debug("loading config: ", *configFileLocation)
conf, err := config.LoadConfig(*configFileLocation)
if err != nil {
lgr.Error("failed to load config: ", err)
os.Exit(1)
}
if errs := conf.Validate(); len(errs) > 0 {
lgr.Debug("validation errors detected:")
for i, err := range errs {
lgr.Debug("[err %d] %s\n", i, err)
}
os.Exit(1)
}
conf.SetContext(contex)
shell := ishell.New()
shell.SetHomeHistoryPath(".glorious/history")
// display welcome info.
shell.Println(banner)
agnt := agent.NewAgent(conf, *configFileLocation, lgr)
if *daemonMode {
if err := conf.Init(); err != nil {
lgr.Error("failed to initialize config, err: ", err)
os.Exit(1)
}
if err := runServer(agnt, *addr, lgr); err != nil {
lgr.Error(err)
os.Exit(1)
}
return
}
conn, err := net.Dial("tcp", *addr)
if err != nil {
lgr.Error(err)
os.Exit(1)
}
if n, err := conn.Write(MAGIC_COOKIE_V1); err != nil {
lgr.Error(err)
os.Exit(1)
} else if n != len(MAGIC_COOKIE_V1) {
lgr.Errorf(
"failed to write all of the magic cookie, wrote %d of %d bytes\n",
n,
len(MAGIC_COOKIE_V1),
)
os.Exit(1)
}
client := jsonrpc.NewClient(conn)
// register a function for "greet" command.
shell.AddCmd(&ishell.Cmd{
Name: "greet",
Help: "greet user",
Func: func(c *ishell.Context) {
lgr.Debug("command invoked: ", c.Cmd.Name)
var greeting string
if err := client.Call("Agent.Greet", c.Args, &greeting); err != nil {
lgr.Error(err)
return
}
c.Println(greeting)
},
})
shell.AddCmd(&ishell.Cmd{
Name: "config",
Help: "Display config information",
Func: func(c *ishell.Context) {
lgr.Debug("command invoked: ", c.Cmd.Name)
var units []agent.UnitConfig
if err := client.Call("Agent.Config", struct{}{}, &units); err != nil {
lgr.Error(err)
return
}
c.Printf("%-20s| %-9s| Description\n", "Name", "# units")
for _, unit := range units {
c.Printf("%-20s| %-9s| %s\n",
unit.Name,
fmt.Sprintf("%d units", unit.NumSlots),
unit.Description,
)
}
},
})
shell.AddCmd(&ishell.Cmd{
Name: "status",
Help: "Display status information",
Func: func(c *ishell.Context) {
lgr.Debug("command invoked: ", c.Cmd.Name)
var units []agent.UnitStatus
if err := client.Call("Agent.Status", struct{}{}, &units); err != nil {
lgr.Error(err)
return
}
c.Printf("%-20s|%-20s| %-9s\n", "Name", "Groups", "Status")
for _, unit := range units {
c.Printf("%-20s|%-20s| %-9s\n",
unit.Name,
strings.Join(unit.Groups, ", "),
unit.Status,
)
}
},
})
shell.AddCmd(&ishell.Cmd{
Name: "reload",
Help: "Reload the glorious config",
Func: func(c *ishell.Context) {
lgr.Debug("command invoked: ", c.Cmd.Name)
var respErr error
if err := client.Call("Agent.Reload", struct{}{}, &respErr); err != nil {
lgr.Error(err)
return
} else if respErr != nil {
lgr.Error(err)
return
}
c.Printf("Reloaded glorious config from %s\n", *configFileLocation)
},
})
shell.AddCmd(&ishell.Cmd{
Name: "start",
Help: "Start a given unit",
Func: func(c *ishell.Context) {
lgr.Debug("command invoked: ", c.Cmd.Name)
for _, arg := range c.Args {
c.Printf("starting %q...\n", arg)
var respErr string
if err := client.Call(
"Agent.StartUnit",
arg,
&respErr,
); err != nil {
c.Println(err)
} else if len(respErr) > 0 {
c.Println(respErr)
} else {
c.Println("done")
}
}
},
})
storeCmd := &ishell.Cmd{
Name: "store",
Help: "Access the glorious store",
Func: func(c *ishell.Context) {
lgr.Debug("command invoked: ", c.Cmd.Name)
c.Println(c.Cmd.HelpText())
},
}
storeCmd.AddCmd(&ishell.Cmd{
Name: "get",
Help: "Return value for given key",
Func: func(c *ishell.Context) {
lgr.Debug("command invoked: ", c.Cmd.Name)
if len(c.Args) == 0 {
c.Println("must provide key to retrieve value for")
return
}
req := agent.StoreGetValuesRequest{
Keys: c.Args,
}
var resp agent.StoreGetValuesResponse
if err := client.Call(
"Agent.StoreGetValues",
&req,
&resp,
); err != nil {
c.Println(err)
return
}
for key, value := range resp.Values {
c.Printf("%q: %q\n", key, value)
}
},
})
storeCmd.AddCmd(&ishell.Cmd{
Name: "set",
Help: "Stores the key/value pair in the internal store",
Func: func(c *ishell.Context) {
lgr.Debug("command invoked: ", c.Cmd.Name)
if len(c.Args) != 2 {
c.Println("May only provide a single key value pair")
return
}
req := agent.StorePutValueRequest{
Key: c.Args[0],
Value: c.Args[1],
}
var resp agent.ErrResponse
if err := client.Call(
"Agent.StorePutValue",
&req,
&resp,
); err != nil {
c.Println(err)
return
} else if len(resp.Err) > 0 {
c.Println(resp.Err)
return
}
},
})
shell.AddCmd(storeCmd)
shell.AddCmd(&ishell.Cmd{
Name: "stop",
Help: "Stops given units",
Func: func(c *ishell.Context) {
lgr.Debug("command invoked: ", c.Cmd.Name)
for _, arg := range c.Args {
c.Printf("stopping %q...", arg)
var respErr string
if err := client.Call(
"Agent.StopUnit",
arg,
&respErr,
); err != nil {
c.Println(err)
} else if len(respErr) > 0 {
c.Println(respErr)
} else {
c.Println("done")
}
}
},
})
shell.AddCmd(&ishell.Cmd{
Name: "tail",
Help: "Tails a unit",
Func: func(c *ishell.Context) {
lgr.Debug("command invoked: ", c.Cmd.Name)
// be cheaper if we can
unitName := c.Args[0]
unit, ok := conf.GetUnit(unitName)
if !ok {
c.Printf("unknown unit %q, aborting\n", unitName)
return
}
// if there are no more args, run a following tail, if
// there's a number, run a number sliced tail
if err := unit.Tail(); err != nil {
c.Println(err)
}
},
})
// run shell
shell.Run()
}
const banner = `
_ _
__ _| | ___ _ __(_) ___ _ _ ___
/ _ | |/ _ \| '__| |/ _ \| | | / __|
| (_| | | (_) | | | | (_) | |_| \__ \
\__, |_|\___/|_| |_|\___/ \__,_|___/
|___/
`