mirror of
https://github.com/OpenNebula/one.git
synced 2026-11-20 20:02:38 +09:00
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.
This commit is contained in:
1 parent
5fcb9de8e8
commit
b085ae9591
6 files changed
+245
-16
No files matched your search
@@ -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
|
||||
@@ -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)
|
||||
|
||||
@@ -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": {
|
||||
|
||||
@@ -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('<div id="vnc_dialog" title="VNC connection"></div>');
|
||||
|
||||
@@ -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 = '<a class="vnc" href="#" vnc_host="'+host+'" vnc_port="'+port+'" vnc_pw="'+password+'">';
|
||||
var gr_icon;
|
||||
if (graphics && graphics.TYPE == "vnc" && state == "ACTIVE"){
|
||||
gr_icon = '<a class="vnc" href="#" vm_id="'+vm.ID+'">';
|
||||
gr_icon += '<img src="images/vnc_on.png" alt="Open VNC Session" /></a>';
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -185,6 +185,12 @@ function notifyError(msg){
|
||||
$.jGrowl(msg, {theme: "jGrowl-notify-error", sticky: true });
|
||||
}
|
||||
|
||||
function notifyMessage(msg){
|
||||
msg = "<h1>Info</h1>" + msg;
|
||||
|
||||
$.jGrowl(msg, {theme: "jGrowl-notify-submit"});
|
||||
}
|
||||
|
||||
// Returns an HTML string with the json keys and values in the form
|
||||
// key: value<br />
|
||||
// It recursively explores objects, and flattens their contents in
|
||||
|
||||
@@ -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
|
||||
##############################################################################
|
||||
|
||||
Reference in new issue
Block a user