From b3b18df5a2c9c232318915947ed0198080e70ebd Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Tue, 25 Sep 2012 19:52:24 +0200 Subject: [PATCH 01/17] feature #1383: Initial merge of the patch contributed by Ricardo Duarte to add support for EC2 Keypairs --- src/cloud/ec2/etc/templates/m1.small.erb | 9 +- src/cloud/ec2/lib/EC2QueryServer.rb | 33 ++-- src/cloud/ec2/lib/econe-server.rb | 6 + src/cloud/ec2/lib/instance.rb | 9 +- src/cloud/ec2/lib/keypair.rb | 153 ++++++++++++++++++ src/cloud/ec2/lib/views/create_keypair.erb | 5 + src/cloud/ec2/lib/views/delete_keypair.erb | 4 + .../ec2/lib/views/describe_instances.erb | 6 +- src/cloud/ec2/lib/views/describe_keypairs.erb | 11 ++ 9 files changed, 220 insertions(+), 16 deletions(-) create mode 100644 src/cloud/ec2/lib/keypair.rb create mode 100644 src/cloud/ec2/lib/views/create_keypair.erb create mode 100644 src/cloud/ec2/lib/views/delete_keypair.erb create mode 100644 src/cloud/ec2/lib/views/describe_keypairs.erb diff --git a/src/cloud/ec2/etc/templates/m1.small.erb b/src/cloud/ec2/etc/templates/m1.small.erb index 4883e539fd..f651f1752b 100644 --- a/src/cloud/ec2/etc/templates/m1.small.erb +++ b/src/cloud/ec2/etc/templates/m1.small.erb @@ -18,7 +18,12 @@ NIC=[NETWORK_ID=] IMAGE_ID = <%= erb_vm_info[:ec2_img_id] %> INSTANCE_TYPE = <%= erb_vm_info[:instance_type ]%> - -<% if erb_vm_info[:user_data] %> + +<% if erb_vm_info[:user_data] && erb_vm_info[:public_key] %> +CONTEXT = [ EC2_USER_DATA="<%= erb_vm_info[:user_data] %>", EC2_PUBLIC_KEY="<%= erb_vm_info[:public_key] %>", EC2_KEYNAME ="<%= erb_vm_info[:key_name] %>" ] +<% elsif erb_vm_info[:user_data] %> CONTEXT = [ EC2_USER_DATA="<%= erb_vm_info[:user_data] %>" ] +<% elsif erb_vm_info[:public_key] %> +CONTEXT = [ EC2_PUBLIC_KEY="<%= erb_vm_info[:public_key] %>", EC2_KEYNAME ="<%= erb_vm_info[:key_name] %>" ] <% end %> + diff --git a/src/cloud/ec2/lib/EC2QueryServer.rb b/src/cloud/ec2/lib/EC2QueryServer.rb index 7367c3278b..95d07d3fff 100644 --- a/src/cloud/ec2/lib/EC2QueryServer.rb +++ b/src/cloud/ec2/lib/EC2QueryServer.rb @@ -17,14 +17,25 @@ require 'rubygems' require 'erb' require 'time' -require 'AWS' require 'base64' -require 'CloudServer' +require 'json' +require 'openssl' +require 'digest/md5' +require 'AWS' + +require 'CloudServer' require 'ImageEC2' + require 'ebs' require 'elastic_ip' require 'instance' +require 'keypair' + +################################################################################ +# Extends the OpenNebula::Error class to include an EC2 render of error +# messages +################################################################################ module OpenNebula EC2_ERROR = %q{ @@ -54,12 +65,14 @@ end ############################################################################### class EC2QueryServer < CloudServer - ########################################################################### - + ############################################################################ + # + # + ############################################################################ def initialize(client, oneadmin_client, config, logger) super(config, logger) - @client = client + @client = client @oneadmin_client = oneadmin_client if config[:ssl_server] @@ -68,12 +81,15 @@ class EC2QueryServer < CloudServer @base_url="http://#{config[:server]}:#{config[:port]}" end + # ----------- Load EC2 API modules ------------ + if @config[:elasticips_vnet_id].nil? logger.error { 'ElasticIP module not loaded' } else extend ElasticIP end + extend Keypair extend EBS extend Instance end @@ -83,8 +99,7 @@ class EC2QueryServer < CloudServer ########################################################################### def describe_availability_zones(params) - response = ERB.new( - File.read(@config[:views]+"/describe_availability_zones.erb")) + response = ERB.new(File.read(@config[:views]+"/describe_availability_zones.erb")) return response.result(binding), 200 end @@ -153,9 +168,8 @@ class EC2QueryServer < CloudServer return response.result(binding), 200 end - ########################################################################### - # Elastic IP + # Provide defaults for Elastic IP if not loaded ########################################################################### def allocate_address(params) return OpenNebula::Error.new('Unsupported') @@ -182,7 +196,6 @@ class EC2QueryServer < CloudServer ########################################################################### private - def render_launch_time(vm) return "#{Time.at(vm["STIME"].to_i).xmlschema}" end diff --git a/src/cloud/ec2/lib/econe-server.rb b/src/cloud/ec2/lib/econe-server.rb index 925d643d49..541508efc9 100644 --- a/src/cloud/ec2/lib/econe-server.rb +++ b/src/cloud/ec2/lib/econe-server.rb @@ -208,6 +208,12 @@ def do_http_request(params) result,rc = @econe_server.detach_volume(params) when 'DeleteVolume' result,rc = @econe_server.delete_volume(params) + when 'DescribeKeyPairs' + result,rc = @econe_server.describe_keypairs(params) + when 'CreateKeyPair' + result,rc = @econe_server.create_keypair(params) + when 'DeleteKeyPair' + result,rc = @econe_server.delete_keypair(params) else result = OpenNebula::Error.new( "#{params['Action']} feature is not supported", diff --git a/src/cloud/ec2/lib/instance.rb b/src/cloud/ec2/lib/instance.rb index 951c080dd4..2fa75872c1 100644 --- a/src/cloud/ec2/lib/instance.rb +++ b/src/cloud/ec2/lib/instance.rb @@ -59,15 +59,18 @@ module Instance end # Get the image - tmp, img=params['ImageId'].split('-') + tmp, img = params['ImageId'].split('-') # Build the VM - erb_vm_info=Hash.new + erb_vm_info = Hash.new + erb_vm_info[:img_id] = img.to_i erb_vm_info[:ec2_img_id] = params['ImageId'] erb_vm_info[:instance_type] = instance_type_name erb_vm_info[:template] = path erb_vm_info[:user_data] = params['UserData'] + erb_vm_info[:public_key] = fetch_publickey(params) + erb_vm_info[:key_name] = params['KeyName'] template = ERB.new(File.read(erb_vm_info[:template])) template_text = template.result(binding) @@ -184,4 +187,4 @@ module Instance instance_id = "i-" + sprintf('%08i', vm.id) return "#{instance_id}" end -end \ No newline at end of file +end diff --git a/src/cloud/ec2/lib/keypair.rb b/src/cloud/ec2/lib/keypair.rb new file mode 100644 index 0000000000..55710ef5e9 --- /dev/null +++ b/src/cloud/ec2/lib/keypair.rb @@ -0,0 +1,153 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +module Keypair + ############################################################################ + # Extends the OpenNebula::User class to include Keypair management + ############################################################################ + class ::OpenNebula::User + + EC2_KP_XPATH = '/USER/TEMPLATE/EC2_KEYPAIRS' + EC2_KP_ELEM = 'EC2_KEYPAIRS' + + ######################################################################## + # Extracts a key pair for the user. Keypairs are stored in the user + # template as base64 json documents + # @param [OpenNebula::User] the user + # + # @return [Hash] with the keypairs. It may be empty + ######################################################################## + def add_keypair(keypairs) + kp = keypairs.to_json + kp64 = Base64.encode64(kp) + + add_element('TEMPLATE', EC2_KP_ELEM => kp64) + + return update(template_xml) + end + + ######################################################################## + # Extracts a key pair for the user. Keypairs are stored in the user + # template as base64 json documents + # @param [OpenNebula::User] the user + # + # @return [Hash] with the keypairs. It may be empty + ######################################################################## + def get_keypair + if has_elements?(EC2_KP_XPATH) + kp64 = Base64.decode64(user[EC2_KP_XPATH]) + kp = JSON.parse(kp64) + else + kp = Hash.new + end + + return kp + end + end + + ############################################################################ + # KeyPair managment functions for EC2 Query API Server + ############################################################################ + + ############################################################################ + # + ############################################################################ + def create_keypair(params) + erb_version = params['Version'] + erb_keyname = params['KeyName'] + erb_user_name = params['AWSAccessKeyId'] + + user = User.new_with_id(OpenNebula::User::SELF, @client) + user.info + + kp = user.get_keypair + rsa_kp = OpenSSL::PKey::RSA.generate(2048) + + erb_private_key = rsa_kp + erb_public_key = rsa_kp.public_key + + erb_key_fingerprint = Digest::MD5.hexdigest(rsa_kp.to_der).gsub(/(.{2})(?=.)/, '\1:\2') + + kp[erb_keyname] = { + "fingerprint" => erb_key_fingerprint, + "public_key" => erb_public_key + } + + rc = user.add_keypair(kp) + + return rc if OpenNebula::is_error?(rc) + + response = ERB.new(File.read(@config[:views]+"/create_keypair.erb")) + return response.result(binding), 200 + end + + ############################################################################ + # + ############################################################################ + def describe_keypairs(params) + erb_version = params['Version'] + erb_user_name = params['AWSAccessKeyId'] + + user = User.new_with_id(OpenNebula::User::SELF, @client) + user.info + + erb_keypairs = user.get_keypair + + response = ERB.new(File.read(@config[:views]+"/describe_keypairs.erb")) + return response.result(binding), 200 + end + + ############################################################################ + # + ############################################################################ + def delete_keypair(params) + erb_version = params['Version'] + erb_user_name = params['AWSAccessKeyId'] + erb_key_name = params['KeyName'] + erb_result = "false" + + user = User.new_with_id(OpenNebula::User::SELF, @client) + user.info + + kp = user.get_keypair + + if !kp.empty? + kp.delete(erb_key_name) + rc = user.add_keypair(kp) + + erb_result = "true" if !OpenNebula::is_error?(rc) + end + + response = ERB.new(File.read(@config[:views]+"/delete_keypair.erb")) + return response.result(binding), 200 + end + + ############################################################################ + # + ############################################################################ + def fetch_publickey(params) + keyname = params['KeyName'] + + user = User.new_with_id(OpenNebula::User::SELF, @client) + user.info + + kp = user.get_keypair + + return nil if keyname.nil? || kp.empty? || kp[keyname].nil? + + kp[keyname]['public_key'] + end +end diff --git a/src/cloud/ec2/lib/views/create_keypair.erb b/src/cloud/ec2/lib/views/create_keypair.erb new file mode 100644 index 0000000000..ef0669ab85 --- /dev/null +++ b/src/cloud/ec2/lib/views/create_keypair.erb @@ -0,0 +1,5 @@ + + <%= erb_keyname %> + <%= erb_key_fingerprint %> + <%= erb_private_key %> + diff --git a/src/cloud/ec2/lib/views/delete_keypair.erb b/src/cloud/ec2/lib/views/delete_keypair.erb new file mode 100644 index 0000000000..5822d79f6c --- /dev/null +++ b/src/cloud/ec2/lib/views/delete_keypair.erb @@ -0,0 +1,4 @@ + + + <%= erb_result %> + diff --git a/src/cloud/ec2/lib/views/describe_instances.erb b/src/cloud/ec2/lib/views/describe_instances.erb index 3718770fbb..38a6833bc6 100644 --- a/src/cloud/ec2/lib/views/describe_instances.erb +++ b/src/cloud/ec2/lib/views/describe_instances.erb @@ -20,7 +20,11 @@ <%= vm["TEMPLATE/NIC/IP"] %> <%= vm["TEMPLATE/NIC/IP"] %> - default + <% if vm['TEMPLATE/CONTEXT/EC2_KEYNAME'].nil? %> + none + <% else %> + <%= vm['TEMPLATE/CONTEXT/EC2_KEYNAME'] %> + <% end %> 0 <%= vm['TEMPLATE/INSTANCE_TYPE'] %> diff --git a/src/cloud/ec2/lib/views/describe_keypairs.erb b/src/cloud/ec2/lib/views/describe_keypairs.erb new file mode 100644 index 0000000000..cdbe99a4fb --- /dev/null +++ b/src/cloud/ec2/lib/views/describe_keypairs.erb @@ -0,0 +1,11 @@ + + + + <% erb_keypairs.each_pair do |k,v| %> + + <%= k %> + <%= v['fingerprint'] %> + + <% end %> + + From f1bc72ae4c67ed9a39e084c07408cf6b1ae286ec Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Tue, 25 Sep 2012 20:03:15 +0200 Subject: [PATCH 02/17] feature #1383: Include new files in install.sh --- install.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/install.sh b/install.sh index 3215a0d39b..3092147e5f 100755 --- a/install.sh +++ b/install.sh @@ -1096,6 +1096,7 @@ ECO_LIB_FILES="src/cloud/ec2/lib/EC2QueryClient.rb \ src/cloud/ec2/lib/elastic_ip.rb \ src/cloud/ec2/lib/ebs.rb \ src/cloud/ec2/lib/instance.rb \ + src/cloud/ec2/lib/keypair.rb \ src/cloud/ec2/lib/econe-server.rb" ECO_LIB_CLIENT_FILES="src/cloud/ec2/lib/EC2QueryClient.rb" @@ -1116,6 +1117,9 @@ ECO_LIB_VIEW_FILES="src/cloud/ec2/lib/views/describe_images.erb \ src/cloud/ec2/lib/views/disassociate_address.erb \ src/cloud/ec2/lib/views/describe_addresses.erb \ src/cloud/ec2/lib/views/release_address.erb \ + src/cloud/ec2/lib/views/create_keypair.erb \ + src/cloud/ec2/lib/views/delete_keypair.erb \ + src/cloud/ec2/lib/views/describe_keypairs.erb \ src/cloud/ec2/lib/views/terminate_instances.erb \ src/cloud/ec2/lib/views/stop_instances.erb \ src/cloud/ec2/lib/views/reboot_instances.erb \ From dc5b09d22fd6707cde0340c634e54cac76ceb69a Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Tue, 25 Sep 2012 22:56:11 +0200 Subject: [PATCH 03/17] feature #1383: Fixes bug for keypair module --- src/cloud/ec2/lib/keypair.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cloud/ec2/lib/keypair.rb b/src/cloud/ec2/lib/keypair.rb index 55710ef5e9..e253bd5977 100644 --- a/src/cloud/ec2/lib/keypair.rb +++ b/src/cloud/ec2/lib/keypair.rb @@ -48,7 +48,7 @@ module Keypair ######################################################################## def get_keypair if has_elements?(EC2_KP_XPATH) - kp64 = Base64.decode64(user[EC2_KP_XPATH]) + kp64 = Base64.decode64(self[EC2_KP_XPATH]) kp = JSON.parse(kp64) else kp = Hash.new From 54eafbefdcce5ccc4a2319d76cfec0b6a7dd4fe5 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Tue, 25 Sep 2012 23:14:36 +0200 Subject: [PATCH 04/17] feature #1383: Client functions to deal with keypairs --- src/cloud/ec2/lib/EC2QueryClient.rb | 56 +++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/cloud/ec2/lib/EC2QueryClient.rb b/src/cloud/ec2/lib/EC2QueryClient.rb index 03a92c2768..e19f500c03 100644 --- a/src/cloud/ec2/lib/EC2QueryClient.rb +++ b/src/cloud/ec2/lib/EC2QueryClient.rb @@ -444,5 +444,61 @@ module EC2QueryClient return response end + + ###################################################################### + # Lists available key pairs + # @param name[String] of the kaypair + # @return keypairs[Hash] + # {"xmlns"=>"http://ec2.amazonaws.com/doc/2010-08-31/", + # "keySet"=>{"item"=>[ + # {"keyName"=>"...", "keyFingerprint"=>"..."}]}} + ###################################################################### + def describe_keypairs() + begin + response = @ec2_connection.describe_keypairs + rescue Exception => e + error = CloudClient::Error.new(e.message) + return error + end + + return response + end + + ###################################################################### + # Creates a new key pair + # @param name[String] of the kaypair + # @return keypair[Hash] + # {"xmlns"=>"http://ec2.amazonaws.com/doc/2010-08-31/", + # "keySet"=>{"item"=>[ + # {"keyName"=>"...", "keyFingerprint"=>"..."}]}} + ###################################################################### + def create_keypair(name) + begin + response = @ec2_connection.create_keypair(:key_name => name) + rescue Exception => e + error = CloudClient::Error.new(e.message) + return error + end + + return response + end + + ###################################################################### + # Deletes a new key pair + # @param name[String] of the kaypair + # @return response[Hash] + # {"xmlns"=>"http://ec2.amazonaws.com/doc/2010-08-31/", + # "return"=>{"true/false"} + ###################################################################### + def delete_keypair(name) + begin + response = @ec2_connection.delete_keypair(:key_name => name) + rescue Exception => e + error = CloudClient::Error.new(e.message) + return error + end + + return response + end end end From 27a746f97cc775db052e4b65a188404dc1161135 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Wed, 26 Sep 2012 01:02:38 +0200 Subject: [PATCH 05/17] feature #1383: Includes some features as suggested in another patch from Ricardo Duarte. SSH Key is now generated and keypair cannot be deleted if it is being used --- src/cloud/ec2/lib/EC2QueryServer.rb | 1 + src/cloud/ec2/lib/keypair.rb | 27 ++++++++++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/cloud/ec2/lib/EC2QueryServer.rb b/src/cloud/ec2/lib/EC2QueryServer.rb index 95d07d3fff..73a789814f 100644 --- a/src/cloud/ec2/lib/EC2QueryServer.rb +++ b/src/cloud/ec2/lib/EC2QueryServer.rb @@ -21,6 +21,7 @@ require 'base64' require 'json' require 'openssl' require 'digest/md5' +require 'net/ssh' require 'AWS' diff --git a/src/cloud/ec2/lib/keypair.rb b/src/cloud/ec2/lib/keypair.rb index e253bd5977..dc8fe4a6e5 100644 --- a/src/cloud/ec2/lib/keypair.rb +++ b/src/cloud/ec2/lib/keypair.rb @@ -80,10 +80,14 @@ module Keypair erb_public_key = rsa_kp.public_key erb_key_fingerprint = Digest::MD5.hexdigest(rsa_kp.to_der).gsub(/(.{2})(?=.)/, '\1:\2') - + erb_ssh_public_key = erb_public_key.ssh_type << + " " << + [ erb_public_key.to_blob ].pack('m0') << + " " << + erb_keyname kp[erb_keyname] = { "fingerprint" => erb_key_fingerprint, - "public_key" => erb_public_key + "public_key" => erb_ssh_public_key } rc = user.add_keypair(kp) @@ -119,16 +123,21 @@ module Keypair erb_key_name = params['KeyName'] erb_result = "false" - user = User.new_with_id(OpenNebula::User::SELF, @client) - user.info + vmpool = VirtualMachinePool.new(@client, OpenNebula::Pool::INFO_ALL) + vmpool.info - kp = user.get_keypair + if !vmpool["/VM_POOL/VM/TEMPLATE/CONTEXT[EC2_KEYNAME=\'#{erb_key_name}\']"] + user = User.new_with_id(OpenNebula::User::SELF, @client) + user.info - if !kp.empty? - kp.delete(erb_key_name) - rc = user.add_keypair(kp) + kp = user.get_keypair - erb_result = "true" if !OpenNebula::is_error?(rc) + if kp.has_key?(erb_key_name) + kp.delete(erb_key_name) + rc = user.add_keypair(kp) + + erb_result = "true" if !OpenNebula::is_error?(rc) + end end response = ERB.new(File.read(@config[:views]+"/delete_keypair.erb")) From 307ee69c38f985219edc89aa7d4912b4222c41d2 Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Wed, 26 Sep 2012 11:44:46 +0200 Subject: [PATCH 06/17] feature #1383: Check errors in keypair creation. Move requires to keypair module --- src/cloud/ec2/lib/EC2QueryServer.rb | 5 +---- src/cloud/ec2/lib/keypair.rb | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/cloud/ec2/lib/EC2QueryServer.rb b/src/cloud/ec2/lib/EC2QueryServer.rb index 73a789814f..ec18f1504f 100644 --- a/src/cloud/ec2/lib/EC2QueryServer.rb +++ b/src/cloud/ec2/lib/EC2QueryServer.rb @@ -18,10 +18,7 @@ require 'rubygems' require 'erb' require 'time' require 'base64' -require 'json' -require 'openssl' -require 'digest/md5' -require 'net/ssh' + require 'AWS' diff --git a/src/cloud/ec2/lib/keypair.rb b/src/cloud/ec2/lib/keypair.rb index dc8fe4a6e5..fb438c6288 100644 --- a/src/cloud/ec2/lib/keypair.rb +++ b/src/cloud/ec2/lib/keypair.rb @@ -14,6 +14,11 @@ # limitations under the License. # #--------------------------------------------------------------------------- # +require 'json' +require 'openssl' +require 'digest/md5' +require 'net/ssh' + module Keypair ############################################################################ # Extends the OpenNebula::User class to include Keypair management @@ -73,13 +78,19 @@ module Keypair user = User.new_with_id(OpenNebula::User::SELF, @client) user.info - kp = user.get_keypair - rsa_kp = OpenSSL::PKey::RSA.generate(2048) + begin + kp = user.get_keypair + rsa_kp = OpenSSL::PKey::RSA.generate(2048) + rescue Exception => e + return OpenNebula::Error.new("Error in create_keypair: #{e.message}") + end erb_private_key = rsa_kp erb_public_key = rsa_kp.public_key - erb_key_fingerprint = Digest::MD5.hexdigest(rsa_kp.to_der).gsub(/(.{2})(?=.)/, '\1:\2') + erb_key_fingerprint = Digest::MD5.hexdigest(rsa_kp.to_der) + erb_key_fingerprint.gsub!(/(.{2})(?=.)/, '\1:\2') + erb_ssh_public_key = erb_public_key.ssh_type << " " << [ erb_public_key.to_blob ].pack('m0') << From 4e478fc80f655c3b90d65e8481a18a5601630ebf Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 27 Sep 2012 00:04:13 +0200 Subject: [PATCH 07/17] feature #1383: Fix DSL-CLI argument check for non-command based tools --- src/cli/command_parser.rb | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/cli/command_parser.rb b/src/cli/command_parser.rb index be6318687f..9d7b4222cd 100644 --- a/src/cli/command_parser.rb +++ b/src/cli/command_parser.rb @@ -90,6 +90,12 @@ module CommandParser @description = str end + # Defines the name of the command + # @param [String] str + def name(str) + @name = str + end + # Defines a block that will be used to parse the arguments # of the command. Formats defined using this method con be used # in the arguments section of the command method, when defining a new @@ -386,12 +392,14 @@ module CommandParser def run comm_name="" + if @main - comm=@main + comm_name = @name + comm = @main elsif if @args[0] && !@args[0].match(/^-/) - comm_name=@args.shift.to_sym - comm=@commands[comm_name] + comm_name = @args.shift.to_sym + comm = @commands[comm_name] end end @@ -470,8 +478,13 @@ module CommandParser end puts puts "Usage:" - print " #{name} " - print_command(@commands[name]) + + if @main + print " #{@usage}\n" + else + print " #{name} " + print_command(@commands[name]) + end exit -1 else id=0 From a905f8872ca19bee9ebd0a7028bc26e38643c36e Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 27 Sep 2012 00:28:37 +0200 Subject: [PATCH 08/17] feature-1383: Added keypair CLI --- install.sh | 3 + src/cloud/ec2/bin/econe-create-keypair | 75 ++++++++++++++++++++ src/cloud/ec2/bin/econe-delete-keypair | 76 ++++++++++++++++++++ src/cloud/ec2/bin/econe-describe-keypairs | 85 +++++++++++++++++++++++ src/cloud/ec2/lib/EC2QueryClient.rb | 7 +- 5 files changed, 243 insertions(+), 3 deletions(-) create mode 100755 src/cloud/ec2/bin/econe-create-keypair create mode 100755 src/cloud/ec2/bin/econe-delete-keypair create mode 100755 src/cloud/ec2/bin/econe-describe-keypairs diff --git a/install.sh b/install.sh index 3092147e5f..839a7af3ca 100755 --- a/install.sh +++ b/install.sh @@ -1129,11 +1129,14 @@ ECO_BIN_FILES="src/cloud/ec2/bin/econe-server \ src/cloud/ec2/bin/econe-describe-images \ src/cloud/ec2/bin/econe-describe-volumes \ src/cloud/ec2/bin/econe-describe-instances \ + src/cloud/ec2/bin/econe-describe-keypairs \ src/cloud/ec2/bin/econe-register \ src/cloud/ec2/bin/econe-attach-volume \ src/cloud/ec2/bin/econe-detach-volume \ src/cloud/ec2/bin/econe-delete-volume \ + src/cloud/ec2/bin/econe-delete-keypair \ src/cloud/ec2/bin/econe-create-volume \ + src/cloud/ec2/bin/econe-create-keypair \ src/cloud/ec2/bin/econe-run-instances \ src/cloud/ec2/bin/econe-terminate-instances \ src/cloud/ec2/bin/econe-start-instances \ diff --git a/src/cloud/ec2/bin/econe-create-keypair b/src/cloud/ec2/bin/econe-create-keypair new file mode 100755 index 0000000000..b49d8c0ac8 --- /dev/null +++ b/src/cloud/ec2/bin/econe-create-keypair @@ -0,0 +1,75 @@ +#!/usr/bin/env ruby +# -------------------------------------------------------------------------- # +# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # +ONE_LOCATION=ENV["ONE_LOCATION"] + +if !ONE_LOCATION + RUBY_LIB_LOCATION="/usr/lib/one/ruby" +else + RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" +end + +$: << RUBY_LIB_LOCATION +$: << RUBY_LIB_LOCATION+"/cloud" + + +require 'cli/command_parser' +require 'cli/cli_helper' +require 'econe/EC2QueryClient' + +include CloudCLI + +CommandParser::CmdParser.new(ARGV) do + name "econe-create-keypair" + usage "econe-create-keypair [OPTIONS] " + version CloudCLI.version_text + description <<-EOT +Creates the named keypair. On success returns the keypair identifier and +fingerprint, and the private key +EOT + + option [ + CommandParser::VERBOSE, + CommandParser::HELP, + EC2QueryClient::ACCESS_KEY, + EC2QueryClient::SECRET_KEY, + EC2QueryClient::URL + ] + + main :keyName 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.create_keypair(args[0]) + + if CloudClient::is_error?(rc) + exit_with_code -1, "#{cmd_name}: #{rc.message}" + else + STDOUT.puts "keyName: #{rc['keyName']}" + STDOUT.puts "Fingerprint: #{rc['keyFingerprint']}" + STDOUT.puts "Private Key:" + STDOUT.puts rc['keyMaterial'] + + exit_with_code 0 + end + end +end diff --git a/src/cloud/ec2/bin/econe-delete-keypair b/src/cloud/ec2/bin/econe-delete-keypair new file mode 100755 index 0000000000..001c41bb0c --- /dev/null +++ b/src/cloud/ec2/bin/econe-delete-keypair @@ -0,0 +1,76 @@ +#!/usr/bin/env ruby +# -------------------------------------------------------------------------- # +# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # +ONE_LOCATION=ENV["ONE_LOCATION"] + +if !ONE_LOCATION + RUBY_LIB_LOCATION="/usr/lib/one/ruby" +else + RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" +end + +$: << RUBY_LIB_LOCATION +$: << RUBY_LIB_LOCATION+"/cloud" + + +require 'cli/command_parser' +require 'cli/cli_helper' +require 'econe/EC2QueryClient' + +include CloudCLI + +CommandParser::CmdParser.new(ARGV) do + name "econe-delete-keypair" + usage "econe-delete-keypair [OPTIONS] " + version CloudCLI.version_text + description <<-EOT +Deletes the named keypair, removes the associated keys. On success returns the +identifier of the removed key. +EOT + + option [ + CommandParser::VERBOSE, + CommandParser::HELP, + EC2QueryClient::ACCESS_KEY, + EC2QueryClient::SECRET_KEY, + EC2QueryClient::URL + ] + + main :keyName 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.delete_keypair(args[0]) + + if CloudClient::is_error?(rc) + exit_with_code -1, "#{cmd_name}: #{rc.message}" + else + if rc["return"] == "false" + STDERR.puts "Key #{args[0]} could not be removed" + else + STDERR.puts args[0] + end + + exit_with_code 0 + end + end +end diff --git a/src/cloud/ec2/bin/econe-describe-keypairs b/src/cloud/ec2/bin/econe-describe-keypairs new file mode 100755 index 0000000000..f0e7b047be --- /dev/null +++ b/src/cloud/ec2/bin/econe-describe-keypairs @@ -0,0 +1,85 @@ +#!/usr/bin/env ruby +# -------------------------------------------------------------------------- # +# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # +ONE_LOCATION=ENV["ONE_LOCATION"] + +if !ONE_LOCATION + RUBY_LIB_LOCATION="/usr/lib/one/ruby" +else + RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" +end + +$: << RUBY_LIB_LOCATION +$: << RUBY_LIB_LOCATION+"/cloud" + + +require 'cli/command_parser' +require 'cli/cli_helper' +require 'econe/EC2QueryClient' + +include CloudCLI + +TABLE = CLIHelper::ShowTable.new(nil, self) do + column :keyName, "keyName", :left, :size=>15 do |d| + d["keyName"] + end + + column :keyFingerprint, "keyFingerprint", :left, :size=>65 do |d| + d["keyFingerprint"] + end + + default :keyName, :keyFingerprint +end + +CommandParser::CmdParser.new(ARGV) do + usage "econe-describe-keypairs [OPTIONS]" + version CloudCLI.version_text + description "List and describe the key pairs available to you." + + 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_keypairs + + if CloudClient::is_error?(rc) + exit_with_code -1, "#{cmd_name}: #{rc.message}" + else + if rc.empty? || rc['keySet'].nil? || rc['keySet']['item'].nil? + kps = [] + else + kps = rc['keySet']['item'] + end + + TABLE.show(kps) + exit_with_code 0 + end + end +end diff --git a/src/cloud/ec2/lib/EC2QueryClient.rb b/src/cloud/ec2/lib/EC2QueryClient.rb index 06c98686ae..8fe2383e24 100644 --- a/src/cloud/ec2/lib/EC2QueryClient.rb +++ b/src/cloud/ec2/lib/EC2QueryClient.rb @@ -501,9 +501,10 @@ module EC2QueryClient # Creates a new key pair # @param name[String] of the kaypair # @return keypair[Hash] - # {"xmlns"=>"http://ec2.amazonaws.com/doc/2010-08-31/", - # "keySet"=>{"item"=>[ - # {"keyName"=>"...", "keyFingerprint"=>"..."}]}} + # {"xmlns"=>"http://ec2.amazonaws.com/doc/2010-08-31", + # "keyName"=>"...", + # "keyFingerprint"=>"...", + # "keyMaterial"=>"..."} ###################################################################### def create_keypair(name) begin From d5aa8b1fd9de1877d4026d0215e638c25cab68ee Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Thu, 27 Sep 2012 11:31:41 +0200 Subject: [PATCH 09/17] feature #1383: Fix return code for ec2-delete-keypair --- src/cloud/ec2/bin/econe-delete-keypair | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/cloud/ec2/bin/econe-delete-keypair b/src/cloud/ec2/bin/econe-delete-keypair index 001c41bb0c..3e8e2adbe5 100755 --- a/src/cloud/ec2/bin/econe-delete-keypair +++ b/src/cloud/ec2/bin/econe-delete-keypair @@ -65,9 +65,7 @@ EOT exit_with_code -1, "#{cmd_name}: #{rc.message}" else if rc["return"] == "false" - STDERR.puts "Key #{args[0]} could not be removed" - else - STDERR.puts args[0] + exit_with_code -1, "Key #{args[0]} could not be removed" end exit_with_code 0 From db7326bc0f182c456d8806d1d6cba348962c01eb Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Mon, 1 Oct 2012 15:36:50 +0200 Subject: [PATCH 10/17] feature #1383: Check if item key exists in describe* commands --- src/cloud/ec2/bin/econe-describe-addresses | 8 +++++++- src/cloud/ec2/bin/econe-describe-images | 8 +++++++- src/cloud/ec2/bin/econe-describe-instances | 10 +++++++++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/cloud/ec2/bin/econe-describe-addresses b/src/cloud/ec2/bin/econe-describe-addresses index 0a73cd0118..0dce55e95b 100755 --- a/src/cloud/ec2/bin/econe-describe-addresses +++ b/src/cloud/ec2/bin/econe-describe-addresses @@ -74,7 +74,13 @@ CommandParser::CmdParser.new(ARGV) do if CloudClient::is_error?(rc) exit_with_code -1, "#{cmd_name}: #{rc.message}" else - TABLE.show(rc['addressesSet']['item'] || []) + if rc.empty? || rc['addressesSet'].nil? || rc['addressesSet']['item'].nil? + addrs = [] + else + addrs = rc['addressesSet']['item'] + end + + TABLE.show(addrs) exit_with_code 0 end end diff --git a/src/cloud/ec2/bin/econe-describe-images b/src/cloud/ec2/bin/econe-describe-images index 0aa78d012d..7ada55841e 100755 --- a/src/cloud/ec2/bin/econe-describe-images +++ b/src/cloud/ec2/bin/econe-describe-images @@ -86,7 +86,13 @@ used with an OpenNebula Cloud." if CloudClient::is_error?(rc) exit_with_code -1, "#{cmd_name}: #{rc.message}" else - TABLE.show(rc['imagesSet']['item'] || []) + if rc.empty? || rc['imagesSet'].nil? || rc['imagesSet']['item'].nil? + imgs = [] + else + imgs = rc['imagesSet']['item'] + end + + TABLE.show(imgs) exit_with_code 0 end end diff --git a/src/cloud/ec2/bin/econe-describe-instances b/src/cloud/ec2/bin/econe-describe-instances index 1ef55014ca..89a68f0684 100755 --- a/src/cloud/ec2/bin/econe-describe-instances +++ b/src/cloud/ec2/bin/econe-describe-instances @@ -84,7 +84,15 @@ CommandParser::CmdParser.new(ARGV) do if CloudClient::is_error?(rc) exit_with_code -1, "#{cmd_name}: #{rc.message}" else - TABLE.show(rc['reservationSet']['item'][0]['instancesSet']['item'] || []) + if rc.empty? || + rc['reservationSet']['item'][0]['instancesSet'].nil? || + rc['reservationSet']['item'][0]['instancesSet']['item'].nil? + insts = [] + else + insts = rc['reservationSet']['item'][0]['instancesSet']['item'] + end + + TABLE.show(insts) exit_with_code 0 end end From 7b0c8a29ed1330adc9385fb75a5f8e560204ce39 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Mon, 1 Oct 2012 16:51:27 +0200 Subject: [PATCH 11/17] feature #1383: Add net-ssh gem to install_gems --- share/install_gems/install_gems | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/share/install_gems/install_gems b/share/install_gems/install_gems index 63941923aa..915a3e043f 100755 --- a/share/install_gems/install_gems +++ b/share/install_gems/install_gems @@ -15,7 +15,7 @@ end GROUPS={ :quota => [SQLITE, 'sequel'], :sunstone => ['json', 'rack', 'sinatra', 'thin', 'sequel', SQLITE], - :cloud => %w{amazon-ec2 rack sinatra thin uuidtools curb json}, + :cloud => %w{amazon-ec2 rack sinatra thin uuidtools curb json net-ssh}, :ozones_client => %w{json}, :ozones_server => %w{json sequel}+[ SQLITE, 'mysql' From de0cc59a651555ff413eb56d2443b020136a77ad Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Mon, 1 Oct 2012 16:53:14 +0200 Subject: [PATCH 12/17] feature #1383: Use the first usage word for name if it is not defined --- src/cli/command_parser.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cli/command_parser.rb b/src/cli/command_parser.rb index 9d7b4222cd..3f34026087 100644 --- a/src/cli/command_parser.rb +++ b/src/cli/command_parser.rb @@ -75,7 +75,8 @@ module CommandParser # Defines the usage information of the command # @param [String] str def usage(str) - @usage=str + @usage = str + @name ||= @usage.split(' ').first end # Defines the version the command From c403a5b48e52af3b90d5be49a52cfd9771a1ad9d Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Mon, 1 Oct 2012 17:28:15 +0200 Subject: [PATCH 13/17] feature #1433: http proxy support for CloudClient.rb, contributed by Laurent Grawet --- src/cloud/common/CloudClient.rb | 3 ++- src/cloud/marketplace/lib/marketplace_client.rb | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/cloud/common/CloudClient.rb b/src/cloud/common/CloudClient.rb index 3efebd4b5a..b7ee78569b 100644 --- a/src/cloud/common/CloudClient.rb +++ b/src/cloud/common/CloudClient.rb @@ -83,7 +83,8 @@ module CloudClient # is set if needed. # ######################################################################### def self.http_start(url, timeout, &block) - http = Net::HTTP.new(url.host, url.port) + proxy = ENV['http_proxy'] ? URI.parse(ENV['http_proxy']) : OpenStruct.new + http = Net::HTTP::Proxy(proxy.host, proxy.port).new(url.host, url.port) if timeout http.read_timeout = timeout.to_i diff --git a/src/cloud/marketplace/lib/marketplace_client.rb b/src/cloud/marketplace/lib/marketplace_client.rb index 176ec7e319..1db514a09f 100644 --- a/src/cloud/marketplace/lib/marketplace_client.rb +++ b/src/cloud/marketplace/lib/marketplace_client.rb @@ -27,16 +27,16 @@ module Market @uri = URI.parse(url) @user_agent = "OpenNebula #{CloudClient::VERSION} (#{user_agent})" + @proxy = ENV['http_proxy'] ? URI.parse(ENV['http_proxy']) : OpenStruct.new end def get(path) - req = Net::HTTP::Get.new(path) - + req = Net::HTTP::Proxy(@proxy.host, @proxy.port)::Get.new(path) do_request(req) end def post(path, body) - req = Net::HTTP::Post.new(path) + req = Net::HTTP::Proxy(@proxy.host, @proxy.port)::Post.new(path) req.body = body do_request(req) From 7d0effb1d59e887f3c82a60214f34660e0fdf2e9 Mon Sep 17 00:00:00 2001 From: Tino Vazquez Date: Mon, 1 Oct 2012 18:26:14 +0200 Subject: [PATCH 14/17] Avoid ruby warnings for ambigous with parenthesis --- src/vmm_mad/remotes/vmware/vmware_driver.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/vmm_mad/remotes/vmware/vmware_driver.rb b/src/vmm_mad/remotes/vmware/vmware_driver.rb index 7ca1079eea..530c2c5e4b 100644 --- a/src/vmm_mad/remotes/vmware/vmware_driver.rb +++ b/src/vmm_mad/remotes/vmware/vmware_driver.rb @@ -73,7 +73,7 @@ class VMwareDriver if not domain_defined?(id) deploy_id = define_domain(dfile) - exit -1 if deploy_id.nil? + exit(-1) if deploy_id.nil? else deploy_id = "one-#{id}" end @@ -187,12 +187,12 @@ class VMwareDriver dfile = vm_folder + "/" + last_deployment_file rescue => e OpenNebula.log_error("Cannot open checkpoint #{e.message}") - exit -1 + exit(-1) end deploy_id = define_domain(dfile) - exit -1 if deploy_id.nil? + exit(-1) if deploy_id.nil? # Revert snapshot VM # Note: This assumes the checkpoint name is "checkpoint", to change From 630fc0950c9a825f05f11c03d421fbd8da9bd7ed Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Mon, 1 Oct 2012 18:58:17 +0200 Subject: [PATCH 15/17] Fix bug in vmm mad when operations are all local --- src/mad/ruby/ssh_stream.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/mad/ruby/ssh_stream.rb b/src/mad/ruby/ssh_stream.rb index a72abdfb18..85bbb1427c 100644 --- a/src/mad/ruby/ssh_stream.rb +++ b/src/mad/ruby/ssh_stream.rb @@ -60,6 +60,8 @@ class SshStream end def close + return if !@alive + begin @stdin.puts "\nexit" rescue #rescue from EPIPE if ssh command exited already From 2f18afaaa506ba6743cf199c102eb4649edd63d0 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Mon, 1 Oct 2012 19:06:46 +0200 Subject: [PATCH 16/17] feature #1388: Change default port to 4568 for econe-server --- src/cloud/ec2/etc/econe.conf | 2 +- src/cloud/ec2/lib/EC2QueryClient.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cloud/ec2/etc/econe.conf b/src/cloud/ec2/etc/econe.conf index 8fa7faac65..bef637a457 100644 --- a/src/cloud/ec2/etc/econe.conf +++ b/src/cloud/ec2/etc/econe.conf @@ -26,7 +26,7 @@ # Host and port where econe server will run :host: localhost -:port: 4567 +:port: 4568 # SSL proxy URL that serves the API (set if is being used) #:ssl_server: https://service.endpoint.fqdn:port/ diff --git a/src/cloud/ec2/lib/EC2QueryClient.rb b/src/cloud/ec2/lib/EC2QueryClient.rb index 8fe2383e24..fa8c87aaa6 100644 --- a/src/cloud/ec2/lib/EC2QueryClient.rb +++ b/src/cloud/ec2/lib/EC2QueryClient.rb @@ -97,7 +97,7 @@ module EC2QueryClient if $ec2url endpoint = $ec2url else - endpoint = "http://localhost:4567" + endpoint = "http://localhost:4568" end end From e22c9a0f6063e17abfc808b339c6f796694ed789 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Tue, 2 Oct 2012 11:53:02 +0200 Subject: [PATCH 17/17] Revert "feature #1388: Change default port to 4568 for econe-server" This reverts commit 2f18afaaa506ba6743cf199c102eb4649edd63d0. --- src/cloud/ec2/etc/econe.conf | 2 +- src/cloud/ec2/lib/EC2QueryClient.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cloud/ec2/etc/econe.conf b/src/cloud/ec2/etc/econe.conf index bef637a457..8fa7faac65 100644 --- a/src/cloud/ec2/etc/econe.conf +++ b/src/cloud/ec2/etc/econe.conf @@ -26,7 +26,7 @@ # Host and port where econe server will run :host: localhost -:port: 4568 +:port: 4567 # SSL proxy URL that serves the API (set if is being used) #:ssl_server: https://service.endpoint.fqdn:port/ diff --git a/src/cloud/ec2/lib/EC2QueryClient.rb b/src/cloud/ec2/lib/EC2QueryClient.rb index fa8c87aaa6..8fe2383e24 100644 --- a/src/cloud/ec2/lib/EC2QueryClient.rb +++ b/src/cloud/ec2/lib/EC2QueryClient.rb @@ -97,7 +97,7 @@ module EC2QueryClient if $ec2url endpoint = $ec2url else - endpoint = "http://localhost:4568" + endpoint = "http://localhost:4567" end end