From b085ae9591510803cd88b22e17e070bb26ff9482 Mon Sep 17 00:00:00 2001 From: Hector Sanjuan Date: Mon, 25 Apr 2011 11:30:36 +0200 Subject: [PATCH] Feature #564: VNC ready for testing. Added startvnc and stopvnc operations to the server and the opennebula.js so it looks like any other Sunstone action. Reworked error reporting to the standard form in the server. Fixed bugs and did basic testing. --- .../OpenNebulaJSON/VirtualMachineJSON.rb | 60 +++++++++++++++++- src/sunstone/models/SunstoneServer.rb | 23 +++++++ src/sunstone/public/js/opennebula.js | 60 ++++++++++++++++++ src/sunstone/public/js/plugins/vms-tab.js | 63 ++++++++++++++----- src/sunstone/public/js/sunstone-util.js | 6 ++ src/sunstone/sunstone-server.rb | 49 +++++++++++++++ 6 files changed, 245 insertions(+), 16 deletions(-) diff --git a/src/sunstone/models/OpenNebulaJSON/VirtualMachineJSON.rb b/src/sunstone/models/OpenNebulaJSON/VirtualMachineJSON.rb index 55f6cada51..3b6e2a312f 100644 --- a/src/sunstone/models/OpenNebulaJSON/VirtualMachineJSON.rb +++ b/src/sunstone/models/OpenNebulaJSON/VirtualMachineJSON.rb @@ -55,7 +55,9 @@ module OpenNebulaJSON when "restart" then self.restart when "saveas" then self.save_as(action_hash['params']) when "shutdown" then self.shutdown - when "resubmit" then self.resubmit + when "resubmit" then self.resubmit + when "startvnc" then self.startvnc + when "stopvnc" then self.stopvnc else error_msg = "#{action_hash['perform']} action not " << " available for this resource" @@ -118,5 +120,61 @@ module OpenNebulaJSON super(params['disk_id'].to_i, image.id) end + + def startvnc(port) + result = self.info(); + if OpenNebula.is_error?(result) + return result + end + + if self['LCM_STATE'] != "3" + return OpenNebula::Error.new("VM is not running"); + end + + if self['TEMPLATE/GRAPHICS/TYPE'] != "vnc" + return OpenNebula::Error.new("VM has no VNC configured"); + end + + if self['TEMPLATE/GRAPHICS/PORT'] + vnc_port = self['TEMPLATE/GRAPHICS/PORT'] + else + return OpenNebula::Error.new("VM has no VNC port set"); + end + + if self['TEMPLATE/GRAPHICS/PASSWD'] + vnc_pw = self['TEMPLATE/GRAPHICS/PASSWD'] + else + vnc_pw = "" + end + + host = self['HISTORY/HOSTNAME'] + + #we are ready for the party + + final_port = 29876+port.to_i; + + # puts "Launch noVNC on #{final_port} listenting to #{host}:#{vnc_port}" + # So here we launch the noVNC server listening on the final_port + # and serving as proxy for the VM host on the configured VNC port. + # TODO - This path is in public... + pipe = IO.popen("#{File.dirname(__FILE__)}/../../public/vendor/noVNC/utils/launch.sh --listen #{final_port} --vnc #{host}:#{vnc_port}") + + return {:pipe => pipe, :port => final_port, :password => vnc_pw} + + end + + def stopvnc(pipe) + #am I allowed to do something affecting this machine? + result = self.info(); + if OpenNebula.is_error?(result) + return result + end + pid = pipe.pid + # puts "Killing noVNC with pid #{pid}" + Process.kill('KILL',pid) + pipe.close + return + end + end end diff --git a/src/sunstone/models/SunstoneServer.rb b/src/sunstone/models/SunstoneServer.rb index ea690c33a9..c8a33f4140 100644 --- a/src/sunstone/models/SunstoneServer.rb +++ b/src/sunstone/models/SunstoneServer.rb @@ -213,6 +213,29 @@ class SunstoneServer end end + + ######################################################################## + # VNC + ######################################################################## + + def startvnc(id) + rc = retrieve_resource("vm", id) + if OpenNebula.is_error?(rc) + return rc + end + return rc.startvnc(id) + end + + def stopvnc(id,pipe) + rc = retrieve_resource("vm", id) + if OpenNebula.is_error?(rc) + return rc + end + return rc.stopvnc(pipe) + + end + + private def retrieve_resource(kind, id) diff --git a/src/sunstone/public/js/opennebula.js b/src/sunstone/public/js/opennebula.js index ce40c52746..7d822313c7 100644 --- a/src/sunstone/public/js/opennebula.js +++ b/src/sunstone/public/js/opennebula.js @@ -1235,7 +1235,67 @@ var OpenNebula = { } } }); + }, + + "startvnc" : function(params){ + var callback = params.success; + var callback_error = params.error; + var id = params.data.id; + var resource = OpenNebula.VM.resource; + + var method = "startvnc"; + var action = OpenNebula.Helper.action(method); + var request = OpenNebula.Helper.request(resource,method, id); + $.ajax({ + url: "vm/"+id+"/startvnc", + type: "POST", + dataType: "json", + success: function(response) + { + if (callback) + { + callback(request,response); + } + }, + error: function(response) + { + if (callback_error) + { + callback_error(request, OpenNebula.Error(response)); + } + } + }); + }, + + "stopvnc" : function(params){ + var callback = params.success; + var callback_error = params.error; + var id = params.data.id; + var resource = OpenNebula.VM.resource; + + var method = "stopvnc"; + var action = OpenNebula.Helper.action(method); + var request = OpenNebula.Helper.request(resource,method, id); + $.ajax({ + url: "vm/"+id+"/stopvnc", + type: "POST", + success: function() + { + if (callback) + { + callback(request); + } + }, + error: function(response) + { + if (callback_error) + { + callback_error(request, OpenNebula.Error(response)); + } + } + }); } + }, "Cluster": { diff --git a/src/sunstone/public/js/plugins/vms-tab.js b/src/sunstone/public/js/plugins/vms-tab.js index 3b1bd6b383..a16715e849 100644 --- a/src/sunstone/public/js/plugins/vms-tab.js +++ b/src/sunstone/public/js/plugins/vms-tab.js @@ -741,6 +741,21 @@ var vm_actions = { $("#vm_log pre").html(''); onError(request,error_json); } + }, + + "VM.startvnc" : { + type: "single", + call: OpenNebula.VM.startvnc, + callback: vncCallback, + error: onError + }, + + "VM.stopvnc" : { + type: "single", + call: OpenNebula.VM.stopvnc, + callback: null, + error: onError, + notify: true } } @@ -1947,10 +1962,8 @@ function setVMAutorefresh(){ },INTERVAL+someTime()); //so that not all refreshing is done at the same time } -//setups VNC application -function setupVNC(){ - var updateVNCState = function updateState(rfb, state, oldstate, msg) { +function updateVNCState(rfb, state, oldstate, msg) { var s, sb, cad, klass; s = $D('VNC_status'); sb = $D('VNC_status_bar'); @@ -1983,6 +1996,9 @@ function setupVNC(){ } } +//setups VNC application +function setupVNC(){ + //Append to DOM $('div#dialogs').append('
'); @@ -2014,7 +2030,24 @@ function setupVNC(){ resizable:true, }); + $( "#vnc_dialog" ).bind( "dialogclose", function(event, ui) { + var id = $("#vnc_dialog").attr("vm_id"); + Sunstone.runAction("VM.stopvnc",id); + + }); + $('.vnc').live("click",function(){ + //Which VM is it? + var id = $(this).attr("vm_id"); + //Set attribute to dialog + $('#vnc_dialog').attr("vm_id",id); + //Request proxy server start + Sunstone.runAction("VM.startvnc",id); + }); + +} + +function vncCallback(request,response){ rfb = new RFB({'target': $D('VNC_canvas'), 'encrypt': false, 'true_color': true, @@ -2022,26 +2055,26 @@ function setupVNC(){ 'shared': true, 'updateState': updateVNCState}); //fetch things from clicked element host - port - password - var port=$(this).attr("vnc_port"); - var pw=$(this).attr("vnc_pw"); + var port = response["port"]; + + //Hopefully this is returning sunstone server address, where + //the proxy is running + var host = window.location.hostname; + var pw = response["password"]; + rfb.connect(host, port, pw); + //And voilĂ , we can open the dialog $('#vnc_dialog').dialog('open'); - - - }); - + } function vncIcon(vm){ var graphics = vm.TEMPLATE.GRAPHICS; var state = OpenNebula.Helper.resource_state("vm",vm.STATE); - var gr_icon, host, port, password; - if (graphics && graphics.TYPE == "vnc" && state == "ACTIVE" && vm.HISTORY.HOSTNAME){ - host = vm.HISTORY.HOSTNAME; - port = graphics.PORT ? graphics.PORT : ""; - password = graphics.PASSWD ? graphics.PASSWD : ""; - gr_icon = ''; + var gr_icon; + if (graphics && graphics.TYPE == "vnc" && state == "ACTIVE"){ + gr_icon = ''; gr_icon += 'Open VNC Session'; } else { diff --git a/src/sunstone/public/js/sunstone-util.js b/src/sunstone/public/js/sunstone-util.js index 59a2c54434..2979d0459b 100644 --- a/src/sunstone/public/js/sunstone-util.js +++ b/src/sunstone/public/js/sunstone-util.js @@ -185,6 +185,12 @@ function notifyError(msg){ $.jGrowl(msg, {theme: "jGrowl-notify-error", sticky: true }); } +function notifyMessage(msg){ + msg = "

Info

" + msg; + + $.jGrowl(msg, {theme: "jGrowl-notify-submit"}); +} + // Returns an HTML string with the json keys and values in the form // key: value
// It recursively explores objects, and flattens their contents in diff --git a/src/sunstone/sunstone-server.rb b/src/sunstone/sunstone-server.rb index 0f8440a631..ec87db7483 100755 --- a/src/sunstone/sunstone-server.rb +++ b/src/sunstone/sunstone-server.rb @@ -163,6 +163,55 @@ post '/:pool' do @SunstoneServer.create_resource(params[:pool], request.body.read) end +post '/vm/:id/stopvnc' do + vm = params[:id] + vnc_hash = session['vnc'] + + if !vnc_hash + msg = "Ups, no VNC sessions information found. Cannot stop VNC" + return [500, OpenNebula::Error.new(msg).to_json] + elsif !vnc_hash[vm] + msg = "It seems there is no VNC proxy running for this machine" + return [500, OpenNebula::Error.new(msg).to_json] + end + + pipe = session['vnc'][vm] + rc = @SunstoneServer.stopvnc(vm,pipe) + + if (OpenNebula.is_error?(rc)) + return [500, rc.to_json] + else + session['vnc'].delete(vm) + return + end + +end + +post '/vm/:id/startvnc' do + vm = params[:id] + info = @SunstoneServer.startvnc(vm) + + if OpenNebula.is_error?(info) + return [500,info.to_json] + end + + port = info[:port] #port to connect to + pw = info[:password] + pipe = info[:pipe] + + vnc_hash = session['vnc'] + + if !vnc_hash + session['vnc']= {} + elsif vnc_hash and vnc_hash[vm] + return [500, OpenNebula::Error.new("There is a VNC server running for this VM").to_json] + end + + session['vnc'][vm]=pipe; + return {:port => port, :password => pw }.to_json + +end + ############################################################################## # Perform an action on a Resource ##############################################################################