1
0
mirror of https://github.com/OpenNebula/one.git synced 2024-12-24 21:34:01 +03:00

Decoupling EC2Query Client from OCA

git-svn-id: http://svn.opennebula.org/one/trunk@881 3034c82b-c49b-4eb3-8279-a7acafdc01c0
This commit is contained in:
Rubén S. Montero 2009-10-22 23:27:36 +00:00
parent 14ae27a2ee
commit ddd9f3743b
8 changed files with 82 additions and 64 deletions

View File

@ -32,17 +32,28 @@ begin
rescue LoadError
end
class CloudClient
###############################################################################
# The CloudClient module contains general functionality to implement a
# Cloud Client
###############################################################################
module CloudClient
# #########################################################################
# Default location for the authentication file
# #########################################################################
DEFAULT_AUTH_FILE = ENV["HOME"]+"/.one/one_auth"
# #########################################################################
# Gets authorization credentials from ONE_AUTH or default
# auth file.
#
# Raises an error if authorization is not found
def get_one_auth
if ENV["ONE_AUTH"] and !ENV["ONE_AUTH"].empty? and
File.file?(ENV["ONE_AUTH"])
# #########################################################################
def self.get_one_auth
if ENV["ONE_AUTH"] and !ENV["ONE_AUTH"].empty? and
File.file?(ENV["ONE_AUTH"])
one_auth=File.read(ENV["ONE_AUTH"]).strip.split(':')
elsif File.file?(ENV["HOME"]+"/.one/one_auth")
one_auth=File.read(ENV["HOME"]+"/.one/one_auth").strip.split(':')
elsif File.file?(DEFAULT_AUTH_FILE)
one_auth=File.read(DEFAULT_AUTH_FILE).strip.split(':')
else
raise "No authorization data present"
end
@ -51,10 +62,12 @@ class CloudClient
one_auth
end
# #########################################################################
# Starts an http connection and calls the block provided. SSL flag
# is set if needed.
def http_start(url, &block)
# #########################################################################
def self.http_start(url, &block)
http = Net::HTTP.new(url.host, url.port)
if url.scheme=='https'
http.use_ssl = true
@ -71,13 +84,37 @@ class CloudClient
exit -1
end
end
# Command line help functions
module CLIHelpers
# Returns the command name
def cmd_name
File.basename($0)
# #########################################################################
# The Error Class represents a generic error in the Cloud Client
# library. It contains a readable representation of the error.
# #########################################################################
class Error
attr_reader :message
# +message+ a description of the error
def initialize(message=nil)
@message=message
end
def to_s()
@message
end
end
end
# #########################################################################
# Returns true if the object returned by a method of the OpenNebula
# library is an Error
# #########################################################################
def self.is_error?(value)
value.class==CloudClient::Error
end
end
# Command line help functions
module CloudCLI
# Returns the command name
def cmd_name
File.basename($0)
end
end

View File

@ -55,11 +55,12 @@ $: << RUBY_LIB_LOCATION
$: << RUBY_LIB_LOCATION+"/cloud"
require 'econe/EC2QueryClient'
require 'CloudClient'
require 'getoptlong'
require 'rdoc/usage'
require 'pp'
include CloudClient::CLIHelpers
include CloudCLI
opts = GetoptLong.new(
['--help', '-h',GetoptLong::NO_ARGUMENT],
@ -105,7 +106,7 @@ end
rc = ec2_client.describe_images()
if OpenNebula::is_error?(rc)
if CloudClient::is_error?(rc)
puts "#{cmd_name}: #{rc.message}"
exit -1
end
@ -127,8 +128,3 @@ end
exit 0

View File

@ -54,11 +54,12 @@ $: << RUBY_LIB_LOCATION
$: << RUBY_LIB_LOCATION+"/cloud"
require 'econe/EC2QueryClient'
require 'CloudClient'
require 'getoptlong'
require 'rdoc/usage'
require 'pp'
include CloudClient::CLIHelpers
include CloudCLI
opts = GetoptLong.new(
['--help', '-h',GetoptLong::NO_ARGUMENT],
@ -104,7 +105,7 @@ end
rc = ec2_client.describe_instances()
if OpenNebula::is_error?(rc)
if CloudClient::is_error?(rc)
puts "#{cmd_name}: #{rc.message}"
exit -1
end
@ -125,9 +126,3 @@ if instances
}
end
exit 0

View File

@ -59,11 +59,12 @@ $: << RUBY_LIB_LOCATION
$: << RUBY_LIB_LOCATION+"/cloud"
require 'econe/EC2QueryClient'
require 'CloudClient'
require 'getoptlong'
require 'rdoc/usage'
require 'pp'
include CloudClient::CLIHelpers
include CloudCLI
opts = GetoptLong.new(
['--help', '-h',GetoptLong::NO_ARGUMENT],
@ -116,7 +117,7 @@ end
rc = ec2_client.register_image(image_id)
if OpenNebula::is_error?(rc)
if CloudClient::is_error?(rc)
puts "#{cmd_name}: #{rc.message}"
exit -1
end
@ -124,9 +125,3 @@ end
puts "Success: ImageId #{rc['imageId']}"
exit 0

View File

@ -57,11 +57,12 @@ $: << RUBY_LIB_LOCATION
$: << RUBY_LIB_LOCATION+"/cloud"
require 'econe/EC2QueryClient'
require 'CloudClient'
require 'getoptlong'
require 'rdoc/usage'
require 'pp'
include CloudClient::CLIHelpers
include CloudCLI
opts = GetoptLong.new(
['--help', '-h',GetoptLong::NO_ARGUMENT],
@ -122,7 +123,7 @@ end
rc = ec2_client.run_instances(image_id,type)
if OpenNebula::is_error?(rc)
if CloudClient::is_error?(rc)
puts "#{cmd_name}: #{rc.message}"
exit -1
end
@ -141,9 +142,3 @@ images.each { |img|
}
exit 0

View File

@ -56,11 +56,12 @@ $: << RUBY_LIB_LOCATION
$: << RUBY_LIB_LOCATION+"/cloud"
require 'econe/EC2QueryClient'
require 'CloudClient'
require 'getoptlong'
require 'rdoc/usage'
require 'pp'
include CloudClient::CLIHelpers
include CloudCLI
opts = GetoptLong.new(
['--help', '-h',GetoptLong::NO_ARGUMENT],
@ -111,7 +112,7 @@ end
rc = ec2_client.terminate_instances(instance)
if OpenNebula::is_error?(rc)
if CloudClient::is_error?(rc)
puts "#{cmd_name}: #{rc.message}"
exit -1
end

View File

@ -56,11 +56,12 @@ $: << RUBY_LIB_LOCATION
$: << RUBY_LIB_LOCATION+"/cloud"
require 'econe/EC2QueryClient'
require 'CloudClient'
require 'getoptlong'
require 'rdoc/usage'
require 'pp'
include CloudClient::CLIHelpers
include CloudCLI
opts = GetoptLong.new(
['--help', '-h',GetoptLong::NO_ARGUMENT],
@ -113,7 +114,7 @@ end
rc = ec2_client.upload_image(image, curb)
if OpenNebula::is_error?(rc)
if CloudClient::is_error?(rc)
puts "#{cmd_name}: #{rc.message}"
exit -1
end

View File

@ -23,8 +23,6 @@ if ENV["EC2_URL"]
ENV["EC2_URL"]=nil
end
require 'OpenNebula'
require 'CloudClient'
require 'AWS'
@ -33,7 +31,9 @@ module EC2QueryClient
#
#
##########################################################################
class Client < CloudClient
class Client
include CloudClient
API_VERSION = '2008-12-01'
@ -50,9 +50,9 @@ module EC2QueryClient
elsif ENV["EC2_ACCESS_KEY"] and ENV["EC2_SECRET_KEY"]
ec2auth = [ENV["EC2_ACCESS_KEY"], ENV["EC2_SECRET_KEY"]]
else
ec2auth=get_one_auth
ec2auth=CloudClient::get_one_auth
end
if !ec2auth
raise "No authorization data present"
end
@ -89,7 +89,7 @@ module EC2QueryClient
begin
response = @ec2_connection.describe_instances
rescue Exception => e
error = OpenNebula::Error.new(e.message)
error = Error.new(e.message)
return error
end
@ -109,7 +109,7 @@ module EC2QueryClient
:instance_type => type
)
rescue Exception => e
error = OpenNebula::Error.new(e.message)
error = Error.new(e.message)
return error
end
@ -126,7 +126,7 @@ module EC2QueryClient
:instance_id => instance_id
)
rescue Exception => e
error = OpenNebula::Error.new(e.message)
error = Error.new(e.message)
return error
end
@ -166,7 +166,7 @@ module EC2QueryClient
if connection.response_code == 200
return AWS::Response.parse(:xml => connection.body_str)
else
return OpenNebula::Error.new(connection.body_str)
return Error.new(connection.body_str)
end
else
params["Signature"]=sig
@ -185,7 +185,7 @@ module EC2QueryClient
if res.code == '200'
return AWS::Response.parse(:xml => res.body)
else
return OpenNebula::Error.new(res.body)
return Error.new(res.body)
end
end
end
@ -200,7 +200,7 @@ module EC2QueryClient
:image_location => image_id
)
rescue Exception => e
error = OpenNebula::Error.new(e.message)
error = Error.new(e.message)
return error
end
@ -216,7 +216,7 @@ module EC2QueryClient
begin
response = @ec2_connection.describe_images
rescue Exception => e
error = OpenNebula::Error.new(e.message)
error = Error.new(e.message)
return error
end
@ -224,5 +224,3 @@ module EC2QueryClient
end
end
end