Skip to content

Commit

Permalink
Merge branch 'release/0.1.3' into production
Browse files Browse the repository at this point in the history
  • Loading branch information
rmg committed Aug 26, 2014
2 parents fce5316 + d451c94 commit 0846be0
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 91 deletions.
12 changes: 12 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
2014-08-21, Version 0.1.3
=========================

* usage: clarify pack and branch meaning (Sam Roberts)

* Remove --redeploy and make git always force push (Krishna Raman)

* Add ability to deploy npm package (tarball) (Krishna Raman)

* Add usage enhancements (Krishna Raman)


2014-08-05, Version 0.1.2
=========================

Expand Down
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ Deploy a node application package built using [strong-build](http://github.com/s
## Usage

```
usage: sl-deploy [options] URL
usage: sl-deploy [options] URL [PACK|BRANCH]
Deploy a node application to a StrongLoop process manager
Options:
-h,--help Print this message and exit.
-v,--version Print version and exit.
Git specific options:
--branch BRANCH Deploy a specified branch.
(default: current branch)
-h,--help Print this message and exit.
-v,--version Print version and exit.
-c,--config CFG Deploy a specified configuration (default is "default").
Arguments:
URL The URL of the StrongLoop process manager
URL The URL of the StrongLoop process manager
eg: http://127.0.0.1:7777
PACK Deploy an NPM package/tarball.
BRANCH Deploy a git branch.
Default behaviour is to deploy the git branch "deploy".
```
103 changes: 37 additions & 66 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,42 @@
var assert = require('assert');
var debug = require('debug')('strong-deploy');
var Parser = require('posix-getopt').BasicParser;
var path = require('path');
var shell = require('shelljs');
var util = require('util');
var url = require('url');

var performGitDeployment = require('./lib/git').performGitDeployment;
var performHttpPutDeployment = require('./lib/put-file').performHttpPutDeployment;

function printHelp($0, prn) {
prn('usage: %s [options] URL', $0);
prn('usage: %s [options] URL [PACK|BRANCH]', $0);
prn('');
prn('Deploy a node application to a StrongLoop process manager');
prn('');
prn('Options:');
prn(' -h,--help Print this message and exit.');
prn(' -v,--version Print version and exit.');
prn('');
prn('Git specific options:');
prn(' --branch BRANCH Deploy a specified branch.');
prn(' (default: current branch)');
prn(' -h,--help Print this message and exit.');
prn(' -v,--version Print version and exit.');
prn(' -c,--config CFG Deploy a specified configuration (default is "default").');
prn('');
prn('Arguments:');
prn(' URL The URL of the StrongLoop process manager');
}

function getCurrentBranch() {
var output = shell.exec('git symbolic-ref --short HEAD', {silent: true});
if (output.code !== 0) {
return Error('This directory does not contain a valid git repository');
}
return output.output.trim();
}

function isValidBranch(branchName) {
var output = shell.exec(
util.format('git rev-parse --abbrev-ref %s', branchName),
{silent: true});
return output.code === 0;
}

function isValidGitURL(url) {
var output = shell.exec(
util.format('git ls-remote %s', url),
{silent: true});
return output.code === 0;
}

function doGitPush(gitURL, branch, callback) {
shell.exec(
util.format('git push %s %s:%s', gitURL, branch, branch),
callback);
prn(' URL The URL of the StrongLoop process manager');
prn(' eg: http://127.0.0.1:7777');
prn(' PACK Deploy an NPM package/tarball.');
prn(' BRANCH Deploy a git branch.');
prn('');
prn('Default behaviour is to deploy the git branch "deploy".');
}

// for unit tests
exports._getCurrentBranch = getCurrentBranch;
exports.deploy = function deploy(argv, callback) {
var $0 = process.env.SLC_COMMAND ?
'slc ' + process.env.SLC_COMMAND :
path.basename(argv[1]);
var parser = new Parser([
':v(version)',
'h(help)',
'b:(branch)'
'c:(config)',
].join(''),
argv);
var option;
var error;
var branch;
var config = 'default';
var branchOrPack;

while ((option = parser.getopt()) !== undefined) {
switch (option.option) {
Expand All @@ -75,8 +46,11 @@ exports.deploy = function deploy(argv, callback) {
case 'h':
printHelp($0, console.log);
return callback();
case 'b':
branch = option.optarg;
case 'c':
config = option.optarg;
break;
case 'p':
npmPkg = option.optarg;
break;
default:
console.error('Invalid usage (near option \'%s\'), try `%s --help`.',
Expand All @@ -85,35 +59,32 @@ exports.deploy = function deploy(argv, callback) {
}
}

if (argv.length - parser.optind() != 1) {
var numArgs = argv.length - parser.optind();
if (numArgs < 1 || numArgs > 2) {
console.error('Invalid usage, try `%s --help`.', $0);
return callback(Error('usage'));
}

branch = branch || getCurrentBranch();
if (branch instanceof Error) {
console.error(branch.message);
callback(branch);
}
config = config || 'default';

if (!isValidBranch(branch)) {
console.error('Branch `%s` is not available in this repository', branch);
return callback(Error('invalid branch'));
var baseURL = argv[parser.optind()];
if (numArgs === 2) {
branchOrPack = argv[parser.optind() + 1];
}
branchOrPack = branchOrPack || 'deploy';

var deployURL = argv[parser.optind()];
if (!isValidGitURL(deployURL)) {
console.error('URL `%s` is not valid', deployURL);
return callback(Error('invalid url'));
}

doGitPush(deployURL, branch, function(er, p) {
function cb(er) {
if (er) {
console.error('Deployment unsuccessful');
callback(Error('Deployment unsuccessful'));
} else {
console.log('Deployed branch `%s` to `%s`', branch, deployURL);
console.log('Deployed `%s` to `%s`', branchOrPack, baseURL);
callback();
}
});
}

if (shell.test('-f', path.resolve(branchOrPack))) {
performHttpPutDeployment(baseURL, config, branchOrPack, cb);
} else {
performGitDeployment(baseURL, config, branchOrPack, cb);
}
};
50 changes: 50 additions & 0 deletions lib/git.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var shell = require('shelljs');
var util = require('util');

function getCurrentBranch() {
var output = shell.exec('git symbolic-ref --short HEAD', {silent: true});
if (output.code !== 0) {
return Error('This directory does not contain a valid git repository');
}
return output.output.trim();
}

function isValidBranch(branchName) {
var output = shell.exec(
util.format('git rev-parse --abbrev-ref %s', branchName),
{silent: true});
return output.code === 0;
}

function isValidGitURL(url) {
var output = shell.exec(
util.format('git ls-remote %s', url),
{silent: true});
return output.code === 0;
}

function doGitPush(gitURL, branch, callback) {
shell.exec(
util.format('git push -f %s %s:%s', gitURL, branch, branch),
callback);
}

function performGitDeployment(baseUrl, config, branch, callback) {
var deployURL = baseUrl + '/' + config;

if (!isValidBranch(branch)) {
console.error('Branch `%s` is not available in this repository', branch);
return callback(Error('invalid branch'));
}

if (!isValidGitURL(deployURL)) {
console.error('URL `%s` is not valid', deployURL);
return callback(Error('invalid url'));
}

doGitPush(deployURL, branch, callback);
}

exports.performGitDeployment = performGitDeployment;
// for unit tests
exports._getCurrentBranch = getCurrentBranch;
55 changes: 55 additions & 0 deletions lib/put-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
var fs = require('fs');
var http = require('http');
var path = require('path');
var url = require('url');

function performHttpPutDeployment(baseURL, config, npmPkg, callback) {
npmPkg = path.resolve(npmPkg);

var fileReader = fs.createReadStream(npmPkg);
fileReader.on('error', function(err) {
console.error('Error while reading file: %s', err);
callback(err);
});

var postURL = url.parse(baseURL);
if (postURL.protocol !== 'http:') {
console.error('Invalid URL `%s`. Only http:// URLs are supported.',
baseURL
);
return callback(Error('Invalid URL'));
}

var postOptions = {
auth: postURL.auth,
hostname: postURL.hostname,
port: postURL.port || 80,
path: '/' + config,
method: 'PUT'
};

var req = http.request(postOptions, function(res) {
if (res.statusCode === 200) {
callback();
}
console.error('Deploy `%s` to `%s` failed. HTTP Code: %d',
npmPkg,
baseURL,
res.statusCode
);
callback(Error('HTTP error'));
});

req.on('error', function(err) {
console.error('Deploy `%s` to `%s` failed: %s',
npmPkg,
baseURL,
err.message
);
callback(err);
});

fileReader.pipe(req);
}

exports.performHttpPutDeployment = performHttpPutDeployment;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "strong-deploy",
"description": "Deploy a node application to a StrongLoop process manager",
"version": "0.1.2",
"version": "0.1.3",
"repository": {
"type": "git",
"url": "git://github.com/strongloop/strong-deploy.git"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
var http = require('http');
var cicada = require('cicada');
var shell = require('shelljs');
var child_process = require('child_process');
var childProcess = require('child_process');
var assert = require('assert');
var getCurrentBranch = require('../index.js')._getCurrentBranch;

shell.rm('-rf', '.test_artifacts');
var ci = cicada('.test_artifacts');
var server = http.createServer(ci.handle);
var ok = false;

ci.once('commit', function(commit) {
assert(commit.repo === 'repo1');
var branch = getCurrentBranch();
assert(commit.repo === 'default');
var branch = 'deploy';
assert(!(branch instanceof Error));
assert(commit.branch === branch);
ok = true;
server.close();
});

server.once('listening', function() {
var deploy = child_process.fork(
childProcess.fork(
require.resolve('../bin/sl-deploy'),
['http://localhost:5255/repo1']
['http://localhost:5255']
);
});

Expand Down
8 changes: 4 additions & 4 deletions test/test-deploy-specific-branch.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var http = require('http');
var cicada = require('cicada');
var shell = require('shelljs');
var child_process = require('child_process');
var childProcess = require('child_process');
var assert = require('assert');
var getCurrentBranch = require('../index.js')._getCurrentBranch;
var getCurrentBranch = require('../lib/git.js')._getCurrentBranch;

shell.rm('-rf', '.test_artifacts');
var ci = cicada('.test_artifacts');
Expand All @@ -25,9 +25,9 @@ ci.once('commit', function(commit) {
});

server.once('listening', function() {
var deploy = child_process.fork(
childProcess.fork(
require.resolve('../bin/sl-deploy'),
['--branch', pushBranch, 'http://localhost:5255/repo2']
['--config', 'repo2', 'http://localhost:5255', pushBranch]
);
});

Expand Down
22 changes: 22 additions & 0 deletions test/test-put-file-deploy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var assert = require('assert');
var http = require('http');
var childProcess = require('child_process');

var ok = false;
var server = http.createServer(function(req, res) {
assert(req.url === '/repo2');
assert(req.method === 'PUT');
ok = true;

res.end();
server.close();
});
server.listen(0);

server.on('listening', function() {
var port = server.address().port;
childProcess.fork(
require.resolve('../bin/sl-deploy'),
['--config', 'repo2', 'http://127.0.0.1:' + port, __filename]
);
});
Loading

0 comments on commit 0846be0

Please sign in to comment.