From c95884aa5163ab86eabf2a19d52d97061e30fc4a Mon Sep 17 00:00:00 2001 From: "Carlos J. Herrera" Date: Thu, 21 Jan 2021 07:49:14 -0500 Subject: [PATCH] F #4001: default VM Templates in vCenter in MarketApp (#629) --- src/cli/onemarketapp | 17 ++++++- src/oca/ruby/opennebula/marketplaceapp_ext.rb | 44 ++++++++++++++----- src/vmm_mad/remotes/vcenter/vcenterrc | 5 ++- 3 files changed, 53 insertions(+), 13 deletions(-) diff --git a/src/cli/onemarketapp b/src/cli/onemarketapp index 708fd32bfb..057b4f8f65 100755 --- a/src/cli/onemarketapp +++ b/src/cli/onemarketapp @@ -42,6 +42,9 @@ require 'one_helper/onemarketapp_helper' require 'one_helper/onemarket_helper' require 'one_helper/onedatastore_helper' +require 'vcenter_driver' +CONFIG = VCenterConf.new + CommandParser::CmdParser.new(ARGV) do usage '`onemarket` [] []' version OpenNebulaHelper::ONE_VERSION @@ -101,6 +104,13 @@ CommandParser::CmdParser.new(ARGV) do end } + TEMPLATE = { + :name => 'template', + :large => '--template template_id ', + :description => 'Associate with VM template', + :format => Integer, + } + MARKET = { :name => 'market', :large => '--market market_id', @@ -123,7 +133,8 @@ CommandParser::CmdParser.new(ARGV) do OneDatastoreHelper::FILE_DATASTORE, OneMarketPlaceAppHelper::VMNAME, TAG, - NO] + NO, + TEMPLATE] IMPORT_OPTIONS = [YES, NO, MARKET, OneMarketPlaceAppHelper::VMNAME] ######################################################################## @@ -272,7 +283,9 @@ CommandParser::CmdParser.new(ARGV) do :name => args[1], :vmtemplate_name => options[:vmname], :url_args => tag, - :notemplate => options[:no] == 'no' + :notemplate => options[:no] == 'no', + :template => options[:template], + :default_template => CONFIG[:default_template], ) if OpenNebula.is_error?(rc) diff --git a/src/oca/ruby/opennebula/marketplaceapp_ext.rb b/src/oca/ruby/opennebula/marketplaceapp_ext.rb index d1badc655c..834107a192 100644 --- a/src/oca/ruby/opennebula/marketplaceapp_ext.rb +++ b/src/oca/ruby/opennebula/marketplaceapp_ext.rb @@ -89,12 +89,14 @@ module OpenNebula::MarketPlaceAppExt private # Exports an OpenNebula Image from this marketplace app - # @param optoins to export the image + # @param options to export the image # :vmtemplate_name [String] name of new image and template # :url_args [String] optional URL arguments # :dsid [String] Datastore id to create the image # :f_dsid [String] Files Datastore id # :notemplate [Bool] if true do not create vm_template (if any) + # :template [Integer] Template id to use with image + # :default_template [String] Default template id for vCenter # @return [Hash] # :image [Array] of the new image # :image_type [String] of the new image (CONTEXT, KERNEL, CDROM) @@ -181,19 +183,41 @@ module OpenNebula::MarketPlaceAppExt return rc_info end - tmpl = Base64.decode64(self['TEMPLATE/VMTEMPLATE64']) - tmpl << <<-EOT + if !options[:template].nil? + template_id = options[:template] - NAME = "#{options[:vmtemplate_name] || options[:name]}" - DISK = [ IMAGE_ID = "#{image.id}" ] - EOT + template_id = options[:default_template] if template_id == -1 - vmtpl = Template.new(Template.build_xml, @client) + template = Template.new_with_id(template_id, @client) - rc = vmtpl.allocate(tmpl) - rc = vmtpl.id unless OpenNebula.is_error?(rc) + vmtpl_id = template.clone(options[:vmtemplate_name] || options[:name]) - rc_info[:vmtemplate] = [rc] + tmpl << <<-EOT + + NAME = "#{options[:vmtemplate_name] || options[:name]}" + DISK = [ IMAGE_ID = "#{image.id}" ] + EOT + + template = Template.new_with_id(vmtpl_id, @client) + + template.update(tmpl, true) + + rc_info[:vmtemplate] = [vmtpl_id] + else + tmpl = Base64.decode64(self['TEMPLATE/VMTEMPLATE64']) + tmpl << <<-EOT + + NAME = "#{options[:vmtemplate_name] || options[:name]}" + DISK = [ IMAGE_ID = "#{image.id}" ] + EOT + + vmtpl = Template.new(Template.build_xml, @client) + + rc = vmtpl.allocate(tmpl) + rc = vmtpl.id unless OpenNebula.is_error?(rc) + + rc_info[:vmtemplate] = [rc] + end rc_info end diff --git a/src/vmm_mad/remotes/vcenter/vcenterrc b/src/vmm_mad/remotes/vcenter/vcenterrc index 52ea919a79..c85fc301f3 100644 --- a/src/vmm_mad/remotes/vcenter/vcenterrc +++ b/src/vmm_mad/remotes/vcenter/vcenterrc @@ -35,4 +35,7 @@ :memory_dumps: true # Cache expiration for monitoring -:cache_expire: 120 \ No newline at end of file +:cache_expire: 120 + +# Default template when import from MarketPlace +:default_template: -1