-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.js
330 lines (323 loc) · 9.94 KB
/
Utils.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
var gutil = require('gulp-util');
var when = require('when');
var inquirer = require('inquirer');
var _ = require('underscore');
var runSequence = require('run-sequence');
var rsync = require('./lib/rsync');
var remoteExec = require('./lib/remoteExec');
var escapeShell = require('./lib/escapeShell');
function subLog(message) {
gutil.log('... '+message);
}
function logDone() {
subLog(gutil.colors.green('Done'));
}
var Utils = function(args) {
this.args = args;
}
_.extend(Utils.prototype, {
/**
* Ask yes/no question to user.
*
* Side effects:
* - if 'silent' command line param is enabled, the prompt will be skipped and the promised resolved.
*
* @param {string} message Message to display
* @param {boolean} def Default choice if user enters nothing (false if not specified)
* @param {boolean} silent If true, skip user input and just resolve the promise.
* @return {Promise} Resolves if user answers Yes. Rejects if its a No.
*/
confirm: function(message, def, silent) {
var args = this.args;
return when.promise(function(resolve, reject) {
if (silent || args.silent) {
gutil.log("Skipping prompt: " + message);
resolve();
return;
}
inquirer.prompt([{
type: 'confirm',
name: 'answer',
message: message,
default: (def === void 0) ? false : def,
}], function(answers) {
if (answers.answer) {
resolve();
}
else {
reject();
}
})
});
},
/**
* Stops a remote upstart
*
* @param {string} connStr Remote SH connection string of host/user
* @param {string} upstartName Name of upstart to restart on remote host
* @return {Promise}
*/
stopRemoteUpstart: function(connStr, upstartName) {
gutil.log('Stopping ' + gutil.colors.cyan(upstartName) + '...');
var unknownJob = false;
return remoteExec(connStr, "stop "+escapeShell(upstartName), { noLog: true })
.catch(function(err) {
if (/Unknown Instance/i.test(err)) {
// Ignore Unknown instance error
return when.resolve();
}
else if (/Unknown job/i.test(err)) {
// Ignore unknown job error but give the user a warning
unknownJob = true;
return when.resolve();
}
else {
subLog(gutil.colors.red(err));
return when.reject(err);
}
})
.then(function() {
if (unknownJob) {
subLog(gutil.colors.yellow('Warning: Upstart script "' + upstartName + '" does not exist. Could not stop. Moving on anyway...'));
}
else {
logDone();
}
});
},
/**
* Start a remote upstart
*
* @param {string} connStr Remote SH connection string of host/user
* @param {string} upstartName Name of upstart to restart on remote host
* @return {Promise}
*/
startRemoteUpstart: function(connStr, upstartName) {
gutil.log('Starting '+gutil.colors.cyan(upstartName)+'...');
var unknownJob = false;
return remoteExec(connStr, "start "+escapeShell(upstartName), { noLog: true })
.catch(function(err) {
if (/Unknown job/i.test(err)) {
// Ignore unknown job error but give the user a warning
unknownJob = true;
return when.resolve();
}
else {
subLog(gutil.colors.red(err));
return when.reject(err);
}
})
.then(function() {
if (unknownJob) {
subLog(gutil.colors.yellow('Warning: Upstart script "' + upstartName + '" does not exist. Could not start. Moving on anyway...'));
}
else {
logDone();
}
});
},
/**
* Restarts a remote upstart
*
* @param {string} connStr Remote SH connection string of host/user
* @param {string} upstartName Name of upstart to restart on remote host
* @return {Promise}
*/
restartRemoteUpstart: function(connStr, upstartName) {
gutil.log('Restarting '+gutil.colors.cyan(upstartName)+'...');
return remoteExec(connStr, [
"stop "+escapeShell(upstartName),
"start "+escapeShell(upstartName)
], { noBail: true })
.then(function() {
logDone();
});
},
/**
* Creates a directory structure on the remote host if it doesn't already exist
*
* Does a 'mkdir -p'
*
* @param {string} connStr Remote SSH connection string of host/user
* @param {string} directory Absolute directory
* @return {Promise}
*/
removeRemoteDirectory: function(connStr, directory) {
gutil.log('Removing directory (if not already there): ' + gutil.colors.magenta(directory) + '...');
return remoteExec(connStr, 'rm -fr '+escapeShell(directory))
.then(function() {
logDone();
});
},
/**
* Creates a directory structure on the remote host if it doesn't already exist
*
* Does a 'mkdir -p'
*
* @param {string} connStr Remote SSH connection string of host/user
* @param {string} directory Absolute directory
* @return {Promise}
*/
moveRemote: function(connStr, src, dst) {
gutil.log('Moving ' + gutil.colors.magenta(src) + ' to ' + gutil.colors.magenta(dst) + '...');
return remoteExec(connStr, 'mv ' +escapeShell(src) + ' ' + escapeShell(dst), { noLog: true })
.then(function() {
logDone();
});
},
/**
* Creates a directory structure on the remote host if it doesn't already exist
*
* Does a 'mkdir -p'
*
* @param {string} connStr Remote SSH connection string of host/user
* @param {string} directory Absolute directory
* @return {Promise}
*/
ensureRemoteDirectory: function(connStr, directory) {
gutil.log('Creating directory (if not already there): ' + gutil.colors.magenta(directory) + '...');
return remoteExec(connStr, 'mkdir -p '+escapeShell(directory))
.then(function() {
logDone();
});
},
remoteSymlink: function(connStr, src, dst) {
gutil.log('Symlinking ' + gutil.colors.magenta(src) + ' to ' + gutil.colors.magenta(dst) + ' ...');
return remoteExec(connStr, [
'rm -f '+escapeShell(dst),
'ln -sf '+escapeShell(src)+' '+escapeShell(dst)
])
.then(function() {
logDone();
});
},
remoteNpmInstall: function(connStr, directory) {
gutil.log('NPM Installing at ' + gutil.colors.magenta(directory) + '...');
return remoteExec(connStr, [
'cd ' + escapeShell(directory),
'if [ -f .nvmrc ] && ( hash nvm 2>/dev/null ); then',
' nvm install',
' echo "### NVM INSTALL INVOKED ###"',
'fi',
'echo "### NODE VERSION: $(node --version) ###"',
'echo "### NPM VERSION: $(npm --version) ###"',
'npm install --production'
])
.then(function(result) {
var nodeMatches = result.stdout.match(/### NODE VERSION: (.*) ###/);
var npmMatches = result.stdout.match(/### NPM VERSION: (.*) ###/);
var nvmMatches = result.stdout.match(/### NVM INSTALL INVOKED ###/);
if (nvmMatches) {
subLog('NVM Install Invoked');
}
if (nodeMatches) {
subLog('Node Version: '+nodeMatches[1]);
}
if (npmMatches) {
subLog('NPM Version: '+npmMatches[1]);
}
logDone();
});
},
/**
* Do standard rsync from local srcPath to remote destPath.
*
* @param {string} srcPath Local source path
* @param {string} destConnStr Remote SSH connection string of host/user
* @param {string} destPath Remote destination path
* @return {Promise}
*/
rsyncApp: function(srcPath, destConnStr, destPath) {
gutil.log('Rsyncing...');
return rsync({
ssh: true,
src: srcPath,
dest: destConnStr+':'+destPath,
recursive: true,
delete: true,
args: ['--verbose'],
exclude: [
".git*",
"node_modules/",
".DS_Store",
"README.md",
"environment.json",
"environment.*.json",
"/config.js",
"config.*.js",
"deploy-config.js"
]
}).then(function() {
logDone();
});
},
/**
* Pushes a file from a local path to a remote path
*
* @param {string} srcPath Local source path
* @param {string} destConnStr Remote SSH connection string of host/user
* @param {string} destPath Remote destination path
* @return {Promise}
*/
pushFile: function(srcPath, destConnStr, destPath) {
gutil.log('Pushing config...');
return rsync({
ssh: true,
src: srcPath,
dest: destConnStr+':'+destPath,
args: ['--verbose']
}).then(function() {
logDone();
});
},
/**
* Pulls a file from a remote path to a local path
*
* @param {string} srcConnStr Remote SSH connection string of host/user
* @param {string} srcPath Remote source path
* @param {string} destPath Local destination path
* @return {Promise}
*/
pullFile: function(srcConnStr, srcPath, destPath) {
gutil.log('Pulling config to ' + gutil.colors.magenta(destPath) + '...');
return rsync({
ssh: true,
src: srcConnStr+':'+srcPath,
dest: destPath,
args: ['--verbose']
}).then(function() {
logDone();
});
},
streamToPromise: function(stream) {
return when.promise(function(resolve, reject){
stream.on('end', function() {
resolve();
});
});
},
/**
* Call runSequence wrapped in a promise.
*
* Call this just like runSequence() but do not include the callback parameter.
*
* @return {Promise}
*/
runSequencePromise: function(task1, task2, task3 /* ... */) {
var args = Array.prototype.slice.call(arguments);
return when.promise(function(resolve, reject) {
// Push the runSequence callback (must be last argument)
args.push(function(err, result) {
if (err) {
reject(err);
}
else {
resolve(result);
}
});
// Call it
runSequence.apply(global, args);
});
}
});
module.exports = Utils;