-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
271 lines (232 loc) · 9.8 KB
/
app.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
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
var Q = require('q');
var ssh2Client = require('ssh2').Client;
var path = require('path');
var exec = require('child_process').exec;
var _initialized = Q.defer();
function Candyman(config) {
this.config = config || {};
initializeTargetDevices.call(this);
}
////////////////////////////////
///////// INITIALIZATION
////////////////////////////////
function initializeTargetDevices() {
var config = this.config;
config.targetDevices.forEach(function (d) {
//defaults
d.sshPort = d.sshPort || config.sshPort || 22;
d.projectName = d.projectName || config.projectName || 'myproject';
d.root = d.root + '/' + d.projectName;
d.user = d.user || config.user || 'root';
d.root = d.root || config.root || '/home/' + d.user;
console.log(d.root);
d.password = d.password || config.password;
d.startFile = d.startFile || config.startFile || 'app.js';
d.execRemoteAsync = function (command) {
var deferred = Q.defer();
var ssh2client = new ssh2Client();
var connectOptions = { host: d.hostname, port: d.sshPort, username: d.user, password: d.password };
ssh2client.on('ready', function () {
ssh2client.exec(command, function (err, stream) {
if (err) throw err;
stream
.on('close', function (code, signal) {
//console.log('close with code ' + code + ' and signal ' + signal);
ssh2client.end();
deferred.resolve();
})
.on('data', function (data) { console.log(data); })
.stderr.on('data', function (err) {
deferred.reject('Error executing command [' + command + '] with error [' + err + ']');
});
})
});
ssh2client.connect(connectOptions);
return deferred.promise;
};
d.execLocalAsync = function (command) {
var deferred = Q.defer();
exec(command, function (err, stdout, stderr) {
if (stdout) console.log(stdout);
if (stderr) console.log(stderr);
if (!err) deferred.resolve();
else deferred.reject(err);
});
return deferred.promise;
}
});
_initialized.resolve();
//TODO: assure target devices can be contacted (ping?)
}
////////////////////////////////
///////// TASKS
////////////////////////////////
Candyman.prototype.deploy = function () {
var task = 'deploy';
console.log('\nStarting ' + task + ' task\n============================');
var deviceTasks = this.config.targetDevices.map(function (d) {
return _initialized.promise
//make the remote directory
.then(function () {
return d.execRemoteAsync('mkdir -p ' + d.root); //make sure the directory exists
})
//copy files
//TODO: should not be hard coded... default to all .js and .json or allow config include or exclude
.then(function () {
return d.execLocalAsync('scp index.js config.js package.json ' + d.user + '@' + d.hostname + ':' + d.root)
})
//catch errors
.catch(function (err) {
console.log('Error in ' + task + ' task on ' + d.hostname + ': ' + err);
});
})
return Q.all(deviceTasks);
};
Candyman.prototype.packageRestore = function () {
var task = 'package-restore';
console.log('\nStarting ' + task + ' task\n============================');
var deviceTasks = this.config.targetDevices.map(function (d) {
return _initialized.promise
//make the remote directory
.then(function () {
d.execRemoteAsync('cd ' + d.root + '; npm install --production')
})
//we need the mraa library
.then(function () {
return d.execRemoteAsync('cp -r /usr/lib/node_modules/mraa ' + d.root + '/node_modules; ');
})
//catch errors
.catch(function (err) {
console.log('Error in ' + task + ' task on ' + d.hostname + ': ' + err);
});
})
return Q.all(deviceTasks);
};
Candyman.prototype.generateConfig = function () {
var task = 'generate-config';
console.log('\nStarting ' + task + ' task\n============================');
var deviceTasks = this.config.targetDevices.map(function (d) {
return _initialized.promise //make sure candyman is initialized
//
.then(function () {
var configText =
"module.exports = {" +
" deviceName: '" + d.devicename + "'" +
"}";
var command = 'echo "' + configText + '" > ' + d.root + '/config.js';
return d.execRemoteAsync(command)
})
//catch errors
.catch(function (err) {
console.log('Error in ' + task + ' task on ' + d.hostname + ': ' + err);
});
})
return Q.all(deviceTasks);
};
Candyman.prototype.setStartup = function () {
var task = 'set-startup';
console.log('\nStarting ' + task + ' task\n============================');
var deviceTasks = this.config.targetDevices.map(function (d) {
return _initialized.promise //make sure candyman is initialized
//
.then(function () {
var serviceText =
"[Unit]\n" +
" Description = Node startup app service for starting a node process\n" +
" After = mdns.service\n" +
"[Service]\n" +
" ExecStart = /usr/bin/node " + d.root + "/" + d.startFile + "\n" +
" Restart = on-failure\n" +
" RestartSec = 2s\n" +
"[Install]\n" +
" WantedBy=default.target\n";
var command = 'systemctl stop nodeup.service; ' +
'echo "' + serviceText + '" > /etc/systemd/system/nodeup.service; ' +
'systemctl daemon-reload; ' +
'systemctl enable nodeup.service; ' +
'systemctl start nodeup.service';
return d.execRemoteAsync(command)
})
//catch errors
.catch(function (err) {
console.log('Error in ' + task + ' task on ' + d.hostname + ': ' + err);
});
})
return Q.all(deviceTasks);
};
Candyman.prototype.restartService = function () {
var task = 'restart-service';
console.log('\nStarting ' + task + ' task\n============================');
var deviceTasks = this.config.targetDevices.map(function (d) {
return _initialized.promise //make sure candyman is initialized
//restart nodeup.service service
.then(function () {
var command = 'systemctl restart nodeup.service';
return d.execRemoteAsync(command)
})
//catch errors
.catch(function (err) {
console.log('Error in ' + task + ' task on ' + d.hostname + ': ' + err);
});
})
return Q.all(deviceTasks);
};
Candyman.prototype.reboot = function () {
var task = 'reboot';
console.log('\nStarting ' + task + ' task\n============================');
var deviceTasks = this.config.targetDevices.map(function (d) {
return _initialized.promise //make sure candyman is initialized
.then(function () {
var command = 'reboot';
return d.execRemoteAsync(command)
})
//catch errors
.catch(function (err) {
console.log('Error in ' + task + ' task on ' + d.hostname + ': ' + err);
});
})
return Q.all(deviceTasks);
};
Candyman.prototype.killProcesses = function () {
var task = 'kill-processes';
console.log('\nStarting ' + task + ' task\n============================');
var deviceTasks = this.config.targetDevices.map(function (d) {
return _initialized.promise //make sure candyman is initialized
.then(function () {
var command = 'kill -9 `ps | grep "' + d.projectName + '/' + d.startFile + '" | grep -v grep | awk \' { print $1 }\'`';
return d.execRemoteAsync(command)
})
//catch errors
.catch(function (err) {
console.log('Error in ' + task + ' task on ' + d.hostname + ': ' + err);
});
})
return Q.all(deviceTasks);
};
Candyman.prototype.execute = function () {
var task = 'execute';
console.log('\nStarting ' + task + ' task\n============================');
var deviceTasks = this.config.targetDevices.map(function (d) {
return _initialized.promise //make sure candyman is initialized
.then(function () {
var command = 'node ' + d.root + '/' + d.startFile;
return d.execRemoteAsync(command)
})
//catch errors
.catch(function (err) {
console.log('Error in ' + task + ' task on ' + d.hostname + ': ' + err);
});
})
return Q.all(deviceTasks);
};
////////////////////////////////
///////// FUNCTIONS
////////////////////////////////
function execCallback(err, stream) {
if (err) throw err;
stream
.on('close', function (code, signal) { console.log('Stream closed with code ' + code + ' and signal ' + signal); conn.end() })
.on('data', function (data) { console.log(data); })
.stderr.on('data', function (err) { console.log('!!!!!!!!!!!!!! Error: ' + err); });
}
module.exports = Candyman;