@@ -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 ( / ^ ( C u r r e n t S n a p s h o t U U I D | S n a p s h o t N a m e | S n a p s h o t U U I D ) .* \= " ( .* ) " $ / , 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 ( / U U I D \: ( [ a - f 0 - 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+
170242function 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} ;
0 commit comments