Skip to content

Commit

Permalink
Deploy: t2 push will deploy a shell script containing the correct nam…
Browse files Browse the repository at this point in the history
…e corresponding to the deployed project's entry point. Fixes gh-460
  • Loading branch information
rwaldron committed Nov 25, 2015
1 parent ead744c commit 4dbe3cd
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 24 deletions.
41 changes: 21 additions & 20 deletions lib/tessel/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var path = require('path');

// Third Party Dependencies
var browserify = require('browserify');
var tags = require('common-tags');
var fs = require('fs-extra');
var fsTemp = require('fs-temp');
var glob = require('glob');
Expand All @@ -17,7 +18,6 @@ var logs = require('../logs');
var Tessel = require('./tessel');

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.
Expand Down Expand Up @@ -164,7 +164,7 @@ Tessel.prototype.restartScript = function(opts) {
.then(function() {
if (isPush) {
// Start the script from flash memory
return actions.startPushedScript(self, opts.entryPoint)
return actions.startPushedScript(self, opts.entryPoint, opts)
.then(resolve).catch(reject);
} else {
// Start the script in RAM
Expand Down Expand Up @@ -556,9 +556,11 @@ actions.tarBundle = function(opts) {
};

actions.runScript = function(t, filepath, entryPoint, opts) {
var actualEntryPointName = opts.slim ? opts.slimPath : entryPoint;

logs.info('Running %s...', opts.slim ? 'bundled project' : entryPoint);
return new Promise(function(resolve) {
return t.connection.exec(commands.runScript(filepath, opts.slim ? opts.slimPath : entryPoint))
return t.connection.exec(commands.runScript(filepath, actualEntryPointName))
.then(function(remoteProcess) {

// When the stream closes
Expand All @@ -572,26 +574,27 @@ actions.runScript = function(t, filepath, entryPoint, opts) {
};

actions.pushScript = function(t, entryPoint, opts) {
var actualEntryPointName = opts.slim ? opts.slimPath : entryPoint;
// Write the node start file
return actions.writeToFile(t)
return actions.writeToFile(t, actualEntryPointName)
.then(function start() {
// Then start the script
return actions.startPushedScript(t, opts.slim ? opts.slimPath : entryPoint);
return actions.startPushedScript(t, actualEntryPointName, opts);
});
};

actions.writeToFile = function(t) {
return new Promise(function(resolve, reject) {
actions.writeToFile = function(t, entryPoint) {
return new Promise(function(resolve) {
// Path of the script to run a Node project
var executablePath = path.join(Tessel.REMOTE_PUSH_PATH, PUSH_START_SCRIPT_NAME);
var shellScriptPath = path.join(Tessel.REMOTE_PUSH_PATH, PUSH_START_SCRIPT_NAME);
// Open a stdin pipe tp the file
return t.connection.exec(commands.openStdinToFile(executablePath))
return t.connection.exec(commands.openStdinToFile(shellScriptPath))
// 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))
return t.connection.exec(commands.setExecutablePermissions(shellScriptPath))
.then(function(remoteProcess) {
// When that process completes
remoteProcess.once('close', function() {
Expand All @@ -602,25 +605,23 @@ actions.writeToFile = function(t) {
});
});

// 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);
});
var shellScript = tags.stripIndent `
#!/bin/sh
cd /app
exec node ${entryPoint}
`;
remoteProcess.stdin.end(new Buffer(shellScript.trim()));
});
});
};

actions.startPushedScript = function(tessel, entryPoint) {
actions.startPushedScript = function(tessel, entryPoint, opts) {
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.receive(remoteProcess).then(function() {
logs.info('Running %s...', entryPoint);
logs.info('Running %s...', opts.slim ? 'bundled project' : entryPoint);
return resolve();
});
});
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"bluebird": "^2.9.6",
"browserify": "^11.2.0",
"colors": "^1.1.0",
"common-tags": "0.0.1",
"debug": "^2.2.0",
"envfile": "^1.0.0",
"fs-extra": "^0.18.0",
Expand Down
3 changes: 0 additions & 3 deletions resources/start_node_script.sh

This file was deleted.

3 changes: 2 additions & 1 deletion test/.jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"cli": true,
"LAN": true,
"TesselSeeker": true,
"USB": true
"USB": true,
"tags": true
}
}
1 change: 1 addition & 0 deletions test/common/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ global.osenv = require('osenv');
global.sinon = require('sinon');
global.sshpk = require('sshpk');
global.ssh = require('ssh2');
global.tags = require('common-tags');
global.tar = require('tar');
global.uglify = require('uglify-js');

Expand Down
43 changes: 43 additions & 0 deletions test/unit/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,49 @@ exports['Tessel.prototype.deployScript'] = {
}.bind(this));
},

writeToFileDefaultEntryPoint: function(test) {
test.expect(1);

var shellScript = tags.stripIndent `
#!/bin/sh
cd /app
exec node index.js
`;
this.end.restore();
this.end = sandbox.stub(this.tessel._rps.stdin, 'end', function(buffer) {
test.equal(buffer.toString(), shellScript);
test.done();
}.bind(this));

this.exec = sandbox.stub(this.tessel.connection, 'exec', function() {
return Promise.resolve(this.tessel._rps);
}.bind(this));

deploy.writeToFile(this.tessel, 'index.js');
},


writeToFileSendsCorrectEntryPoint: function(test) {
test.expect(1);

var shellScript = tags.stripIndent `
#!/bin/sh
cd /app
exec node __tessel_program__.js
`;
this.end.restore();
this.end = sandbox.stub(this.tessel._rps.stdin, 'end', function(buffer) {
test.equal(buffer.toString(), shellScript);
test.done();
}.bind(this));

this.exec = sandbox.stub(this.tessel.connection, 'exec', function() {
return Promise.resolve(this.tessel._rps);
}.bind(this));

deploy.writeToFile(this.tessel, '__tessel_program__.js');
},

processCompletionOrder: function(test) {

var self = this;
Expand Down

0 comments on commit 4dbe3cd

Please sign in to comment.