diff --git a/lib/shipit.js b/lib/shipit.js index b02fee5..09b1540 100644 --- a/lib/shipit.js +++ b/lib/shipit.js @@ -1,6 +1,6 @@ var path = require('path'); var _ = require('lodash'); - +var util = require('util'); var Shipit = module.exports; /** @@ -56,17 +56,19 @@ function computeReleaseDirname(result) { Shipit.getCurrentReleaseDirname = function() { var shipit = this; - return shipit.remote('readlink ' + shipit.currentPath) - .then(function (results) { - if (!results) { - shipit.log('No current release found.'); - return null; - } - + return shipit.remote(util.format('if [ -h %s ]; then readlink %s; fi', shipit.currentPath, shipit.currentPath)) + .then(function(results) { + results = results || []; var releaseDirnames = results.map(computeReleaseDirname); - if (!equalValues(releaseDirnames)) + if (!equalValues(releaseDirnames)) { throw new Error('Remote servers are not synced.'); + } + + if (!releaseDirnames[0]) { + shipit.log('No current release found.'); + return null; + } return releaseDirnames[0]; }); @@ -107,7 +109,7 @@ Shipit.getReleases = function() { var shipit = this; return shipit.remote('ls -r1 ' + shipit.releasesPath) - .then(function (results) { + .then(function(results) { var releases = results.map(computeReleases); if (!equalValues(releases)) diff --git a/test/unit/tasks/deploy/update.js b/test/unit/tasks/deploy/update.js index ac295a9..c1d58fb 100644 --- a/test/unit/tasks/deploy/update.js +++ b/test/unit/tasks/deploy/update.js @@ -156,7 +156,7 @@ describe('deploy:update task', function () { ]); } - if (command === 'readlink /remote/deploy/current') { + if (command === 'if [ -h /remote/deploy/current ]; then readlink /remote/deploy/current; fi') { return Promise.resolve([ {stdout: '/remote/deploy/releases/20141704123137'} ]); diff --git a/test/unit/tasks/rollback/init.js b/test/unit/tasks/rollback/init.js index 386b69c..acbff3a 100644 --- a/test/unit/tasks/rollback/init.js +++ b/test/unit/tasks/rollback/init.js @@ -6,6 +6,7 @@ var Promise = require('bluebird'); describe('rollback:init task', function () { var shipit; + var readLinkCommand = 'if [ -h /remote/deploy/current ]; then readlink /remote/deploy/current; fi'; beforeEach(function () { shipit = new Shipit({ @@ -28,7 +29,7 @@ describe('rollback:init task', function () { describe('unsync server', function () { beforeEach(function () { sinon.stub(shipit, 'remote', function (command) { - if (command === 'readlink /remote/deploy/current') + if (command === readLinkCommand) return Promise.resolve([ {stdout: '/remote/deploy/releases/20141704123138'}, {stdout: '/remote/deploy/releases/20141704123137'} @@ -51,7 +52,7 @@ describe('rollback:init task', function () { describe('bad release dirname', function () { beforeEach(function () { sinon.stub(shipit, 'remote', function (command) { - if (command === 'readlink /remote/deploy/current') + if (command === readLinkCommand) return Promise.resolve([]); }); }); @@ -73,7 +74,7 @@ describe('rollback:init task', function () { describe('unsync server', function () { beforeEach(function () { sinon.stub(shipit, 'remote', function (command) { - if (command === 'readlink /remote/deploy/current') + if (command === readLinkCommand) return Promise.resolve([ {stdout: '/remote/deploy/releases/20141704123137'} ]); @@ -100,7 +101,7 @@ describe('rollback:init task', function () { describe('bad releases', function () { beforeEach(function () { sinon.stub(shipit, 'remote', function (command) { - if (command === 'readlink /remote/deploy/current') + if (command === readLinkCommand) return Promise.resolve([ {stdout: '/remote/deploy/releases/20141704123137'} ]); @@ -125,7 +126,7 @@ describe('rollback:init task', function () { describe('release not exists', function () { beforeEach(function () { sinon.stub(shipit, 'remote', function (command) { - if (command === 'readlink /remote/deploy/current') + if (command === readLinkCommand) return Promise.resolve([ {stdout: '/remote/deploy/releases/20141704123137'} ]); @@ -151,7 +152,7 @@ describe('rollback:init task', function () { describe('all good', function () { beforeEach(function () { sinon.stub(shipit, 'remote', function (command) { - if (command === 'readlink /remote/deploy/current') + if (command === readLinkCommand) return Promise.resolve([ {stdout: '/remote/deploy/releases/20141704123137\n'} ]); @@ -171,7 +172,7 @@ describe('rollback:init task', function () { if (err) return done(err); expect(shipit.currentPath).to.equal('/remote/deploy/current'); expect(shipit.releasesPath).to.equal('/remote/deploy/releases'); - expect(shipit.remote).to.be.calledWith('readlink /remote/deploy/current'); + expect(shipit.remote).to.be.calledWith(readLinkCommand); expect(shipit.remote).to.be.calledWith('ls -r1 /remote/deploy/releases'); expect(shipit.releaseDirname).to.equal('20141704123136'); expect(shipit.releasePath).to.equal('/remote/deploy/releases/20141704123136');