From 94bce889ec17f153db7752cec6b2f900f7ad7b15 Mon Sep 17 00:00:00 2001 From: Jaime Melis Date: Thu, 10 Mar 2016 17:21:46 +0100 Subject: [PATCH] Feature #4217: onemarketapp create accepts dynamic template building --- src/cli/one_helper/onemarketapp_helper.rb | 68 +++++++++++++++++++++++ src/cli/onemarketapp | 30 +++++++++- 2 files changed, 95 insertions(+), 3 deletions(-) diff --git a/src/cli/one_helper/onemarketapp_helper.rb b/src/cli/one_helper/onemarketapp_helper.rb index b971e3c1b5..12cbb0ae6c 100644 --- a/src/cli/one_helper/onemarketapp_helper.rb +++ b/src/cli/one_helper/onemarketapp_helper.rb @@ -17,6 +17,32 @@ require 'one_helper' class OneMarketPlaceAppHelper < OpenNebulaHelper::OneHelper + TEMPLATE_OPTIONS=[ + { + :name => "name", + :large => "--name name", + :format => String, + :description => "Name of the new MarketPlaceApp" + }, + { + :name => "description", + :large => "--description description", + :format => String, + :description => "Description for the new MarketPlaceApp" + }, + { + :name => "image", + :large => "--image id|name" , + :description => "Selects the image", + :format => String, + :template_key => "origin_id", + :proc => lambda { |o, options| + OpenNebulaHelper.rname_to_id(o, "IMAGE") + } + }, + OpenNebulaHelper::DRY + ] + def self.rname "MARKETPLACEAPP" end @@ -74,6 +100,18 @@ class OneMarketPlaceAppHelper < OpenNebulaHelper::OneHelper table end + def self.create_template_options_used?(options) + # Get the template options names as symbols. options hash + # uses symbols + template_options=self::TEMPLATE_OPTIONS.map do |o| + o[:name].to_sym + end + + # Check if one at least one of the template options is + # in options hash + (template_options-options.keys)!=template_options + end + private def factory(id=nil) @@ -141,4 +179,34 @@ class OneMarketPlaceAppHelper < OpenNebulaHelper::OneHelper puts end + + def self.create_variables(options, name) + if Array===name + names=name + else + names=[name] + end + + t='' + names.each do |n| + if options[n] + t<<"#{n.to_s.upcase}=\"#{options[n]}\"\n" + end + end + + t + end + + def self.create_datastore_template(options) + template_options=TEMPLATE_OPTIONS.map do |o| + o[:name].to_sym + end + + template=create_variables(options, template_options-[:dry,:image]) + + template<<"ORIGIN_ID=#{options[:image]}\n" if options[:image] + template << "TYPE=image\n" + + [0, template] + end end diff --git a/src/cli/onemarketapp b/src/cli/onemarketapp index 84cade3758..17bf709499 100755 --- a/src/cli/onemarketapp +++ b/src/cli/onemarketapp @@ -88,7 +88,8 @@ CommandParser::CmdParser.new(ARGV) do Creates a new marketplace app in the given marketplace EOT - command :create, create_desc, :file, :options=>CREATE_OPTIONS do + command :create, create_desc, [:file, nil], :options=>CREATE_OPTIONS + + OneMarketPlaceAppHelper::TEMPLATE_OPTIONS do if options[:marketplace].nil? STDERR.puts "Marketplace to save the app is mandatory: " @@ -96,12 +97,35 @@ CommandParser::CmdParser.new(ARGV) do exit(-1) end + if args[0] && OneMarketPlaceAppHelper.create_template_options_used?(options) + STDERR.puts "You can not use both template file and template"<< + " creation options." + next -1 + end + helper.create_resource(options) do |app| begin - template=File.read(args[0]) + if args[0] + template=File.read(args[0]) + else + res = OneMarketPlaceAppHelper.create_datastore_template(options) + + if res.first != 0 + STDERR.puts res.last + next -1 + end + + template = res.last + end + + if options[:dry] + puts template + exit 0 + end + app.allocate(template, options[:marketplace]) rescue => e - STDERR.puts e.messsage + STDERR.puts e.message exit(-1) end end