Skip to content

Commit

Permalink
deploy:rollback
Browse files Browse the repository at this point in the history
  • Loading branch information
1602 committed Feb 8, 2012
1 parent 3244a8c commit dbcb64d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
24 changes: 21 additions & 3 deletions cockout/deploy.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ ensure 'releasesPath', -> path.resolve(roco.deployTo, roco.releasesDir)
ensure 'sharedPath', -> path.resolve(roco.deployTo, roco.sharedDir)
ensure 'currentPath', -> path.resolve(roco.deployTo, roco.currentDir)
ensure 'releasePath', -> path.resolve(roco.releasesPath, ''+roco.releaseName)
ensure 'previousReleasePath', -> path.resolve(roco.releasesPath, ''+roco.previousRelease)
ensure 'latestReleasePath', -> path.resolve(roco.releasesPath, ''+roco.latestRelease)
ensure 'env', 'production'
ensure 'nodeEntry', 'server.js'
ensure 'appPort', 3001

set 'hosts', ['node-js.ru', 'railwayjs.com']

namespace 'deploy', ->

task "test", ->
Expand All @@ -39,7 +39,15 @@ namespace 'deploy', ->
Pull latest changes from SCM and symlink latest release
as current release
"""
task 'update', (done) -> sequence 'updateCode', 'symlink', done
task 'update', (done) -> sequence 'prepare', 'updateCode', 'symlink', done

task 'prepare', (done) ->
run "ls -x #{roco.releasesPath}", (res) ->
rs = res[0].out.replace(/^\s+|\s+$/g, '').split(/\s+/).sort()
set 'releases', rs
set 'latestRelease', rs[rs.length - 1]
set 'previousRelease', rs[rs.length - 2]
done()

task 'updateCode', (done) ->
localRun "git ls-remote #{roco.repository} #{roco.branch}", (x) ->
Expand Down Expand Up @@ -78,6 +86,16 @@ namespace 'deploy', ->
task 'stop', (done) ->
run "sudo stop #{roco.application}", done

task 'rollback', (done) ->
sequence 'prepare', 'rollback:code', 'restart', 'rollback:cleanup', done

task 'rollback:code', (done) ->
if roco.previousRelease
run "rm #{roco.currentPath}; ln -s #{roco.previousReleasePath} #{roco.currentPath}", done

task 'rollback:cleanup', (done) ->
run "if [ `readlink #{roco.currentPath}` != #{roco.latestReleasePath} ]; then rm -rf #{roco.latestReleasePath}; fi", done

task 'setup', (done) ->
dirs = [roco.deployTo, roco.releasesPath, roco.sharedPath, roco.sharedPath + '/log'].join(' ')
run """
Expand Down
22 changes: 17 additions & 5 deletions lib/rockout.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Roco.prototype.init = function () {
var packageFile = path.resolve(cwd, 'package.json');
var configFiles = [
'/etc/roco.coffee',
'~/.roco.coffee',
path.resolve(process.env.HOME, '.roco.coffee'),
path.resolve(cwd, 'Roco.coffee'),
path.resolve(cwd, 'config/Roco.coffee')
];
Expand All @@ -57,8 +57,12 @@ Roco.prototype.init = function () {
}
}

if (process.env.APP) {
roco.application = process.env.APP;
}

configFiles.forEach(function (configFile) {
if (path.existsSync(configFile)) {
if (path.existsSync(path.resolve(configFile))) {
roco.load(configFile);
}
});
Expand Down Expand Up @@ -117,13 +121,21 @@ function spawnProcess(command, options, callback) {
var prefix = command === 'ssh' ? ' [' + options[0] + '] ' : '';

child.stderr.on('data', function (chunk) {
log(prefix + chunk.toString().replace(/\s+$/, ''));
log(addBeauty(chunk));
});
var res = [];
child.stdout.on('data', function (chunk) {
res.push(chunk.toString().replace(/\s+$/, ''));
log(prefix + chunk.toString());
res.push(chunk.toString());
log(addBeauty(chunk));
});

function addBeauty(buf) {
return prefix + buf
.toString()
.replace(/\s+$/, '')
.replace(/\n/g, '\n' + prefix);
}

child.on('exit', function (code) {
if (callback) {
callback(code === 0 ? null : code, res && res.join('\n'));
Expand Down

0 comments on commit dbcb64d

Please sign in to comment.