-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.js
131 lines (108 loc) · 3.14 KB
/
deploy.js
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
var child_process = require('child_process'),
config = JSON.parse(fs.readFileSync('config.json')),
os = require('os');
var monitors = {
}
var notifierTypes = {
"irc": function(opts) {
var Client = require('irc').Client, bot = new Client(opts.server, opts.nick || 'sfu-bot');
return function(notification, done) {
opts.channels.forEach(function(channel) {
bot.join(channel);
bot.say(channel, notification.body);
bot.part(channel);
});
done();
}
},
"email": function(opts) {
var mailer = require("nodemailer").createTransport(opts.transport, opts.settings)
return function(notification, done) {
var mailOptions = {
from: opts.from, // sender address
to: opts.to.join(', '), // list of receivers
subject: (opts.prefix ? opts.prefix + ' ' : '') + notification.title, // Subject line
text: notification.body, // plaintext body
//html: "<b>Hello world ✔</b>" // html body
}
mailer.sendMail(mail, done);
}
}
}
var notifiers = config.notifications.map(function(notifier) {
return notifierTypes[notifier.type](notifier.settings);
});
function notify(notification, done) {
async.forEach(notifiers, function(notifier, next) {
notifier(notification, next);
}, done);
}
function deploy(entry) {
var path = config.path+'/'+entry.id, branch = entry.branch || 'deploy';
async.series([
function git(next) {
fs.exists(path+'/.git', function(exists) {
if (exists)
child_process.execFile('git', ['pull', '--recurse-submodules', 'origin', branch], { cwd: path }, next)
else
child_process.execFile('git', ['clone', '--recursive', '--branch', branch, url], { cwd: path }, next)
});
},
function setup(next) {
child_process.execFile('npm', ['install'], { cwd: path }, next);
}
function spawn(next) {
if (monitors[entry.id]) {
monitors[entry.id].restart();
next();
}
else {
var monitor = new (forever.Monitor)(path+'/server.js', {
max: os.cpus.length,
silent: true,
options: [ ],
env: { DEPLOY_NAME: entry.id, NODE_ENV: 'production' },
cwd: path,
logFile: 'xx',
outFile: 'yy',
errFile: 'zz'
});
monitor.on('start', function() {
}).on('exit', function() {
}).on('error', function(err) {
notify({ title: 'Process Error '+entry.id, body: err })
});
monitors[entry.id] = monitor;
next();
}
}
], function(err) {
if (err) {
notify({ title: 'Error Deploying '+entry.id, body: err })
}
});
}
// GitHub hooks
(function github() {
var http = require('http'), express = require('express'), app = express();
// Verifier for github's signature
function verify(req, res, buf) {
if (!req.headers['x-hub-signature']) return;
var signature = req.headers['x-hub-signature'],
hmac = crypto.createHmac('sha1', secret);
if (hmac.update(chunk).digest('hex') !== hmac.digest('hex'))
throw new Error();
}
// Body parsers
app.use(express.json({ verify: verify }));
app.use(express.urlencoded({ verify: verify }))
// Route handler
app.post('/', function(req, res) {
console.log(req.body);
res.send(200);
});
// Listen
http.createServer(app).listen(0, function() {
this.address()
});
})()