/
deploy.js
322 lines (279 loc) · 9.65 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
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
var Tessel = require('./tessel');
var commands = require('./commands');
var logs = require('../logs');
var fs = require('fs');
var path = require('path');
var tar = require('tar');
var Ignore = require('fstream-ignore');
var PUSH_START_SCRIPT_NAME = 'start';
var NODE_PUSH_SCRIPT = __dirname + '/../../resources/start_node_script.sh';
// Used to store local functionality and allow
// exporting those definitions for testing.
var actions = {};
/*
Run a script on a Tessel
param opts: unused so far
*/
Tessel.prototype.deployScript = function(opts) {
var self = this;
// Only an _explicit_ `true` will set push mode
var isPush = opts.push === true;
return new Promise(function(resolve, reject) {
var filepath = isPush ? Tessel.PUSH_PATH : Tessel.RUN_PATH;
// Stop running any existing scripts
return actions.execRemoteCommand(self, 'stopRunningScript', filepath)
.then(function() {
var prom;
if (opts.single) {
// Always be sure the appropriate dir is created
prom = actions.execRemoteCommand(self, 'createFolder', filepath);
} else {
// Delete any code that was previously at this file path
prom = actions.execRemoteCommand(self, 'deleteFolder', filepath);
// Create the folder again
prom = prom.then(function() {
return actions.execRemoteCommand(self, 'createFolder', filepath);
});
}
// Bundle and send tarred code to T2
return prom.then(function() {
return actions.sendBundle(self, filepath, opts);
})
.then(function(script) {
if (isPush) {
// Push the script into flash
return actions.pushScript(self, script).then(resolve);
} else {
// Push the code into ram
return actions.runScript(self, filepath, script).then(resolve);
}
});
})
.catch(reject);
});
};
Tessel.prototype.restartScript = function(opts) {
var self = this;
var isPush = opts.type === 'flash';
var filepath = isPush ? Tessel.PUSH_PATH : Tessel.RUN_PATH;
return new Promise(function(resolve, reject) {
return self.simpleExec(commands.readFile(filepath + opts.entryPoint))
.then(function() {
if (isPush) {
// Start the script from flash memory
return actions.startPushedScript(self, opts.entryPoint)
.then(resolve).catch(reject);
} else {
// Start the script in RAM
return actions.runScript(self, filepath, opts.entryPoint)
.then(resolve).catch(reject);
}
})
.catch(function(error) {
if (error.message.indexOf('No such file or directory') !== -1) {
error = '"' + opts.entryPoint + '" not found on ' + self.name;
}
return reject(error);
});
});
};
actions.execRemoteCommand = function(tessel, command, filepath) {
return tessel.simpleExec(commands[command](filepath))
.catch(function() {});
};
actions.findProject = function(opts) {
return new Promise(function(resolve, reject) {
var single = opts.single;
var file = opts.entryPoint;
var home = process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'];
var checkPkgJson = false;
var isDirectory = false;
// Addresses an encountered edge case where
// paths wouldn't resolve correctly:
//
// > fs.realpathSync("~/foo");
// Error: ENOENT, no such file or directory '/Users/me/foo/~'
// > path.dirname("~/foo")
// '~'
// > path.resolve("~/foo")
// '/Users/me/foo/~/foo'
//
// ...And so on...
//
if (/^~/.test(file)) {
file = file.replace(/^~/, home);
}
try {
// This will throw if the file or directory doesn't
// exist. The cost of the try/catch is negligible.
isDirectory = fs.lstatSync(file).isDirectory();
} catch (error) {
reject(error.message);
}
if (isDirectory && single) {
return reject('You can only push a single file, not a directory');
}
if (isDirectory) {
file = path.join(file, 'index.js');
checkPkgJson = true;
}
var pushdir = fs.realpathSync(path.dirname(file));
var relpath = '';
while (path.dirname(pushdir) !== pushdir &&
!fs.existsSync(path.join(pushdir, 'package.json'))) {
relpath = path.join(path.basename(pushdir), relpath);
pushdir = path.dirname(pushdir);
}
if (path.dirname(pushdir) === pushdir) {
reject('Invalid project directory');
}
var program = path.join(pushdir, relpath, path.basename(file));
var pkgJson = '';
var basename = '';
if (checkPkgJson && !single) {
pkgJson = require(path.join(pushdir, 'package.json'));
if (pkgJson.main) {
basename = path.basename(program);
program = path.normalize(program.replace(basename, pkgJson.main));
}
}
resolve({
pushdir: pushdir,
program: program,
entryPoint: path.join(relpath, path.basename(program)),
});
});
};
actions.sendBundle = function(tessel, filepath, opts) {
return new Promise(function(resolve, reject) {
// Execute the remote untar process command
return tessel.connection.exec(commands.untarStdin(filepath))
// Once the process starts running
.then(function(remoteProcess) {
actions.findProject(opts).then(function(project) {
opts.target = path.resolve(process.cwd(), project.pushdir);
opts.resolvedEntryPoint = project.entryPoint;
actions.tarBundle(opts).then(function(bundle) {
// RAM or Flash for log
var memtype;
if (opts.push) {
memtype = 'Flash';
} else {
memtype = 'RAM';
}
// Log write
logs.info('Writing %s to %s on %s (%d kB)...', project.entryPoint, memtype, tessel.name, bundle.length / 1000);
tessel.forward(remoteProcess).then(function() {
logs.info('Deployed.');
resolve(project.entryPoint);
}).catch(reject);
// Write the code bundle to the hardware
remoteProcess.stdin.end(bundle);
});
}).catch(reject);
});
});
};
actions.tarBundle = function(opts) {
var target = opts.target || process.cwd();
return new Promise(function(resolve, reject) {
var buffers = [];
var pack = tar.Pack();
var fstream = new Ignore({
path: target,
ignoreFiles: ['.tesselignore']
});
if (opts.single) {
fstream.addIgnoreRules(['*', '!' + opts.resolvedEntryPoint]);
}
fstream.basename = '';
pack._noProprietary = true;
// This ensures that the remote root directory
// is the same level as the directory containing
// our program entry-point files.
fstream.on('entry', function(entry) {
entry.root = {
path: entry.path
};
});
// Send the ignore-filtered file stream into the tar packer
fstream.pipe(pack)
.on('data', function(chunk) {
buffers.push(chunk);
})
.on('error', function(data) {
reject(data);
})
.on('end', function() {
resolve(Buffer.concat(buffers));
});
});
};
actions.runScript = function(t, filepath, entryPoint) {
logs.info('Running %s...', entryPoint);
return new Promise(function(resolve) {
return t.connection.exec(commands.runScript(filepath, entryPoint))
.then(function(remoteProcess) {
// When the stream closes
remoteProcess.once('close', resolve);
// Pipe data and errors
remoteProcess.stdout.pipe(process.stdout);
remoteProcess.stderr.pipe(process.stderr);
});
});
};
actions.pushScript = function(t, entryPoint) {
// Write the node start file
return actions.writeToFile(t)
.then(function start() {
// Then start the script
return actions.startPushedScript(t, entryPoint);
});
};
actions.writeToFile = function(t) {
return new Promise(function(resolve, reject) {
// Path of the script to run a Node project
var executablePath = path.join(Tessel.PUSH_PATH, PUSH_START_SCRIPT_NAME);
// Open a stdin pipe tp the file
return t.connection.exec(commands.openStdinToFile(executablePath))
// If it was opened successfully
.then(function(remoteProcess) {
// When the remote process finishes
remoteProcess.stdin.once('finish', function() {
// Set the perimissions on the file to be executable
return t.connection.exec(commands.setExecutablePermissions(executablePath))
.then(function(remoteProcess) {
// When that process completes
remoteProcess.once('close', function() {
// Let the user know
logs.info('You may now disconnect from the Tessel. Your code will be run whenever Tessel boots up. To remove this code, use `tessel erase`.');
return resolve();
});
});
});
// Read the contents of the node start script
fs.readFile(NODE_PUSH_SCRIPT, function(err, data) {
if (err) {
return reject(err);
}
// Write the Node start script
remoteProcess.stdin.end(data);
});
});
});
};
actions.startPushedScript = function(tessel, entryPoint) {
return new Promise(function(resolve) {
// Once it has been written, run the script with Node
return tessel.connection.exec(commands.startPushedScript())
.then(function(remoteProcess) {
return tessel.forward(remoteProcess).then(function() {
logs.info('Running %s...', entryPoint);
return resolve();
});
});
});
};
if (global.IS_TEST_ENV) {
module.exports = actions;
}