/
controllers.go
489 lines (435 loc) · 11.4 KB
/
controllers.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
package main
import (
"encoding/json"
"net/http"
"github.com/asaskevich/govalidator"
"github.com/julienschmidt/httprouter"
"github.com/samalba/dockerclient"
)
// Register contains all information that should
// be passed into a valid POST /register call
type Register struct {
Name string `valid:"alphanum,required"`
Password string `valid:"alphanum,required"`
}
// RegisterSuccess is used upon a successful
// POST /register call
type RegisterSuccess struct {
Success bool
Message string
}
// RegisterController handles account creation
func RegisterController(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var s Register
var sr = RegisterSuccess{false, ""}
json.NewDecoder(r.Body).Decode(&s)
r.Body.Close()
// Result ignored since a nil err value tells us
// the same thing that a result of true does.
if _, err := govalidator.ValidateStruct(s); err != nil {
sr.Message = err.Error()
w.WriteHeader(400)
w.Write(Marshal(sr))
return
}
if err := (User{
Username: s.Name,
Password: s.Password,
}.Create()); err != nil {
sr.Message = err.Error()
w.WriteHeader(400)
w.Write(Marshal(sr))
return
}
sr.Message = "Users has been created"
sr.Success = true
w.Write(Marshal(sr))
return
}
// Login although the same as the Register struct using the same
// struct for both did not feel right and makes the code
// slightly confusing.
type Login struct {
Name string `valid:"alphanum,required"`
Password string `valid:"alphanum,required"`
}
// LoginSuccess is used upon a successful
// POST /login call
type LoginSuccess struct {
Success bool
Message string
Token string
}
// LoginController handles authentication and token generation
func LoginController(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var s Login
var sr = LoginSuccess{false, "", ""}
json.NewDecoder(r.Body).Decode(&s)
r.Body.Close()
_, err := govalidator.ValidateStruct(s)
if err != nil {
sr.Message = err.Error()
w.WriteHeader(400)
w.Write(Marshal(sr))
return
}
user := User{}
if err := (user.Polulate(s.Name)); err != nil {
sr.Message = err.Error()
w.WriteHeader(400)
w.Write(Marshal(sr))
return
}
if err := validPassword(s.Password, user.Password); err != nil {
sr.Message = err.Error()
w.WriteHeader(401)
w.Write(Marshal(sr))
return
}
if err := user.GenerateToken(); err != nil {
sr.Message = err.Error()
w.WriteHeader(400)
w.Write(Marshal(sr))
return
}
sr.Token = user.Token
sr.Success = true
w.Write(Marshal(sr))
return
}
// ListResourceSuccess is used upon a successful
// GET /resource call
type ListResourceSuccess struct {
Success bool
Message string
Resources []Resource
}
// ListResourceController brings back a list of all resources
// that are current available to that user
func ListResourceController(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
sr := ListResourceSuccess{false, "", nil}
if err := VerifyToken(r.Header.Get("token")); err != nil {
sr.Message = err.Error()
w.WriteHeader(401)
w.Write(Marshal(sr))
return
}
resources, err := Resource{}.FetchAll()
if err != nil {
sr.Message = err.Error()
w.WriteHeader(400)
w.Write(Marshal(sr))
return
}
sr.Success = true
sr.Resources = resources
w.Write(Marshal(sr))
return
}
// CreateResource validates incomming requests
type CreateResource struct {
Name string `valid:"alphanum,required"`
File string `valid:"required"`
}
// CreateResourceSuccess ...
type CreateResourceSuccess struct {
Success bool
Message string
}
// CreateResourceController creates a new resource that all users now have access to
func CreateResourceController(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var s CreateResource
sr := CreateResourceSuccess{false, ""}
if err := VerifyToken(r.Header.Get("token")); err != nil {
sr.Message = err.Error()
w.WriteHeader(401)
w.Write(Marshal(sr))
return
}
json.NewDecoder(r.Body).Decode(&s)
r.Body.Close()
_, err := govalidator.ValidateStruct(s)
if err != nil {
sr.Message = err.Error()
w.WriteHeader(400)
w.Write(Marshal(sr))
return
}
if err := (Resource{
Name: s.Name,
File: s.File,
}.Create()); err != nil {
sr.Message = err.Error()
w.WriteHeader(400)
w.Write(Marshal(sr))
return
}
sr.Success = true
sr.Message = "Your resource is now available for use in new containers"
w.Write(Marshal(sr))
return
}
// CreateMachine validates that the input is valid
// to an extent... Validation could be better for
// this controller.
type CreateMachine struct {
Name string `valid:"alphanum,required"`
Username string `valid:"alphanum,required"`
IP string `valid:"required"`
}
// CreateMachineSuccess is returned
// when the controller has successfully
// returned
type CreateMachineSuccess struct {
Success bool
Message string
}
// CreateMachineController adds a new machine that can be used to launch containers on
func CreateMachineController(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var s CreateMachine
sr := CreateMachineSuccess{false, ""}
if err := VerifyToken(r.Header.Get("token")); err != nil {
sr.Message = err.Error()
w.WriteHeader(401)
w.Write(Marshal(sr))
return
}
json.NewDecoder(r.Body).Decode(&s)
r.Body.Close()
_, err := govalidator.ValidateStruct(s)
if err != nil {
sr.Message = err.Error()
w.WriteHeader(400)
w.Write(Marshal(sr))
return
}
if err := (Machine{
Name: s.Name,
Username: s.Username,
IP: s.IP,
}.Create()); err != nil {
sr.Message = err.Error()
w.WriteHeader(400)
w.Write(Marshal(sr))
return
}
sr.Success = true
sr.Message = "Your machine has been added to the pool of machines"
w.Write(Marshal(sr))
return
}
// CreateLease validates that a resource name
// Has been specified
type CreateLease struct {
ResourceName string `valid:"alphanum,required"` // As set in the resources table
LeaseName string `valid:"alphanum,required"` // name of container on remote machine
}
// CreateLeaseSuccess ...
type CreateLeaseSuccess struct {
Success bool
Message string
Username string
Machine string
Name string
}
// CreateLeaseController creates a new machine for a user to use
func CreateLeaseController(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var s CreateLease
sr := CreateLeaseSuccess{false, "", "", "", ""}
if err := VerifyToken(r.Header.Get("token")); err != nil {
sr.Message = err.Error()
w.WriteHeader(401)
w.Write(Marshal(sr))
return
}
json.NewDecoder(r.Body).Decode(&s)
r.Body.Close()
_, err := govalidator.ValidateStruct(s)
if err != nil {
sr.Message = err.Error()
w.WriteHeader(400)
w.Write(Marshal(sr))
return
}
// Fetch machine to deploy to
m := Machine{}
if err := m.FetchRand(); err != nil {
sr.Message = err.Error()
w.WriteHeader(400)
w.Write(Marshal(sr))
return
}
// Fetch type of resource to build
res := Resource{}
if err := res.Fetch(s.ResourceName); err != nil {
sr.Message = err.Error()
w.WriteHeader(400)
w.Write(Marshal(sr))
return
}
// Create a tar file in memory that can be used
// to send to docker to create a new image without
// ever writing to disk
tar, err := MakeTarFile(res.File)
if err != nil {
sr.Message = err.Error()
w.WriteHeader(400)
w.Write(Marshal(sr))
return
}
// Connect to remote machine
docker, err := dockerclient.NewDockerClient("http://"+m.IP+":5555", nil)
if err != nil {
sr.Message = err.Error()
w.WriteHeader(400)
w.Write(Marshal(sr))
return
}
// Build a docker image on remote machine
buildImageConfig := &dockerclient.BuildImage{
Context: tar,
RepoName: res.Name,
SuppressOutput: false,
}
// Try to create image on remote docker machine
reader, err := docker.BuildImage(buildImageConfig)
if err != nil {
sr.Message = err.Error()
w.WriteHeader(400)
w.Write(Marshal(sr))
return
}
// If called before checking for an error and an error occurs you will
// get a null pointer dereferenced error.
defer reader.Close()
// These two commands are super helpful but it is returned as
// a stream of json... In a better implementation we would want to
// send this back to the client as it comes in. For now I have just
// left it in to help with debugging.
// text, _ := ioutil.ReadAll(reader)
// fmt.Println(string(text))
// Create a container
containerConfig := &dockerclient.ContainerConfig{
Image: res.Name,
Cmd: []string{"ping", "8.8.8.8"},
AttachStdin: true,
Tty: true}
containerID, err := docker.CreateContainer(containerConfig, s.LeaseName)
if err != nil {
sr.Message = err.Error()
w.WriteHeader(400)
w.Write(Marshal(sr))
return
}
// Start the container on randomly chosen machine
hostConfig := &dockerclient.HostConfig{}
// For some reason this API returns the full 64 character hex String
// which can't be used when starting the container as it only takes the
// first 12 characters. Possibly a bad api or exists for legacy reasons.
err = docker.StartContainer(containerID[0:12], hostConfig)
if err != nil {
sr.Message = err.Error()
w.WriteHeader(400)
w.Write(Marshal(sr))
return
}
// Save Lease in database
Lease{
Name: s.LeaseName,
Username: m.Username,
MachineName: m.IP,
}.Create()
sr.Success = true
sr.Message = "Your lease was successfully fulfilled"
sr.Machine = m.IP
sr.Username = m.Username
sr.Name = s.LeaseName
w.Write(Marshal(sr))
return
}
// ListLeaseSuccess ...
type ListLeaseSuccess struct {
Success bool
Message string
Leases []Lease
}
// ListLeasesController returns all leases for a specific client
func ListLeasesController(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
sr := ListLeaseSuccess{false, "", nil}
if err := VerifyToken(r.Header.Get("token")); err != nil {
sr.Message = err.Error()
w.WriteHeader(401)
w.Write(Marshal(sr))
return
}
leases, err := Lease{}.FetchAll()
if err != nil {
sr.Message = err.Error()
w.WriteHeader(401)
w.Write(Marshal(sr))
return
}
sr.Success = true
sr.Leases = leases
w.Write(Marshal(sr))
return
}
// DeleteLease requires the name of the
// container so that it can be deleted
type DeleteLease struct {
Name string `valid:"alphanum,required"`
}
// DeleteLeaseSuccess ...
type DeleteLeaseSuccess struct {
Success bool
Message string
}
// DeleteLeaseController removes a lease from the remote machine
func DeleteLeaseController(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var s DeleteLease
sr := DeleteLeaseSuccess{}
if err := VerifyToken(r.Header.Get("token")); err != nil {
sr.Message = err.Error()
w.WriteHeader(401)
w.Write(Marshal(sr))
return
}
json.NewDecoder(r.Body).Decode(&s)
r.Body.Close()
_, err := govalidator.ValidateStruct(s)
if err != nil {
sr.Message = err.Error()
w.WriteHeader(400)
w.Write(Marshal(sr))
return
}
// Get information about lease so we know what machine it is located on
l := Lease{}
if err := l.Fetch(s.Name); err != nil {
sr.Message = err.Error()
w.WriteHeader(400)
w.Write(Marshal(sr))
return
}
// Connect to machine that the lease is on
docker, err := dockerclient.NewDockerClient("http://"+l.MachineName+":5555", nil)
if err != nil {
sr.Message = err.Error()
w.WriteHeader(400)
w.Write(Marshal(sr))
return
}
// Remove the container
if err := docker.RemoveContainer(s.Name, true, true); err != nil {
sr.Message = err.Error()
w.WriteHeader(400)
w.Write(Marshal(sr))
return
}
// Remove container from the database
l.Delete()
sr.Success = true
sr.Message = "Your lease has been removed"
w.Write(Marshal(sr))
return
}