1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-25 02:50:08 +03:00

Feature #4217: onemarketapp create accepts dynamic template building

This commit is contained in:
Jaime Melis 2016-03-10 17:21:46 +01:00
parent d751456cfd
commit 94bce889ec
2 changed files with 95 additions and 3 deletions

View File

@ -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

View File

@ -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