-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.coffee
353 lines (266 loc) · 7.45 KB
/
index.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
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
(require 'cson-config').load()
colors = require 'colors'
express = require 'express'
util = require 'util'
url = require 'url'
net = require 'net'
GLOBAL.config = process.config
config = process.config
Drekmore = require './lib/drekmore'
tokenParser = require './lib/tokenParser'
Book = require './lib/book'
require 'coffee-trace'
dm = new Drekmore(config)
book = new Book(config)
logstashUrl = url.parse config.logstash
logs = require('./lib/logs') config.elasticsearch
app = express()
app.use (req, res, next)->
console.log "#{req.method} #{req.path}"
next()
app.use express.urlencoded()
app.use express.json()
app.use express.logger()
#app.use express.bodyParser()
app.use express.static __dirname + "/public"
app.use tokenParser
app.use app.router
app.use (err, req, res, next) ->
err = message: err if typeof err is 'string'
express.errorHandler()(err, req, res, next)
sanitizeApp = (app) ->
app += '.git' unless app.match /\.git/
app
###
List application processes
:app - application name
:branch - branch name
###
app.get '/apps/:app/:branch/ps', (req, res, next) ->
app = sanitizeApp req.params.app
branch = req.params.branch
dm.findInstances app, branch, (err, instances) ->
return next err if err
res.json instances
###
Stop all processes
:app - application name
:branch - branch name
###
app.get '/apps/:app/:branch/ps/stop', (req, res, next) ->
app = sanitizeApp req.params.app
branch = req.params.branch
dm.stopApplication app, branch, () ->
res.json
status: 'ok'
message: 'Asi sem je zabil, ale nekontroloval jsem to'
###
Restart all processes
:app - application name
:branch - branch name
###
app.get '/apps/:app/:branch/ps/restart', (req, res, next) ->
app = sanitizeApp req.params.app
branch = req.params.branch
dm.restart app, branch, (done) ->
res.json
status: 'ok'
message: 'Naplanovan restart'
###
Start new process
:app - application name
:branch - branch
###
app.post '/apps/:app/:branch/ps', (req, res, next) ->
# console.log 'xxxxxxx'
app = sanitizeApp req.params.app
branch = req.params.branch
command = req.body.command
userEnv = req.body.env
dm.runProcessRendezvous app, branch, command, userEnv, (err, process) ->
return next err if err
res.json process
###
Get application scaling
:app - application name
:branch - branch
###
app.get '/apps/:app/:branch/ps/scale', (req, res, next) ->
app = sanitizeApp req.params.app
branch = req.params.branch
dm.getConfig app, branch, (err, config) ->
console.log arguments
return next err if err
br = config.branches[branch]
res.json br.scale
###
Scale
:app - application name
:branch - branch
:scales - {"web": 2, "daemon": 4}
###
app.post '/apps/:app/:branch/ps/scale', (req, res, next) ->
return next "Missing scales" unless req.body.scales?
app = sanitizeApp req.params.app
branch = req.params.branch
scales = req.body.scales
dm.setScaling app, branch, scales, (err, done) ->
return next err if err
res.json done
app.all '/git/:repo/*', (req, res, next) ->
return next 'Unauthorized' if req.token isnt 'cM7I84LFT9s29u0tnxrvZaMze677ZE60'
next()
###
Build revision from git
Private! githook only
###
app.post '/git/:repo/:branch/:rev', (req, res, next) ->
p = req.params
dm.buildStream req, p.repo, p.branch, p.rev, (err, build)->
if err
console.log err
res.write " Sorry, there is no Todie to serve, please try it again later...".yellow
res.write "\n"
res.end "94ed473f82c3d1791899c7a732fc8fd0_exit_404\n"
return
endIsHandled = no
build.on 'data', (data) ->
if data.msg
res.write data.msg
else if data.result
endIsHandled = yes # prevent failure handler
dm.saveBuild data.result, (err, data) ->
console.log "------------------------", arguments
if err
console.log err
res.end "94ed473f82c3d1791899c7a732fc8fd0_exit_1\n"
return
res.write "-----> Build stored: v#{data.version}\n"
res.end "94ed473f82c3d1791899c7a732fc8fd0_exit_0\n"
else if data.exitCode?
res.end "94ed473f82c3d1791899c7a732fc8fd0_exit_#{data.exitCode}\n"
else
console.log "Unknown data from toadwart", data
build.on 'end', (exitCode) ->
return if endIsHandled
exitCode = 1 unless exitCode
res.end "94ed473f82c3d1791899c7a732fc8fd0_exit_#{exitCode}\n"
build.on 'error', (error)->
console.log "toadie build error: ",error
res.write " Toadie rejected connection, please try it again later...\n"
res.end "94ed473f82c3d1791899c7a732fc8fd0_exit_1\n"
build.run()
###
###
app.get '/toadwart/register/:ip/:port', (req, res, next) ->
p = req.params
dm.registerToadwart p.ip, p.port, (err, done) ->
return next err if err
res.json done
app.get '/toadwart/unregister/:id', (req, res, next)->
dm.unRegisterToadwart req.params.id, (err, done)->
return next err if err
res.json {done: yes}
app.get '/toadwarts', (req, res, next)->
dm.getToadwartsStatus yes, (err, data)->
return next err if err
res.json data
app.get '/datacenters', (req, res, next)->
res.json "mrdka": yes
app.get '/datacenter/register/:name', (req, res, next)->
dm.registerDatacenter req.params.name, [], (err, done)->
return next err if err
res.json done
###
List all applications
###
app.get '/apps/', (req, res, next) ->
dm.listApps (err, applications) ->
return next err if err
res.json applications
###
Get application logs
TODO: branch
:app - application name
:branch - branch name
:worker - worker
###
app.get '/apps/:app/:branch/:worker/logs', (req, res, next) ->
###
app = sanitizeApp req.params.app
branch = req.params.branch
tail = req.query.tail
start = new Date().getTime() * 1000
util.log util.inspect tail
# TODO test only
book.getLogs app, branch, res
###
options =
app: req.params.app
worker: req.params.worker
lines: req.query.n || 100
logs options, (err, data) ->
return next err if err
result = data.map format
res.end result.join('')
###
#Get application logs tail
#TODO: branch
:app - application name
:branch - branch name
:worker - worker
###
app.get '/apps/:app/:branch/:worker/tail', (req, res) ->
app = req.params.app
worker = req.params.worker
util.log "Tail request start"
interval = setInterval () ->
# Send some data for keeping socket alive
res.write new Buffer [0x00]
, 30000
res.on 'close', () ->
clearInterval interval
connected = no
client = new net.Socket()
client.connect logstashUrl.port, logstashUrl.hostname, ->
connected = yes
filter =
filter:
app: app
worker: worker
console.log filter
client.write JSON.stringify filter
#TODO: json stream parse
client.on 'data', (data) ->
try
msg = JSON.parse data.toString()
res.write format msg
catch err
util.log 'Invalid data: ' + data
client.on 'end', ->
res.end()
client.on 'error', (err) ->
util.log util.inspect err
res.end()
req.on 'close', ->
util.log "Tail request close"
client.end() if connected
req.on 'error', (err) ->
util.log util.inspect err
client.end() if connected
###
#Get application release log
:app - application name
###
app.get '/apps/:app/:branch/releases/:release?', (req, res) ->
app = sanitizeApp req.params.app
branch = req.params.branch
release = req.params.release
dm.getReleases app, branch, release, (err, releases) ->
return next err if err
res.json releases
format = (msg) ->
console.log msg
"#{msg['@timestamp']} #{msg['source'] || 'app'}[#{msg['worker']}]: #{msg['message']}\n"
app.listen config.port
util.log "server listening on #{config.port}"