Skip to content

Commit

Permalink
[feature] Non-interactive snapshot activation, fixes #478
Browse files Browse the repository at this point in the history
  • Loading branch information
julianduque committed Dec 5, 2013
1 parent 62679fd commit 993f43d
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 50 deletions.
42 changes: 24 additions & 18 deletions lib/jitsu/commands/snapshots.js
Expand Up @@ -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
Expand All @@ -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) {
//
Expand All @@ -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) {
Expand All @@ -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();
});
});
}
Expand All @@ -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) {
Expand Down
91 changes: 59 additions & 32 deletions test/commands/snapshots-test.js
Expand Up @@ -4,7 +4,7 @@
* (C) 2010, Nodejitsu Inc.
*
*/

var nock = require('nock'),
assert = require('assert'),
vows = require('vows'),
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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' })
Expand Down Expand Up @@ -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' })
Expand All @@ -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() {

Expand All @@ -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' })
Expand All @@ -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' })
Expand All @@ -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' })
Expand All @@ -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);
Expand All @@ -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' })
Expand Down Expand Up @@ -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' })
Expand Down

0 comments on commit 993f43d

Please sign in to comment.