Skip to content

Commit ee72998

Browse files
Merge pull request #34 from rondonjon/master
Add snapshot list/take/delete operations and test scripts
2 parents da19e49 + 045b688 commit ee72998

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

lib/virtualbox.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,78 @@ function acpisleepbutton(vmname, callback) {
167167
});
168168
}
169169

170+
function snapshotList(vmname, callback) {
171+
logging.info('Listing snapshots for VM "%s"', vmname);
172+
vboxmanage('snapshot "' + vmname + '" list --machinereadable', function(error, stdout) {
173+
174+
if (error) {
175+
callback(error);
176+
return;
177+
}
178+
179+
var s;
180+
var snapshots = [];
181+
var currentSnapshot;
182+
var lines = (stdout || '').split('\n');
183+
184+
lines.forEach(function(line) {
185+
line.replace(/^(CurrentSnapshotUUID|SnapshotName|SnapshotUUID).*\="(.*)"$/, function(l, k, v) {
186+
if (k === 'CurrentSnapshotUUID') {
187+
currentSnapshot = v;
188+
}
189+
else if (k === 'SnapshotName') {
190+
s = {
191+
'name': v
192+
};
193+
snapshots.push(s);
194+
}
195+
else {
196+
s.uuid = v;
197+
}
198+
});
199+
});
200+
201+
callback(null, snapshots, currentSnapshot);
202+
});
203+
}
204+
205+
function snapshotTake(vmname, name, /*optional*/ description, /*optional*/ live, callback) {
206+
logging.info('Taking snapshot for VM "%s"', vmname);
207+
208+
if(typeof description === 'function') {
209+
callback = description;
210+
description = undefined;
211+
}
212+
else if(typeof live === 'function') {
213+
callback = live;
214+
live = false;
215+
}
216+
217+
var cmd = 'snapshot ' + JSON.stringify(vmname) + ' take ' + JSON.stringify(name);
218+
219+
if(description) {
220+
cmd += ' --description ' + JSON.stringify(description);
221+
}
222+
223+
if(live === true) {
224+
cmd += ' --live';
225+
}
226+
227+
vboxmanage(cmd, function(error, stdout) {
228+
var uuid;
229+
stdout.trim().replace(/UUID\: ([a-f0-9\-]+)$/, function(l, u) {
230+
uuid = u;
231+
});
232+
callback(error, uuid);
233+
});
234+
}
235+
236+
function snapshotDelete(vmname, uuid, callback) {
237+
logging.info('Deleting snapshot "%s" for VM "%s"', uuid, vmname);
238+
var cmd = 'snapshot ' + JSON.stringify(vmname) + ' delete ' + JSON.stringify(uuid);
239+
vboxmanage(cmd, callback);
240+
}
241+
170242
function keyboardputscancode(vmname, codes, callback) {
171243
var codeStr = codes.map(function(code) {
172244
var s = code.toString(16);
@@ -332,6 +404,9 @@ module.exports = {
332404
'acpipowerbutton': acpipowerbutton,
333405
'guestproperty': guestproperty,
334406
'keyboardputscancode': keyboardputscancode,
407+
'snapshotList': snapshotList,
408+
'snapshotTake': snapshotTake,
409+
'snapshotDelete': snapshotDelete,
335410

336411
'SCAN_CODES': require('./scan-codes')
337412
};

test/snapshot-delete.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"use strict";
2+
3+
var virtualbox = require('../lib/virtualbox'),
4+
vm = process.argv[2],
5+
uuid = process.argv[3];
6+
7+
virtualbox.snapshotDelete(vm, uuid, function(error) {
8+
if(error) {
9+
throw error;
10+
}
11+
});

test/snapshot-list.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use strict";
2+
3+
var virtualbox = require('../lib/virtualbox'),
4+
vm = process.argv.slice(2);
5+
6+
virtualbox.snapshotList(vm, function(error, snapshotList, currentSnapshotUUID) {
7+
if(error) {
8+
throw error;
9+
}
10+
11+
if(snapshotList) {
12+
console.log(JSON.stringify(snapshotList), JSON.stringify(currentSnapshotUUID));
13+
}
14+
});

test/snapshot-take.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use strict";
2+
3+
var virtualbox = require('../lib/virtualbox'),
4+
vm = process.argv[2],
5+
name = process.argv[3];
6+
7+
virtualbox.snapshotTake(vm, name, function(error, uuid) {
8+
if(error) {
9+
throw error;
10+
}
11+
if(uuid) {
12+
console.log('UUID: ' + uuid);
13+
}
14+
});

0 commit comments

Comments
 (0)