diff --git a/install.sh b/install.sh index 05923925db..f6c8d83463 100755 --- a/install.sh +++ b/install.sh @@ -317,6 +317,10 @@ VAR_DIRS="$VAR_LOCATION/remotes \ $VAR_LOCATION/remotes/im/az-probes.d/vm/monitor \ $VAR_LOCATION/remotes/im/az-probes.d/vm/status \ $VAR_LOCATION/remotes/im/one.d \ + $VAR_LOCATION/remotes/im/one-probes.d/host/monitor \ + $VAR_LOCATION/remotes/im/one-probes.d/host/system \ + $VAR_LOCATION/remotes/im/one-probes.d/vm/monitor \ + $VAR_LOCATION/remotes/im/one-probes.d/vm/status \ $VAR_LOCATION/remotes/im/packet.d \ $VAR_LOCATION/remotes/pm \ $VAR_LOCATION/remotes/pm/ec2 \ @@ -528,6 +532,10 @@ INSTALL_FILES=( IM_PROBES_EC2_HOST_SYSTEM_FILES:$VAR_LOCATION/remotes/im/ec2-probes.d/host/system IM_PROBES_EC2_VM_MONITOR_FILES:$VAR_LOCATION/remotes/im/ec2-probes.d/vm/monitor IM_PROBES_EC2_VM_STATUS_FILES:$VAR_LOCATION/remotes/im/ec2-probes.d/vm/status + IM_PROBES_ONE_HOST_MONITOR_FILES:$VAR_LOCATION/remotes/im/one-probes.d/host/monitor + IM_PROBES_ONE_HOST_SYSTEM_FILES:$VAR_LOCATION/remotes/im/one-probes.d/host/system + IM_PROBES_ONE_VM_MONITOR_FILES:$VAR_LOCATION/remotes/im/one-probes.d/vm/monitor + IM_PROBES_ONE_VM_STATUS_FILES:$VAR_LOCATION/remotes/im/one-probes.d/vm/status IM_PROBES_VERSION:$VAR_LOCATION/remotes IM_PROBES_FIRECRACKER_HOST_BEACON_FILES:$VAR_LOCATION/remotes/im/firecracker-probes.d/host/beacon IM_PROBES_FIRECRACKER_HOST_MONITOR_FILES:$VAR_LOCATION/remotes/im/firecracker-probes.d/host/monitor @@ -1140,7 +1148,6 @@ VMM_EXEC_ONE_SCRIPTS="src/vmm_mad/remotes/one/cancel \ src/vmm_mad/remotes/one/reboot \ src/vmm_mad/remotes/one/reset \ src/vmm_mad/remotes/one/save \ - src/vmm_mad/remotes/one/poll \ src/vmm_mad/remotes/one/shutdown \ src/vmm_mad/remotes/one/reconfigure \ src/vmm_mad/remotes/one/prereconfigure" @@ -1333,7 +1340,24 @@ IM_PROBES_AZ_VM_MONITOR_FILES="\ IM_PROBES_AZ_VM_STATUS_FILES="\ src/im_mad/remotes/az-probes.d/vm/status/probe_vm_status.rb" -IM_PROBES_ONE_FILES="src/im_mad/remotes/one.d/poll" +# ONE monitord-client +IM_PROBES_ONE_FILES="\ + src/im_mad/remotes/one.d/monitord-client_control.sh \ + src/im_mad/remotes/one.d/monitord-client.rb" + +# ONE probes +IM_PROBES_ONE_HOST_MONITOR_FILES="\ + src/im_mad/remotes/one-probes.d/host/monitor/probe_host_monitor.rb" + +IM_PROBES_ONE_HOST_SYSTEM_FILES="\ + src/im_mad/remotes/one-probes.d/host/system/probe_host_system.rb" + +IM_PROBES_ONE_VM_MONITOR_FILES="\ + src/im_mad/remotes/one-probes.d/vm/monitor/probe_vm_monitor.rb" + +IM_PROBES_ONE_VM_STATUS_FILES="\ + src/im_mad/remotes/one-probes.d/vm/status/probe_vm_status.rb" + IM_PROBES_PACKET_FILES="src/im_mad/remotes/packet.d/poll" diff --git a/src/im_mad/remotes/one-probes.d/host/monitor/probe_host_monitor.rb b/src/im_mad/remotes/one-probes.d/host/monitor/probe_host_monitor.rb new file mode 100755 index 0000000000..2b00bd5a07 --- /dev/null +++ b/src/im_mad/remotes/one-probes.d/host/monitor/probe_host_monitor.rb @@ -0,0 +1,45 @@ +#!/usr/bin/env ruby + +# -------------------------------------------------------------------------- # +# Copyright 2002-2019, 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. # +# -------------------------------------------------------------------------- # + +ONE_LOCATION = ENV['ONE_LOCATION'] unless defined? ONE_LOCATION + +if !ONE_LOCATION + RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby' + GEMS_LOCATION ||= '/usr/share/one/gems' +else + RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby' + GEMS_LOCATION ||= ONE_LOCATION + '/share/gems' +end + +if File.directory?(GEMS_LOCATION) + Gem.use_paths(GEMS_LOCATION) +end + +$LOAD_PATH << RUBY_LIB_LOCATION + +require 'opennebula_driver' +host = ARGV[-1] +host_id = ARGV[-2] + +one2one_drv = One2OneDriver.new(host, host_id) + +begin + puts one2one_drv.probe_host_monitor +rescue StandardError => e + OpenNebula.handle_driver_exception('im probe_host_monitor', e, host) +end diff --git a/src/im_mad/remotes/one-probes.d/host/system/probe_host_system.rb b/src/im_mad/remotes/one-probes.d/host/system/probe_host_system.rb new file mode 100755 index 0000000000..9dbaf22c25 --- /dev/null +++ b/src/im_mad/remotes/one-probes.d/host/system/probe_host_system.rb @@ -0,0 +1,45 @@ +#!/usr/bin/env ruby + +# -------------------------------------------------------------------------- # +# Copyright 2002-2019, 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. # +# -------------------------------------------------------------------------- # + +ONE_LOCATION = ENV['ONE_LOCATION'] unless defined? ONE_LOCATION + +if !ONE_LOCATION + RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby' + GEMS_LOCATION ||= '/usr/share/one/gems' +else + RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby' + GEMS_LOCATION ||= ONE_LOCATION + '/share/gems' +end + +if File.directory?(GEMS_LOCATION) + Gem.use_paths(GEMS_LOCATION) +end + +$LOAD_PATH << RUBY_LIB_LOCATION + +require 'opennebula_driver' +host = ARGV[-1] +host_id = ARGV[-2] + +one2one_drv = One2OneDriver.new(host, host_id) + +begin + puts one2one_drv.probe_host_system +rescue StandardError => e + OpenNebula.handle_driver_exception('im probe_host_system', e, host) +end diff --git a/src/im_mad/remotes/one.d/poll b/src/im_mad/remotes/one-probes.d/vm/monitor/probe_vm_monitor.rb similarity index 75% rename from src/im_mad/remotes/one.d/poll rename to src/im_mad/remotes/one-probes.d/vm/monitor/probe_vm_monitor.rb index d5fa3e2388..fc3fbb1e10 100755 --- a/src/im_mad/remotes/one.d/poll +++ b/src/im_mad/remotes/one-probes.d/vm/monitor/probe_vm_monitor.rb @@ -16,14 +16,14 @@ # limitations under the License. # # -------------------------------------------------------------------------- # -ONE_LOCATION = ENV['ONE_LOCATION'] if !defined?(ONE_LOCATION) +ONE_LOCATION = ENV['ONE_LOCATION'] unless defined? ONE_LOCATION if !ONE_LOCATION - RUBY_LIB_LOCATION = '/usr/lib/one/ruby' if !defined?(RUBY_LIB_LOCATION) - GEMS_LOCATION = '/usr/share/one/gems' if !defined?(GEMS_LOCATION) + RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby' + GEMS_LOCATION ||= '/usr/share/one/gems' else - RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' if !defined?(RUBY_LIB_LOCATION) - GEMS_LOCATION = ONE_LOCATION + '/share/gems' if !defined?(GEMS_LOCATION) + RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby' + GEMS_LOCATION ||= ONE_LOCATION + '/share/gems' end if File.directory?(GEMS_LOCATION) @@ -36,7 +36,10 @@ require 'opennebula_driver' host = ARGV[-1] host_id = ARGV[-2] -one2one_drv = OpenNebulaDriver.new(host, host_id) - -one2one_drv.monitor_all_vms +one2one_drv = One2OneDriver.new(host, host_id) +begin + puts one2one_drv.probe_vm_monitor +rescue StandardError => e + OpenNebula.handle_driver_exception('im probe_vm_monitor', e, host) +end diff --git a/src/im_mad/remotes/one-probes.d/vm/status/probe_vm_status.rb b/src/im_mad/remotes/one-probes.d/vm/status/probe_vm_status.rb new file mode 100755 index 0000000000..bef127d081 --- /dev/null +++ b/src/im_mad/remotes/one-probes.d/vm/status/probe_vm_status.rb @@ -0,0 +1,49 @@ +#!/usr/bin/env ruby + +# -------------------------------------------------------------------------- # +# Copyright 2002-2019, 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. # +# -------------------------------------------------------------------------- # + +ONE_LOCATION = ENV['ONE_LOCATION'] unless defined? ONE_LOCATION + +if !ONE_LOCATION + RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby' + GEMS_LOCATION ||= '/usr/share/one/gems' +else + RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby' + GEMS_LOCATION ||= ONE_LOCATION + '/share/gems' +end + +if File.directory?(GEMS_LOCATION) + Gem.use_paths(GEMS_LOCATION) +end + +$LOAD_PATH << RUBY_LIB_LOCATION + +require 'opennebula_driver' +require_relative '../../../lib/probe_db' + +host = ARGV[-1] +host_id = ARGV[-2] + +begin + vmdb = VirtualMachineDB.new('one2one', + :missing_state => 'UNKNOWN', + :sync => 180) + vmdb.purge + puts vmdb.to_status(host, host_id) +rescue StandardError => e + OpenNebula.handle_driver_exception('im probe_vm_status', e, host) +end diff --git a/src/im_mad/remotes/one.d/monitord-client.rb b/src/im_mad/remotes/one.d/monitord-client.rb new file mode 120000 index 0000000000..83ff041040 --- /dev/null +++ b/src/im_mad/remotes/one.d/monitord-client.rb @@ -0,0 +1 @@ +../common.d/monitord-client.rb \ No newline at end of file diff --git a/src/im_mad/remotes/one.d/monitord-client_control.sh b/src/im_mad/remotes/one.d/monitord-client_control.sh new file mode 120000 index 0000000000..23e6d7e502 --- /dev/null +++ b/src/im_mad/remotes/one.d/monitord-client_control.sh @@ -0,0 +1 @@ +../common.d/monitord-client_control.sh \ No newline at end of file diff --git a/src/mad/ruby/PublicCloudDriver.rb b/src/mad/ruby/PublicCloudDriver.rb index 8c1ff9b06f..c511a9a92a 100644 --- a/src/mad/ruby/PublicCloudDriver.rb +++ b/src/mad/ruby/PublicCloudDriver.rb @@ -51,6 +51,7 @@ module PublicCloudDriver data = "HYPERVISOR=#{@hypervisor}\n" data << "PUBLIC_CLOUD=YES\n" + data << "PRIORITY=-1\n" data << "TOTALCPU=#{total_cpu}\n" data << "TOTALMEMORY=#{total_mem}\n" @@ -187,8 +188,8 @@ module PublicCloudDriver end # Hash to ONE template format - def hash_to_template(h, pref = '', post = '') - tmpl = h.to_a.map {|e| "#{e[0].to_s.upcase}=\"#{e[1]}\"" }.join(', ') + def hash_to_template(h, pref = '', post = '', delim = ', ') + tmpl = h.to_a.map {|e| "#{e[0].to_s.upcase}=\"#{e[1]}\"" }.join(delim) pref + tmpl + post end diff --git a/src/vmm_mad/remotes/az/az_driver.rb b/src/vmm_mad/remotes/az/az_driver.rb index 8519fc9abd..dbb5948c47 100755 --- a/src/vmm_mad/remotes/az/az_driver.rb +++ b/src/vmm_mad/remotes/az/az_driver.rb @@ -788,8 +788,8 @@ end ############################################################################ module DomainList - def self.state_info(id, name) - az = AzureDriver.new(id, name) + def self.state_info(name, id) + az = AzureDriver.new(name, id) vms = az.retreive_vms_data diff --git a/src/vmm_mad/remotes/ec2/ec2_driver.rb b/src/vmm_mad/remotes/ec2/ec2_driver.rb index a52a97d880..9c5ed65ce6 100755 --- a/src/vmm_mad/remotes/ec2/ec2_driver.rb +++ b/src/vmm_mad/remotes/ec2/ec2_driver.rb @@ -31,7 +31,7 @@ end EC2_DRIVER_CONF = "#{ETC_LOCATION}/ec2_driver.conf" EC2_DRIVER_DEFAULT = "#{ETC_LOCATION}/ec2_driver.default" -EC2_DATABASE_PATH = "#{VAR_LOCATION}/remotes/im/ec2.d/ec-cache.db" +EC2_DATABASE_PATH = "#{VAR_LOCATION}/remotes/im/ec2.d/ec2-cache.db" if File.directory?(GEMS_LOCATION) Gem.use_paths(GEMS_LOCATION) @@ -1022,8 +1022,8 @@ end ############################################################################ module DomainList - def self.state_info(*args) - ec2 = EC2Driver.new(*args) + def self.state_info(name, id) + ec2 = EC2Driver.new(name, id) vms = ec2.retreive_vms_data diff --git a/src/vmm_mad/remotes/one/cancel b/src/vmm_mad/remotes/one/cancel index a7b2179642..0259205363 100755 --- a/src/vmm_mad/remotes/one/cancel +++ b/src/vmm_mad/remotes/one/cancel @@ -44,5 +44,5 @@ vm.info lcm_state = vm.lcm_state_str -one2one_drv = OpenNebulaDriver.new(host) +one2one_drv = One2OneDriver.new(host) one2one_drv.cancel(deploy_id, lcm_state) diff --git a/src/vmm_mad/remotes/one/deploy b/src/vmm_mad/remotes/one/deploy index 863b6c247a..cd45ec4c22 100755 --- a/src/vmm_mad/remotes/one/deploy +++ b/src/vmm_mad/remotes/one/deploy @@ -46,7 +46,7 @@ vm.info lcm_state = vm.lcm_state_str deploy_id = vm.deploy_id -one2one_drv = OpenNebulaDriver.new(host) +one2one_drv = One2OneDriver.new(host) text=File.read(dfile) diff --git a/src/vmm_mad/remotes/one/opennebula_driver.rb b/src/vmm_mad/remotes/one/opennebula_driver.rb index 2d0e05a7ce..0000f8b12b 100755 --- a/src/vmm_mad/remotes/one/opennebula_driver.rb +++ b/src/vmm_mad/remotes/one/opennebula_driver.rb @@ -14,16 +14,18 @@ # See the License for the specific language governing permissions and # # limitations under the License. # # -------------------------------------------------------------------------- # -ONE_LOCATION = ENV['ONE_LOCATION'] if !defined?(ONE_LOCATION) +ONE_LOCATION = ENV['ONE_LOCATION'] unless defined? ONE_LOCATION if !ONE_LOCATION - RUBY_LIB_LOCATION = '/usr/lib/one/ruby' if !defined?(RUBY_LIB_LOCATION) - GEMS_LOCATION = '/usr/share/one/gems' if !defined?(GEMS_LOCATION) - ETC_LOCATION = '/etc/one/' if !defined?(ETC_LOCATION) + RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby' + GEMS_LOCATION ||= '/usr/share/one/gems' + ETC_LOCATION ||= '/etc/one/' + VAR_LOCATION ||= '/var/lib/one/' else - RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby' if !defined?(RUBY_LIB_LOCATION) - GEMS_LOCATION = ONE_LOCATION + '/share/gems' if !defined?(GEMS_LOCATION) - ETC_LOCATION = ONE_LOCATION + '/etc/' if !defined?(ETC_LOCATION) + RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby' + GEMS_LOCATION ||= ONE_LOCATION + '/share/gems' + ETC_LOCATION ||= ONE_LOCATION + '/etc/' + VAR_LOCATION ||= ONE_LOCATION + '/var/' end # Load credentials and environment @@ -39,573 +41,558 @@ require 'CommandManager' require 'scripts_common' require 'rexml/document' require 'VirtualMachineDriver' +require 'PublicCloudDriver' require 'opennebula' # The main class for the driver -class OpenNebulaDriver - ACTION = VirtualMachineDriver::ACTION - POLL_ATTRIBUTE = VirtualMachineDriver::POLL_ATTRIBUTE - VM_STATE = VirtualMachineDriver::VM_STATE +class One2OneDriver - LIMIT_DEFAULT = "-1" - LIMIT_UNLIMITED = "-2" + include PublicCloudDriver - UNLIMITED_CPU_VALUE = "100000" - UNLIMITED_MEMORY_VALUE = "1073741824" + LIMIT_DEFAULT = '-1' + LIMIT_UNLIMITED = '-2' - # Local deploy ids will be formed with this prefix and the remote VM ID - #DEPLOY_ID_PREFIX = "one-" - DEPLOY_ID_PREFIX = "opennebula-hybrid-" + UNLIMITED_CPU_VALUE = '100000' + UNLIMITED_MEMORY_VALUE = '1073741824' - # Remote VM names will be formed with this prefix, plus the local VM ID - REMOTE_NAME_PREFIX = "remote-opennebula-" + # Local deploy ids will be formed with this prefix and the remote VM ID + # DEPLOY_ID_PREFIX = "one-" + DEPLOY_ID_PREFIX = 'opennebula-hybrid-' - # constructor, loads credentials and endpoint - def initialize(host_name, host_id=nil) - @host_name = host_name - @host_id = host_id + # Remote VM names will be formed with this prefix, plus the local VM ID + REMOTE_NAME_PREFIX = 'remote-opennebula-' - client = OpenNebula::Client.new - if host_id.nil? - host_pool = OpenNebula::HostPool.new(client) - host_pool.info - objects=host_pool.select {|object| object.name==host_name } - host_id = objects.first.id - end + # constructor, loads credentials and endpoint + def initialize(host, id = nil) + @hypervisor = 'opennebula' + @host = host + @xmlhost = host_info(host, id) + @public_cloud_conf = {} - host = OpenNebula::Host.new_with_id(host_id, client) - host.info(true) + # create or open cache db + @db = InstanceCache.new(File.join(VAR_LOCATION, 'remotes', + 'im', 'one.d', 'one-cache.db')) - region = {} + region = {} - ["user", "password", "endpoint", "capacity"].each do |key| - if key == "capacity" - region[key] = {} - ["cpu", "memory"].each do |key_c| - value = host.retrieve_elements("/HOST/TEMPLATE/ONE_#{key.upcase}/#{key_c.upcase}")[0] - if host.retrieve_elements("/HOST/TEMPLATE/ONE_#{key.upcase}/#{key_c.upcase}")[0].nil? || value == "" - raise "Region for host #{host} does not have '#{key_c.upcase}' defined in host template" - end - region[key][key_c] = value.to_i - end - else - value = host.retrieve_elements("/HOST/TEMPLATE/ONE_#{key.upcase}")[0] - if host.retrieve_elements("/HOST/TEMPLATE/ONE_#{key.upcase}")[0].nil? || value == "" - raise "Region for host #{host} does not have '#{key.upcase}' defined in host template" - end - region[key] = value - end - end + %w[user password endpoint capacity].each do |key| + if key == 'capacity' + region[key] = {} + %w[cpu memory].each do |key_c| + xpath = "/HOST/TEMPLATE/ONE_#{key.upcase}/#{key_c.upcase}" + value = @xmlhost.retrieve_elements(xpath)[0] + if @xmlhost.retrieve_elements(xpath)[0].nil? || value == '' + raise "Region for host #{@xmlhost} does not have "\ + "'#{key_c.upcase}' defined in host template" + end - region["password"] = host["/HOST/TEMPLATE/ONE_PASSWORD"], + region[key][key_c] = value.to_i + end + else + xpath = "/HOST/TEMPLATE/ONE_#{key.upcase}" + value = @xmlhost.retrieve_elements(xpath)[0] + if @xmlhost.retrieve_elements(xpath)[0].nil? || value == '' + raise "Region for host #{@xmlhost} does not have "\ + "'#{key.upcase}' defined in host template" + end - secret = "#{region['user']}:#{region['password']}" - - @client = OpenNebula::Client.new(secret, region['endpoint'], :sync => true) - - @cpu = region['capacity']['cpu'] - @memory = region['capacity']['memory'] - - @cpu = 0 if @cpu.nil? - @memory = 0 if @memory.nil? - end - - # DEPLOY action, also sets ports and ip if needed - def deploy(id, host, xml_text, lcm_state, deploy_id) - if lcm_state == "BOOT" || lcm_state == "BOOT_FAILURE" - one_info = get_deployment_info(host, xml_text) - - #load_default_template_values - - tid = one_value(one_info, 'TEMPLATE_ID') - - if (tid.nil? || tid == "") - STDERR.puts("Cannot find TEMPLATE_ID in deployment file") - exit(-1) - end - - extra_template = "REMOTE_OPENNEBULA = YES\n"<< - "REMOTE_OPENNEBULA_VM_ID = #{id}\n" - - # The OpenNebula context will be included - xml = OpenNebula::XMLElement.new - xml.initialize_xml(xml_text, 'VM') - - if xml.has_elements?('TEMPLATE/CONTEXT') - # Since there is only 1 level ',' will not be added - context_str = xml.template_like_str('TEMPLATE/CONTEXT') - - if xml['TEMPLATE/CONTEXT/TOKEN'] == 'YES' - # TODO use OneGate library. See ec2_driver.rb - token_str = generate_onegate_token(xml) - if token_str - context_str << "\nONEGATE_TOKEN=\"#{token_str}\"" - end + region[key] = value + end end - extra_template << context_str - end + secret = "#{region['user']}:#{region['password']}" - t = OpenNebula::Template.new_with_id(tid, @client) - rc = t.instantiate(REMOTE_NAME_PREFIX+id, true, extra_template, false) + @client = OpenNebula::Client.new(secret, region['endpoint'], + :sync => true) - if OpenNebula.is_error?(rc) - STDERR.puts(rc.to_str()) - exit(-1) - end + @cpu = region['capacity']['cpu'] + @memory = region['capacity']['memory'] - deploy_id = "#{DEPLOY_ID_PREFIX}#{rc}" - vm = get_remote_vm(deploy_id) - - if !context_str.nil? - new_context_update = "CONTEXT = [" << context_str <<"]" - new_context_update = new_context_update.gsub("\n", ",\n") - rc = vm.updateconf(new_context_update) - end - - if OpenNebula.is_error?(rc) - STDERR.puts(rc.to_str()) - exit(-1) - end - - vm.release - - rc = vm.update("REMOTE_OPENNEBULA_DEPLOY_ID = \"#{deploy_id}\"", true) - - if OpenNebula.is_error?(rc) - STDERR.puts("Error adding REMOTE_OPENNEBULA_DEPLOY_ID attribute to VM #{rc}: #{rc.to_str()}") - end - - puts(deploy_id) - else - restore(deploy_id) - deploy_id - end - end - - # Shutdown an instance - def shutdown(deploy_id, lcm_state) - vm = get_remote_vm(deploy_id) - - case lcm_state - when "SHUTDOWN" - rc = vm.terminate - when "SHUTDOWN_POWEROFF" - rc = vm.poweroff - when "SHUTDOWN_UNDEPLOY" - rc = vm.undeploy + @cpu = 0 if @cpu.nil? + @memory = 0 if @memory.nil? end - if OpenNebula.is_error?(rc) - STDERR.puts(rc.to_str()) - exit(-1) - end - end + def host_capacity(_xmlhost) + user = OpenNebula::User.new_with_id('-1', @client) + rc = user.info - # Reboot an instance - def reboot(deploy_id) - vm = get_remote_vm(deploy_id) - rc = vm.reboot() + if OpenNebula.is_error?(rc) + STDERR.puts("Error getting remote user information: #{rc.to_str}") + exit(-1) + end - if OpenNebula.is_error?(rc) - STDERR.puts(rc.to_str()) - exit(-1) - end - end + group = OpenNebula::Group.new_with_id('-1', @client) + rc = group.info - # Reboot (hard) an instance - def reset(deploy_id) - vm = get_remote_vm(deploy_id) - rc = vm.reboot(true) - - if OpenNebula.is_error?(rc) - STDERR.puts(rc.to_str()) - exit(-1) - end - end - - # Cancel an instance - def cancel(deploy_id, lcm_state) - vm = get_remote_vm(deploy_id) - - case lcm_state - when "SHUTDOWN" - rc = vm.terminate(true) - when "SHUTDOWN_POWEROFF" - rc = vm.poweroff(true) - when "SHUTDOWN_UNDEPLOY" - rc = vm.undeploy(true) - end - - if OpenNebula.is_error?(rc) - STDERR.puts(rc.to_str()) - exit(-1) - end - end - - # Save an instance - def save(deploy_id) - vm = get_remote_vm(deploy_id) - - rc = vm.suspend() - - if OpenNebula.is_error?(rc) - STDERR.puts(rc.to_str()) - exit(-1) - end - end - - # Resumes an instance - def restore(deploy_id) - vm = get_remote_vm(deploy_id) - rc = vm.resume() - - if OpenNebula.is_error?(rc) - STDERR.puts(rc.to_str()) - exit(-1) - end - end - - # Get info (IP, and state) for an instance - def poll(id, deploy_id) - vm = get_remote_vm(deploy_id) - rc = vm.info - - if OpenNebula.is_error?(rc) - STDERR.puts(rc.to_str()) - exit(-1) - end - - puts parse_poll(vm) - end - - # Get the info of all the remote instances - def monitor_all_vms - user = OpenNebula::User.new_with_id("-1", @client) - rc = user.info - - if OpenNebula.is_error?(rc) - STDERR.puts("Error getting remote user information: #{rc.to_str()}") - exit(-1) - end - - group = OpenNebula::Group.new_with_id("-1", @client) - rc = group.info - - if OpenNebula.is_error?(rc) - STDERR.puts("Error getting remote group information: #{rc.to_str()}") - exit(-1) - end - - if @cpu != 0 - totalcpu = @cpu - else - u_cpu = user['VM_QUOTA/VM/CPU'] - g_cpu = group['VM_QUOTA/VM/CPU'] - - if u_cpu == LIMIT_DEFAULT || u_cpu.nil? - u_cpu = user['DEFAULT_USER_QUOTAS/VM_QUOTA/VM/CPU'] - end - - if g_cpu == LIMIT_DEFAULT || g_cpu.nil? - g_cpu = group['DEFAULT_GROUP_QUOTAS/VM_QUOTA/VM/CPU'] - end - - u_cpu = LIMIT_UNLIMITED if u_cpu.nil? - g_cpu = LIMIT_UNLIMITED if g_cpu.nil? - - u_cpu = UNLIMITED_CPU_VALUE if u_cpu == LIMIT_UNLIMITED - g_cpu = UNLIMITED_CPU_VALUE if g_cpu == LIMIT_UNLIMITED - - totalcpu = ([u_cpu.to_f, g_cpu.to_f].min * 100).round - end - - if @memory != 0 - totalmemory = @memory - else - u_memory = user['VM_QUOTA/VM/MEMORY'] - g_memory = group['VM_QUOTA/VM/MEMORY'] - - if u_memory == LIMIT_DEFAULT || u_memory.nil? - u_memory = user['DEFAULT_USER_QUOTAS/VM_QUOTA/VM/MEMORY'] - end - - if g_memory == LIMIT_DEFAULT || g_memory.nil? - g_memory = group['DEFAULT_GROUP_QUOTAS/VM_QUOTA/VM/MEMORY'] - end - - u_memory = LIMIT_UNLIMITED if u_memory.nil? - g_memory = LIMIT_UNLIMITED if g_memory.nil? - - u_memory = UNLIMITED_MEMORY_VALUE if u_memory == LIMIT_UNLIMITED - g_memory = UNLIMITED_MEMORY_VALUE if g_memory == LIMIT_UNLIMITED - - totalmemory = [u_memory.to_i, g_memory.to_i].min - end - - host_info = "HYPERVISOR=opennebula\n" - host_info << "PUBLIC_CLOUD=YES\n" - host_info << "PRIORITY=-1\n" - host_info << "TOTALMEMORY=#{totalmemory * 1024}\n" - host_info << "TOTALCPU=#{totalcpu}\n" - host_info << "HOSTNAME=\"#{@host_name}\"\n" - - vms_info = "VM_POLL=YES\n" - - if user['VM_QUOTA/VM/CPU_USED'].nil? - usedcpu = 0 - else - usedcpu = (user['VM_QUOTA/VM/CPU_USED'].to_f * 100).round - end - - if user['VM_QUOTA/VM/MEMORY_USED'].nil? - usedmemory = 0 - else - usedmemory = user['VM_QUOTA/VM/MEMORY_USED'].to_i * 1024 - end - - vmpool = OpenNebula::VirtualMachinePool.new(@client, - OpenNebula::VirtualMachinePool::INFO_ALL_VM) - rc = vmpool.info - - if OpenNebula.is_error?(rc) - STDERR.puts(rc.to_str()) - exit(-1) - end - - vmpool.each do |vm| - poll_data = parse_poll(vm) - - deploy_id = vm["USER_TEMPLATE/REMOTE_OPENNEBULA_DEPLOY_ID"] || "#{DEPLOY_ID_PREFIX}#{vm.id()}" - vmid = vm["USER_TEMPLATE/REMOTE_OPENNEBULA_VM_ID"] || "-1" - - vm_template_to_one = vm_to_import(vm) - vm_template_to_one = Base64.encode64(vm_template_to_one).gsub("\n","") - - vms_info << "VM=[\n" - vms_info << " ID=\"#{vmid}\",\n" - vms_info << " DEPLOY_ID=\"#{deploy_id}\",\n" - vms_info << " VM_NAME=#{vm.name},\n" - vms_info << " IMPORT_TEMPLATE=\"#{vm_template_to_one}\",\n" - vms_info << " POLL=\"#{poll_data}\" ]\n" - end - - host_info << "USEDMEMORY=#{usedmemory}\n" - host_info << "USEDCPU=#{usedcpu}\n" - host_info << "FREEMEMORY=#{(totalmemory - usedmemory)}\n" - host_info << "FREECPU=#{(totalcpu - usedcpu)}\n" - - puts host_info - puts vms_info - end - - private - - # Get the OpenNebula hybrid section of the template. With more than one section - # the HOST element is used and matched with the host - def get_deployment_info(host, xml_text) - xml = REXML::Document.new xml_text - - one = nil - - all_one_elements = xml.root.get_elements("//USER_TEMPLATE/PUBLIC_CLOUD") - - # First, let's see if we have an one site that matches - # our desired host name - all_one_elements.each { |element| - cloud = element.elements["HOST"] - if cloud && cloud.text.upcase == host.upcase - one = element - end - } - - if !one - # If we don't find the one site, and ONE just - # knows about one one site, let's use that - if all_one_elements.size == 1 - one = all_one_elements[0] - else - STDERR.puts("Cannot find PUBLIC_CLOUD element in deployment "\ - " file or no HOST site matching the requested in the "\ - "template.") - exit(-1) - end - end - - one - end - - # Retrieve the vm information from the instance - def parse_poll(vm) - begin - vm_hash = vm.to_hash() - - state = "" - - state = case vm.state_str - when "INIT" || "PENDING" || "HOLD" || "CLONING" - VM_STATE[:active] - when "ACTIVE" - case vm.lcm_state_str - when /_FAILURE$/ || "UNKNOWN" - VM_STATE[:error] + if OpenNebula.is_error?(rc) + STDERR.puts("Error getting remote group information: #{rc.to_str}") + exit(-1) + end + if @cpu != 0 + totalcpu = @cpu else - VM_STATE[:active] + u_cpu = user['VM_QUOTA/VM/CPU'] + g_cpu = group['VM_QUOTA/VM/CPU'] + + if u_cpu == LIMIT_DEFAULT || u_cpu.nil? + u_cpu = user['DEFAULT_USER_QUOTAS/VM_QUOTA/VM/CPU'] + end + + if g_cpu == LIMIT_DEFAULT || g_cpu.nil? + g_cpu = group['DEFAULT_GROUP_QUOTAS/VM_QUOTA/VM/CPU'] + end + + u_cpu = LIMIT_UNLIMITED if u_cpu.nil? + g_cpu = LIMIT_UNLIMITED if g_cpu.nil? + + u_cpu = UNLIMITED_CPU_VALUE if u_cpu == LIMIT_UNLIMITED + g_cpu = UNLIMITED_CPU_VALUE if g_cpu == LIMIT_UNLIMITED + + totalcpu = ([u_cpu.to_f, g_cpu.to_f].min * 100).round end - when "STOPPED" || "SUSPENDED" - VM_STATE[:paused] - when "DONE" || "POWEROFF" || "UNDEPLOYED" - VM_STATE[:deleted] - when "FAILED" || "CLONING_FAILURE" - VM_STATE[:error] - else - VM_STATE[:unknown] - end - info = "#{POLL_ATTRIBUTE[:state]}=#{state} " + if @memory != 0 + totalmemory = @memory + else + u_memory = user['VM_QUOTA/VM/MEMORY'] + g_memory = group['VM_QUOTA/VM/MEMORY'] - if state != VM_STATE[:active] - return info - end + if u_memory == LIMIT_DEFAULT || u_memory.nil? + u_memory = user['DEFAULT_USER_QUOTAS/VM_QUOTA/VM/MEMORY'] + end - monitoring = vm_hash['VM']['MONITORING'] + if g_memory == LIMIT_DEFAULT || g_memory.nil? + g_memory = group['DEFAULT_GROUP_QUOTAS/VM_QUOTA/VM/MEMORY'] + end - if (!monitoring.nil?) - info << vm.template_like_str('MONITORING', true).gsub("\n", " ").gsub('"', '').gsub(/\[ */, "[").gsub(/, */,",").gsub(/ *\]/, "]").gsub(/STATE=./,"") - end + u_memory = LIMIT_UNLIMITED if u_memory.nil? + g_memory = LIMIT_UNLIMITED if g_memory.nil? - return info - rescue - # Unkown state if exception occurs retrieving information from - # an instance - "#{POLL_ATTRIBUTE[:state]}=#{VM_STATE[:unknown]} " - end - end + u_memory = UNLIMITED_MEMORY_VALUE if u_memory == LIMIT_UNLIMITED + g_memory = UNLIMITED_MEMORY_VALUE if g_memory == LIMIT_UNLIMITED - # Returns the value of the xml specified by the name or the default - # one if it does not exist - # +xml+: REXML Document, containing one hybrid information - # +name+: String, xpath expression to retrieve the value - # +block+: Block, block to be applied to the value before returning it - def one_value(xml, name, &block) - value = value_from_xml(xml, name) || @defaults[name] - if block_given? && value - block.call(value) - else - value - end - end + totalmemory = [u_memory.to_i, g_memory.to_i].min + end - def value_from_xml(xml, name) - if xml - element = xml.elements[name] - element.text.strip if element && element.text - end - end - - # Load the default values that will be used to create a new instance, if - # not provided in the template. These values are defined in the - # ONE_DRIVER_DEFAULT file - def load_default_template_values - @defaults = Hash.new - - if File.exists?(ONE_DRIVER_DEFAULT) - fd = File.new(ONE_DRIVER_DEFAULT) - xml = REXML::Document.new fd - fd.close() - - return if !xml || !xml.root - - xml.elements.each("/TEMPLATE/PUBLIC_CLOUD/*") do |e| - @defaults[e.name] = e.text - end - end - end - - # Retrive the vm object for the remote opennebula - def get_remote_vm(deploy_id) - begin - match = deploy_id.match( /#{DEPLOY_ID_PREFIX}(.*)/ ) - - if (match.nil?) - raise "Deploy ID #{deploy_id} was not created with this driver" - end - - id = match[1] - - return OpenNebula::VirtualMachine.new_with_id(id, @client) - rescue => e - STDERR.puts e.message - exit(-1) - end - end - - # Build template for importation - def vm_to_import(vm) - cpu = vm['TEMPLATE/CPU'] - vcpu = vm['TEMPLATE/VCPU'] - mem = vm['TEMPLATE/MEMORY'] - - template_id = vm['TEMPLATE/TEMPLATE_ID'] - - deploy_id = "#{DEPLOY_ID_PREFIX}#{vm.id()}" - - str = "NAME = \"Instance from #{vm.name()}\"\n"\ - "CPU = \"#{cpu}\"\n"\ - "VCPU = \"#{1}\"\n"\ - "MEMORY = \"#{mem}\"\n"\ - "HYPERVISOR = \"opennebula\"\n"\ - "PUBLIC_CLOUD = [\n"\ - " TYPE =\"opennebula\",\n"\ - " TEMPLATE_ID =\"#{template_id}\"\n"\ - "]\n"\ - "IMPORT_VM_ID = \"#{deploy_id}\"\n"\ - "SCHED_REQUIREMENTS=\"NAME=\\\"#{@host_name}\\\"\"\n"\ - "DESCRIPTION = \"Instance imported from a remote OpenNebula, from VM instance"\ - " #{vm.id()}\"\n" - - str - end - - # TODO move this method to a OneGate library. See ec2_driver.rb - def generate_onegate_token(xml) - # Create the OneGate token string - vmid_str = xml["ID"] - stime_str = xml["STIME"] - str_to_encrypt = "#{vmid_str}:#{stime_str}" - - user_id = xml['TEMPLATE/CREATED_BY'] - - if user_id.nil? - STDERR.puts {"VMID:#{vmid} CREATED_BY not present" \ - " in the VM TEMPLATE"} - return nil + [totalcpu, totalmemory * 1024] end - user = OpenNebula::User.new_with_id(user_id, - OpenNebula::Client.new) - rc = user.info - - if OpenNebula.is_error?(rc) - STDERR.puts {"VMID:#{vmid} user.info" \ - " error: #{rc.message}"} - return nil + def instance_type_capacity(type) + if type =~ %r{(\d+\.?\d*)cpu/(\d+)memory} + cpu = (Regexp.last_match(1).to_f * 100).ceil + memory = Regexp.last_match(2).to_i * 1024 + return [cpu, memory] + end + [0, 0] end - token_password = user['TEMPLATE/TOKEN_PASSWORD'] + def fetch_vms_data(with_monitoring: false) + # rubocop:disable Layout/LineLength + vmpool = OpenNebula::VirtualMachinePool.new(@client, + OpenNebula::VirtualMachinePool::INFO_ALL_VM) + # rubocop:enable Layout/LineLength + vmpool.info - if token_password.nil? - STDERR.puts {"VMID:#{vmid} TOKEN_PASSWORD not present"\ - " in the USER:#{user_id} TEMPLATE"} - return nil + vms = [] + vmpool.each do |remote_vm| + id = remote_vm['USER_TEMPLATE/REMOTE_OPENNEBULA_VM_ID'] || '-1' + deploy_id = remote_vm['USER_TEMPLATE/REMOTE_OPENNEBULA_DEPLOY_ID'] \ + || "#{DEPLOY_ID_PREFIX}#{remote_vm.id}" + cpu = remote_vm['TEMPLATE/CPU'] + memory = remote_vm['TEMPLATE/MEMORY'] + + vm = { :id => id, + :uuid => deploy_id, + :name => remote_vm.name, + :state => vm_state(remote_vm), + :type => "#{cpu}cpu/#{memory}memory" } + + if with_monitoring + vm[:monitor] = get_vm_monitor_data(remote_vm) + end + vms << vm + end + + vms end - cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc") - cipher.encrypt - cipher.key = token_password - onegate_token = cipher.update(str_to_encrypt) - onegate_token << cipher.final + def vm_state(vm) + case vm.state_str + when 'INIT' || 'PENDING' || 'HOLD' || 'CLONING' + 'RUNNING' + when 'ACTIVE' + case vm.lcm_state_str + when /_FAILURE$/ || 'UNKNOWN' + 'FAILED' + else + 'RUNNING' + end + when 'STOPPED' || 'SUSPENDED' + 'STOPPED' + when 'DONE' || 'POWEROFF' || 'UNDEPLOYED' + 'DONE' + when 'FAILED' || 'CLONING_FAILURE' + 'FAILED' + else + 'UNKNOWN' + end + end + + def get_vm_monitor_data(vm) + data = vm.monitoring(%w[CPU DISKRDBYTES DISKRDIOPS DISKWRBYTES + DISKWRIOPS MEMORY NETRX NETTX]) + + # take only last monitoring entry + # TODO: verify timestamp is fresh + data = data.map {|key, entries| [key, entries.last.last] }.to_h + + data = hash_to_template(data, '', '', "\n") + + Base64.encode64(data).gsub("\n", '') + end + + # DEPLOY action, also sets ports and ip if needed + def deploy(id, host, xml_text, lcm_state, deploy_id) + if %w[BOOT BOOT_FAILURE].include? lcm_state + one_info = get_deployment_info(host, xml_text) + + # load_default_template_values + + tid = one_value(one_info, 'TEMPLATE_ID') + + if tid.nil? || tid == '' + STDERR.puts('Cannot find TEMPLATE_ID in deployment file') + exit(-1) + end + + extra_template = "REMOTE_OPENNEBULA = YES\n"\ + "REMOTE_OPENNEBULA_VM_ID = #{id}\n" + + # The OpenNebula context will be included + xml = OpenNebula::XMLElement.new + xml.initialize_xml(xml_text, 'VM') + + if xml.has_elements?('TEMPLATE/CONTEXT') + # Since there is only 1 level ',' will not be added + context_str = xml.template_like_str('TEMPLATE/CONTEXT') + + if xml['TEMPLATE/CONTEXT/TOKEN'] == 'YES' + # TODO: use OneGate library. See ec2_driver.rb + token_str = generate_onegate_token(xml) + if token_str + context_str << "\nONEGATE_TOKEN=\"#{token_str}\"" + end + end + + extra_template << context_str + end + + t = OpenNebula::Template.new_with_id(tid, @client) + rc = t.instantiate(REMOTE_NAME_PREFIX+id, true, extra_template, + false) + + if OpenNebula.is_error?(rc) + STDERR.puts(rc.to_str) + exit(-1) + end + + deploy_id = "#{DEPLOY_ID_PREFIX}#{rc}" + vm = get_remote_vm(deploy_id) + + if !context_str.nil? + new_context_update = 'CONTEXT = [' << context_str << ']' + new_context_update = new_context_update.gsub("\n", ",\n") + rc = vm.updateconf(new_context_update) + end + + if OpenNebula.is_error?(rc) + STDERR.puts(rc.to_str) + exit(-1) + end + + vm.release + + rc = vm.update("REMOTE_OPENNEBULA_DEPLOY_ID = \"#{deploy_id}\"", + true) + + if OpenNebula.is_error?(rc) + STDERR.puts('Error adding REMOTE_OPENNEBULA_DEPLOY_ID ' \ + "attribute to VM #{rc}: #{rc.to_str}") + end + + puts(deploy_id) + else + restore(deploy_id) + deploy_id + end + end + + # Shutdown an instance + def shutdown(deploy_id, lcm_state) + vm = get_remote_vm(deploy_id) + + case lcm_state + when 'SHUTDOWN' + rc = vm.terminate + when 'SHUTDOWN_POWEROFF' + rc = vm.poweroff + when 'SHUTDOWN_UNDEPLOY' + rc = vm.undeploy + end + + return unless OpenNebula.is_error?(rc) + + STDERR.puts(rc.to_str) + exit(-1) + end + + # Reboot an instance + def reboot(deploy_id) + vm = get_remote_vm(deploy_id) + rc = vm.reboot + + return unless OpenNebula.is_error?(rc) + + STDERR.puts(rc.to_str) + exit(-1) + end + + # Reboot (hard) an instance + def reset(deploy_id) + vm = get_remote_vm(deploy_id) + rc = vm.reboot(true) + + return unless OpenNebula.is_error?(rc) + + STDERR.puts(rc.to_str) + exit(-1) + end + + # Cancel an instance + def cancel(deploy_id, lcm_state) + vm = get_remote_vm(deploy_id) + + case lcm_state + when 'SHUTDOWN' + rc = vm.terminate(true) + when 'SHUTDOWN_POWEROFF' + rc = vm.poweroff(true) + when 'SHUTDOWN_UNDEPLOY' + rc = vm.undeploy(true) + end + + return unless OpenNebula.is_error?(rc) + + STDERR.puts(rc.to_str) + exit(-1) + end + + # Save an instance + def save(deploy_id) + vm = get_remote_vm(deploy_id) + + rc = vm.suspend + + return unless OpenNebula.is_error?(rc) + + STDERR.puts(rc.to_str) + exit(-1) + end + + # Resumes an instance + def restore(deploy_id) + vm = get_remote_vm(deploy_id) + rc = vm.resume + + return unless OpenNebula.is_error?(rc) + + STDERR.puts(rc.to_str) + exit(-1) + end + + #--------------------------------------------------------------------------- + # Monitor Interface + #--------------------------------------------------------------------------- + def probe_host_system + # call probe_host_system from PublicCloudDriver module + super(@db, 180, @xmlhost) + end + + def probe_host_monitor + # call probe_host_monitor from PublicCloudDriver module + super(@db, 180, @xmlhost) + end + + def retreive_vms_data + # call vms_data from PublicCloudDriver module + vms_data(@db, 180) + end + + private + + # Get the OpenNebula hybrid section of the template. With more than one + # section the HOST element is used and matched with the host + def get_deployment_info(host, xml_text) + xml = REXML::Document.new xml_text + + one = nil + + all_one_elements = xml.root.get_elements('//USER_TEMPLATE/PUBLIC_CLOUD') + + # First, let's see if we have an one site that matches + # our desired host name + all_one_elements.each do |element| + cloud = element.elements['HOST'] + if cloud && cloud.text.upcase == host.upcase + one = element + end + end + + if !one + # If we don't find the one site, and ONE just + # knows about one one site, let's use that + if all_one_elements.size == 1 + one = all_one_elements[0] + else + STDERR.puts('Cannot find PUBLIC_CLOUD element in deployment '\ + ' file or no HOST site matching the requested in the '\ + 'template.') + exit(-1) + end + end + + one + end + + # Returns the value of the xml specified by the name or the default + # one if it does not exist + # +xml+: REXML Document, containing one hybrid information + # +name+: String, xpath expression to retrieve the value + # +block+: Block, block to be applied to the value before returning it + def one_value(xml, name, &block) + value = value_from_xml(xml, name) || @defaults[name] + if block_given? && value + block.call(value) + else + value + end + end + + def value_from_xml(xml, name) + return unless xml + + element = xml.elements[name] + element.text.strip if element && element.text + end + + # Load the default values that will be used to create a new instance, if + # not provided in the template. These values are defined in the + # ONE_DRIVER_DEFAULT file + def load_default_template_values + @defaults = {} + + return unless File.exist?(ONE_DRIVER_DEFAULT) + + fd = File.new(ONE_DRIVER_DEFAULT) + xml = REXML::Document.new fd + fd.close + + return if !xml || !xml.root + + xml.elements.each('/TEMPLATE/PUBLIC_CLOUD/*') do |e| + @defaults[e.name] = e.text + end + end + + # Retrive the vm object for the remote opennebula + def get_remote_vm(deploy_id) + begin + match = deploy_id.match(/#{DEPLOY_ID_PREFIX}(.*)/) + + if match.nil? + raise "Deploy ID #{deploy_id} was not created with this driver" + end + + id = match[1] + + OpenNebula::VirtualMachine.new_with_id(id, @client) + rescue StandardError => e + STDERR.puts e.message + exit(-1) + end + end + + # TODO: move this method to a OneGate library. See ec2_driver.rb + def generate_onegate_token(xml) + # Create the OneGate token string + vmid_str = xml['ID'] + stime_str = xml['STIME'] + str_to_encrypt = "#{vmid_str}:#{stime_str}" + + user_id = xml['TEMPLATE/CREATED_BY'] + + if user_id.nil? + STDERR.puts do + "VMID:#{vmid} CREATED_BY not present" \ + ' in the VM TEMPLATE' + end + return + end + + user = OpenNebula::User.new_with_id(user_id, + OpenNebula::Client.new) + rc = user.info + + if OpenNebula.is_error?(rc) + STDERR.puts do + "VMID:#{vmid} user.info" \ + " error: #{rc.message}" + end + return + end + + token_password = user['TEMPLATE/TOKEN_PASSWORD'] + + if token_password.nil? + STDERR.puts do + "VMID:#{vmid} TOKEN_PASSWORD not present"\ + " in the USER:#{user_id} TEMPLATE" + end + return + end + + cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc') + cipher.encrypt + cipher.key = token_password + onegate_token = cipher.update(str_to_encrypt) + onegate_token << cipher.final + + Base64.encode64(onegate_token).chop + end + +end + +########################################################################### +# Module Interface +# Interface for probe_db - VirtualMachineDB +############################################################################ +module DomainList + + def self.state_info(name, id) + one2one_drv = One2OneDriver.new(name, id) + + vms = one2one_drv.retreive_vms_data + + info = {} + vms.each do |vm| + info[vm[:uuid]] = { :id => vm[:id], + :uuid => vm[:uuid], + :name => vm[:name], + :state => vm[:state], + :hyperv => 'opennebula' } + end + + info + end - onegate_token_64 = Base64.encode64(onegate_token).chop - end end diff --git a/src/vmm_mad/remotes/one/reboot b/src/vmm_mad/remotes/one/reboot index 9cbc383577..d93de9e100 100755 --- a/src/vmm_mad/remotes/one/reboot +++ b/src/vmm_mad/remotes/one/reboot @@ -38,5 +38,5 @@ require 'opennebula_driver' deploy_id = ARGV[0] host = ARGV[1] -one2one_drv = OpenNebulaDriver.new(host) +one2one_drv = One2OneDriver.new(host) one2one_drv.reboot(deploy_id) diff --git a/src/vmm_mad/remotes/one/reset b/src/vmm_mad/remotes/one/reset index dc43dd5d87..902d613494 100755 --- a/src/vmm_mad/remotes/one/reset +++ b/src/vmm_mad/remotes/one/reset @@ -38,5 +38,5 @@ require 'opennebula_driver' deploy_id = ARGV[1] host = ARGV[0] -one2one_drv = OpenNebulaDriver.new(host) +one2one_drv = One2OneDriver.new(host) one2one_drv.reset(deploy_id) diff --git a/src/vmm_mad/remotes/one/save b/src/vmm_mad/remotes/one/save index 37b777853c..f02571d253 100755 --- a/src/vmm_mad/remotes/one/save +++ b/src/vmm_mad/remotes/one/save @@ -39,5 +39,5 @@ deploy_id = ARGV[0] file = ARGV[1] host = ARGV[2] -one2one_drv = OpenNebulaDriver.new(host) +one2one_drv = One2OneDriver.new(host) one2one_drv.save(deploy_id) diff --git a/src/vmm_mad/remotes/one/shutdown b/src/vmm_mad/remotes/one/shutdown index f1b0f3e9cb..a58811a109 100755 --- a/src/vmm_mad/remotes/one/shutdown +++ b/src/vmm_mad/remotes/one/shutdown @@ -45,5 +45,5 @@ vm.info lcm_state = vm.lcm_state_str -one2one_drv = OpenNebulaDriver.new(host) +one2one_drv = One2OneDriver.new(host) one2one_drv.shutdown(deploy_id, lcm_state)