forked from cakecatz/garage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.coffee
80 lines (66 loc) · 1.77 KB
/
app.coffee
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
express = require('express')
app = express()
vagrant = require 'vagrant.js'
garage = require './lib/garage'
bodyParser = require 'body-parser'
path = require 'path'
server = require('http').createServer(app)
io = require('socket.io')(server)
setting = garage.load_setting_file './setting.json'
garage.init setting
# for development
p = console.log
## setup express
app.use bodyParser.json()
app.use bodyParser.urlencoded {
extended: true
}
app.set 'views', './public'
app.set 'view engine', 'ejs'
app.use express.static (__dirname + '/public')
app.get '/', (req, res)->
vagrant.status (statusArr)->
vfile = garage.status setting
res.render 'index', {
title: setting.name
vm: statusArr
vfile: vfile.vms
}
app.get '/refresh', (req, res)->
vagrant.status (statusArr)->
vfile = garage.status setting
res.send JSON.stringify {
vms: statusArr
vfile: vfile.vms
}
app.get '/new', (req, res)->
vagrant.boxList (boxList)->
res.render 'new', {
title: setting.name
boxes: boxList
}
app.get '/:id([0-9a-z]+)/destroy', (req, res)->
vagrant.destroy req.params.id, (result)->
res.send result
app.get '/vagrantfile/:uuid([0-9a-z\-]+)/:control([a-z]+)', (req, res)->
vagrantFilePath = path.join __dirname, setting.garageFilePath, req.params.uuid
switch req.params.control
when 'up'
vagrant.up vagrantFilePath, (stdout, stderr)->
p stdout
res.send '0'
when 'delete'
garage.deleteVfile vagrantFilePath, req.params.uuid, (result)->
res.send result
else
res.send '-2'
app.post '/vagrantfile/new', (req, res)->
garage.newVagrantFile req.body, setting, (err, data)->
garage.addMachine2GarageFile setting, data
if err
res.send '-1'
else
res.send '0'
p 'localhost:' + setting.port
#app.listen setting.port
server.listen setting.port