Permalink
Browse files

[feature] Non-interactive snapshot activation, fixes #478

  • Loading branch information...
julianduque committed Dec 5, 2013
1 parent 62679fd commit 993f43d01e30f95a39abd2b350ad12d6bd2e201d
Showing with 83 additions and 50 deletions.
  1. +24 −18 lib/jitsu/commands/snapshots.js
  2. +59 −32 test/commands/snapshots-test.js
@@ -181,7 +181,7 @@ snapshots.create.usage = [
'jitsu snapshots create <app-name>'
];

snapshots.activate = function (name, callback) {
snapshots.activate = function (name, id, callback) {

//
// Allows arbitrary amount of arguments
@@ -190,13 +190,14 @@ snapshots.activate = function (name, callback) {
var args = utile.args(arguments);
callback = args.callback;
name = args[0] || null;
id = args[1] || null;
}

if (!name) {
snapshots.list(null, function (err, results, name) {
executeActivate(err, results, name);
});
} else {
} else if (!id) {
snapshots.list(name, function (err, results) {
if (err) {
//
@@ -209,9 +210,10 @@ snapshots.activate = function (name, callback) {
}
executeActivate(err, results, name);
});
} else {
activateSnapshot(name, id, callback);
}


function executeActivate (err, snapshots, name) {
jitsu.prompt.get(['snapshot'], function (err, result) {
var snapshot = snapshots.filter(function (snap) {
@@ -221,25 +223,29 @@ snapshots.activate = function (name, callback) {
return callback(new Error('Cannot find snapshot with name: ' + result['snapshot'].magenta), true);
}

jitsu.log.info('Activating snapshot ' + snapshot.id.magenta);
activateSnapshot(name, snapshot.id, callback);
});
}

function activateSnapshot(name, id, callback) {
jitsu.log.info('Activating snapshot ' + id.magenta);

jitsu.snapshots.activate(name, snapshot.id, function (err, result) {
jitsu.snapshots.activate(name, id, function (err, result) {
if (err) {
return callback(err);
}
jitsu.log.info('Snapshot ' + id.magenta + ' is now active');
//
// Remark: After activating a snapshot, we immediately stop && start it,
// as to update the running version of the snapshot
//
jitsu.commands.start(name, function (err) {
if (err) {
return callback(err);
}
jitsu.log.info('Snapshot ' + snapshot.id.magenta + ' is now active');
//
// Remark: After activating a snapshot, we immediately stop && start it,
// as to update the running version of the snapshot
//
jitsu.commands.start(name, function (err) {
if (err) {
return callback(err);
}

jitsu.log.info('Snapshot ' + snapshot.id.magenta + ' is now running');
callback();
});
jitsu.log.info('Snapshot ' + id.magenta + ' is now running');
callback();
});
});
}
@@ -250,7 +256,7 @@ snapshots.activate.usage = [
'Activates a snapshot for the application in the current directory',
'',
'jitsu snapshots activate',
'jitsu snapshots activate <app-name>'
'jitsu snapshots activate <app-name> <snapshot-id>'
];

snapshots.fetch = function (name, callback) {
@@ -4,7 +4,7 @@
* (C) 2010, Nodejitsu Inc.
*
*/

var nock = require('nock'),
assert = require('assert'),
vows = require('vows'),
@@ -21,15 +21,15 @@ var mainDirectory = process.cwd();
var fixturesDir = path.join(__dirname, '..', 'fixtures'),
loggedOutFile = path.join(fixturesDir, 'logged-out-jitsuconf')
loggedOutConf = fs.readFileSync(loggedOutFile, 'utf8');

var cloud = [{ drones: 0, provider: 'jitsu', datacenter: 'foobar' }],
endpoints = {
"endpoints": {
"jitsu": {
"foobar": "api.mockjitsu.com"
}
}
};
};

// Snapshots tests with specified app names
vows.describe('jitsu/commands/snapshots').addBatch({
@@ -38,10 +38,10 @@ vows.describe('jitsu/commands/snapshots').addBatch({
.get('/apps/tester/application/snapshots')
.reply(200, {
snapshots: [{
id: '0.0.0',
ctime: new Date(),
id: '0.0.0',
ctime: new Date(),
md5: 'q34rq43r5t5g4w56t45t'
}]
}]
}, { 'x-powered-by': 'Nodejitsu' });
})
}).addBatch({
@@ -57,10 +57,10 @@ vows.describe('jitsu/commands/snapshots').addBatch({
.get('/apps/tester/application/snapshots')
.reply(200, {
snapshots: [{
id: '0.0.0',
ctime: new Date(),
id: '0.0.0',
ctime: new Date(),
md5: 'q34rq43r5t5g4w56t45t'
}]
}]
}, { 'x-powered-by': 'Nodejitsu' });
})
}).addBatch({
@@ -69,10 +69,10 @@ vows.describe('jitsu/commands/snapshots').addBatch({
.get('/apps/tester/application2/snapshots')
.reply(200, {
snapshots: [{
id: '0.0.0',
ctime: new Date(),
id: '0.0.0',
ctime: new Date(),
md5: 'q34rq43r5t5g4w56t45t'
}]
}]
}, { 'x-powered-by': 'Nodejitsu' });
})
}).addBatch({
@@ -90,8 +90,8 @@ vows.describe('jitsu/commands/snapshots').addBatch({
.get('/apps/tester/application2/snapshots')
.reply(200, {
snapshots: [{
id: '0.0.0-1',
ctime: new Date(),
id: '0.0.0-1',
ctime: new Date(),
md5: 'q34rq43r5t5g4w56t45t'
}]
}, { 'x-powered-by': 'Nodejitsu' })
@@ -120,8 +120,8 @@ vows.describe('jitsu/commands/snapshots').addBatch({
.get('/apps/tester/application2/snapshots')
.reply(200, {
snapshots: [{
id: '0.0.0-1',
ctime: new Date(),
id: '0.0.0-1',
ctime: new Date(),
md5: 'q34rq43r5t5g4w56t45t'
}]
}, { 'x-powered-by': 'Nodejitsu' })
@@ -141,6 +141,33 @@ vows.describe('jitsu/commands/snapshots').addBatch({
}
}, { 'x-powered-by': 'Nodejitsu' });
})
}).addBatch({
'snapshots activate application1 0.0.0-1': shouldNodejitsuOk(function setup() {
nock('https://api.mockjitsu.com')
.get('/apps/tester/application2/snapshots')
.reply(200, {
snapshots: [{
id: '0.0.0-1',
ctime: new Date(),
md5: 'q34rq43r5t5g4w56t45t'
}]
}, { 'x-powered-by': 'Nodejitsu' })
.post('/apps/tester/application1/snapshots/0.0.0-1/activate', {})
.reply(200, '', { 'x-powered-by': 'Nodejitsu' })
.get('/endpoints')
.reply(200, endpoints, { 'x-powered-by': 'Nodejitsu' })
.get('/apps/tester/application1/cloud')
.reply(200, cloud, { 'x-powered-by': 'Nodejitsu' })
.post('/apps/tester/application1/snapshots/0.0.0-1/activate', {})
.reply(200, '', { 'x-powered-by': 'Nodejitsu' })
.post('/apps/tester/application1/start', {})
.reply(200, '', { 'x-powered-by': 'Nodejitsu' })
.get('/apps/tester/application1', '')
.reply(200, { app: {
subdomain: "tester.application1"
}
}, { 'x-powered-by': 'Nodejitsu' });
})
}).addBatch({
'snapshots activate application2': shouldNodejitsuOk('should prompt for credentials', function setup() {

@@ -155,8 +182,8 @@ vows.describe('jitsu/commands/snapshots').addBatch({
.get('/apps/tester/application2/snapshots')
.reply(200, {
snapshots: [{
id: '0.0.0-1',
ctime: new Date(),
id: '0.0.0-1',
ctime: new Date(),
md5: 'q34rq43r5t5g4w56t45t'
}]
}, { 'x-powered-by': 'Nodejitsu' })
@@ -181,13 +208,13 @@ vows.describe('jitsu/commands/snapshots').addBatch({
jitsu.prompt.override.answer = 'yes';
jitsu.prompt.override.snapshot = '0.0.0-1';
jitsu.prompt.override.destroy = 'yes';

nock('https://api.mockjitsu.com')
.get('/apps/tester/application3/snapshots')
.reply(200, {
snapshots: [{
id: '0.0.0-1',
ctime: new Date(),
id: '0.0.0-1',
ctime: new Date(),
md5: 'q34rq43r5t5g4w56t45t'
}]
}, { 'x-powered-by': 'Nodejitsu' })
@@ -205,13 +232,13 @@ vows.describe('jitsu/commands/snapshots').addBatch({
jitsu.prompt.override.answer = 'yes';
jitsu.prompt.override.snapshot = '0.0.0-1';
jitsu.prompt.override.destroy = 'yes';

nock('https://api.mockjitsu.com')
.get('/apps/tester/application3/snapshots')
.reply(200, {
snapshots: [{
id: '0.0.0-1',
ctime: new Date(),
id: '0.0.0-1',
ctime: new Date(),
md5: 'q34rq43r5t5g4w56t45t'
}]
}, { 'x-powered-by': 'Nodejitsu' })
@@ -228,10 +255,10 @@ vows.describe('jitsu/commands/snapshots').addBatch({
.get('/apps/tester/example-app/snapshots')
.reply(200, {
snapshots: [{
id: '0.0.0',
ctime: new Date(),
id: '0.0.0',
ctime: new Date(),
md5: 'q34rq43r5t5g4w56t45t'
}]
}]
}, { 'x-powered-by': 'Nodejitsu' });
}, function assertion (err) {
process.chdir(mainDirectory);
@@ -247,8 +274,8 @@ vows.describe('jitsu/commands/snapshots').addBatch({
.get('/apps/tester/example-app/snapshots')
.reply(200, {
snapshots: [{
id: '0.0.0-1',
ctime: new Date(),
id: '0.0.0-1',
ctime: new Date(),
md5: 'q34rq43r5t5g4w56t45t'
}]
}, { 'x-powered-by': 'Nodejitsu' })
@@ -277,13 +304,13 @@ vows.describe('jitsu/commands/snapshots').addBatch({
jitsu.prompt.override.destroy = 'yes';

useAppFixture();

nock('https://api.mockjitsu.com')
.get('/apps/tester/example-app/snapshots')
.reply(200, {
snapshots: [{
id: '0.0.0-1',
ctime: new Date(),
id: '0.0.0-1',
ctime: new Date(),
md5: 'q34rq43r5t5g4w56t45t'
}]
}, { 'x-powered-by': 'Nodejitsu' })

0 comments on commit 993f43d

Please sign in to comment.