From 904edd62cc217045bc8a2ce8784f977fa112375c Mon Sep 17 00:00:00 2001 From: Sergio Semedi Barranco Date: Wed, 28 Jun 2017 12:38:16 +0200 Subject: [PATCH] F #5136 ec2_driver.conf purge (#357) * fix opennebula log * F #5136 ec2_driver capacities replaced * F #5136 required ec2 region_name template att * F #5136 ec2_driver.conf removed regions * F #5136 decrypt moved to oca utils --- src/im_mad/remotes/ec2.d/poll | 4 +- src/oca/ruby/opennebula/utils.rb | 15 ++++++ src/vmm_mad/remotes/ec2/ec2_driver.conf | 55 -------------------- src/vmm_mad/remotes/ec2/ec2_driver.rb | 67 ++++++++++++------------- 4 files changed, 50 insertions(+), 91 deletions(-) diff --git a/src/im_mad/remotes/ec2.d/poll b/src/im_mad/remotes/ec2.d/poll index f6a793329b..f2807fdc01 100755 --- a/src/im_mad/remotes/ec2.d/poll +++ b/src/im_mad/remotes/ec2.d/poll @@ -36,9 +36,9 @@ begin ec2_drv.monitor_all_vms rescue Exception => e - opennebula::log_error(e.message) + OpenNebula.log_error(e.message) STDERR.puts "********** STACK TRACE ************" STDERR.puts e.backtrace STDERR.puts "***********************************" - exit -1 + exit -1 end diff --git a/src/oca/ruby/opennebula/utils.rb b/src/oca/ruby/opennebula/utils.rb index 2469590329..b4b03b7021 100644 --- a/src/oca/ruby/opennebula/utils.rb +++ b/src/oca/ruby/opennebula/utils.rb @@ -32,4 +32,19 @@ module OpenNebula return res end + # receive hashed values (res) with a token + # returns original values + def self.decrypt(res, token) + opts = {} + + res.each do |key, encrypted_value| + decipher = OpenSSL::Cipher::AES.new(256,:CBC) + decipher.decrypt + decipher.key = token[0..31] + plain = decipher.update(Base64::decode64(encrypted_value)) + decipher.final + opts[key] = plain + end + return opts + end + end diff --git a/src/vmm_mad/remotes/ec2/ec2_driver.conf b/src/vmm_mad/remotes/ec2/ec2_driver.conf index 44b4e3814d..17a9499078 100644 --- a/src/vmm_mad/remotes/ec2/ec2_driver.conf +++ b/src/vmm_mad/remotes/ec2/ec2_driver.conf @@ -1,60 +1,5 @@ proxy_uri: state_wait_timeout_seconds: 300 -regions: - default: - region_name: us-east-1 - capacity: - m1.small: 5 - m1.large: 0 - m1.xlarge: 0 - us-east-1: - region_name: us-east-1 - capacity: - m1.small: - m1.large: - m1.xlarge: - us-west-2: - region_name: us-west-2 - capacity: - m1.small: - m1.large: - m1.xlarge: - us-west-1: - region_name: us-west-1 - capacity: - m1.small: - m1.large: - m1.xlarge: - eu-west-1: - region_name: eu-west-1 - capacity: - m1.small: - m1.large: - m1.xlarge: - ap-southeast-1: - region_name: ap-southeast-1 - capacity: - m1.small: - m1.large: - m1.xlarge: - ap-southeast-2: - region_name: ap-southeast-2 - capacity: - m1.small: - m1.large: - m1.xlarge: - ap-northeast-1: - region_name: ap-northeast-1 - capacity: - m1.small: - m1.large: - m1.xlarge: - sa-east-1: - region_name: sa-east-1 - capacity: - m1.small: - m1.large: - m1.xlarge: instance_types: c1.medium: cpu: 2 diff --git a/src/vmm_mad/remotes/ec2/ec2_driver.rb b/src/vmm_mad/remotes/ec2/ec2_driver.rb index 23b822d0b3..6b55ba3cf7 100755 --- a/src/vmm_mad/remotes/ec2/ec2_driver.rb +++ b/src/vmm_mad/remotes/ec2/ec2_driver.rb @@ -247,21 +247,19 @@ class EC2Driver @instance_types = PUBLIC_CLOUD_EC2_CONF['instance_types'] conn_opts = get_connect_info(host) - regions = PUBLIC_CLOUD_EC2_CONF['regions'] access_key = conn_opts[:access] secret_key = conn_opts[:secret] + region_name = conn_opts[:region] - @region = regions[host] || regions["default"] - #sanitize region data raise "access_key_id not defined for #{host}" if access_key.nil? raise "secret_access_key not defined for #{host}" if secret_key.nil? - raise "region_name not defined for #{host}" if @region['region_name'].nil? + raise "region_name not defined for #{host}" if region_name.nil? Aws.config.merge!({ :access_key_id => access_key, :secret_access_key => secret_key, - :region => @region['region_name'] + :region => region_name }) if (proxy_uri = PUBLIC_CLOUD_EC2_CONF['proxy_uri']) @@ -270,20 +268,10 @@ class EC2Driver @ec2 = Aws::EC2::Resource.new end - def decrypt(res, token) - opts = {} - - res.each do |key, encrypted_value| - decipher = OpenSSL::Cipher::AES.new(256,:CBC) - decipher.decrypt - decipher.key = token[0..31] - plain = decipher.update(Base64::decode64(encrypted_value)) + decipher.final - opts[key] = plain - end - return opts - - end + # Check the current template of host + # to retrieve connection information + # needed for Amazon def get_connect_info(host) conn_opts={} @@ -306,12 +294,13 @@ class EC2Driver :access => xmlhost["TEMPLATE/EC2_ACCESS"], :secret => xmlhost["TEMPLATE/EC2_SECRET"] } - begin - conn_opts = decrypt(conn_opts, token) + begin + conn_opts = OpenNebula.decrypt(conn_opts, token) + conn_opts[:region] = xmlhost["TEMPLATE/REGION_NAME"] rescue - raise "HOST: #{host} must have ec2 credentials in order to work properly" + raise "HOST: #{host} must have ec2 credentials and region in order to work properly" end - + return conn_opts end @@ -319,7 +308,6 @@ class EC2Driver def deploy(id, host, xml_text, lcm_state, deploy_id) # Restore if we need to - if lcm_state != "BOOT" && lcm_state != "BOOT_FAILURE" restore(deploy_id) return deploy_id @@ -394,7 +382,7 @@ class EC2Driver instance.create_tags(:tags => tag_array) if tag_array.length > 0 elastic_ip = ec2_value(ec2_info, 'ELASTICIP') - + wait_state('running', instance.id) if elastic_ip @@ -465,17 +453,33 @@ class EC2Driver puts parse_poll(i, vm, do_cw, cw_mon_time) end + # Parse template instance type into + # Amazon ec2 format (M1SMALL => m1.small) + def parse_inst_type(type) + fixed_type = type[0..1]<< '.' << type[2..type.length+1] + return fixed_type.downcase + end + # Get the info of all the EC2 instances. An EC2 instance must include # the ONE_ID tag, otherwise it will be ignored def monitor_all_vms totalmemory = 0 totalcpu = 0 - @region['capacity'].each { |name, size| - cpu, mem = instance_type_capacity(name) - totalmemory += mem * size.to_i - totalcpu += cpu * size.to_i - } + # Get last cloudwatch monitoring time + host_obj = OpenNebula::Host.new_with_id(@host_id, + OpenNebula::Client.new) + host_obj.info + cw_mon_time = host_obj["/HOST/TEMPLATE/CWMONTIME"] + capacity = host_obj.to_hash["HOST"]["TEMPLATE"]["CAPACITY"] + if !capacity.nil? && Hash === capacity + capacity.each{ |name, value| + name = parse_inst_type(name) + cpu, mem = instance_type_capacity(name) + totalmemory += mem * value.to_i + totalcpu += cpu * value.to_i + } + end host_info = "HYPERVISOR=ec2\n" host_info << "PUBLIC_CLOUD=YES\n" @@ -499,11 +503,6 @@ class EC2Driver vpool.info onevm_info = {} - # Get last cloudwatch monitoring time - host_obj = OpenNebula::Host.new_with_id(@host_id, - OpenNebula::Client.new) - host_obj.info - cw_mon_time = host_obj["/HOST/TEMPLATE/CWMONTIME"] if !cw_mon_time cw_mon_time = Time.now.to_i