From 6bc7332d4842d2f1bbbd16eecff0b33b71c73e7e Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Tue, 28 Aug 2012 17:49:10 +0200 Subject: [PATCH] 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)