Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
nbourguig committed Jul 13, 2016
0 parents commit d4d06db
Show file tree
Hide file tree
Showing 13 changed files with 584 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
config.json
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2015 Andy Jiang

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Empty file added README.md
Empty file.
22 changes: 22 additions & 0 deletions bin/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2015 Andy Jiang

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

61 changes: 61 additions & 0 deletions bin/bstalk
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env node

var exists = require('fs').existsSync,
program = require('commander'),
resolve = require('path').resolve,
spawn = require('child_process').spawn,
config = require('nconf'),
fs = require("fs"),
path = require("path"),
logger = require('../lib/logger');

/**
* Usage.
*/

program
.version(require('../package').version)
.usage('<command> [options]');

/**
* Help.
*/

program.on('--help', function(){
console.log(' Commands:');
console.log();
console.log(' bstalk --version Print version');
console.log(' bstalk config Create config file');
console.log(' bstalk openconfig Open config file for edition');
console.log(' bstalk repos Display list of all repositories');
console.log(' bstalk deploy <repo> <env> [comment] Deploy environment <env> on <repo>');
console.log();
});

/**
* Parse.
*/

program.parse(process.argv);
if (!program.args.length) program.help();

/**
* Settings.
*/

var cmd = program.args[0];
var args = process.argv.slice(3);
var bin = resolve(__dirname, 'bstalk-' + cmd);

if (!exists(bin)) {
logger.log('There is no `%s` command.', cmd);
console.log();
program.help();
}

/**
* Spawn a new, forwarded child process for the subcommand.
*/

var child = spawn(bin, args, { stdio: 'inherit' });
child.on('close', process.exit.bind(process));
16 changes: 16 additions & 0 deletions bin/bstalk-config
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env node

var program = require('commander'),
logger = require('../lib/logger'),
config = require('../lib/config');

/*
|--------------------------------------------------------------------------
| Program
|--------------------------------------------------------------------------
*/
program
.usage('')
.parse(process.argv);

config.createConfigFile();
83 changes: 83 additions & 0 deletions bin/bstalk-deploy
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env node

var program = require('commander'),
_ = require('underscore'),
Grid = require('term-grid'),
logger = require('../lib/logger'),
beanstalk = require('../lib/api');

/*
|--------------------------------------------------------------------------
| Program
|--------------------------------------------------------------------------
*/
program
.usage('<repo> <env>')
.parse(process.argv);

var repoName = program.args[0],
envName = program.args[1],
comment = program.args[2];

if (!repoName || !envName) {
logger.fatal('You need to pass in the <repo> and the <env> arguments.');
process.exit(1);
}


function checkReleaseState(repoName, releaseId, delay) {
setTimeout(function(){
beanstalk.release(repoName, releaseId, function(release){
switch (release.state) {

case 'waiting':
logger.log('waiting...');
checkReleaseState(repoName, releaseId, 2000);
break;

case 'pending':
logger.log('pending...');
checkReleaseState(repoName, releaseId, 2000);
break;

case 'skipped':
logger.fatal('Deployment skipped.');
break;

case 'failed':
logger.fatal('Deployment failed.');
break;

case 'success':
logger.log('Deployed successfully.');
break;

default:
logger.log('Unknown state : ' + release.state);
break;
}
});

}, delay || 0);
}

// First we ge the repository
beanstalk.repo(repoName, function(data){
var repo = data.repository;

// Getting all repo environments
beanstalk.getEnvironments(repoName, function(envs){
var env = envs[envName];

if(!env){
logger.fatal('Cannot find the requested environment. Avaialable ones are : ' + _.pluck(envs, 'name'));
process.exit(1);
}

// Fire the deployment
beanstalk.deploy(repoName, env.id, env.current_version, comment, function(release){
checkReleaseState(repoName, release.id);
});

});
});
19 changes: 19 additions & 0 deletions bin/bstalk-openconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env node

var program = require('commander'),
logger = require('../lib/logger'),
config = require('../lib/config'),
open = require('open');

/*
|--------------------------------------------------------------------------
| Program
|--------------------------------------------------------------------------
*/
program
.usage('kk')
.parse(process.argv);

var file = config.file();
console.log('Opening : ' + file);
open(file);
32 changes: 32 additions & 0 deletions bin/bstalk-repos
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env node

var program = require('commander'),
_ = require('underscore'),
Grid = require('term-grid'),
logger = require('../lib/logger'),
beanstalk = require('../lib/api');

/*
|--------------------------------------------------------------------------
| Program
|--------------------------------------------------------------------------
*/
program
.usage('')
.parse(process.argv);

beanstalk.getRepositories(function(repos){

var items = _.map(repos, function(repo){
var item = [];
item.push(repo.repository.id);
item.push(repo.repository.name);

return item;
});

var grid = new Grid(items);
grid.draw();

console.log('Repositories count : ' + items.length);
});
124 changes: 124 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
var request = require("superagent"),
_ = require("underscore"),
config = require("./config").get(),
logger = require("./logger");


function reportError(err) {
logger.log(err);
logger.fatal('Beanstalk API - '+ err.error);
process.exit(1);
}

function api(endpoint, method) {
var vmethod = typeof method === 'undefined' ? 'GET' : 'POST';
var url = 'https://' + config.account + '.beanstalkapp.com/api/' + endpoint + '.json';
var req = vmethod === 'GET' ? request.get(url) : request.post(url);

return req
.auth(config.username, config.token)
.set('Content-Type', 'application/json')
.set('User-Agent', 'nikaia-bstalk');
}

function getRepositories(cb) {
api('repositories').end(function(err, res){
if(err){
reportError(err);
}

cb(res.body);
});
}

function repo(repoName, cb) {
logger.log('Getting repo...');

api('repositories/' + repoName).end(function(err, res){
if(err){
reportError(err);
}

cb(res.body);
});
}

function getEnvironments(repoName, cb) {
logger.log('Getting server environment...');

api(repoName + '/server_environments').end(function(err, res){
if(err){
reportError(err);
}

cb(
_.indexBy(
_.map(res.body, function(item){
return item.server_environment
}),
'name'
)
);
});
}

function environment(repoName, serverEnvironmentId, cb) {
logger.log('Getting server environments...');

api(repoName + '/server_enironments/' + serverEnvironmentId).end(function(err, res){
if(err){
reportError(err);
}

cb(res.body);
});
}

function deploy(repoName, serverEnvironmentId, revision, comment, cb) {
logger.log('Deploying...');

var release = {
revision: revision
};

if(comment){
release.comment = comment;
}

api(repoName + '/releases.json?environment_id='+serverEnvironmentId, 'POST')
.send({ release: release })
.end(function(err, res){
if(err){
reportError(err);
}

cb(res.body.release);
});
}


function release(repoName, releaseId, cb) {
api(repoName + '/releases/' + releaseId).end(function(err, res){
if(err){
reportError(err);
}

cb(res.body.release);
});
}





/*
|--------------------------------------------------------------------------
| Exports
|--------------------------------------------------------------------------
*/
exports.getRepositories = getRepositories;
exports.repo = repo;
exports.getEnvironments = getEnvironments;
exports.environment = environment;
exports.deploy = deploy;
exports.release = release;
Loading

0 comments on commit d4d06db

Please sign in to comment.