forked from Noctem/Monocle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.js
More file actions
123 lines (96 loc) · 2.87 KB
/
deploy.js
File metadata and controls
123 lines (96 loc) · 2.87 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
'use strict';
require('dotenv').config();
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var async =require('async');
var exec = require('child_process').exec;
var fs = require('fs');
var concurrency = parseInt(process.env.CONCURRENCY || 10);
var queue = async.queue(function(task, callback){
var head = task;
console.log("Deploying next: ", head.sn);
var commands = [];
var configFile;
if (head.server_port && head.config_file) {
configFile = '/tmp/monocle.config.'+head.server_port+'.py';
fs.writeFileSync('/tmp/monocle.config.'+head.server_port+'.py', head.config_file);
}
if (head.sn) {
commands.push(['SN',head.sn].join('='));
}
if (head.worker_name) {
commands.push(['WORKER_NAME',head.worker_name].join('='));
}
if (head.server_host) {
commands.push(['SERVER_HOST',head.server_host].join('='));
}
if (head.server_port) {
commands.push(['SERVER_PORT',head.server_port].join('='));
}
if (head.bootstrap) {
commands.push(['BOOTSTRAP',head.bootstrap].join('='));
}
if (head.no_pickle) {
commands.push(['NO_PICKLE',head.no_pickle].join('='));
}
if (configFile) {
commands.push(['CONFIG_FILE',configFile].join('='));
}
commands.push(['/usr/local/rbenv/shims/bundle exec cap production deploy']);
var child = exec(commands.join(' '), {
cwd: process.env.DEPLOY_CWD,
uid: parseInt(process.env.DEPLOY_UID),
gid: parseInt(process.env.DEPLOY_GID),
stdio:[
process.stdin,
process.stdout,
process.stderr,
],
}, function(error, stdout, stderr) {
if (!error) {
fs.unlinkSync(configFile);
}
console.log('stdout: ', stdout);
console.log('stderr: ', stderr);
if (error !== null) {
console.log('exec error: ', error);
}
setTimeout(function() {
callback();
}, 1000);
});
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
}, concurrency);
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/', function (req, res) {
console.log('/');
res.send({status:'Deployer active'});
});
app.post('/deploy', function (req, res) {
var deployConfig = {
sn: req.body.sn,
worker_name: req.body.worker_name,
server_host: req.body.server_host,
server_port: parseInt(req.body.server_port),
bootstrap: req.body.bootstrap,
no_pickle: req.body.no_pickle,
config_file: req.body.config_file,
};
console.log("Registering deploy: %s", deployConfig.worker_name);
queue.push(deployConfig, function(err) {
if (err) {
console.log('Error during processing %s.', deployConfig.sn, err);
} else {
console.log('Finished processing %s.', deployConfig.sn);
}
});
res.send({
message: "Queuing deployer",
config: deployConfig,
});
});
app.listen(5588, function () {
console.log('Monocle deployer listening on port 5588')
});