From 674d68901681b8ec2f9b433d58384c4c25d847d3 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Fri, 21 Sep 2012 23:30:40 +0200 Subject: [PATCH] feature #1327: Extend arguments only support long options. Add examples to create methods using options. Restructure checks for options and template files --- src/cli/one_helper/oneimage_helper.rb | 13 ++--- src/cli/oneimage | 44 +++++++++++----- src/cli/onetemplate | 56 ++++++++++++++------- src/cli/onevm | 72 ++++++++++++++++----------- 4 files changed, 117 insertions(+), 68 deletions(-) diff --git a/src/cli/one_helper/oneimage_helper.rb b/src/cli/one_helper/oneimage_helper.rb index c06eaf6076..44bc84c3b5 100644 --- a/src/cli/one_helper/oneimage_helper.rb +++ b/src/cli/one_helper/oneimage_helper.rb @@ -20,8 +20,7 @@ class OneImageHelper < OpenNebulaHelper::OneHelper TEMPLATE_OPTIONS=[ { :name => "name", - :short => "-n name", - :large => "--name type", + :large => "--name name", :format => String, :description => "Name of the new image" }, @@ -33,10 +32,9 @@ class OneImageHelper < OpenNebulaHelper::OneHelper }, { :name => "type", - :short => "-t type", :large => "--type type", :format => String, - :description => "Type of the new Image", + :description => "Type of the new Image: OS, CDROM or DATABLOCK", :proc => lambda do |o, options| type=o.strip.upcase @@ -49,14 +47,13 @@ class OneImageHelper < OpenNebulaHelper::OneHelper }, { :name => "persistent", - :short => "-p", :large => "--persistent", :description => "Tells if the image will be persistent" }, { :name => "prefix", :large => "--prefix prefix", - :description => "Device prefix for the disk (hd, sd, xvd or vd)", + :description => "Device prefix for the disk (eg. hd, sd, xvd or vd)", :format => String, :proc => lambda do |o, options| prefix=o.strip.downcase @@ -182,7 +179,7 @@ class OneImageHelper < OpenNebulaHelper::OneHelper type_str = Image::IMAGE_TYPES[id] return Image::SHORT_IMAGE_TYPES[type_str] end - + def format_pool(options) config_file = self.class.table_conf @@ -217,7 +214,7 @@ class OneImageHelper < OpenNebulaHelper::OneHelper :size=>15 do |d| OpenNebulaHelper.time_to_str(d["REGTIME"]) end - + column :PERSISTENT, "Whether the Image is persistent or not", :size=>3 do |d| OpenNebulaHelper.boolean_to_str(d["PERSISTENT"]) diff --git a/src/cli/oneimage b/src/cli/oneimage index 697d757a85..c63ca447c8 100755 --- a/src/cli/oneimage +++ b/src/cli/oneimage @@ -77,11 +77,32 @@ cmd=CommandParser::CmdParser.new(ARGV) do ######################################################################## create_desc = <<-EOT.unindent - Creates a new Image from the given template file + Creates a new Image + Examples: + - using a template description file: + + oneimage create -d default centOS.tmpl + + - new image "arch" using a path of type centOS: + + oneimage create -d default --name arch --path /tmp/arch.img + + - new persistent image, OS type and qcow2 format: + + oneimage create -d 1 --name ubuntu --path /tmp/ubuntu.qcow2 \\ + --prefix sd --type OS --driver qcow2 \\ + --description "A OS plain installation" + + - a datablock image of 400MB: + + oneimage create -d 1 --name data --type DATABLOCK --size 400 \\ + --fstype ext2 + EOT - command :create, create_desc, [:file, nil], :options=>CREATE_OPTIONS+ + command :create, create_desc, [:file, nil], :options=>CREATE_OPTIONS + OneImageHelper::TEMPLATE_OPTIONS do + if options[:datastore].nil? STDERR.puts "Datastore to save the image is mandatory: " STDERR.puts "\t -d datastore_id" @@ -94,22 +115,21 @@ cmd=CommandParser::CmdParser.new(ARGV) do next -1 end - res=OneImageHelper.create_image_template(options) - - if res.first!=0 - STDERR.puts res.last - next -1 - end - helper.create_resource(options) do |image| begin if args[0] template=File.read(args[0]) else - template='' + res = OneImageHelper.create_image_template(options) + + if res.first != 0 + STDERR.puts res.last + next -1 + end + + template = res.last end - template< e STDERR.puts e.messsage @@ -156,7 +176,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do end unpublish_desc = <<-EOT.unindent - DEPRECATED, use chmod instead. Unpublishes the given Image. A private + DEPRECATED, use chmod instead. Unpublishes the given Image. A private Image can't be used by any other user. EOT diff --git a/src/cli/onetemplate b/src/cli/onetemplate index e2f24d8537..627bce893c 100755 --- a/src/cli/onetemplate +++ b/src/cli/onetemplate @@ -79,12 +79,26 @@ cmd=CommandParser::CmdParser.new(ARGV) do ######################################################################## create_desc = <<-EOT.unindent - Creates a new Template from the given template file + Creates a new VM Template from the given description + + Examples: + - using a VM Template description file: + + onetemplate create vm_description.tmpl + + - new VM Template named "arch vm" with a disk and a nic: + + onetemplate create --name "arch vm" --memory 128 --cpu 1 --disk arch \\ + --network private_lan + + - using two disks: + + onevm create --name "test vm" --memory 128 --cpu 1 --disk arch,data + EOT - command :create, create_desc, [:file, nil], - :options=>OpenNebulaHelper::TEMPLATE_OPTIONS do - res=OpenNebulaHelper.create_template(options) + command :create, create_desc, [:file, nil], :options => + OpenNebulaHelper::TEMPLATE_OPTIONS do if args[0] && OpenNebulaHelper.create_template_options_used?(options) STDERR.puts "You can not use both template file and template"<< @@ -92,20 +106,26 @@ cmd=CommandParser::CmdParser.new(ARGV) do next -1 end - if res.first!=0 - STDERR.puts res.last - next -1 - end + helper.create_resource(options) do |tmpl| + begin + if args[0] + template = File.read(args[0]) + else + res = OpenNebulaHelper.create_template(options) - helper.create_resource(options) do |t| - if args[0] - template=File.read(args[0]) - else - template='' + if res.first != 0 + STDERR.puts res.last + next -1 + end + + template = res.last + end + + tmpl.allocate(template) + rescue => e + STDERR.puts e.messsage + exit -1 end - - template<[OneVMHelper::MULTIPLE]+ - OpenNebulaHelper::TEMPLATE_OPTIONS do - number = options[:multiple] || 1 + command :create, create_desc, [:file, nil], :options => + [OneVMHelper::MULTIPLE] + OpenNebulaHelper::TEMPLATE_OPTIONS do - exit_code=nil + number = options[:multiple] || 1 + exit_code = nil - res=OpenNebulaHelper.create_template(options) - - if res.first!=0 - STDERR.puts res.last + if args[0] && OpenNebulaHelper.create_template_options_used?(options) + STDERR.puts "You can not use both template file and template"<< + " creation options." next -1 end - if args[0] - if OpenNebulaHelper.create_template_options_used?(options) - STDERR.puts "You can not use both template file and template"<< - " creation options." - next -1 - end - - begin + begin + if args[0] template=File.read(args[0]) - rescue - STDERR.puts "Error reading template." - next -1 + else + res = OpenNebulaHelper.create_template(options) + + if res.first != 0 + STDERR.puts res.last + next -1 + end + + template = res.last end - else - template='' + rescue Exception => e + STDERR.puts "Error reading template." + next -1 end - template<