-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d4d06db
Showing
13 changed files
with
584 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
config.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
|
||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.