From 34392d2f87c0bf4cdde7a76e6f3f57ef7af34cf9 Mon Sep 17 00:00:00 2001 From: Jorge Miguel Lobo Escalona Date: Wed, 30 Mar 2022 11:20:59 +0200 Subject: [PATCH] B #~: fix header VMRC sunstone (#1891) --- src/sunstone/models/SunstoneServer.rb | 28 +++ src/sunstone/models/sunstone_vmrc.rb | 64 +++++++ src/sunstone/public/app/opennebula/vm.js | 52 ++++-- .../public/app/tabs/vms-tab/actions.js | 172 +++++++++--------- src/sunstone/sunstone-server.rb | 13 ++ 5 files changed, 224 insertions(+), 105 deletions(-) create mode 100644 src/sunstone/models/sunstone_vmrc.rb diff --git a/src/sunstone/models/SunstoneServer.rb b/src/sunstone/models/SunstoneServer.rb index cf0df91257..5092f040d2 100644 --- a/src/sunstone/models/SunstoneServer.rb +++ b/src/sunstone/models/SunstoneServer.rb @@ -21,6 +21,7 @@ include OpenNebulaJSON require 'sunstone_vnc' require 'sunstone_guac' +require 'sunstone_vmrc' require 'sunstone_vm_helper' require 'OpenNebulaAddons' require 'OpenNebulaJSON/JSONUtils' @@ -343,6 +344,33 @@ class SunstoneServer < CloudServer return guac.proxy(resource, type_connection, client) end + ######################################################################## + # VMRC + ######################################################################## + def startvmrc(id, vmrc, _client=nil) + resource = retrieve_resource("vm", id) + if OpenNebula.is_error?(resource) + return [404, resource.to_json] + end + vm_pool = VirtualMachinePool.new(@client, -1) + user_pool = UserPool.new(@client) + + rc = user_pool.info + if OpenNebula.is_error?(rc) + puts rc.message + exit -1 + end + + rc = vm_pool.info + if OpenNebula.is_error?(rc) + puts rc.message + exit -1 + end + + client = _client.nil? ? @client : _client + return vmrc.proxy(resource, client) + end + ######################################################################## # Accounting & Monitoring ######################################################################## diff --git a/src/sunstone/models/sunstone_vmrc.rb b/src/sunstone/models/sunstone_vmrc.rb new file mode 100644 index 0000000000..86186e2f6c --- /dev/null +++ b/src/sunstone/models/sunstone_vmrc.rb @@ -0,0 +1,64 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2021, OpenNebula Project, OpenNebula Systems # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +#----------------------------------------------------------------------------# +# This class provides support for launching and stopping a vmrc proxy server # +#----------------------------------------------------------------------------# + +require 'rubygems' +require 'json' +require 'opennebula' +require 'base64' +require 'openssl' +require 'vcenter_driver' +require 'fileutils' +require 'sunstone_remotes' + +# Class for necessary VMRC ticket creation +class SunstoneVMRC < SunstoneRemoteConnections + + attr_reader :proxy_port + + def initialize(logger, options = {}) + super + end + + def proxy(vm_resource, client = nil) + # Check configurations and VM attributes + unless allowed_console_states.include?(vm_resource['LCM_STATE']) + error_message = "Wrong state (#{vm_resource['LCM_STATE']}) to + open a VMRC session" + return error(400, error_message) + end + + unless vm_resource['USER_TEMPLATE/HYPERVISOR'] == 'vcenter' + return error(400, 'VMRC Connection is only for vcenter hipervisor') + end + + unless vm_resource['MONITORING/VCENTER_ESX_HOST'] + error_message = 'Could not determine the vCenter ESX host where + the VM is running. Wait till the VCENTER_ESX_HOST attribute is + retrieved once the host has been monitored' + return error(400, error_message) + end + + info = SunstoneVMHelper.get_remote_info(vm_resource) + encode_info = Base64.encode64(info.to_json) + + [200, { :info => encode_info }.to_json] + end + +end diff --git a/src/sunstone/public/app/opennebula/vm.js b/src/sunstone/public/app/opennebula/vm.js index 317a91e7a9..89beaaa54c 100644 --- a/src/sunstone/public/app/opennebula/vm.js +++ b/src/sunstone/public/app/opennebula/vm.js @@ -23,7 +23,7 @@ define(function(require) { Locale = require("utils/locale"), Config = require("sunstone-config"), Navigation = require("utils/navigation"), - OpenNebulaHost = require('opennebula/host'); + OpenNebulaHost = require("opennebula/host"); var RESOURCE = "VM"; @@ -634,11 +634,25 @@ define(function(require) { var request = OpenNebulaHelper.request(resource, null, params.data); $.ajax({ - url: Config.publicFireedgeEndpoint + "/fireedge/api/vcenter/token/" + vm_id, - type: "GET", - headers: {"Authorization": fireedge_token}, - success: function(response) { - return callback ? callback(request, response) : null; + url: "vm/" + vm_id + "/startvmrc", + type: "POST", + dataType: "JSON", + success: function(vminfo) { + + $.ajax({ + url: Config.publicFireedgeEndpoint + "/fireedge/api/vcenter/token/" + vm_id, + type: "GET", + headers: {"Authorization": fireedge_token}, + success: function(token) { + token.info = vminfo.info; + return callback ? callback(request, token) : null; + }, + error: function(response) { + return callback_error ? + callback_error(request, OpenNebulaError(response)) : null; + } + }); + }, error: function(response) { return callback_error ? @@ -770,10 +784,10 @@ define(function(require) { } }, "hostnameStr": function(element) { - return hostnameStr(element) + return hostnameStr(element); }, "hostnameStrLink": function(element) { - return hostnameStr(element, true) + return hostnameStr(element, true); }, "clusterStr": function(element) { var state = element.STATE; @@ -867,17 +881,17 @@ define(function(require) { })[0]; if (nic) { - var ip = '' + hostnameStr(vm, navigationLink) + ''; - var externalPortRange = '' + nic.EXTERNAL_PORT_RANGE + ''; - var internalPortRange = '' + nic.INTERNAL_PORT_RANGE.split('/')[0].replace('-', ':') + '' + var ip = "" + hostnameStr(vm, navigationLink) + ""; + var externalPortRange = "" + nic.EXTERNAL_PORT_RANGE + ""; + var internalPortRange = "" + nic.INTERNAL_PORT_RANGE.split("/")[0].replace("-", ":") + ""; - return ip + ' ports ' + externalPortRange + ' forwarded to VM ports ' + internalPortRange; + return ip + " ports " + externalPortRange + " forwarded to VM ports " + internalPortRange; } } function _promiseGetVm(options = {}) { options = $.extend({ - id: '', + id: "", async: true }, options); @@ -1067,7 +1081,7 @@ define(function(require) { else if (ips.length === 1) return "

"+ips[0]+"

"; - var sshWithPortForwarding = getSshWithPortForwarding(element) || ''; + var sshWithPortForwarding = getSshWithPortForwarding(element) || ""; var firstIP = ipsHtml.split("")[0]; ipsHtml = ipsHtml.split("")[1]; ipsHtml = @@ -1127,7 +1141,7 @@ define(function(require) { var ip = nic.EXTERNAL_IP || nic.IP || nic.IP6 || nic.MAC || nic.IP6_ULA + " " + identation + nic.IP6_GLOBAL; nic_and_ip = nic.NIC_ID + ": " + ip; if (nic.EXTERNAL_IP) - nic_and_ip = "" + nic_and_ip + "" + nic_and_ip = "" + nic_and_ip + ""; column.append(nic_and_ip + ""); first=false; } @@ -1163,9 +1177,9 @@ define(function(require) { var alias_ip; if (alias.IP) - alias_ip = identation + "> " + alias.IP + alias_ip = identation + "> " + alias.IP; else if (alias.IP6) - alias_ip = identation + "> " + alias.IP6 + alias_ip = identation + "> " + alias.IP6; else if (alias.IP6_ULA && alias.IP6_GLOBAL) alias_ip = alias.IP6_ULA + " " + identation + "> " + alias.IP6_GLOBAL; @@ -1224,9 +1238,9 @@ define(function(require) { var alias_ip; if (alias.IP) - alias_ip = identation + "> " + alias.IP + alias_ip = identation + "> " + alias.IP; else if (alias.IP6) - alias_ip = identation + "> " + alias.IP6 + alias_ip = identation + "> " + alias.IP6; else if (alias.IP6_ULA && alias.IP6_GLOBAL) alias_ip = alias.IP6_ULA + "
" + identation + "> " + alias.IP6_GLOBAL; diff --git a/src/sunstone/public/app/tabs/vms-tab/actions.js b/src/sunstone/public/app/tabs/vms-tab/actions.js index 65afdfa83c..8f8f248416 100644 --- a/src/sunstone/public/app/tabs/vms-tab/actions.js +++ b/src/sunstone/public/app/tabs/vms-tab/actions.js @@ -15,23 +15,23 @@ /* -------------------------------------------------------------------------- */ define(function(require) { - var Config = require('sunstone-config'); - var Sunstone = require('sunstone'); - var Notifier = require('utils/notifier'); - var Locale = require('utils/locale'); - var OpenNebulaVM = require('opennebula/vm'); - var CommonActions = require('utils/common-actions'); - var Files = require('utils/files'); - var RemoteActions = require('utils/remote-actions'); + var Config = require("sunstone-config"); + var Sunstone = require("sunstone"); + var Notifier = require("utils/notifier"); + var Locale = require("utils/locale"); + var OpenNebulaVM = require("opennebula/vm"); + var CommonActions = require("utils/common-actions"); + var Files = require("utils/files"); + var RemoteActions = require("utils/remote-actions"); - var CREATE_APP_DIALOG_ID = require('tabs/marketplaceapps-tab/form-panels/create/formPanelId'); - var CREATE_DIALOG_ID = require('./form-panels/create/formPanelId'); - var DEPLOY_DIALOG_ID = require('./dialogs/deploy/dialogId'); - var MARKETPLACEAPPS_TAB_ID = require('tabs/marketplaceapps-tab/tabId'); - var MIGRATE_DIALOG_ID = require('./dialogs/migrate/dialogId'); - var SAVE_AS_TEMPLATE_DIALOG_ID = require('./dialogs/saveas-template/dialogId'); - var TAB_ID = require('./tabId'); - var UPDATECONF_FORM_ID = require('./form-panels/updateconf/formPanelId'); + var CREATE_APP_DIALOG_ID = require("tabs/marketplaceapps-tab/form-panels/create/formPanelId"); + var CREATE_DIALOG_ID = require("./form-panels/create/formPanelId"); + var DEPLOY_DIALOG_ID = require("./dialogs/deploy/dialogId"); + var MARKETPLACEAPPS_TAB_ID = require("tabs/marketplaceapps-tab/tabId"); + var MIGRATE_DIALOG_ID = require("./dialogs/migrate/dialogId"); + var SAVE_AS_TEMPLATE_DIALOG_ID = require("./dialogs/saveas-template/dialogId"); + var TAB_ID = require("./tabId"); + var UPDATECONF_FORM_ID = require("./form-panels/updateconf/formPanelId"); var XML_ROOT = "VM"; var RESOURCE = "VM"; @@ -55,13 +55,13 @@ define(function(require) { $(".provision_refresh_info", ".provision_list_vms").click(); } else { Sunstone.getDataTable(TAB_ID).updateElement(request, response); - if (Sunstone.rightInfoVisible($('#' + TAB_ID))) { + if (Sunstone.rightInfoVisible($("#" + TAB_ID))) { Sunstone.insertPanels(TAB_ID, response); } } - if(response && - response.VM && - response.VM.USER_TEMPLATE && + if(response && + response.VM && + response.VM.USER_TEMPLATE && response.VM.USER_TEMPLATE.HYPERVISOR && response.VM.USER_TEMPLATE.HYPERVISOR === "firecracker"){ $(".not_firecracker").addClass("hide"); @@ -70,57 +70,57 @@ define(function(require) { error: Notifier.onError }, "VM.refresh": _commonActions.refresh(), - "VM.chown": _commonActions.multipleAction('chown'), - "VM.chgrp": _commonActions.multipleAction('chgrp'), - "VM.hold": _commonActions.multipleAction('hold'), - "VM.release": _commonActions.multipleAction('release'), - "VM.suspend": _commonActions.multipleAction('suspend'), - "VM.resume": _commonActions.multipleAction('resume'), - "VM.stop": _commonActions.multipleAction('stop'), - "VM.reboot": _commonActions.multipleAction('reboot'), - "VM.reboot_hard": _commonActions.multipleAction('reboot_hard'), - "VM.poweroff": _commonActions.multipleAction('poweroff'), - "VM.poweroff_hard": _commonActions.multipleAction('poweroff_hard'), - "VM.undeploy": _commonActions.multipleAction('undeploy'), - "VM.undeploy_hard": _commonActions.multipleAction('undeploy_hard'), - "VM.terminate": _commonActions.multipleAction('terminate'), - "VM.terminate_hard": _commonActions.multipleAction('terminate_hard'), - "VM.recover": _commonActions.multipleAction('recover'), - "VM.resched": _commonActions.multipleAction('resched'), - "VM.unresched": _commonActions.multipleAction('unresched'), - "VM.lockM": _commonActions.multipleAction('lock', false), - "VM.lockU": _commonActions.multipleAction('lock', false), - "VM.lockA": _commonActions.multipleAction('lock', false), - "VM.unlock": _commonActions.multipleAction('unlock', false), + "VM.chown": _commonActions.multipleAction("chown"), + "VM.chgrp": _commonActions.multipleAction("chgrp"), + "VM.hold": _commonActions.multipleAction("hold"), + "VM.release": _commonActions.multipleAction("release"), + "VM.suspend": _commonActions.multipleAction("suspend"), + "VM.resume": _commonActions.multipleAction("resume"), + "VM.stop": _commonActions.multipleAction("stop"), + "VM.reboot": _commonActions.multipleAction("reboot"), + "VM.reboot_hard": _commonActions.multipleAction("reboot_hard"), + "VM.poweroff": _commonActions.multipleAction("poweroff"), + "VM.poweroff_hard": _commonActions.multipleAction("poweroff_hard"), + "VM.undeploy": _commonActions.multipleAction("undeploy"), + "VM.undeploy_hard": _commonActions.multipleAction("undeploy_hard"), + "VM.terminate": _commonActions.multipleAction("terminate"), + "VM.terminate_hard": _commonActions.multipleAction("terminate_hard"), + "VM.recover": _commonActions.multipleAction("recover"), + "VM.resched": _commonActions.multipleAction("resched"), + "VM.unresched": _commonActions.multipleAction("unresched"), + "VM.lockM": _commonActions.multipleAction("lock", false), + "VM.lockU": _commonActions.multipleAction("lock", false), + "VM.lockA": _commonActions.multipleAction("lock", false), + "VM.unlock": _commonActions.multipleAction("unlock", false), - "VM.chmod": _commonActions.singleAction('chmod'), - "VM.rename": _commonActions.singleAction('rename'), + "VM.chmod": _commonActions.singleAction("chmod"), + "VM.rename": _commonActions.singleAction("rename"), "VM.update_template": _commonActions.updateTemplate(), "VM.append_template": _commonActions.appendTemplate(), - "VM.deploy_action": _commonActions.singleAction('deploy'), - "VM.migrate_action": _commonActions.singleAction('migrate'), - "VM.migrate_poff_action": _commonActions.singleAction('migrate_poff'), - "VM.migrate_poff_hard_action": _commonActions.singleAction('migrate_poff_hard'), - "VM.migrate_live_action": _commonActions.singleAction('livemigrate'), - "VM.attachdisk": _commonActions.singleAction('attachdisk'), - "VM.detachdisk": _commonActions.singleAction('detachdisk'), - "VM.attachnic": _commonActions.singleAction('attachnic'), - "VM.detachnic": _commonActions.singleAction('detachnic'), - "VM.resize": _commonActions.singleAction('resize'), - "VM.disk_resize": _commonActions.singleAction('disk_resize'), - "VM.snapshot_create": _commonActions.singleAction('snapshot_create'), - "VM.snapshot_revert": _commonActions.singleAction('snapshot_revert'), - "VM.snapshot_delete": _commonActions.singleAction('snapshot_delete'), - "VM.disk_snapshot_create": _commonActions.singleAction('disk_snapshot_create'), - "VM.disk_snapshot_revert": _commonActions.singleAction('disk_snapshot_revert'), - "VM.disk_snapshot_rename": _commonActions.singleAction('disk_snapshot_rename'), - "VM.disk_snapshot_delete": _commonActions.singleAction('disk_snapshot_delete'), - "VM.disk_saveas" : _commonActions.singleAction('disk_saveas'), - "VM.sched_action_add" : _commonActions.singleAction('sched_action_add'), - "VM.sched_action_delete" : _commonActions.singleAction('sched_action_delete'), - "VM.sched_action_update" : _commonActions.singleAction('sched_action_update'), - "VM.attachsg" : _commonActions.singleAction('attachsg'), - "VM.detachsg" : _commonActions.singleAction('detachsg'), + "VM.deploy_action": _commonActions.singleAction("deploy"), + "VM.migrate_action": _commonActions.singleAction("migrate"), + "VM.migrate_poff_action": _commonActions.singleAction("migrate_poff"), + "VM.migrate_poff_hard_action": _commonActions.singleAction("migrate_poff_hard"), + "VM.migrate_live_action": _commonActions.singleAction("livemigrate"), + "VM.attachdisk": _commonActions.singleAction("attachdisk"), + "VM.detachdisk": _commonActions.singleAction("detachdisk"), + "VM.attachnic": _commonActions.singleAction("attachnic"), + "VM.detachnic": _commonActions.singleAction("detachnic"), + "VM.resize": _commonActions.singleAction("resize"), + "VM.disk_resize": _commonActions.singleAction("disk_resize"), + "VM.snapshot_create": _commonActions.singleAction("snapshot_create"), + "VM.snapshot_revert": _commonActions.singleAction("snapshot_revert"), + "VM.snapshot_delete": _commonActions.singleAction("snapshot_delete"), + "VM.disk_snapshot_create": _commonActions.singleAction("disk_snapshot_create"), + "VM.disk_snapshot_revert": _commonActions.singleAction("disk_snapshot_revert"), + "VM.disk_snapshot_rename": _commonActions.singleAction("disk_snapshot_rename"), + "VM.disk_snapshot_delete": _commonActions.singleAction("disk_snapshot_delete"), + "VM.disk_saveas" : _commonActions.singleAction("disk_saveas"), + "VM.sched_action_add" : _commonActions.singleAction("sched_action_add"), + "VM.sched_action_delete" : _commonActions.singleAction("sched_action_delete"), + "VM.sched_action_update" : _commonActions.singleAction("sched_action_update"), + "VM.attachsg" : _commonActions.singleAction("attachsg"), + "VM.detachsg" : _commonActions.singleAction("detachsg"), "VM.create_dialog" : { type: "custom", @@ -195,7 +195,7 @@ define(function(require) { type: "custom", call: function(args) { var vm = Sunstone.getElementRightInfo(TAB_ID); - var rdpIp = OpenNebulaVM.isConnectionSupported(vm, 'rdp'); + var rdpIp = OpenNebulaVM.isConnectionSupported(vm, "rdp"); if (args && args.ip && args.name) { var credentials = {}; @@ -268,11 +268,11 @@ define(function(require) { callback: function(request, response) { var link = RemoteActions.getLink(response,{ port: Config.vncProxyPort, - connnection_type: 'vnc', + connnection_type: "vnc", extra_params: [ - 'port=' + Config.vncProxyPort, - 'encrypt=' + Config.vncWSS, - !Config.requestVNCPassword && 'password=' + response.password + "port=" + Config.vncProxyPort, + "encrypt=" + Config.vncWSS, + !Config.requestVNCPassword && "password=" + response.password ] }); // Open in a new tab the noVNC connection @@ -301,8 +301,8 @@ define(function(require) { var link = RemoteActions.getLink(response,{ host: fireedge_endpoint.hostname, port: fireedge_endpoint.port, - connnection_type: 'vmrc', - extra_path: '/fireedge/vmrc/' + response.data, + connnection_type: "vmrc", + extra_path: "/fireedge/vmrc/" + response.data, }); // Open in a new tab the VMRC connection window.open(link); @@ -325,11 +325,11 @@ define(function(require) { call: OpenNebulaVM.vnc, callback: function(request, response) { var link = RemoteActions.getLink(response, { - port: Config.vncProxyPort, - connnection_type: 'spice', + port: Config.vncProxyPort, + connnection_type: "spice", extra_params: [ - 'password=' + response.password, - 'encrypt=' + config.user_config.vnc_wss, + "password=" + response.password, + "encrypt=" + config.user_config.vnc_wss, ] }); // Open in a new tab the SPICE connection @@ -344,7 +344,7 @@ define(function(require) { type: "custom", call: function() { $.each(Sunstone.getDataTable(TAB_ID).elements(), function(_, elem) { - Sunstone.runAction("VM.startguac_action", elem, 'vnc'); + Sunstone.runAction("VM.startguac_action", elem, "vnc"); }); }, error: function(req, resp) { @@ -355,7 +355,7 @@ define(function(require) { type: "custom", call: function() { $.each(Sunstone.getDataTable(TAB_ID).elements(), function(_, elem) { - Sunstone.runAction("VM.startguac_action", elem, 'rdp'); + Sunstone.runAction("VM.startguac_action", elem, "rdp"); }); }, error: function(req, resp) { @@ -366,7 +366,7 @@ define(function(require) { type: "custom", call: function() { $.each(Sunstone.getDataTable(TAB_ID).elements(), function(_, elem) { - Sunstone.runAction("VM.startguac_action", elem, 'ssh'); + Sunstone.runAction("VM.startguac_action", elem, "ssh"); }); }, error: function(req, resp) { @@ -380,9 +380,9 @@ define(function(require) { var protocolConnection = request.request.data[0].extra_param; var link = RemoteActions.getLink(response, { - connnection_type: 'guac', - extra_path: '/fireedge/guacamole', - extra_params: ['type=' + protocolConnection] + connnection_type: "guac", + extra_path: "/fireedge/guacamole", + extra_params: ["type=" + protocolConnection] }); // Open in a new tab the noVNC connection window.open(link); @@ -431,7 +431,7 @@ define(function(require) { return false; } - var resourceId = '' + selectedNodes[0]; + var resourceId = "" + selectedNodes[0]; OpenNebulaVM.show({ data : { diff --git a/src/sunstone/sunstone-server.rb b/src/sunstone/sunstone-server.rb index 075a435d38..653d7f325e 100755 --- a/src/sunstone/sunstone-server.rb +++ b/src/sunstone/sunstone-server.rb @@ -284,9 +284,13 @@ $vnc = SunstoneVNC.new($conf, logger) #init Guacamole proxy $guac = SunstoneGuac.new(logger) +#init VMRC proxy +$vmrc = SunstoneVMRC.new(logger) + configure do set :run, false set :vnc, $vnc + set :vmrc, $vmrc set :erb, :trim => '-' end @@ -1209,6 +1213,15 @@ post '/vm/:id/guac/:type' do @SunstoneServer.startguac(vm_id, type_connection, $guac, user) end +############################################################################## +# Start VMRC Session for a target VM +############################################################################## +post '/vm/:id/startvmrc' do + vm_id = params[:id] + serveradmin_client = $cloud_auth.client(nil, session[:active_zone_endpoint]) + @SunstoneServer.startvmrc(vm_id, $vmrc, serveradmin_client) +end + ############################################################################## # Perform an action on a Resource ##############################################################################