From 6428a3d609a965de4907b0bbee607194a5cebf3a Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Thu, 14 Mar 2013 18:01:14 +0100 Subject: [PATCH] feature #1664: Add snapshot tab to VM tab --- .../OpenNebulaJSON/VirtualMachineJSON.rb | 15 + src/sunstone/public/css/app.css | 2 +- src/sunstone/public/js/opennebula.js | 15 + src/sunstone/public/js/plugins/vms-tab.js | 272 +++++++++++++++++- src/sunstone/public/sass/app.scss | 2 +- 5 files changed, 298 insertions(+), 8 deletions(-) diff --git a/src/sunstone/models/OpenNebulaJSON/VirtualMachineJSON.rb b/src/sunstone/models/OpenNebulaJSON/VirtualMachineJSON.rb index 7f17a17ae2..12dc37e3d0 100644 --- a/src/sunstone/models/OpenNebulaJSON/VirtualMachineJSON.rb +++ b/src/sunstone/models/OpenNebulaJSON/VirtualMachineJSON.rb @@ -56,6 +56,9 @@ module OpenNebulaJSON when "restart" then self.restart when "reset" then self.reset when "saveas" then self.save_as(action_hash['params']) + when "snapshot_create" then self.snapshot_create(action_hash['params']) + when "snapshot_revert" then self.snapshot_revert(action_hash['params']) + when "snapshot_delete" then self.snapshot_delete(action_hash['params']) when "shutdown" then self.shutdown when "reboot" then self.reboot when "poweroff" then self.poweroff @@ -96,6 +99,18 @@ module OpenNebulaJSON super(params['disk_id'].to_i, params['image_name'], params['type'], params['hot']) end + def snapshot_create(params=Hash.new) + super(params['snapshot_name']) + end + + def snapshot_revert(params=Hash.new) + super(params['snapshot_id'].to_i) + end + + def snapshot_delete(params=Hash.new) + super(params['snapshot_id'].to_i) + end + def chown(params=Hash.new) super(params['owner_id'].to_i,params['group_id'].to_i) end diff --git a/src/sunstone/public/css/app.css b/src/sunstone/public/css/app.css index f2540dfd46..9df0b0c224 100644 --- a/src/sunstone/public/css/app.css +++ b/src/sunstone/public/css/app.css @@ -6772,7 +6772,7 @@ a.footer.button { /* line 769, ../sass/app.scss */ .vm_log_error { - color: #c0392b; + color: #ff8d81; } /* line 773, ../sass/app.scss */ diff --git a/src/sunstone/public/js/opennebula.js b/src/sunstone/public/js/opennebula.js index 208d4037a0..9b3d5dcb18 100644 --- a/src/sunstone/public/js/opennebula.js +++ b/src/sunstone/public/js/opennebula.js @@ -696,6 +696,21 @@ var OpenNebula = { OpenNebula.Action.simple_action(params,OpenNebula.VM.resource, "saveas",action_obj); }, + "snapshot_create": function(params){ + var action_obj = params.data.extra_param; + OpenNebula.Action.simple_action(params,OpenNebula.VM.resource, + "snapshot_create",action_obj); + }, + "snapshot_revert": function(params){ + var action_obj = params.data.extra_param; + OpenNebula.Action.simple_action(params,OpenNebula.VM.resource, + "snapshot_revert",action_obj); + }, + "snapshot_delete": function(params){ + var action_obj = params.data.extra_param; + OpenNebula.Action.simple_action(params,OpenNebula.VM.resource, + "snapshot_delete",action_obj); + }, "vnc" : function(params,startstop){ var callback = params.success; var callback_error = params.error; diff --git a/src/sunstone/public/js/plugins/vms-tab.js b/src/sunstone/public/js/plugins/vms-tab.js index 31c0551ed1..996fa27060 100644 --- a/src/sunstone/public/js/plugins/vms-tab.js +++ b/src/sunstone/public/js/plugins/vms-tab.js @@ -221,6 +221,15 @@ var vm_actions = { }, error: onError }, + "VM.showsnapshots" : { + type: "single", + call: OpenNebula.VM.show, + callback: function(request, vm){ + updateVMachineElement(request, vm); + updateVMSnapshotsInfo(request, vm); + }, + error: onError + }, "VM.refresh" : { type: "custom", call : function (){ @@ -363,6 +372,34 @@ var vm_actions = { notify: true }, + "VM.snapshot_create" : { + type: "single", + call: OpenNebula.VM.snapshot_create, + callback: function(request) { + Sunstone.runAction("VM.showsnapshots", request.request.data[0]); + }, + error:onError, + notify: true + }, + "VM.snapshot_revert" : { + type: "single", + call: OpenNebula.VM.snapshot_revert, + callback: function(request) { + Sunstone.runAction("VM.showsnapshots", request.request.data[0]); + }, + error:onError, + notify: true + }, + "VM.snapshot_delete" : { + type: "single", + call: OpenNebula.VM.snapshot_delete, + callback: function(request) { + Sunstone.runAction("VM.showsnapshots", request.request.data[0]); + }, + error:onError, + notify: true + }, + "VM.shutdown" : { type: "multiple", call: OpenNebula.VM.shutdown, @@ -1244,8 +1281,13 @@ function updateVMInfo(request,vm){ content: printCapacity(vm_info) }; + var snapshot_tab = { + title: tr("Snapshots"), + content: printSnapshots(vm_info) + }; + var template_tab = { - title: tr("VM Template"), + title: tr("Template"), content: '\ '+ @@ -1254,13 +1296,13 @@ function updateVMInfo(request,vm){ }; var log_tab = { - title: tr("VM log"), + title: tr("Log"), content: '
'+spinner+'
' }; var history_tab = { - title: tr("History information"), + title: tr("History"), content: generateHistoryTable(vm_info) }; @@ -1287,9 +1329,10 @@ function updateVMInfo(request,vm){ Sunstone.updateInfoPanelTab("vm_info_panel","vm_capacity_tab",capacity_tab); Sunstone.updateInfoPanelTab("vm_info_panel","vm_hotplugging_tab",hotplugging_tab); Sunstone.updateInfoPanelTab("vm_info_panel","vm_network_tab",network_tab); + Sunstone.updateInfoPanelTab("vm_info_panel","vm_snapshot_tab",snapshot_tab); + Sunstone.updateInfoPanelTab("vm_info_panel","vm_history_tab",history_tab); Sunstone.updateInfoPanelTab("vm_info_panel","vm_template_tab",template_tab); Sunstone.updateInfoPanelTab("vm_info_panel","vm_log_tab",log_tab); - Sunstone.updateInfoPanelTab("vm_info_panel","vm_history_tab",history_tab); // TODO: re-use pool_monitor data? @@ -1546,8 +1589,6 @@ function popUpSaveAsDialog(vm_id, disk_id){ - - function setupAttachDiskDialog(){ dialogs_context.append('
'); $attach_disk_dialog = $('#attach_disk_dialog',dialogs_context); @@ -2105,6 +2146,224 @@ function setup_vm_capacity_tab(){ +function updateVMSnapshotsInfo(request,vm){ + $("li#vm_snapshot_tabTab").html(printSnapshots(vm.VM)); +} + +// Generates the HTML for the snapshot tab +// This is a list of disks with the save_as, detach options. +// And a form to attach a new disk to the VM, if it is running. +function printSnapshots(vm_info){ + var html ='\ +
\ +
\ +
\ +
\ +
' + + // If VM is not RUNNING, then we forget about the attach disk form. + if (vm_info.STATE == "3" && vm_info.LCM_STATE == "3"){ + html += '\ +
' + tr("Take snapshot") +'
' + } else { + html += '\ +
' + tr("Take snapshot") +'
' + } + + html += '\ +
\ +
\ +
' + + html += '\ +
\ +
'+tr("VM template")+'
\ + \ + \ + \ + \ + \ + \ + \ + \ + '; + + + var snapshots = [] + if ($.isArray(vm_info.TEMPLATE.SNAPSHOT)) + snapshots = vm_info.TEMPLATE.SNAPSHOT + else if (!$.isEmptyObject(vm_info.TEMPLATE.SNAPSHOT)) + snapshots = [vm_info.TEMPLATE.SNAPSHOT] + + if (!snapshots.length){ + html += '\ + \ + \ + '; + } + else { + + for (var i = 0; i < snapshots.length; i++){ + var snapshot = snapshots[i]; + + if ( + ( // ACTIVE + vm_info.STATE == "3") && + ( // HOTPLUG_SNAPSHOT + vm_info.LCM_STATE == "24")) { + actions = 'snapshot in progress' + } + else { + actions = ''; + + if ((vm_info.STATE == "3" && vm_info.LCM_STATE == "3")) { + actions += ''+tr("Revert")+'  ' + actions += ''+tr("Delete")+'' + } + } + + html += '\ + \ + \ + \ + \ + \ + '; + } + } + + html += '\ + \ +
'+tr("ID")+''+tr("Name")+''+tr("Timestamp")+''+tr("Actions")+'
' + tr("No snapshots to show") + '
' + snapshot.SNAPSHOT_ID + '' + snapshot.NAME + '' + pretty_time(snapshot.TIME) + '' + actions + '
\ + \ + '; + + return html; +} + +function setupSaveAsDialog(){ + dialogs_context.append('
'); + $save_as_dialog = $('#save_as_dialog',dialogs_context); + var dialog = $save_as_dialog; + + dialog.html('
\ +

\ + '+tr("Snapshot")+'\ +

\ +
\ +
\ +
\ +
\ + \ +
\ +
\ + \ +
\ +
\ +
\ +
\ +
\ +
\ +
\ + \ +
\ +
\ + \ +
\ +
\ +
\ +
\ +
\ +
\ +
\ + \ + \ +
\ + ×\ +
') + + dialog.addClass("reveal-modal"); + setupTips(dialog); + + $('#save_as_form',dialog).submit(function(){ + var vm_id = $('#vm_id', this).val(); + var snapshot_name = $('#snapshot_name', this).val(); + + var obj = { + snapshot_name : snapshot_name + }; + + Sunstone.runAction('VM.snapshot_create', vm_id, obj); + + $save_as_dialog.trigger("reveal:close") + return false; + }); +}; + +function popUpSnapshotDialog(vm_id){ + $('#vm_id',$save_as_dialog).val(vm_id); + $save_as_dialog.reveal(); +} + + + + +// Listeners to the disks operations (detach, saveas, attach) +function setup_vm_snapshot_tab(){ + setupSaveAsDialog(); + + $('a.snapshot_revert').live('click', function(){ + console.log("asdfasd") + var b = $(this); + var vm_id = b.parents('form').attr('vmid'); + var snapshot_id = b.parents('tr').attr('snapshot_id'); + + Sunstone.runAction('VM.snapshot_revert', vm_id, {"snapshot_id": snapshot_id}); + + //b.html(spinner); + return false; + }); + + $('a.snapshot_delete').live('click', function(){ + var b = $(this); + var vm_id = b.parents('form').attr('vmid'); + var snapshot_id = b.parents('tr').attr('snapshot_id'); + + Sunstone.runAction('VM.snapshot_delete', vm_id, {"snapshot_id": snapshot_id}); + + //b.html(spinner); + return false; + }); + + $('#take_snapshot').live('click', function(){ + var b = $(this); + var vm_id = b.parents('form').attr('vmid'); + + popUpSnapshotDialog(vm_id); + + //b.html(spinner); + return false; + }); + + $('#refresh_snapshot').live('click', function(){ + var b = $(this); + var vm_id = b.parents('form').attr('vmid'); + Sunstone.runAction("VM.showsnapshots", vm_id); + + return false; + }); +} + + + + + + + + + + + @@ -2359,6 +2618,7 @@ $(document).ready(function(){ hotpluggingOps(); setup_vm_network_tab(); setup_vm_capacity_tab(); + setup_vm_snapshot_tab(); initCheckAllBoxes(dataTable_vMachines); diff --git a/src/sunstone/public/sass/app.scss b/src/sunstone/public/sass/app.scss index cdcbd03474..6bf3395649 100644 --- a/src/sunstone/public/sass/app.scss +++ b/src/sunstone/public/sass/app.scss @@ -767,7 +767,7 @@ a.footer.button { } .vm_log_error { - color: $alertColor; + color: rgb(255, 141, 129); } .noUiSlider {