From c71ba4a27e4e95002caf537515fd20a264a3ffab 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) --- 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 6bc7332d4842d2f1bbbd16eecff0b33b71c73e7e Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Tue, 28 Aug 2012 17:49:10 +0200 Subject: [PATCH 2/3] feature #1190: Use the new DSL in econe commands --- src/cli/command_parser.rb | 16 +- src/cloud/ec2/bin/econe-allocate-address | 119 ++++------- src/cloud/ec2/bin/econe-associate-address | 132 ++++-------- src/cloud/ec2/bin/econe-describe-addresses | 135 ++++--------- src/cloud/ec2/bin/econe-describe-images | 155 +++++--------- src/cloud/ec2/bin/econe-describe-instances | 149 +++++--------- src/cloud/ec2/bin/econe-disassociate-address | 121 +++-------- src/cloud/ec2/bin/econe-register | 123 +++-------- src/cloud/ec2/bin/econe-release-address | 127 ++++-------- src/cloud/ec2/bin/econe-run-instances | 202 ++++++++----------- src/cloud/ec2/bin/econe-terminate-instances | 129 ++++-------- src/cloud/ec2/bin/econe-upload | 136 +++++-------- src/cloud/ec2/lib/EC2QueryClient.rb | 41 +++- 13 files changed, 537 insertions(+), 1048 deletions(-) diff --git a/src/cli/command_parser.rb b/src/cli/command_parser.rb index 75d297f3f1..81efd93956 100644 --- a/src/cli/command_parser.rb +++ b/src/cli/command_parser.rb @@ -364,9 +364,7 @@ module CommandParser cmd_format5 = "#{' '*3}%s" cmd_format10 = "#{' '*8}%s" - if @main - print_command(@main) - else + if !@main puts "## COMMANDS" @commands.each{ |key,value| @@ -388,12 +386,14 @@ module CommandParser printf "#{args_str}" puts - command[:desc].split("\n").each { |l| - printf cmd_format10, l - puts - } + if command[:desc] + command[:desc].split("\n").each { |l| + printf cmd_format10, l + puts + } + end - unless command[:options].empty? + if command[:options] && !command[:options].empty? opts_str=command[:options].flatten.collect{|o| o[:name] }.join(', ') diff --git a/src/cloud/ec2/bin/econe-allocate-address b/src/cloud/ec2/bin/econe-allocate-address index a42ac55a53..8dfa3d5076 100755 --- a/src/cloud/ec2/bin/econe-allocate-address +++ b/src/cloud/ec2/bin/econe-allocate-address @@ -23,103 +23,54 @@ else RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" end - $: << RUBY_LIB_LOCATION $: << RUBY_LIB_LOCATION+"/cloud" -COMMANDS_HELP=<<-EOT -econe-describe-images - -Allocate a new elastic IP address for the user - -Usage: - econe-allocate-address [OPTIONS] - -Options: - --help, -h - Show help - - --access-key , -K - The username of the user - - --secret-key , -S - The password of the user - - --url , -U - Set url as the web service url to use - - --headers, -H - Display column headers - -EOT +require 'cli/command_parser' +require 'cli/cli_helper' require 'econe/EC2QueryClient' -require 'CloudClient' -require 'getoptlong' include CloudCLI +CommandParser::CmdParser.new(ARGV) do + usage "econe-allocate-address [OPTIONS]" + description "Allocate a new elastic IP address for the user" + version CloudCLI.version_text -opts = GetoptLong.new( - ['--help', '-h',GetoptLong::NO_ARGUMENT], - ['--version', '-v',GetoptLong::NO_ARGUMENT], - ['--access-key', '-K',GetoptLong::REQUIRED_ARGUMENT], - ['--secret-key', '-S',GetoptLong::REQUIRED_ARGUMENT], - ['--url', '-U',GetoptLong::REQUIRED_ARGUMENT], - ['--headers', '-H',GetoptLong::NO_ARGUMENT] - ) + option [ + CommandParser::VERBOSE, + CommandParser::HELP, + EC2QueryClient::ACCESS_KEY, + EC2QueryClient::SECRET_KEY, + EC2QueryClient::URL + ] -headers = false -url = nil -access = nil -secret = nil -auth = nil + main do + begin + ec2_client = EC2QueryClient::Client.new( + options[:access_key], + options[:secret_key], + options[:url]) + rescue Exception => e + puts "#{cmd_name}: #{e.message}" + exit -1 + end -begin - opts.each do |opt, arg| - case opt - when '--help' - puts COMMANDS_HELP - return - when '--version' - puts CloudCLI.version_text - exit 0 - when '--access-key' - access = arg - when '--secret-key' - secret = arg - when '--url' - url = arg - when '--headers' - headers = true + addr = ec2_client.allocate_address + + if CloudClient::is_error?(addr) + [-1, "#{cmd_name}: #{addr.message}"] + else + exit_code 0 + + if options[:headers] + CLIHelper.print_header("publicIP") + end + + puts addr['publicIP'] end end -rescue Exception => e - exit -1 -end - -auth = "#{access}:#{secret}" if secret && access - -begin - ec2_client = EC2QueryClient::Client.new(auth,url) -rescue Exception => e - puts "#{cmd_name}: #{e.message}" - exit -1 end -addr = ec2_client.allocate_address - -if CloudClient::is_error?(addr) - puts "#{cmd_name}: #{addr.message}" - exit -1 -end - -if headers - puts "publicIP" - puts "------------------------------------------------------------------------------" -end - -puts addr['publicIP'] - -exit 0 diff --git a/src/cloud/ec2/bin/econe-associate-address b/src/cloud/ec2/bin/econe-associate-address index 14bbcb3271..7abb873578 100755 --- a/src/cloud/ec2/bin/econe-associate-address +++ b/src/cloud/ec2/bin/econe-associate-address @@ -23,113 +23,49 @@ else RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" end - $: << RUBY_LIB_LOCATION $: << RUBY_LIB_LOCATION+"/cloud" -COMMANDS_HELP=<<-EOT -econe-associate-address - -Associate a publicIP of the user with a given instance - -Usage: - econe-associate-address [OPTIONS] PUBLIC-IP INSTANCE-ID - -Options: - - --help, -h - Show help - - --access-key , -K - The username of the user - - --secret-key , -S - The password of the user - - --url , -U - Set url as the web service url to use - - --headers, -H - Display column headers - -PUBLIC-IP: ElasticIP owned by the user. To see the list of ips use econe-describe-addresses -INSTANCE-ID: Id of the instance to be associated with the ElasticIP - -EOT +require 'cli/command_parser' +require 'cli/cli_helper' require 'econe/EC2QueryClient' -require 'CloudClient' -require 'getoptlong' include CloudCLI -opts = GetoptLong.new( - ['--help', '-h',GetoptLong::NO_ARGUMENT], - ['--version', '-v',GetoptLong::NO_ARGUMENT], - ['--access-key', '-K',GetoptLong::REQUIRED_ARGUMENT], - ['--secret-key', '-S',GetoptLong::REQUIRED_ARGUMENT], - ['--url', '-U',GetoptLong::REQUIRED_ARGUMENT], - ['--type', '-t',GetoptLong::REQUIRED_ARGUMENT], - ['--user-data', '-d',GetoptLong::REQUIRED_ARGUMENT], - ['--headers', '-H',GetoptLong::NO_ARGUMENT] - ) +CommandParser::CmdParser.new(ARGV) do + usage "econe-associate-address [OPTIONS] PUBLIC-IP INSTANCE-ID" + version CloudCLI.version_text + description <<-EOT +Associate a publicIP of the user with a given instance +PUBLIC-IP: ElasticIP owned by the user. To see the list of ips use econe-describe-addresses +INSTANCE-ID: Id of the instance to be associated with the ElasticIP +EOT -headers = false -url = nil -access = nil -secret = nil -auth = nil + option [ + CommandParser::VERBOSE, + CommandParser::HELP, + EC2QueryClient::ACCESS_KEY, + EC2QueryClient::SECRET_KEY, + EC2QueryClient::URL + ] -begin - opts.each do |opt, arg| - case opt - when '--help' - puts COMMANDS_HELP - return - when '--version' - puts CloudCLI.version_text - exit 0 - when '--access-key' - access = arg - when '--secret-key' - secret = arg - when '--url' - url = arg - when '--headers' - headers = true + main :public_ip, :instance_id do + begin + ec2_client = EC2QueryClient::Client.new( + options[:access_key], + options[:secret_key], + options[:url]) + rescue Exception => e + exit_with_code -1, "#{cmd_name}: #{e.message}" + end + + rc = ec2_client.associate_address(args[0], args[1]) + + if CloudClient::is_error?(rc) + exit_with_code -1, "#{cmd_name}: #{rc.message}" + else + exit_with_code 0 end end -rescue Exception => e - exit -1 -end - -public_ip = ARGV.shift -instance_id = ARGV.shift - -if !public_ip - puts "#{cmd_name}: missing publicIP parameter" - exit -1 -end - -if !instance_id - puts "#{cmd_name}: missing instanceID parameter" - exit -1 -end - -auth = "#{access}:#{secret}" if secret && access - -begin - ec2_client = EC2QueryClient::Client.new(auth,url) -rescue Exception => e - puts "#{cmd_name}: #{e.message}" - exit -1 -end - -rc = ec2_client.associate_address(public_ip, instance_id) - -if CloudClient::is_error?(rc) - puts "#{cmd_name}: #{rc.message}" - exit -1 -end - -exit 0 +end \ No newline at end of file diff --git a/src/cloud/ec2/bin/econe-describe-addresses b/src/cloud/ec2/bin/econe-describe-addresses index c74e0e584e..ca4d1b0dea 100755 --- a/src/cloud/ec2/bin/econe-describe-addresses +++ b/src/cloud/ec2/bin/econe-describe-addresses @@ -23,111 +23,60 @@ else RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" end - $: << RUBY_LIB_LOCATION $: << RUBY_LIB_LOCATION+"/cloud" -COMMANDS_HELP=<<-EOT -econe-describe-images - -List elastic IP addresses - -Usage: - econe-describe-addresses [OPTIONS] - -Options: - --help, -h - Show help - - --access-key , -K - The username of the user - - --secret-key , -S - The password of the user - - --url , -U - Set url as the web service url to use - - --headers, -H - Display column headers - -EOT +require 'cli/command_parser' +require 'cli/cli_helper' require 'econe/EC2QueryClient' -require 'CloudClient' -require 'getoptlong' include CloudCLI +TABLE = CLIHelper::ShowTable.new(nil, self) do + column :publicIP, "publicIP", :size=>10 do |d| + d["publicIP"] + end -opts = GetoptLong.new( - ['--help', '-h',GetoptLong::NO_ARGUMENT], - ['--version', '-v',GetoptLong::NO_ARGUMENT], - ['--access-key', '-K',GetoptLong::REQUIRED_ARGUMENT], - ['--secret-key', '-S',GetoptLong::REQUIRED_ARGUMENT], - ['--url', '-U',GetoptLong::REQUIRED_ARGUMENT], - ['--headers', '-H',GetoptLong::NO_ARGUMENT] - ) + column :instanceID, "instanceID", :size=>25 do |d| + d["instanceID"] + end -headers = false -url = nil -access = nil -secret = nil -auth = nil + default :publicIP, :instanceID +end -begin - opts.each do |opt, arg| - case opt - when '--help' - puts COMMANDS_HELP - return - when '--version' - puts CloudCLI.version_text - exit 0 - when '--access-key' - access = arg - when '--secret-key' - secret = arg - when '--url' - url = arg - when '--headers' - headers = true + +CommandParser::CmdParser.new(ARGV) do + usage "econe-describe-addresses [OPTIONS]" + version CloudCLI.version_text + description "List elastic IP addresses" + + option [ + CommandParser::VERBOSE, + CommandParser::HELP, + EC2QueryClient::ACCESS_KEY, + EC2QueryClient::SECRET_KEY, + EC2QueryClient::URL + ] + + main do + begin + ec2_client = EC2QueryClient::Client.new( + options[:access_key], + options[:secret_key], + options[:url]) + rescue Exception => e + exit_with_code -1, "#{cmd_name}: #{e.message}" + end + + rc = ec2_client.describe_addresses + + if CloudClient::is_error?(rc) + exit_with_code -1, "#{cmd_name}: #{rc.message}" + else + TABLE.show(rc['addressesSet']['item'] || []) + exit_with_code 0 end end -rescue Exception => e - exit -1 -end - -auth = "#{access}:#{secret}" if secret && access - -begin - ec2_client = EC2QueryClient::Client.new(auth,url) -rescue Exception => e - puts "#{cmd_name}: #{e.message}" - exit -1 end -rc = ec2_client.describe_addresses - -if CloudClient::is_error?(rc) - puts "#{cmd_name}: #{rc.message}" - exit -1 -end - -addresses = rc['addressesSet']['item'] - -fmt = "%-12s %s" - -if headers - puts fmt % ["publicIP", "instanceId"] - puts "------------------------------------------------------------------------------" -end - -if addresses - addresses.each { |addr| - puts fmt % [addr['publicIP'],addr['instanceID']] - } -end - -exit 0 - diff --git a/src/cloud/ec2/bin/econe-describe-images b/src/cloud/ec2/bin/econe-describe-images index 4dfbff2789..e523bbb86a 100755 --- a/src/cloud/ec2/bin/econe-describe-images +++ b/src/cloud/ec2/bin/econe-describe-images @@ -23,117 +23,74 @@ else RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" end - $: << RUBY_LIB_LOCATION $: << RUBY_LIB_LOCATION+"/cloud" -COMMANDS_HELP=<<-EOT -econe-describe-images - -List and describe previously uploaded images of a user to be -used with an OpenNebula Cloud. - -Usage: - econe-describe-images [OPTIONS] - -Options: - --help, -h - Show help - - --access-key , -K - The username of the user - - --secret-key , -S - The password of the user - - --url , -U - Set url as the web service url to use - - --headers, -H - Display column headers - -EOT +require 'cli/command_parser' +require 'cli/cli_helper' require 'econe/EC2QueryClient' -require 'CloudClient' -require 'getoptlong' include CloudCLI -opts = GetoptLong.new( - ['--help', '-h',GetoptLong::NO_ARGUMENT], - ['--version', '-v',GetoptLong::NO_ARGUMENT], - ['--access-key', '-K',GetoptLong::REQUIRED_ARGUMENT], - ['--secret-key', '-S',GetoptLong::REQUIRED_ARGUMENT], - ['--url', '-U',GetoptLong::REQUIRED_ARGUMENT], - ['--headers', '-H',GetoptLong::NO_ARGUMENT] - ) +TABLE = CLIHelper::ShowTable.new(nil, self) do + column :Owner, "Owner", :size=>12 do |d| + d["imageOwnerId"] + end -headers = false -url = nil -access = nil -secret = nil -auth = nil + column :ImageId, "ImageId", :size=>13 do |d| + d["imageId"] + end -begin - opts.each do |opt, arg| - case opt - when '--help' - puts COMMANDS_HELP - return - when '--version' - puts CloudCLI.version_text - exit 0 - when '--access-key' - access = arg - when '--secret-key' - secret = arg - when '--url' - url = arg - when '--headers' - headers = true + column :Status, "Status", :size=>14 do |d| + d["imageState"] + end + + column :Visibility, "Visibility", :size=>12 do |d| + d['isPublic'] == 'true' ? "public" : "private" + end + + column :Location, "Location", :size=>20 do |d| + d["imageLocation"] + end + + default :Owner, :ImageId, :Status, :Visibility, :Location +end + + +CommandParser::CmdParser.new(ARGV) do + usage "econe-describe-images [OPTIONS]" + version CloudCLI.version_text + description "List and describe previously uploaded images of a user to be +used with an OpenNebula Cloud." + + option [ + CommandParser::VERBOSE, + CommandParser::HELP, + EC2QueryClient::ACCESS_KEY, + EC2QueryClient::SECRET_KEY, + EC2QueryClient::URL + ] + + main do + begin + ec2_client = EC2QueryClient::Client.new( + options[:access_key], + options[:secret_key], + options[:url]) + rescue Exception => e + exit_with_code -1, "#{cmd_name}: #{e.message}" + end + + rc = ec2_client.describe_images + + if CloudClient::is_error?(rc) + exit_with_code -1, "#{cmd_name}: #{rc.message}" + else + TABLE.show(rc['imagesSet']['item'] || []) + exit_with_code 0 end end -rescue Exception => e - exit -1 -end - -auth = "#{access}:#{secret}" if secret && access - -begin - ec2_client = EC2QueryClient::Client.new(auth,url) -rescue Exception => e - puts "#{cmd_name}: #{e.message}" - exit -1 end -rc = ec2_client.describe_images() - -if CloudClient::is_error?(rc) - puts "#{cmd_name}: #{rc.message}" - exit -1 -end - -images = rc['imagesSet']['item'] - -fmt = "%-12s %-13s %-14s %-12s %s" - -if headers - puts fmt % ["Owner", "ImageId", "Status", "Visibility", "Location"] - puts "------------------------------------------------------------------------------" -end - -if images - images.each { |img| - if img['isPublic'] == 'true' - visibility = "public" - elsif img['isPublic'] == 'false' - visibility = "private" - end - puts fmt % [img['imageOwnerId'],img['imageId'], img['imageState'], visibility,img['imageLocation']] - } -end - -exit 0 - diff --git a/src/cloud/ec2/bin/econe-describe-instances b/src/cloud/ec2/bin/econe-describe-instances index eeae5dfce2..43a6b146f6 100755 --- a/src/cloud/ec2/bin/econe-describe-instances +++ b/src/cloud/ec2/bin/econe-describe-instances @@ -14,7 +14,6 @@ # See the License for the specific language governing permissions and # # limitations under the License. # #--------------------------------------------------------------------------- # - ONE_LOCATION=ENV["ONE_LOCATION"] if !ONE_LOCATION @@ -26,106 +25,68 @@ end $: << RUBY_LIB_LOCATION $: << RUBY_LIB_LOCATION+"/cloud" -COMMANDS_HELP=<<-EOT -econe-describe-instances - -List and describe running instances - -Usage: - econe-describe-instances [OPTIONS] - -Options: - - --help, -h - Show help - - --access-key , -K - The username of the user - - --secret-key , -S - The password of the user - - --url , -U - Set url as the web service url to use - - --headers, -H - Display column headers - -EOT +require 'cli/command_parser' +require 'cli/cli_helper' require 'econe/EC2QueryClient' -require 'CloudClient' -require 'getoptlong' include CloudCLI -opts = GetoptLong.new( - ['--help', '-h',GetoptLong::NO_ARGUMENT], - ['--version', '-v',GetoptLong::NO_ARGUMENT], - ['--access-key', '-K',GetoptLong::REQUIRED_ARGUMENT], - ['--secret-key', '-S',GetoptLong::REQUIRED_ARGUMENT], - ['--url', '-U',GetoptLong::REQUIRED_ARGUMENT], - ['--headers', '-H',GetoptLong::NO_ARGUMENT] - ) +TABLE = CLIHelper::ShowTable.new(nil, self) do + column :instanceId, "instanceId", :size=>12 do |d| + d["instanceId"] + end -headers = false -url = nil -access = nil -secret = nil -auth = nil + column :ImageId, "ImageId", :size=>13 do |d| + d["imageId"] + end -begin - opts.each do |opt, arg| - case opt - when '--help' - puts COMMANDS_HELP - return - when '--version' - puts CloudCLI.version_text - exit 0 - when '--access-key' - access = arg - when '--secret-key' - secret = arg - when '--url' - url = arg - when '--headers' - headers = true + column :State, "State", :size=>14 do |d| + d["instanceState"]['name'] + end + + column :IP, "IP", :size=>12 do |d| + d['dnsName'] + end + + column :instanceType, "instanceType", :size=>20 do |d| + d["instanceType"] + end + + default :instanceId, :ImageId, :State, :IP, :instanceType +end + + +CommandParser::CmdParser.new(ARGV) do + usage "econe-describe-instances [OPTIONS]" + version CloudCLI.version_text + description "List and describe running instances" + + option [ + CommandParser::VERBOSE, + CommandParser::HELP, + EC2QueryClient::ACCESS_KEY, + EC2QueryClient::SECRET_KEY, + EC2QueryClient::URL + ] + + main do + begin + ec2_client = EC2QueryClient::Client.new( + options[:access_key], + options[:secret_key], + options[:url]) + rescue Exception => e + exit_with_code -1, "#{cmd_name}: #{e.message}" + end + + rc = ec2_client.describe_instances + + if CloudClient::is_error?(rc) + exit_with_code -1, "#{cmd_name}: #{rc.message}" + else + TABLE.show(rc['reservationSet']['item'][0]['instancesSet']['item'] || []) + exit_with_code 0 end end -rescue Exception => e - exit -1 end - -auth = "#{access}:#{secret}" if secret && access - -begin - ec2_client = EC2QueryClient::Client.new(auth,url) -rescue Exception => e - puts "#{cmd_name}: #{e.message}" - exit -1 -end - -rc = ec2_client.describe_instances() - -if CloudClient::is_error?(rc) - puts "#{cmd_name}: #{rc.message}" - exit -1 -end - -instances = rc['reservationSet']['item'][0]['instancesSet']['item'] -owner = rc['reservationSet']['item'][0]['ownerId'] - -fmt = "%-10s %-11s %-13s %-11s %-15s %-10s" - -if headers - puts fmt % ["Owner", "Id", "ImageId", "State", "IP", "Type"] - puts "-----------------------------------------------------------------------------------" -end - -if instances - instances.each { |img| - puts fmt % [owner, img['instanceId'],img['imageId'],img['instanceState']['name'],img['dnsName'],img['instanceType']] - } -end -exit 0 diff --git a/src/cloud/ec2/bin/econe-disassociate-address b/src/cloud/ec2/bin/econe-disassociate-address index 1bd0c66965..0e055b8c44 100755 --- a/src/cloud/ec2/bin/econe-disassociate-address +++ b/src/cloud/ec2/bin/econe-disassociate-address @@ -23,106 +23,49 @@ else RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" end - $: << RUBY_LIB_LOCATION $: << RUBY_LIB_LOCATION+"/cloud" -COMMANDS_HELP=<<-EOT -econe-disasociate-address - -Disasociate a publicIP of the user currently associated with an instance - -Usage: - econe-disasociate-address [OPTIONS] PUBLIC-IP - -Options: - - --help, -h - Show help - - --access-key , -K - The username of the user - - --secret-key , -S - The password of the user - - --url , -U - Set url as the web service url to use - - --headers, -H - Display column headers - -PUBLIC-IP: ElasticIP owned by the user. To see the list of ips use econe-describe-addresses - -EOT +require 'cli/command_parser' +require 'cli/cli_helper' require 'econe/EC2QueryClient' -require 'CloudClient' -require 'getoptlong' include CloudCLI -opts = GetoptLong.new( - ['--help', '-h',GetoptLong::NO_ARGUMENT], - ['--version', '-v',GetoptLong::NO_ARGUMENT], - ['--access-key', '-K',GetoptLong::REQUIRED_ARGUMENT], - ['--secret-key', '-S',GetoptLong::REQUIRED_ARGUMENT], - ['--url', '-U',GetoptLong::REQUIRED_ARGUMENT], - ['--type', '-t',GetoptLong::REQUIRED_ARGUMENT], - ['--user-data', '-d',GetoptLong::REQUIRED_ARGUMENT], - ['--headers', '-H',GetoptLong::NO_ARGUMENT] - ) +CommandParser::CmdParser.new(ARGV) do + usage "econe-disasociate-address [OPTIONS] PUBLIC-IP" + version CloudCLI.version_text + description "<<-EOT +Disasociate a publicIP of the user currently associated with an instance +PUBLIC-IP: ElasticIP owned by the user. To see the list of ips use econe-describe-addresses +EOT" -headers = false -url = nil -access = nil -secret = nil -auth = nil + option [ + CommandParser::VERBOSE, + CommandParser::HELP, + EC2QueryClient::ACCESS_KEY, + EC2QueryClient::SECRET_KEY, + EC2QueryClient::URL + ] -begin - opts.each do |opt, arg| - case opt - when '--help' - puts COMMANDS_HELP - return - when '--version' - puts CloudCLI.version_text - exit 0 - when '--access-key' - access = arg - when '--secret-key' - secret = arg - when '--url' - url = arg - when '--headers' - headers = true + main :public_ip do + begin + ec2_client = EC2QueryClient::Client.new( + options[:access_key], + options[:secret_key], + options[:url]) + rescue Exception => e + exit_with_code -1, "#{cmd_name}: #{e.message}" + end + + rc = ec2_client.disassociate_address(args[0]) + + if CloudClient::is_error?(rc) + exit_with_code -1, "#{cmd_name}: #{rc.message}" + else + exit_with_code 0 end end -rescue Exception => e - exit -1 -end - -public_ip = ARGV.shift - -if !public_ip - puts "#{cmd_name}: missing publicIP parameter" - exit -1 end -auth = "#{access}:#{secret}" if secret && access - -begin - ec2_client = EC2QueryClient::Client.new(auth,url) -rescue Exception => e - puts "#{cmd_name}: #{e.message}" - exit -1 -end - -rc = ec2_client.disassociate_address(public_ip) - -if CloudClient::is_error?(rc) - puts "#{cmd_name}: #{rc.message}" - exit -1 -end - -exit 0 diff --git a/src/cloud/ec2/bin/econe-register b/src/cloud/ec2/bin/econe-register index 5cb4b2dc3c..74cc5523ea 100755 --- a/src/cloud/ec2/bin/econe-register +++ b/src/cloud/ec2/bin/econe-register @@ -26,104 +26,45 @@ end $: << RUBY_LIB_LOCATION $: << RUBY_LIB_LOCATION+"/cloud" -COMMANDS_HELP=<<-EOT -econe-register - -Register a previously uploaded image for use with an -OpenNebula Cloud. - -Usage: - econe-register [OPTIONS] IMAGE-ID - -Options: - - --help, -h - Show help - - --access-key , -K - The username of the user - - --secret-key , -S - The password of the user - - --url , -U - Set url as the web service url to use - - --headers, -H - Display column headers - -IMAGE-ID: The image identification as returned by -the econe-upload command - -EOT +require 'cli/command_parser' +require 'cli/cli_helper' require 'econe/EC2QueryClient' -require 'CloudClient' -require 'getoptlong' include CloudCLI -opts = GetoptLong.new( - ['--help', '-h',GetoptLong::NO_ARGUMENT], - ['--version', '-v',GetoptLong::NO_ARGUMENT], - ['--access-key', '-K',GetoptLong::REQUIRED_ARGUMENT], - ['--secret-key', '-S',GetoptLong::REQUIRED_ARGUMENT], - ['--url', '-U',GetoptLong::REQUIRED_ARGUMENT], - ['--headers', '-H',GetoptLong::NO_ARGUMENT] - ) +CommandParser::CmdParser.new(ARGV) do + usage "econe-register [OPTIONS] IMAGE-ID" + version CloudCLI.version_text + description "<<-EOT +Register a previously uploaded image for use with an OpenNebula Cloud. +IMAGE-ID: The image identification as returned by the econe-upload command +EOT" -headers = false -url = nil -access = nil -secret = nil -auth = nil + option [ + CommandParser::VERBOSE, + CommandParser::HELP, + EC2QueryClient::ACCESS_KEY, + EC2QueryClient::SECRET_KEY, + EC2QueryClient::URL + ] -begin - opts.each do |opt, arg| - case opt - when '--help' - puts COMMANDS_HELP - return - when '--version' - puts CloudCLI.version_text - exit 0 - when '--access-key' - access = arg - when '--secret-key' - secret = arg - when '--url' - url = arg - when '--headers' - headers = true + main :public_ip, :instance_id do + begin + ec2_client = EC2QueryClient::Client.new( + options[:access_key], + options[:secret_key], + options[:url]) + rescue Exception => e + exit_with_code -1, "#{cmd_name}: #{e.message}" + end + + rc = ec2_client.register_image(args[0]) + + if CloudClient::is_error?(rc) + exit_with_code -1, "#{cmd_name}: #{rc.message}" + else + exit_with_code 0, "Success: ImageId #{rc['imageId']}" end end -rescue Exception => e - exit -1 -end - -image_id = ARGV.shift - -if !image_id - puts "#{cmd_name}: missing ImageId parameter" - exit -1 end - -auth = "#{access}:#{secret}" if secret && access - -begin - ec2_client = EC2QueryClient::Client.new(auth,url) -rescue Exception => e - puts "#{cmd_name}: #{e.message}" - exit -1 -end - -rc = ec2_client.register_image(image_id) - -if CloudClient::is_error?(rc) - puts "#{cmd_name}: #{rc.message}" - exit -1 -end - -puts "Success: ImageId #{rc['imageId']}" - -exit 0 diff --git a/src/cloud/ec2/bin/econe-release-address b/src/cloud/ec2/bin/econe-release-address index c344db0135..f03fb8d689 100755 --- a/src/cloud/ec2/bin/econe-release-address +++ b/src/cloud/ec2/bin/econe-release-address @@ -23,106 +23,57 @@ else RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" end - $: << RUBY_LIB_LOCATION $: << RUBY_LIB_LOCATION+"/cloud" -COMMANDS_HELP=<<-EOT -econe-release-address - -Release a publicIP of the user - -Usage: - econe-release-address [OPTIONS] PUBLIC-IP - -Options: - - --help, -h - Show help - - --access-key , -K - The username of the user - - --secret-key , -S - The password of the user - - --url , -U - Set url as the web service url to use - - --headers, -H - Display column headers - -PUBLIC-IP: ElasticIP owned by the user. To see the list of ips use econe-describe-addresses - -EOT +require 'cli/command_parser' +require 'cli/cli_helper' require 'econe/EC2QueryClient' -require 'CloudClient' -require 'getoptlong' include CloudCLI -opts = GetoptLong.new( - ['--help', '-h',GetoptLong::NO_ARGUMENT], - ['--version', '-v',GetoptLong::NO_ARGUMENT], - ['--access-key', '-K',GetoptLong::REQUIRED_ARGUMENT], - ['--secret-key', '-S',GetoptLong::REQUIRED_ARGUMENT], - ['--url', '-U',GetoptLong::REQUIRED_ARGUMENT], - ['--type', '-t',GetoptLong::REQUIRED_ARGUMENT], - ['--user-data', '-d',GetoptLong::REQUIRED_ARGUMENT], - ['--headers', '-H',GetoptLong::NO_ARGUMENT] - ) +CommandParser::CmdParser.new(ARGV) do + usage "econe-release-address [OPTIONS] PUBLIC-IP" + version CloudCLI.version_text + description <<-EOT +Release a publicIP of the user" +PUBLIC-IP: ElasticIP owned by the user. To see the list of ips use econe-describe-addresses +EOT -headers = false -url = nil -access = nil -secret = nil -auth = nil + option [ + CommandParser::VERBOSE, + CommandParser::HELP, + EC2QueryClient::ACCESS_KEY, + EC2QueryClient::SECRET_KEY, + EC2QueryClient::URL + ] -begin - opts.each do |opt, arg| - case opt - when '--help' - puts COMMANDS_HELP - return - when '--version' - puts CloudCLI.version_text - exit 0 - when '--access-key' - access = arg - when '--secret-key' - secret = arg - when '--url' - url = arg - when '--headers' - headers = true + main :public_ip do + begin + ec2_client = EC2QueryClient::Client.new( + options[:access_key], + options[:secret_key], + options[:url]) + rescue Exception => e + puts "#{cmd_name}: #{e.message}" + exit -1 + end + + rc = ec2_client.release_address(args[0]) + + if CloudClient::is_error?(rc) + [-1, "#{cmd_name}: #{rc.message}"] + else + exit_code 0 + + if options[:headers] + CLIHelper.print_header("publicIP") + end + + puts rc['publicIP'] end end -rescue Exception => e - exit -1 -end - -public_ip = ARGV.shift - -if !public_ip - puts "#{cmd_name}: missing publicIP parameter" - exit -1 end -auth = "#{access}:#{secret}" if secret && access -begin - ec2_client = EC2QueryClient::Client.new(auth,url) -rescue Exception => e - puts "#{cmd_name}: #{e.message}" - exit -1 -end - -rc = ec2_client.release_address(public_ip) - -if CloudClient::is_error?(rc) - puts "#{cmd_name}: #{rc.message}" - exit -1 -end - -exit 0 diff --git a/src/cloud/ec2/bin/econe-run-instances b/src/cloud/ec2/bin/econe-run-instances index db3da4bc31..416953cca5 100755 --- a/src/cloud/ec2/bin/econe-run-instances +++ b/src/cloud/ec2/bin/econe-run-instances @@ -23,137 +23,99 @@ else RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" end - $: << RUBY_LIB_LOCATION $: << RUBY_LIB_LOCATION+"/cloud" -COMMANDS_HELP=<<-EOT -econe-run-instances - -Runs an instance of a particular image - -Usage: - econe-run-instances [OPTIONS] IMAGE-ID - -Options: - - --help, -h - Show help - - --access-key , -K - The username of the user - - --secret-key , -S - The password of the user - - --url , -U - Set url as the web service url to use - - --type , -t - OpenNebula template in which is based this instance - - --user-data, -d - Specifies Base64-encoded MIME user data to be made - available to the instance - - --headers, -H - Display column headers - -IMAGE-ID: The image identification as returned by -the econe-upload command - -EOT +require 'cli/command_parser' +require 'cli/cli_helper' require 'econe/EC2QueryClient' -require 'CloudClient' -require 'getoptlong' include CloudCLI -opts = GetoptLong.new( - ['--help', '-h',GetoptLong::NO_ARGUMENT], - ['--version', '-v',GetoptLong::NO_ARGUMENT], - ['--access-key', '-K',GetoptLong::REQUIRED_ARGUMENT], - ['--secret-key', '-S',GetoptLong::REQUIRED_ARGUMENT], - ['--url', '-U',GetoptLong::REQUIRED_ARGUMENT], - ['--type', '-t',GetoptLong::REQUIRED_ARGUMENT], - ['--user-data', '-d',GetoptLong::REQUIRED_ARGUMENT], - ['--headers', '-H',GetoptLong::NO_ARGUMENT] - ) -headers = false -url = nil -access = nil -secret = nil -auth = nil -type = nil -user_data = nil +USER_DATA = { + :name => "user_data", + :short => "-d data", + :large => "--user-data data", + :description => "Specifies Base64-encoded MIME user data to be made + available to the instance", + :format => String +} -begin - opts.each do |opt, arg| - case opt - when '--help' - puts COMMANDS_HELP - return - when '--version' - puts CloudCLI.version_text - exit 0 - when '--access-key' - access = arg - when '--secret-key' - secret = arg - when '--url' - url = arg - when '--type' - type = arg - when '--headers' - headers = true - when '--user-data' - user_data = arg +TYPE = { + :name => "type", + :short => "-t type", + :large => "--type type", + :description => "OpenNebula template in which is based this instance", + :format => String +} + + +TABLE = CLIHelper::ShowTable.new(nil, self) do + column :instanceId, "instanceId", :size=>12 do |d| + d["instanceId"] + end + + column :ImageId, "ImageId", :size=>13 do |d| + d["imageId"] + end + + column :State, "State", :size=>14 do |d| + d["instanceState"]['name'] + end + + column :IP, "IP", :size=>12 do |d| + d['dnsName'] + end + + column :instanceType, "instanceType", :size=>20 do |d| + d["instanceType"] + end + + default :instanceId, :ImageId, :State, :IP, :instanceType +end + + + +CommandParser::CmdParser.new(ARGV) do + usage "econe-run-instances [OPTIONS] IMAGE-ID" + version CloudCLI.version_text + description <<-EOT +Runs an instance of a particular image +IMAGE-ID: The image identification as returned by the econe-upload command +EOT + + option [ + CommandParser::VERBOSE, + CommandParser::HELP, + EC2QueryClient::ACCESS_KEY, + EC2QueryClient::SECRET_KEY, + EC2QueryClient::URL, + USER_DATA, + TYPE + ] + + main :image_id do + begin + ec2_client = EC2QueryClient::Client.new( + options[:access_key], + options[:secret_key], + options[:url]) + rescue Exception => e + puts "#{cmd_name}: #{e.message}" + exit -1 + end + + options[:type] ||= "m1.small" + rc = ec2_client.run_instances(args[0], options[:type], options[:user_data]) + + if CloudClient::is_error?(rc) + exit_with_code -1, "#{cmd_name}: #{rc.message}" + else + TABLE.show(rc['instancesSet']['item'] || []) + exit_with_code 0 end end -rescue Exception => e - exit -1 -end - -image_id = ARGV.shift - -if !image_id - puts "#{cmd_name}: missing IMAGE-ID parameter" - exit -1 end -if !type - type = "m1.small" -end - -auth = "#{access}:#{secret}" if secret && access - -begin - ec2_client = EC2QueryClient::Client.new(auth,url) -rescue Exception => e - puts "#{cmd_name}: #{e.message}" - exit -1 -end - -rc = ec2_client.run_instances(image_id,type, user_data) - -if CloudClient::is_error?(rc) - puts "#{cmd_name}: #{rc.message}" - exit -1 -end - -images = rc['instancesSet']['item'] - -fmt = "%-12s %-13s %-14s %s" - -if headers - puts fmt % ["Owner", "ImageId", "InstanceId", "InstanceType"] - puts "------------------------------------------------------------------------------" -end - -images.each { |img| - puts fmt % [rc['ownerId'],img['imageId'],img['instanceId'],img['instanceType']] - } - -exit 0 diff --git a/src/cloud/ec2/bin/econe-terminate-instances b/src/cloud/ec2/bin/econe-terminate-instances index 07ba5e1f59..c2f923d84d 100755 --- a/src/cloud/ec2/bin/econe-terminate-instances +++ b/src/cloud/ec2/bin/econe-terminate-instances @@ -23,104 +23,55 @@ else RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" end - $: << RUBY_LIB_LOCATION $: << RUBY_LIB_LOCATION+"/cloud" -COMMANDS_HELP=<<-EOT -econe-terminate-instances - -Terminate the selected running instance - -Usage: - econe-register [OPTIONS] INSTANCE-ID - -Options: - - --help, -h - Show help - - --access-key , -K - The username of the user - - --secret-key , -S - The password of the user - - --url , -U - Set url as the web service url to use - -INSTANCE-ID: The instance identification as returned by -the econe-run-instances command - -EOT +require 'cli/command_parser' +require 'cli/cli_helper' require 'econe/EC2QueryClient' -require 'CloudClient' -require 'getoptlong' include CloudCLI -opts = GetoptLong.new( - ['--help', '-h',GetoptLong::NO_ARGUMENT], - ['--version', '-v',GetoptLong::NO_ARGUMENT], - ['--access-key', '-K',GetoptLong::REQUIRED_ARGUMENT], - ['--secret-key', '-S',GetoptLong::REQUIRED_ARGUMENT], - ['--url', '-U',GetoptLong::REQUIRED_ARGUMENT] - ) -url = nil -access = nil -secret = nil -auth = nil +CommandParser::CmdParser.new(ARGV) do + usage "econe-terminate-instances [OPTIONS] INSTANCE-ID" + version CloudCLI.version_text + description <<-EOT +Terminate the selected running instance +INSTANCE-ID: The instance identification as returned by the econe-run-instances command +EOT -begin - opts.each do |opt, arg| - case opt - when '--help' - puts COMMANDS_HELP - return - when '--version' - puts CloudCLI.version_text - exit 0 - when '--access-key' - access = arg - when '--secret-key' - secret = arg - when '--url' - url = arg - when '--headers' - headers = true + option [ + CommandParser::VERBOSE, + CommandParser::HELP, + EC2QueryClient::ACCESS_KEY, + EC2QueryClient::SECRET_KEY, + EC2QueryClient::URL, + USER_DATA, + TYPE + ] + + main :image_id do + begin + ec2_client = EC2QueryClient::Client.new( + options[:access_key], + options[:secret_key], + options[:url]) + rescue Exception => e + puts "#{cmd_name}: #{e.message}" + exit -1 + end + + rc = ec2_client.terminate_instances(args[0]) + + if CloudClient::is_error?(rc) + exit_with_code -1, "#{cmd_name}: #{rc.message}" + else + result = rc['instancesSet']['item'][0] + puts "Success: Terminating #{result['instanceId']} in \ + #{result['previousState']['name']} state" + exit_with_code 0 end end -rescue Exception => e - exit -1 -end - -instance = ARGV.shift - -if !instance - puts "#{cmd_name}: missing INSTANCE-ID parameter" - exit -1 -end - -auth = "#{access}:#{secret}" if secret && access - -begin - ec2_client = EC2QueryClient::Client.new(auth,url) -rescue Exception => e - puts "#{cmd_name}: #{e.message}" - exit -1 -end - -rc = ec2_client.terminate_instances(instance) - -if CloudClient::is_error?(rc) - puts "#{cmd_name}: #{rc.message}" - exit -1 -end - -result = rc['instancesSet']['item'][0] - -puts "Success: Terminating #{result['instanceId']} in #{result['previousState']['name']} state" - -exit 0 +end \ No newline at end of file diff --git a/src/cloud/ec2/bin/econe-upload b/src/cloud/ec2/bin/econe-upload index 55a1bf2792..76606d4f78 100755 --- a/src/cloud/ec2/bin/econe-upload +++ b/src/cloud/ec2/bin/econe-upload @@ -26,104 +26,58 @@ end $: << RUBY_LIB_LOCATION $: << RUBY_LIB_LOCATION+"/cloud" -COMMANDS_HELP=<<-EOT -econe-upload - -Uploads an image for use with an OpenNebula Cloud. This image should -be later register with econe-register using the returned ImageId - -Usage: - econe-upload [OPTIONS] IMAGE-PATH - -Options: - - --help, -h - Show help - - --access-key , -K - The username of the user - - --secret-key , -S - The password of the user - - --url , -U - Set url as the web service url to use - - --multipart, -M - Use 'multipart-post' library instead of Curb/Curl - -IMAGE-PATH: Path to the image to upload - -EOT +require 'cli/command_parser' +require 'cli/cli_helper' require 'econe/EC2QueryClient' -require 'CloudClient' -require 'getoptlong' include CloudCLI -opts = GetoptLong.new( - ['--help', '-h',GetoptLong::NO_ARGUMENT], - ['--version', '-v',GetoptLong::NO_ARGUMENT], - ['--access-key', '-K',GetoptLong::REQUIRED_ARGUMENT], - ['--secret-key', '-S',GetoptLong::REQUIRED_ARGUMENT], - ['--url', '-U',GetoptLong::REQUIRED_ARGUMENT], - ['--multipart', '-M',GetoptLong::NO_ARGUMENT] - ) +MULTIPART = { + :name => "multipart", + :short => "-M", + :large => "--multipart", + :description => "Use 'multipart-post' library instead of Curb/Curl" +} -url = nil -access = nil -secret = nil -auth = nil -curb = true -begin - opts.each do |opt, arg| - case opt - when '--help' - puts COMMANDS_HELP - return - when '--version' - puts CloudCLI.version_text - exit 0 - when '--access-key' - access = arg - when '--secret-key' - secret = arg - when '--url' - url = arg - when '--multipart' - curb = false +CommandParser::CmdParser.new(ARGV) do + usage "econe-upload [OPTIONS] IMAGE-PATH" + version CloudCLI.version_text + description <<-EOT +Uploads an image for use with an OpenNebula Cloud. This image should +be later register with econe-register using the returned ImageId +IMAGE-PATH: Path to the image to upload +EOT + + option [ + CommandParser::VERBOSE, + CommandParser::HELP, + EC2QueryClient::ACCESS_KEY, + EC2QueryClient::SECRET_KEY, + EC2QueryClient::URL, + USER_DATA, + TYPE + ] + + main :file do + begin + ec2_client = EC2QueryClient::Client.new( + options[:access_key], + options[:secret_key], + options[:url]) + rescue Exception => e + puts "#{cmd_name}: #{e.message}" + exit -1 + end + + rc = ec2_client.upload_image(args[0], !options[:multipart]) + + if CloudClient::is_error?(rc) + exit_with_code -1, "#{cmd_name}: #{rc.message}" + else + puts "Success: ImageId #{rc['imageId']}" + exit_with_code 0 end end -rescue Exception => e - exit -1 -end - -image = ARGV.shift - -if !image || !File.exists?(image) - puts "#{cmd_name}: missing IMAGE-PATH parameter or file not found" - exit -1 end - -auth = "#{access}:#{secret}" if secret && access - -begin - ec2_client = EC2QueryClient::Client.new(auth,url) -rescue Exception => e - puts "#{cmd_name}: #{e.message}" - exit -1 -end - -rc = ec2_client.upload_image(image, curb) - -if CloudClient::is_error?(rc) - puts "#{cmd_name}: #{rc.message}" - exit -1 -end - -puts "Success: ImageId #{rc['imageId']}" - -exit 0 - diff --git a/src/cloud/ec2/lib/EC2QueryClient.rb b/src/cloud/ec2/lib/EC2QueryClient.rb index 2615bcf849..16ecbb5e04 100644 --- a/src/cloud/ec2/lib/EC2QueryClient.rb +++ b/src/cloud/ec2/lib/EC2QueryClient.rb @@ -25,6 +25,39 @@ require 'CloudClient' require 'AWS' module EC2QueryClient + + ACCESS_KEY = { + :name => "access_key", + :short => "-K id", + :large => "--access-key id", + :description => "The username of the user", + :format => String + } + + SECRET_KEY = { + :name => "sercret_key", + :short => "-S key", + :large => "--sercret-key key", + :description => "The sha1 hashed password of the user", + :format => String + } + + URL = { + :name => "url", + :short => "-U url", + :large => "--url url", + :description => "Set url as the web service url to use", + :format => String + } + + HEADERS = { + :name => "headers", + :short => "-H", + :large => "--headers", + :description => "Display column headers" + } + + ########################################################################## # # @@ -37,13 +70,13 @@ module EC2QueryClient # # ###################################################################### - def initialize(secret=nil, endpoint=nil, timeout=nil) + def initialize(access=nil, secret=nil, endpoint=nil, timeout=nil) # Autentication ec2auth = nil @timeout = nil - if secret - ec2auth = secret.split(':') + if access && secret + ec2auth = access, secret elsif ENV["EC2_ACCESS_KEY"] and ENV["EC2_SECRET_KEY"] ec2auth = [ENV["EC2_ACCESS_KEY"], ENV["EC2_SECRET_KEY"]] else @@ -268,7 +301,7 @@ module EC2QueryClient def associate_address(public_ip, instance_id) begin response = @ec2_connection.associate_address( - :public_ip => public_ip, + :public_ip => public_ip, :instance_id => instance_id) rescue Exception => e error = CloudClient::Error.new(e.message) From bb69c0b90743e563c5dc0b441973950ae998a889 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Wed, 26 Sep 2012 12:10:06 +0200 Subject: [PATCH 3/3] feature #1190: Adapt old-style CLI to new EC2QueryClient class --- src/cloud/ec2/bin/econe-attach-volume | 5 +---- src/cloud/ec2/bin/econe-create-volume | 5 +---- src/cloud/ec2/bin/econe-delete-volume | 5 +---- src/cloud/ec2/bin/econe-describe-volumes | 5 +---- src/cloud/ec2/bin/econe-detach-volume | 5 +---- src/cloud/ec2/bin/econe-reboot-instances | 5 +---- src/cloud/ec2/bin/econe-start-instances | 5 +---- src/cloud/ec2/bin/econe-stop-instances | 5 +---- 8 files changed, 8 insertions(+), 32 deletions(-) diff --git a/src/cloud/ec2/bin/econe-attach-volume b/src/cloud/ec2/bin/econe-attach-volume index 69db7c1daf..14d25ba5f5 100755 --- a/src/cloud/ec2/bin/econe-attach-volume +++ b/src/cloud/ec2/bin/econe-attach-volume @@ -84,7 +84,6 @@ headers = false url = nil access = nil secret = nil -auth = nil instance = nil device = nil @@ -122,10 +121,8 @@ if !volume_id exit -1 end -auth = "#{access}:#{secret}" if secret && access - begin - ec2_client = EC2QueryClient::Client.new(auth,url) + ec2_client = EC2QueryClient::Client.new(access, secret, url) rescue Exception => e puts "#{cmd_name}: #{e.message}" exit -1 diff --git a/src/cloud/ec2/bin/econe-create-volume b/src/cloud/ec2/bin/econe-create-volume index 3a18eb86b1..ca542f77aa 100755 --- a/src/cloud/ec2/bin/econe-create-volume +++ b/src/cloud/ec2/bin/econe-create-volume @@ -77,7 +77,6 @@ headers = false url = nil access = nil secret = nil -auth = nil size = nil begin @@ -110,10 +109,8 @@ if !size exit -1 end -auth = "#{access}:#{secret}" if secret && access - begin - ec2_client = EC2QueryClient::Client.new(auth,url) + ec2_client = EC2QueryClient::Client.new(access, secret, url) rescue Exception => e puts "#{cmd_name}: #{e.message}" exit -1 diff --git a/src/cloud/ec2/bin/econe-delete-volume b/src/cloud/ec2/bin/econe-delete-volume index 6492e37b53..4a143a82d5 100755 --- a/src/cloud/ec2/bin/econe-delete-volume +++ b/src/cloud/ec2/bin/econe-delete-volume @@ -75,7 +75,6 @@ headers = false url = nil access = nil secret = nil -auth = nil begin opts.each do |opt, arg| @@ -107,10 +106,8 @@ if !volume_id exit -1 end -auth = "#{access}:#{secret}" if secret && access - begin - ec2_client = EC2QueryClient::Client.new(auth,url) + ec2_client = EC2QueryClient::Client.new(access, secret, url) rescue Exception => e puts "#{cmd_name}: #{e.message}" exit -1 diff --git a/src/cloud/ec2/bin/econe-describe-volumes b/src/cloud/ec2/bin/econe-describe-volumes index 14c32f291b..895f2283c4 100755 --- a/src/cloud/ec2/bin/econe-describe-volumes +++ b/src/cloud/ec2/bin/econe-describe-volumes @@ -73,7 +73,6 @@ headers = false url = nil access = nil secret = nil -auth = nil begin opts.each do |opt, arg| @@ -98,10 +97,8 @@ rescue Exception => e exit -1 end -auth = "#{access}:#{secret}" if secret && access - begin - ec2_client = EC2QueryClient::Client.new(auth,url) + ec2_client = EC2QueryClient::Client.new(access, secret, url) rescue Exception => e puts "#{cmd_name}: #{e.message}" exit -1 diff --git a/src/cloud/ec2/bin/econe-detach-volume b/src/cloud/ec2/bin/econe-detach-volume index 666092d2a9..85699eb909 100755 --- a/src/cloud/ec2/bin/econe-detach-volume +++ b/src/cloud/ec2/bin/econe-detach-volume @@ -85,7 +85,6 @@ headers = false url = nil access = nil secret = nil -auth = nil instance = nil device = nil @@ -123,10 +122,8 @@ if !volume_id exit -1 end -auth = "#{access}:#{secret}" if secret && access - begin - ec2_client = EC2QueryClient::Client.new(auth,url) + ec2_client = EC2QueryClient::Client.new(access, secret, url) rescue Exception => e puts "#{cmd_name}: #{e.message}" exit -1 diff --git a/src/cloud/ec2/bin/econe-reboot-instances b/src/cloud/ec2/bin/econe-reboot-instances index d264480249..d6ee877979 100755 --- a/src/cloud/ec2/bin/econe-reboot-instances +++ b/src/cloud/ec2/bin/econe-reboot-instances @@ -71,7 +71,6 @@ opts = GetoptLong.new( url = nil access = nil secret = nil -auth = nil begin opts.each do |opt, arg| @@ -103,10 +102,8 @@ if !instance exit -1 end -auth = "#{access}:#{secret}" if secret && access - begin - ec2_client = EC2QueryClient::Client.new(auth,url) + ec2_client = EC2QueryClient::Client.new(access, secret, url) rescue Exception => e puts "#{cmd_name}: #{e.message}" exit -1 diff --git a/src/cloud/ec2/bin/econe-start-instances b/src/cloud/ec2/bin/econe-start-instances index e2dd0fdd8d..b9fd0a6062 100755 --- a/src/cloud/ec2/bin/econe-start-instances +++ b/src/cloud/ec2/bin/econe-start-instances @@ -71,7 +71,6 @@ opts = GetoptLong.new( url = nil access = nil secret = nil -auth = nil begin opts.each do |opt, arg| @@ -103,10 +102,8 @@ if !instance exit -1 end -auth = "#{access}:#{secret}" if secret && access - begin - ec2_client = EC2QueryClient::Client.new(auth,url) + ec2_client = EC2QueryClient::Client.new(access, secret, url) rescue Exception => e puts "#{cmd_name}: #{e.message}" exit -1 diff --git a/src/cloud/ec2/bin/econe-stop-instances b/src/cloud/ec2/bin/econe-stop-instances index d64afab566..3b8f4865a6 100755 --- a/src/cloud/ec2/bin/econe-stop-instances +++ b/src/cloud/ec2/bin/econe-stop-instances @@ -71,7 +71,6 @@ opts = GetoptLong.new( url = nil access = nil secret = nil -auth = nil begin opts.each do |opt, arg| @@ -103,10 +102,8 @@ if !instance exit -1 end -auth = "#{access}:#{secret}" if secret && access - begin - ec2_client = EC2QueryClient::Client.new(auth,url) + ec2_client = EC2QueryClient::Client.new(access, secret, url) rescue Exception => e puts "#{cmd_name}: #{e.message}" exit -1