-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
executable file
·155 lines (140 loc) · 4.58 KB
/
index.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env node
const express = require('express');
const app = express();
const execFile = require('child_process').execFile;
const fs = require('fs');
const EventEmitter = require('events');
const util = require('util');
const program = require('commander');
const querystring = require('querystring');
const https = require('https');
const url = require('url');
function MupAutodeployEmitter() {
EventEmitter.call(this);
}
util.inherits(MupAutodeployEmitter, EventEmitter);
const mupAutoDeployEmitter = new MupAutodeployEmitter();
function executeCommand(cmd, options) {
return new Promise(function (resolve, reject) {
execFile(cmd, options, function (err, stdout, stderr) {
if (err) {
reject(err);
} else {
resolve(stdout);
}
});
});
}
function sendHttpsPostRequest(protocol, hostname, path, postData) {
var options = {
protocol: protocol,
host: hostname,
path: path,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData)
}
};
var req = https.request(options);
req.write(postData);
req.end();
}
function sendSlackNotification(text) {
var dataString = 'payload=' + encodeURI('{"text": "' + text + '"}');
sendHttpsPostRequest(program.slack.protocol, program.slack.hostname, program.slack.path, dataString);
}
function emitLog(logTxt) {
mupAutoDeployEmitter.emit('log', logTxt);
}
function commandError(error) {
emitLog(error);
}
function locationExists(locationPath) {
return new Promise(function (resolve, reject) {
fs.access(locationPath, fs.F_OK, function (err) {
if (err) {
resolve(false);
} else {
resolve(true);
}
});
});
}
function deployProject() {
return new Promise(function (resolve, reject) {
emitLog('Starting deployment process....');
executeCommand('mup', ['deploy']).then(function (stdout) {
emitLog(stdout);
emitLog('Deployment process done.');
}, commandError);
});
}
app.post('/deploy', function (req, res) {
if (program.token && (!req.query.token || (req.query.token !== program.token))) {
return res.sendStatus(403);
} else {
res.sendStatus(200);
emitLog('Deployment triggered!');
var projectNameStartingIndex = program.gitUrl.lastIndexOf('/') + 1;
var projectNameEndingIndex = program.gitUrl.lastIndexOf('.git');
var branch = program.branch || 'master';
var projectName = program.gitUrl.substr(projectNameStartingIndex, projectNameEndingIndex - projectNameStartingIndex);
locationExists(projectName).then(function (exists) {
if (!exists) {
emitLog('Project has not been cloned yet. Cloning....');
executeCommand('git', ['clone', program.gitUrl]).then(function (stdout) {
emitLog(stdout);
emitLog('Done cloning');
emitLog('Checking out branch ' + branch + '....');
executeCommand('git', ['-C', projectName, 'checkout', 'origin/' + branch]).then(function () {
emitLog(stdout);
emitLog('Checked out branch ' + branch);
deployProject();
}, commandError);
}, commandError);
} else {
emitLog('Checking out branch ' + branch + '....');
executeCommand('git', ['-C', projectName, 'checkout', 'origin/' + branch]).then(function (stdout) {
emitLog(stdout);
emitLog('Checked out branch ' + branch);
emitLog('Pulling changes...');
executeCommand('git', ['-C', projectName, 'pull', 'origin', branch]).then(function () {
emitLog(stdout);
emitLog('Pulled changes');
deployProject();
}, commandError);
}, commandError);
}
});
}
});
function onMupAutoDeployLog(logTxt) {
if (program.verbose) {
console.log(logTxt);
}
if (program.slack) {
sendSlackNotification(logTxt);
}
}
program
.version('0.0.1')
.arguments('<git-url>')
.option('-t --token <secret-token>', 'application access token')
.option('-p, --port <port-number>', 'port to listen')
.option('-b, --branch <branch-name>', 'branch to checkout')
.option('-v, --verbose', 'display deployment information on standard output')
.option('-s, --slack <slack-hook-url>', 'send log to the given <slack-hook-url>')
.action(function (gitUrl) {
var port = program.port || 80;
program.gitUrl = gitUrl;
mupAutoDeployEmitter.on('log', onMupAutoDeployLog);
if (program.slack) {
program.slack = url.parse(program.slack);
}
app.listen(port);
})
.parse(process.argv);
if (!process.argv.slice(2).length) {
program.outputHelp();
}