From 8e5a915c6d0bc5ab501dc9515b4d29eb76d4c900 Mon Sep 17 00:00:00 2001 From: Alejandro Huertas Herrero Date: Wed, 17 Feb 2021 11:08:58 +0100 Subject: [PATCH] M #-: fix minor bugs in provision (#820) --- src/oneprovision/lib/provision/provision.rb | 29 +++++---- .../resources/virtual/marketplaceapp.rb | 61 +++++++++++-------- src/oneprovision/lib/terraform/terraform.rb | 6 ++ 3 files changed, 58 insertions(+), 38 deletions(-) diff --git a/src/oneprovision/lib/provision/provision.rb b/src/oneprovision/lib/provision/provision.rb index 51dffa2830..4876f6c419 100644 --- a/src/oneprovision/lib/provision/provision.rb +++ b/src/oneprovision/lib/provision/provision.rb @@ -168,6 +168,15 @@ module OneProvision { :state => tf_state, :conf => tf_conf } end + # Adds Terraform information to the provision + def add_tf(state, conf) + @body['tf'] = {} unless @body['tf'] + @body['tf']['state'] = state + @body['tf']['conf'] = conf + + update + end + # Get OpenNebula information for specific objects # # @param object [String] Object to check @@ -322,11 +331,7 @@ module OneProvision update_hosts(ips, ids) - if state && conf - @body['tf'] = {} - @body['tf']['state'] = state - @body['tf']['conf'] = conf - end + add_tf(state, conf) if state && conf update end @@ -460,10 +465,7 @@ module OneProvision return [-1, rc.message] if OpenNebula.is_error?(rc) # If it is an array, a host has been deleted - if rc.is_a? Array - @body['tf']['state'] = rc[0] - @body['tf']['conf'] = rc[1] - end + add_tf(rc[0], rc[1]) if rc.is_a? Array @body['provision'][path][object].delete_if do |obj| true if obj['id'].to_s == id.to_s @@ -623,7 +625,7 @@ module OneProvision ret = obj.create resource_objects[r] = [] unless resource_objects[r] - if ret.is_a? Array + if ret.is_a? Hash # Marketplace app unless resource_objects['images'] resource_objects['images'] = [] @@ -633,8 +635,11 @@ module OneProvision resource_objects['templates'] = [] end - resource_objects['images'] << ret[0] - resource_objects['templates'] << ret[1] + resource_objects['images'] << ret[:image] if ret[:image] + + if ret[:template] + resource_objects['templates'] << ret[:template] + end else resource_objects[r] << { 'id' => ret, 'name' => obj.one['NAME'] } diff --git a/src/oneprovision/lib/provision/resources/virtual/marketplaceapp.rb b/src/oneprovision/lib/provision/resources/virtual/marketplaceapp.rb index d459c1d3a4..fd785aad5f 100644 --- a/src/oneprovision/lib/provision/resources/virtual/marketplaceapp.rb +++ b/src/oneprovision/lib/provision/resources/virtual/marketplaceapp.rb @@ -54,6 +54,7 @@ module OneProvision rc = app.info Utils.exception(rc) + app.extend(MarketPlaceAppExt) url_args = "tag=#{@p_template['tag']}" if @p_template['tag'] @@ -67,44 +68,52 @@ module OneProvision :vmtemplate_name => @p_template['vmname'], :url_args => url_args ) + Utils.exception(rc[:image].first) if rc[:image] Utils.exception(rc[:vmtemplate].first) if rc[:vmtemplate] # get new IDs image_id = rc[:image].first template_id = rc[:vmtemplate].first - - OneProvisionLogger.debug( - "Image created with ID: #{image_id}" - ) - - OneProvisionLogger.debug( - "Template created with ID: #{template_id}" - ) - # get new created template and update it with provision ID - @template = Template.new - - @template.info(template_id) + p_id = @p_template['provision']['id'] + ret = {} # get new created image and update it with provision ID - @image = Image.new - p_id = @p_template['provision']['id'] + if image_id + OneProvisionLogger.debug( + "Image created with ID: #{image_id}" + ) - @image.info(image_id) - @image.update_provision_info({ 'wait' => wait, - 'wait_timeout' => timeout, - 'id' => p_id }) + @image = Image.new - @template.update_provision_info({ 'id' => p_id }) + @image.info(image_id) + @image.update_provision_info({ 'wait' => wait, + 'wait_timeout' => timeout, + 'id' => p_id }) - # Change permissions and ownership - @image.template_chown(@p_template) - @image.template_chmod(@p_template) - @template.template_chown(@p_template) - @template.template_chmod(@p_template) + @image.template_chown(@p_template) + @image.template_chmod(@p_template) - ret = [{ 'id' => image_id, 'name' => @image.one['NAME'] }, - { 'id' => template_id, 'name' => @template.one['NAME'] }] + ret[:image] = { 'id' => image_id, + 'name' => @image.one['NAME'] } + end + + # get new created template and update it with provision ID + if template_id + OneProvisionLogger.debug( + "Template created with ID: #{template_id}" + ) + + @template = Template.new + + @template.info(template_id) + @template.update_provision_info({ 'id' => p_id }) + @template.template_chown(@p_template) + @template.template_chmod(@p_template) + + ret[:template] = { 'id' => template_id, + 'name' => @template.one['NAME'] } + end return ret unless wait diff --git a/src/oneprovision/lib/terraform/terraform.rb b/src/oneprovision/lib/terraform/terraform.rb index 40f1068a11..5bd7637bfc 100644 --- a/src/oneprovision/lib/terraform/terraform.rb +++ b/src/oneprovision/lib/terraform/terraform.rb @@ -151,6 +151,12 @@ module OneProvision ) unless s && s.success? + @state = File.read("#{tempdir}/terraform.tfstate") + conf = Base64.encode64(Zlib::Deflate.deflate(@conf)) + state = Base64.encode64(Zlib::Deflate.deflate(@state)) + + provision.add_tf(state, conf) + STDERR.puts '[ERROR] Hosts provision failed!!! ' \ 'Please log in to your console to delete ' \ 'left resources'