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

feature #1327: Add parametrized template creation for onevm and onetemplate

Also moved template file read from the creation loop in onevm create
This commit is contained in:
Javi Fontan 2012-09-18 18:19:23 +02:00
parent b0e0f0de12
commit d9cf939ea3
3 changed files with 153 additions and 4 deletions

View File

@ -67,6 +67,60 @@ EOT
:description => "Describe list columns"
}
# Command line VM template options
TEMPLATE_OPTIONS=[
{
:name => 'name',
:large => '--name name',
:description =>
'Name for the VM',
:format => String
},
{
:name => 'cpu',
:large => '--cpu cpu',
:description =>
'CPU percentage reserved for the VM (1=100% one CPU)',
:format => Float
},
{
:name => 'memory',
:large => '--memory memory',
:description => 'Memory ammount given to the VM',
:format => String,
:proc => lambda do |o,options|
m=o.strip.match(/^(\d+(?:\.\d+)?)(m|mb|g|gb)?$/i)
if !m
[-1, 'Memory value malformed']
else
multiplier=case m[2]
when /(g|gb)/i
1024
else
1
end
value=m[1].to_f*multiplier
[0, value.floor]
end
end
},
{
:name => 'disk',
:large => '--disk disk0,disk1',
:description => 'Disks to attach. To use a disk owned by other user use user[disk]',
:format => Array
},
{
:name => 'network',
:large => '--network network0,network1',
:description => 'Networks to attach. To use a network owned by other user use user[network]',
:format => Array
}
]
OPTIONS = XML, NUMERIC, KILOBYTES
class OneHelper
@ -458,4 +512,64 @@ EOT
str = File.read(path)
str
end
def self.parse_user_object(user_object)
reg=/^([^\[]+)(?:\[([^\]]+)\])?$/
m=user_object.match(reg)
return nil if !m
user=nil
if m[2]
user=m[1]
object=m[2]
else
object=m[1]
end
[user, object]
end
def self.create_disk_net(objects, section, name)
template=''
objects.each do |obj|
res=parse_user_object(obj)
return [-1, "#{section.capitalize} \"#{obj}\" malformed"] if !res
user, object=*res
template<<"#{section.upcase}=[\n"
template<<" #{name.upcase}_UNAME=\"#{user}\",\n" if user
template<<" #{name.upcase}=\"#{object}\"\n"
template<<"]\n"
end if objects
[0, template]
end
def self.create_template(options)
template=''
template<<"NAME=\"#{options[:name]}\"\n" if options[:name]
template<<"CPU=#{options[:cpu]}\n" if options[:cpu]
template<<"MEMORY=#{options[:memory]}\n" if options[:memory]
if options[:disk]
res=create_disk_net(options[:disk], 'DISK', 'IMAGE')
return res if res.first!=0
template<<res.last
end
if options[:network]
res=create_disk_net(options[:network], 'NIC', 'NETWORK')
return res if res.first!=0
template<<res.last
end
[0, template]
end
end

View File

@ -82,9 +82,23 @@ cmd=CommandParser::CmdParser.new(ARGV) do
Creates a new Template from the given template file
EOT
command :create, create_desc, :file do
command :create, create_desc, [:file, nil],
:options=>OpenNebulaHelper::TEMPLATE_OPTIONS do
res=OpenNebulaHelper.create_template(options)
if res.first!=0
STDERR.puts res.last
next -1
end
helper.create_resource(options) do |t|
template=File.read(args[0])
if args[0]
template=File.read(args[0])
else
template=''
end
template<<res.last
t.allocate(template)
end
end

View File

@ -99,14 +99,35 @@ cmd=CommandParser::CmdParser.new(ARGV) do
See 'onetemplate create' and 'onetemplate instantiate'
EOT
command :create, create_desc, :file, :options=>[OneVMHelper::MULTIPLE] do
command :create, create_desc, [:file, nil],
:options=>[OneVMHelper::MULTIPLE]+
OpenNebulaHelper::TEMPLATE_OPTIONS do
number = options[:multiple] || 1
exit_code=nil
res=OpenNebulaHelper.create_template(options)
if res.first!=0
STDERR.puts res.last
next -1
end
if args[0]
begin
template=File.read(args[0])
rescue
STDERR.puts "Error reading template."
next -1
end
else
template=''
end
template<<res.last
number.times do
exit_code=helper.create_resource(options) do |vm|
template=File.read(args[0])
error=vm.allocate(template)
end