-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
107 lines (94 loc) · 3.27 KB
/
Copy pathserver.js
File metadata and controls
107 lines (94 loc) · 3.27 KB
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
var express = require('express')
var helmet = require('helmet')
var bodyParser = require('body-parser')
var config = require('config')
var request = require('request')
var ProcessPool = require('./processPool')
var workers = new ProcessPool(1, process.env.WQUEUE || 10)
var fs = require('fs')
var path = require('path')
var logfile = fs.createWriteStream('./logfile.txt', { flags: 'a' })
process.stdout.write = process.stderr.write = logfile.write.bind(logfile)
var scripts = {}
if (!config.has('scripts')) {
console.log('Error: config not found, or missing scripts value')
process.exitCode = 1
} else {
scripts = config.scripts
console.log('Configuration:')
console.log(JSON.stringify(scripts, null, 2))
var app = express()
app.use(helmet())
app.use(helmet.hidePoweredBy())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
var router = express.Router()
app.set('port', process.env.PORT || process.argv[2] || 5000)
router.get('/:token', function (req, res) {
var token = req.params.token
var script = token && scripts[token]
if (script) {
res.sendFile(path.join(__dirname, './logfile.txt'))
}
})
router.post('/:token', function (req, res, next) {
if (req.body) { console.log('Request: ' + JSON.stringify(req.body, null, 2)) }
var token = req.params.token
var script = token && scripts[token]
if (script) {
workers.acquire('sh', [script], function (err, worker) { // get new worker process to execute script
if (err) {
return res.status(500).send({error: 'Waiting queue length exceded'})
}
if (worker) {
var dhm = new Date()
console.log('>>>>>>>>>>> ' + token)
console.log('[' + token + '] spawn process')
console.log(dhm.toString())
worker.stdout.pipe(process.stdout)
worker.stderr.pipe(process.stderr)
worker.on('error', function (err) {
console.log('Failed to start child process: ' + script + ' with error: ' + err)
workers.release(worker)
})
worker.on('close', function (code) {
process.stdout.write('[' + token + '] child process exited with code ' + code + '\n')
if (req.body && req.body.callback_url) {
var options = {
json: true,
body: {state: 'success'},
method: 'POST',
url: req.body.callback_url
}
console.log('sending callback response')
request(options, function (err, response, body) {
if (err) {
console.error(err)
}
})
}
console.log('<<<<<<<<<<< ' + token + '\n')
workers.release(worker)
})
}
})
}
return res.status(204).send()
})
app.use(router)
app.get('*', function (req, res, next) { // handle non existing routes
var err = new Error()
err.status = 404
next(err)
})
app.use(function (err, req, res, next) {
if (err.status !== 404) {
return next()
}
res.status(404).send('Ressource not found')
})
app.listen(app.get('port'), function () {
console.log('Dockerhook server listening on port ' + app.get('port'))
})
module.exports = app
}