From d9cf939ea3f0b7816e81c5a7e94227da35a0d39b Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Tue, 18 Sep 2012 18:19:23 +0200 Subject: [PATCH] feature #1327: Add parametrized template creation for onevm and onetemplate Also moved template file read from the creation loop in onevm create --- src/cli/one_helper.rb | 114 ++++++++++++++++++++++++++++++++++++++++++ src/cli/onetemplate | 18 ++++++- src/cli/onevm | 25 ++++++++- 3 files changed, 153 insertions(+), 4 deletions(-) diff --git a/src/cli/one_helper.rb b/src/cli/one_helper.rb index 3fcf53f35d..8857c889c7 100644 --- a/src/cli/one_helper.rb +++ b/src/cli/one_helper.rb @@ -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<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<[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<