From 22e3c3fdd6ebe6df4dd4e232b248a161fca064fc Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Thu, 23 Aug 2012 14:22:11 +0200 Subject: [PATCH 1/3] feature #1427: Improve command_parser * Add support for commands without actions (create..): main * Add exit_code method * Add option method instead of set :option (deprecated) * Add format method instead of set :format (deprecated) * Improve option :proc handling(cherry picked from commit 8572f13fe8cac6583d3bff250fa9e7953272d4f4) (cherry picked from commit c71ba4a27e4e95002caf537515fd20a264a3ffab) --- src/cli/command_parser.rb | 353 ++++++++++++---------- src/cli/one_helper/onecluster_helper.rb | 12 +- src/cli/one_helper/onedatastore_helper.rb | 10 +- src/cli/one_helper/onevm_helper.rb | 17 +- 4 files changed, 203 insertions(+), 189 deletions(-) diff --git a/src/cli/command_parser.rb b/src/cli/command_parser.rb index 7c844738f1..75d297f3f1 100644 --- a/src/cli/command_parser.rb +++ b/src/cli/command_parser.rb @@ -54,25 +54,18 @@ module CommandParser attr_reader :options, :args def initialize(args=[], &block) - @opts = Array.new + @available_options = Array.new @commands = Hash.new @formats = Hash.new - @script = nil + + @main = nil + + @exit_code = nil @args = args @options = Hash.new - set :format, :file, "Path to a file" do |arg| - format_file(arg) - end - - set :format, :range, "List of id's in the form 1,8..15" do |arg| - format_range(arg) - end - - set :format, :text, "String" do |arg| - format_text(arg) - end + define_default_formats instance_eval(&block) @@ -80,11 +73,7 @@ module CommandParser end def usage(str) - @usage=< description, + :proc => block + } + end + + def option(options) + if options.instance_of?(Array) + options.each { |o| @available_options << o } + elsif options.instance_of?(Hash) + @available_options << options end end + # Define the exit code to be returned by the command + # @param [Integer] code + def exit_code(code) + @exit_code = code + end + def exit_with_code(code, output=nil) puts output if output exit code @@ -130,28 +131,40 @@ EOT @commands[name.to_sym] = cmd end - def script(*args_format, &block) - @script=Hash.new - @script[:args_format] = Array.new + def main(*args_format, &block) + @main=Hash.new + @main[:arity] = 0 + @main[:args_format] = Array.new args_format.collect {|args| if args.instance_of?(Array) - @script[:arity]+=1 unless args.include?(nil) - @script[:args_format] << args + @main[:arity]+=1 unless args.include?(nil) + @main[:args_format] << args elsif args.instance_of?(Hash) && args[:options] - @opts << args[:options] + @available_options << args[:options] else - @script[:arity]+=1 - @script[:args_format] << [args] + @main[:arity]+=1 + @main[:args_format] << [args] end } - @script[:proc] = block + @main[:proc] = block end + # DEPRECATED, use format and options instead + def set(e, *args, &block) + case e + when :option + option(args[0]) + when :format + format(args[0], args[1], &block) + end + end + + def run comm_name="" - if @script - comm=@script + if @main + comm=@main elsif if @args[0] && !@args[0].match(/^-/) comm_name=@args.shift.to_sym @@ -160,7 +173,7 @@ EOT end if comm.nil? - help + print_help exit -1 end @@ -175,133 +188,17 @@ EOT puts rc[1] exit rc.first else - exit rc + exit(@exit_code || rc) end end end - def help - puts @usage if @usage - puts - puts @description if @description - puts - print_options - puts - print_commands - puts - print_formatters - puts - if @version - puts "## LICENSE" - puts @version - end - end - private - def print_options - puts "## OPTIONS" - - shown_opts = Array.new - opt_format = "#{' '*5}%-25s %s" - @commands.each{ |key,value| - value[:options].flatten.each { |o| - if shown_opts.include?(o[:name]) - next - else - shown_opts << o[:name] - - str = "" - str << o[:short].split(' ').first << ', ' if o[:short] - str << o[:large] - - printf opt_format, str, o[:description] - puts - end - } - } - - @opts.each{ |o| - str = "" - str << o[:short].split(' ').first << ', ' if o[:short] - str << o[:large] - - printf opt_format, str, o[:description] - puts - } - end - - def print_commands - puts "## COMMANDS" - - cmd_format5 = "#{' '*3}%s" - cmd_format10 = "#{' '*8}%s" - @commands.each{ |key,value| - printf cmd_format5, "* #{key} " - - args_str=value[:args_format].collect{ |a| - if a.include?(nil) - "[<#{a.compact.join("|")}>]" - else - "<#{a.join("|")}>" - end - }.join(' ') - printf "#{args_str}" - puts - - value[:desc].split("\n").each { |l| - printf cmd_format10, l - puts - } - - unless value[:options].empty? - opts_str=value[:options].flatten.collect{|o| - o[:name] - }.join(', ') - printf cmd_format10, "valid options: #{opts_str}" - puts - end - puts - } - end - - def print_formatters - puts "## ARGUMENT FORMATS" - - cmd_format5 = "#{' '*3}%s" - cmd_format10 = "#{' '*8}%s" - @formats.each{ |key,value| - printf cmd_format5, "* #{key}" - puts - - value[:desc].split("\n").each { |l| - printf cmd_format10, l - puts - } - - puts - } - end - - def add_option(option) - if option.instance_of?(Array) - option.each { |o| @opts << o } - elsif option.instance_of?(Hash) - @opts << option - end - end - - def add_format(format, description, block) - @formats[format] = { - :desc => description, - :proc => block - } - end - def parse(extra_options) @cmdparse=OptionParser.new do |opts| - merge = @opts - merge = @opts + extra_options if extra_options + merge = @available_options + merge = @available_options + extra_options if extra_options merge.flatten.each do |e| args = [] args << e[:short] if e[:short] @@ -311,9 +208,16 @@ EOT opts.on(*args) do |o| if e[:proc] - e[:proc].call(o, @options) + rc = e[:proc].call(o, @options) + if rc.instance_of?(Array) && rc[0] == 0 + options[e[:name].to_sym] = rc[1] + else + puts rc[1] + puts "option #{e[:name]}: Parsing error" + exit -1 + end elsif e[:name]=="help" - help + print_help exit elsif e[:name]=="version" puts @version @@ -399,7 +303,126 @@ EOT end ######################################################################## - # Formatters for arguments + # Printers + ######################################################################## + + def print_help + if @usage + puts "## SYNOPSIS" + puts + puts @usage + puts + end + puts @description if @description + puts + print_options + puts + print_commands + puts + print_formatters + puts + if @version + puts "## LICENSE" + puts @version + end + end + + + def print_options + puts "## OPTIONS" + + shown_opts = Array.new + opt_format = "#{' '*5}%-25s %s" + @commands.each{ |key,value| + value[:options].flatten.each { |o| + if shown_opts.include?(o[:name]) + next + else + shown_opts << o[:name] + + str = "" + str << o[:short].split(' ').first << ', ' if o[:short] + str << o[:large] + + printf opt_format, str, o[:description] + puts + end + } + } + + @available_options.each{ |o| + str = "" + str << o[:short].split(' ').first << ', ' if o[:short] + str << o[:large] + + printf opt_format, str, o[:description] + puts + } + end + + def print_commands + cmd_format5 = "#{' '*3}%s" + cmd_format10 = "#{' '*8}%s" + + if @main + print_command(@main) + else + puts "## COMMANDS" + + @commands.each{ |key,value| + printf cmd_format5, "* #{key} " + + print_command(value) + } + end + end + + def print_command(command) + args_str=command[:args_format].collect{ |a| + if a.include?(nil) + "[<#{a.compact.join("|")}>]" + else + "<#{a.join("|")}>" + end + }.join(' ') + printf "#{args_str}" + puts + + command[:desc].split("\n").each { |l| + printf cmd_format10, l + puts + } + + unless command[:options].empty? + opts_str=command[:options].flatten.collect{|o| + o[:name] + }.join(', ') + printf cmd_format10, "valid options: #{opts_str}" + puts + end + puts + end + + def print_formatters + puts "## ARGUMENT FORMATS" + + cmd_format5 = "#{' '*3}%s" + cmd_format10 = "#{' '*8}%s" + @formats.each{ |key,value| + printf cmd_format5, "* #{key}" + puts + + value[:desc].split("\n").each { |l| + printf cmd_format10, l + puts + } + + puts + } + end + + ######################################################################## + # Default Formatters for arguments ######################################################################## def format_text(arg) arg.instance_of?(String) ? [0,arg] : [-1] @@ -432,6 +455,20 @@ EOT return 0,ids.uniq end + + def define_default_formats + format :file, "Path to a file" do |arg| + format_file(arg) + end + + format :range, "List of id's in the form 1,8..15" do |arg| + format_range(arg) + end + + format :text, "String" do |arg| + format_text(arg) + end + end end end diff --git a/src/cli/one_helper/onecluster_helper.rb b/src/cli/one_helper/onecluster_helper.rb index d427c4a8f5..7235f2eb6f 100644 --- a/src/cli/one_helper/onecluster_helper.rb +++ b/src/cli/one_helper/onecluster_helper.rb @@ -25,18 +25,10 @@ class OneClusterHelper < OpenNebulaHelper::OneHelper :description => "Selects the cluster", :format => String, :proc => lambda { |o, options| - ch = OneClusterHelper.new - rc, cid = ch.to_id(o) - if rc == 0 - options[:cluster] = cid - else - puts cid - puts "option cluster: Parsing error" - exit -1 - end + OpenNebulaHelper.rname_to_id(o, "CLUSTER") } } - + def self.rname "CLUSTER" end diff --git a/src/cli/one_helper/onedatastore_helper.rb b/src/cli/one_helper/onedatastore_helper.rb index 3ec15e30f7..d8495bfa71 100644 --- a/src/cli/one_helper/onedatastore_helper.rb +++ b/src/cli/one_helper/onedatastore_helper.rb @@ -24,15 +24,7 @@ class OneDatastoreHelper < OpenNebulaHelper::OneHelper :description => "Selects the datastore", :format => String, :proc => lambda { |o, options| - ch = OneDatastoreHelper.new - rc, dsid = ch.to_id(o) - if rc == 0 - options[:datastore] = dsid - else - puts dsid - puts "option datastore: Parsing error" - exit -1 - end + OpenNebulaHelper.rname_to_id(o, "DATASTORE") } } diff --git a/src/cli/one_helper/onevm_helper.rb b/src/cli/one_helper/onevm_helper.rb index 7de8e51a32..e6bafadab4 100644 --- a/src/cli/one_helper/onevm_helper.rb +++ b/src/cli/one_helper/onevm_helper.rb @@ -32,14 +32,7 @@ class OneVMHelper < OpenNebulaHelper::OneHelper :description => "Selects the image", :format => String, :proc => lambda { |o, options| - rc, imid = OpenNebulaHelper.rname_to_id(o, "IMAGE") - if rc == 0 - options[:image] = imid - else - puts imid - puts "option image: Parsing error" - exit -1 - end + OpenNebulaHelper.rname_to_id(o, "IMAGE") } } @@ -188,13 +181,13 @@ class OneVMHelper < OpenNebulaHelper::OneHelper "USED CPU" => "CPU", "NET_TX" => "NET_TX", "NET_RX" => "NET_RX" - } + } - poll_attrs.each { |k,v| + poll_attrs.each { |k,v| if k == "USED CPU" - puts str % [k,vm[v]] + puts str % [k,vm[v]] elsif k == "USED MEMORY" - puts str % [k, OpenNebulaHelper.unit_to_str(vm[v].to_i, {})] + puts str % [k, OpenNebulaHelper.unit_to_str(vm[v].to_i, {})] else puts str % [k, OpenNebulaHelper.unit_to_str(vm[v].to_i/1024, {})] end From 9d07a15c9a378b6e4f848028d9cea4968bf0a171 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Fri, 31 Aug 2012 14:58:12 +0200 Subject: [PATCH 2/3] feature #1356: Add storage clone action --- src/cloud/occi/bin/occi-storage | 30 ++++++++++++++++++++++++++++-- src/cloud/occi/lib/OCCIClient.rb | 12 ++++++++++-- src/cloud/occi/lib/OCCIServer.rb | 25 +++++++++++++++++++++++++ src/cloud/occi/lib/occi-server.rb | 14 ++++++++++++++ 4 files changed, 77 insertions(+), 4 deletions(-) diff --git a/src/cloud/occi/bin/occi-storage b/src/cloud/occi/bin/occi-storage index ad70987f71..ac68b46ae1 100755 --- a/src/cloud/occi/bin/occi-storage +++ b/src/cloud/occi/bin/occi-storage @@ -51,10 +51,14 @@ Commands: * update updates the representation of the storage resource represented by the provided - + * delete deletes the storage resource idenfitied by +* clone + clones the storage resource idenfitied by in a new one + using the given + Options: @@ -205,6 +209,28 @@ case ARGV[0].downcase rc = occi_client.delete_image(image_id) + when 'clone' + image_id = ARGV[1] + name = ARGV[2] + + if !image_id + puts "#{cmd_name} clone: missing storage id parameter" + exit(-1) + end + + if !name + puts "#{cmd_name} clone: missing name parameter" + exit(-1) + end + + xml = "" + xml << " CLONE" + xml << " " + xml << " #{name}" + xml << " " + xml << "" + + rc = occi_client.action_image(image_id, xml) else puts "Command #{ARGV[0]} not valid." exit(-1) @@ -213,7 +239,7 @@ end if CloudClient::is_error?(rc) puts rc.to_s() exit(-1) -else +else str, code = print_xml(rc) puts str exit(code) diff --git a/src/cloud/occi/lib/OCCIClient.rb b/src/cloud/occi/lib/OCCIClient.rb index 9b1ffbf600..f1a6db9243 100755 --- a/src/cloud/occi/lib/OCCIClient.rb +++ b/src/cloud/occi/lib/OCCIClient.rb @@ -279,6 +279,10 @@ module OCCIClient delete('/storage/'+id.to_s) end + def action_image(id, xml) + post('/storage/'+id.to_s+'/action', xml, false) + end + private def get(path, verbose=false) @@ -289,11 +293,15 @@ module OCCIClient do_request(url, req) end - def post(path, xmlfile) + def post(path, xmlfile, file=true) url = URI.parse(@endpoint+path) req = Net::HTTP::Post.new(url.path) - req.body=File.read(xmlfile) + if file + req.body=File.read(xmlfile) + else + req.body=xmlfile + end do_request(url, req) end diff --git a/src/cloud/occi/lib/OCCIServer.rb b/src/cloud/occi/lib/OCCIServer.rb index 792e8839b7..e532f38233 100644 --- a/src/cloud/occi/lib/OCCIServer.rb +++ b/src/cloud/occi/lib/OCCIServer.rb @@ -481,6 +481,31 @@ class OCCIServer < CloudServer return to_occi_xml(image, :code=>200) end + # Clone a STORAGE resource + # request:: _Hash_ hash containing the data of the request + # [return] _String_,_Integer_ STORAGE occi representation or error, + # status code + def clone_storage(request, params, action_xml) + # --- Get the Image --- + image = ImageOCCI.new( + Image.build_xml(params[:id]), + @client) + + rc = image.clone(action_xml["PARAMS/NAME"]) + if OpenNebula.is_error?(rc) + return rc, CloudServer::HTTP_ERROR_CODE[rc.errno] + end + + new_image = ImageOCCI.new( + Image.build_xml(rc), + @client) + + new_image.info + + # --- Prepare XML Response --- + return to_occi_xml(new_image, :code=>201) + end + # Deletes a STORAGE resource (Not yet implemented) # request:: _Hash_ hash containing the data of the request # [return] _String_,_Integer_ Delete confirmation msg or error, diff --git a/src/cloud/occi/lib/occi-server.rb b/src/cloud/occi/lib/occi-server.rb index ab1c630867..ac84485f6a 100644 --- a/src/cloud/occi/lib/occi-server.rb +++ b/src/cloud/occi/lib/occi-server.rb @@ -345,6 +345,20 @@ get '/storage/:id' do treat_response(result,rc) end +post '/storage/:id/action' do + xml = XMLElement.new + xml.initialize_xml(request.body, "ACTION") + + result, rc = case xml['PERFORM'].downcase + when 'clone' then + @occi_server.clone_storage(request, params, xml) + else + halt 403, "Action #{xml['PERFORM']} not supported" + end + + treat_response(result,rc) +end + delete '/storage/:id' do result,rc = @occi_server.delete_storage(request, params) treat_response(result,rc) From 985b077c4b3bebe4d0c5234d54a4650d925307ea Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Fri, 31 Aug 2012 14:58:50 +0200 Subject: [PATCH 3/3] feature #1356: Add attachdisk and detachdisk actions to OCCI --- src/cloud/occi/bin/occi-compute | 59 ++++++++++++++++++++++----- src/cloud/occi/lib/OCCIClient.rb | 4 ++ src/cloud/occi/lib/OCCIServer.rb | 67 +++++++++++++++++++++++++++++++ src/cloud/occi/lib/occi-server.rb | 16 ++++++++ 4 files changed, 137 insertions(+), 9 deletions(-) diff --git a/src/cloud/occi/bin/occi-compute b/src/cloud/occi/bin/occi-compute index 722ee2a91c..0f77ee05b8 100755 --- a/src/cloud/occi/bin/occi-compute +++ b/src/cloud/occi/bin/occi-compute @@ -37,7 +37,7 @@ Usage: Commands: * create - creates a new compute resource described by the provided + creates a new compute resource described by the provided * list @@ -54,6 +54,11 @@ Commands: * delete deletes the compute resource idenfitied by +* attachdisk + attaches an storage to a given compute + +* detachdisk + detaches the target disk form the compute Options: @@ -133,7 +138,7 @@ begin end rescue Exception => e exit(-1) -end +end begin occi_client = OCCIClient::Client.new(url,username,password, timeout, debug, plain) @@ -146,7 +151,7 @@ if !ARGV[0] puts "#{cmd_name}: [COMMAND] not present" puts "#{cmd_name}: Execute #{cmd_name} -h for help." exit(-1) -end +end case ARGV[0].downcase when 'list' @@ -165,13 +170,13 @@ case ARGV[0].downcase when 'show' vm_id = ARGV[1] - if !vm_id + if !vm_id puts "#{cmd_name} show: missing VM-ID parameter" exit(-1) end rc = occi_client.get_vm(vm_id) - + when 'update' vm_xml = ARGV[1] @@ -181,17 +186,53 @@ case ARGV[0].downcase end rc = occi_client.put_vm(vm_xml) - + when 'delete' vm_id = ARGV[1] - if !vm_id + if !vm_id puts "#{cmd_name} delete: missing VM-ID parameter" exit -1 end rc = occi_client.delete_vm(vm_id) - + + when 'attachdisk' + vm_id = ARGV[1] + image_id = ARGV[2] + + if !image_id + puts "#{cmd_name} show: missing storage id parameter" + exit(-1) + end + + xml = "" + xml << " ATTACHDISK" + xml << " " + xml << " " + xml << " " + xml << "" + + rc = occi_client.action_vm(vm_id, xml) + + when 'detachdisk' + vm_id = ARGV[1] + disk_id = ARGV[2] + + if !disk_id + puts "#{cmd_name} show: missing disk id parameter" + exit(-1) + end + + xml = "" + xml << " DETACHDISK" + xml << " " + xml << " " + xml << " " + xml << "" + + rc = occi_client.action_vm(vm_id, xml) + else puts "Command #{ARGV[0]} not valid." exit(-1) @@ -200,7 +241,7 @@ end if CloudClient::is_error?(rc) puts rc.to_s() exit(-1) -else +else str, code = print_xml(rc) puts str exit(code) diff --git a/src/cloud/occi/lib/OCCIClient.rb b/src/cloud/occi/lib/OCCIClient.rb index f1a6db9243..5d5eb2a6a9 100755 --- a/src/cloud/occi/lib/OCCIClient.rb +++ b/src/cloud/occi/lib/OCCIClient.rb @@ -233,6 +233,10 @@ module OCCIClient delete('/compute/'+id.to_s) end + def action_vm(id, xml) + post('/compute/'+id.to_s+'/action', xml, false) + end + ###################################################################### # Retrieves a Virtual Network # :id Virtual Network identifier diff --git a/src/cloud/occi/lib/OCCIServer.rb b/src/cloud/occi/lib/OCCIServer.rb index e532f38233..c6f5ca8e1f 100644 --- a/src/cloud/occi/lib/OCCIServer.rb +++ b/src/cloud/occi/lib/OCCIServer.rb @@ -298,6 +298,73 @@ class OCCIServer < CloudServer return "", 204 end + # Attach a disk to an existing COMPUTE + # @param [Hash] request hash containing the data of the request + # @param [Hash] params hash containing the params of the request + # @param [OpenNebula::XMLElement] action_xml contains the body of the request + # @return [[String, OpenNebula:Error], Integer] + def attach_disk(request, params, action_xml) + # --- Get the VM --- + vm = VirtualMachineOCCI.new( + VirtualMachine.build_xml(params[:id]), + @client) + + image_href = action_xml.attr('PARAMS/STORAGE','href') + if !image_href + error = OpenNebula::Error.new("You have to specify an STORAGE " << + "to be attached") + return error, CloudServer::HTTP_ERROR_CODE[error.errno] + end + + image_id = image_href.split('/').last + target = action_xml['PARAMS/TARGET'] + + if target + template = "DISK = [ IMAGE_ID = #{image_id}, TARGET = #{target} ]" + else + template = "DISK = [ IMAGE_ID = #{image_id}, DEV_PREFIX = sd ]" + end + + # --- Attach DISK --- + result = vm.attachdisk(template) + if OpenNebula.is_error?(result) + return result, CloudServer::HTTP_ERROR_CODE[result.errno] + end + + vm.info + + return to_occi_xml(vm, :code=>202) + end + + # Detach a DISK from an existing COMOUTE + # @param [Hash] request hash containing the data of the request + # @param [Hash] params hash containing the params of the request + # @param [OpenNebula::XMLElement] action_xml contains the body of the request + # @return [[String, OpenNebula:Error], Integer] + def detach_disk(request, params, action_xml) + # --- Get the VM --- + vm = VirtualMachineOCCI.new( + VirtualMachine.build_xml(params[:id]), + @client) + + disk_id = action_xml.attr('PARAMS/DISK','id') + if !disk_id + error = OpenNebula::Error.new("You have to specify a DISK " << + "to be detached") + return error, CloudServer::HTTP_ERROR_CODE[error.errno] + end + + # --- Detach DISK --- + result = vm.detachdisk(disk_id.to_i) + if OpenNebula.is_error?(result) + return result, CloudServer::HTTP_ERROR_CODE[result.errno] + end + + vm.info + + return to_occi_xml(vm, :code=>202) + end + # Updates a COMPUTE resource # request:: _Hash_ hash containing the data of the request # [return] _String_,_Integer_ Update confirmation msg or error, diff --git a/src/cloud/occi/lib/occi-server.rb b/src/cloud/occi/lib/occi-server.rb index ac84485f6a..2137378d92 100644 --- a/src/cloud/occi/lib/occi-server.rb +++ b/src/cloud/occi/lib/occi-server.rb @@ -315,6 +315,22 @@ get '/compute/:id' do treat_response(result,rc) end +post '/compute/:id/action' do + xml = XMLElement.new + xml.initialize_xml(request.body, "ACTION") + + result,rc = case xml['PERFORM'].downcase + when 'attachdisk' then + @occi_server.attach_disk(request, params, xml) + when 'detachdisk' then + @occi_server.detach_disk(request, params, xml) + else + halt 403, "Action #{xml['PERFORM']} not supported" + end + + treat_response(result,rc) +end + delete '/compute/:id' do result,rc = @occi_server.delete_compute(request, params) treat_response(result,rc)