Skip to content
This repository has been archived by the owner on Mar 18, 2018. It is now read-only.

Commit

Permalink
Merge pull request #37 from timkelty/symlink-check
Browse files Browse the repository at this point in the history
Check if symlink exists before reading to prevent error.
  • Loading branch information
gregberge committed Mar 26, 2015
2 parents dfbbdd4 + 3239227 commit e8cf11c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
22 changes: 12 additions & 10 deletions lib/shipit.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var path = require('path');
var _ = require('lodash');

var util = require('util');
var Shipit = module.exports;

/**
Expand Down Expand Up @@ -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];
});
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion test/unit/tasks/deploy/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
]);
Expand Down
15 changes: 8 additions & 7 deletions test/unit/tasks/rollback/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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'}
Expand All @@ -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([]);
});
});
Expand All @@ -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'}
]);
Expand All @@ -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'}
]);
Expand All @@ -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'}
]);
Expand All @@ -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'}
]);
Expand All @@ -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');
Expand Down

0 comments on commit e8cf11c

Please sign in to comment.