From 9f66d8b2e2064af69c84b1ae64608b7aa3a5df28 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Mon, 13 Feb 2012 17:50:49 +0100 Subject: [PATCH] feature #1069: Minor changes in OpenNebulaVNC class. Return user configuration in JSON --- src/sunstone/OpenNebulaVNC.rb | 74 ++++++++++++++++----------- src/sunstone/models/SunstoneServer.rb | 46 ++--------------- src/sunstone/sunstone-server.rb | 24 ++++----- 3 files changed, 59 insertions(+), 85 deletions(-) diff --git a/src/sunstone/OpenNebulaVNC.rb b/src/sunstone/OpenNebulaVNC.rb index e2e3ac3fde..ef628662f2 100644 --- a/src/sunstone/OpenNebulaVNC.rb +++ b/src/sunstone/OpenNebulaVNC.rb @@ -14,31 +14,38 @@ # limitations under the License. # #--------------------------------------------------------------------------- # - -#This file provides support for launching and stopping a websockify proxy - require 'json' +require 'OpenNebula' +# +# This class provides support for launching and stopping a websockify proxy +# class OpenNebulaVNC - def initialize(config,opt={:json_errors => true}) - @proxy_path = config[:vnc_proxy_path] + def initialize(config, opt={:json_errors => true}) + @proxy_path = config[:vnc_proxy_path] @proxy_base_port = config[:vnc_proxy_base_port].to_i + @wss = config[:vnc_proxy_support_wss] - @enable_wss = (@wss == "yes") || (@wss == "only") || (@wss == true) - @cert = @enable_wss? config[:vnc_proxy_cert] : nil - @key = @enable_wss? config[:vnc_proxy_key] : nil - @options=opt - end - def error(code, msg) - if @options[:json_errors] - return [code,OpenNebula::Error.new(msg).to_json] + if (@wss == "yes") || (@wss == "only") || (@wss == true) + @enable_wss = true + @cert = config[:vnc_proxy_cert] + @key = config[:vnc_proxy_key] else - return [code,msg] + @enable_wss = false end + + @options = opt end + # Start a VNC proxy def start(vm_resource) + # Check configurations and VM attributes + + if @proxy_path == nil || @proxy_path.empty? + return error(403,"VNC proxy not configured") + end + if vm_resource['LCM_STATE'] != "3" return error(403,"VM is not running") end @@ -47,43 +54,50 @@ class OpenNebulaVNC return error(403,"VM has no VNC configured") end - # The VM host and its VNC port - host = vm_resource['/VM/HISTORY_RECORDS/HISTORY[last()]/HOSTNAME'] + # Proxy data + host = vm_resource['/VM/HISTORY_RECORDS/HISTORY[last()]/HOSTNAME'] vnc_port = vm_resource['TEMPLATE/GRAPHICS/PORT'] - # The port on which the proxy will listen - proxy_port = @proxy_base_port + vnc_port.to_i - if !@proxy_path || @proxy_path.size == 0 - return error(403,"VNC proxy not configured") - end + proxy_port = @proxy_base_port + vnc_port.to_i proxy_options = "" if @enable_wss - proxy_options += " --cert #{@cert}" - proxy_options += " --key #{@key}" if @key && @key.size > 0 - proxy_options += " --ssl-only" if @wss == "only" + proxy_options << " --cert #{@cert}" + proxy_options << " --key #{@key}" if @key && @key.size > 0 + proxy_options << " --ssl-only" if @wss == "only" end - proxy_cmd = "#{@proxy_path} #{proxy_options} #{proxy_port} #{host}:#{vnc_port}" + cmd ="#{@proxy_path} #{proxy_options} #{proxy_port} #{host}:#{vnc_port}" begin $stderr.puts("Starting vnc proxy: #{proxy_cmd}") - pipe = IO.popen(proxy_cmd) + pipe = IO.popen(cmd) rescue Exception => e - error = Error.new(e.message) - return [500, error.to_json] + return [500, OpenNebula::Error.new(e.message).to_json] end vnc_pw = vm_resource['TEMPLATE/GRAPHICS/PASSWD'] + info = {:pipe => pipe, :port => proxy_port, :password => vnc_pw} - info = {:pipe => pipe, :port => proxy_port, :password => vnc_pw} return [200, info] end - #handle exceptions outside + # Stop a VNC proxy handle exceptions outside def self.stop(pipe) Process.kill('KILL',pipe.pid) pipe.close end + + private + + def error(code, msg) + if @options[:json_errors] + return [code,OpenNebula::Error.new(msg).to_json] + else + return [code,msg] + end + end + + end diff --git a/src/sunstone/models/SunstoneServer.rb b/src/sunstone/models/SunstoneServer.rb index 359a24267e..0c21f8440d 100644 --- a/src/sunstone/models/SunstoneServer.rb +++ b/src/sunstone/models/SunstoneServer.rb @@ -147,37 +147,6 @@ class SunstoneServer end end - ############################################################################ - # Unused - ############################################################################ - # def get_configuration(user_id) - # if user_id != "0" - # return [401, ""] - # end - - # one_config = VAR_LOCATION + "/config" - # config = Hash.new - - # begin - # cfg = File.read(one_config) - # rescue Exception => e - # error = Error.new("Error reading config: #{e.inspect}") - # return [500, error.to_json] - # end - - # cfg.lines do |line| - # m=line.match(/^([^=]+)=(.*)$/) - - # if m - # name=m[1].strip.upcase - # value=m[2].strip - # config[name]=value - # end - # end - - # return [200, config.to_json] - # end - ############################################################################ # ############################################################################ @@ -202,7 +171,6 @@ class SunstoneServer end end - ######################################################################## # VNC ######################################################################## @@ -233,7 +201,6 @@ class SunstoneServer ############################################################################ # ############################################################################ - def get_monitoring(id, resource, monitor_resources, gid) watch_client = case resource when "vm","VM" @@ -262,18 +229,11 @@ class SunstoneServer return [200, rc.to_json] end - ############################################################################ - # - ############################################################################ - - ############################################################################ - # - ############################################################################ - - - private + ############################################################################ + # + ############################################################################ def retrieve_resource(kind, id) resource = case kind when "group" then GroupJSON.new_with_id(id, @client) diff --git a/src/sunstone/sunstone-server.rb b/src/sunstone/sunstone-server.rb index bb9bdeba03..62081e24f2 100755 --- a/src/sunstone/sunstone-server.rb +++ b/src/sunstone/sunstone-server.rb @@ -227,18 +227,18 @@ post '/logout' do end ############################################################################## -# Config and Logs +# User configuration and VM logs ############################################################################## -#get '/config' do -# @SunstoneServer.get_configuration(session[:user_id]) -#end -get '/config/:opt' do - case params[:opt] - when "lang" then session[:lang] - when "wss" then session[:wss] - else "unknown" - end +get '/config' do + uconf = { + :user_config => { + :lang => session[:lang] + :wss => session[:wss] + } + } + + [200, uconf.json] end post '/config' do @@ -250,8 +250,8 @@ post '/config' do body.each do | key,value | case key - when "lang" then session[:lang]=value - when "wss" then session[:wss]=value + when "lang" then session[:lang]= value + when "wss" then session[:wss] = value end end end