From 494ac473723da70f1a1bf28a0238a4a783f195be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tino=20V=C3=A1zquez?= Date: Tue, 6 Jul 2010 17:40:02 +0200 Subject: [PATCH] Request #195 fulfilled, now occi commands may timeout while connected to server Additionally, commands help section has been homogeneously tabbed. --- src/cloud/common/CloudClient.rb | 21 +++++++++- src/cloud/occi/bin/occi-compute | 71 ++++++++++++++++++-------------- src/cloud/occi/bin/occi-network | 69 +++++++++++++++++-------------- src/cloud/occi/bin/occi-storage | 9 +++- src/cloud/occi/lib/OCCIClient.rb | 30 +++++++------- 5 files changed, 120 insertions(+), 80 deletions(-) diff --git a/src/cloud/common/CloudClient.rb b/src/cloud/common/CloudClient.rb index 17c6dc9e66..e36a617b5a 100644 --- a/src/cloud/common/CloudClient.rb +++ b/src/cloud/common/CloudClient.rb @@ -67,8 +67,15 @@ module CloudClient # Starts an http connection and calls the block provided. SSL flag # is set if needed. # ######################################################################### - def self.http_start(url, &block) + def self.http_start(url, timeout, &block) http = Net::HTTP.new(url.host, url.port) + + if timeout + http.read_timeout = timeout.to_i + else + http.read_timeout = 600 + end + if url.scheme=='https' http.use_ssl = true http.verify_mode=OpenSSL::SSL::VERIFY_NONE @@ -79,10 +86,20 @@ module CloudClient block.call(connection) end rescue Errno::ECONNREFUSED => e - str = "Error connecting to server (#{e.to_s})." + str = "Error connecting to server (#{e.to_s}).\n" str << "Server: #{url.host}:#{url.port}" return CloudClient::Error.new(str) + rescue Errno::ETIMEDOUT => e + str = "Error timeout connecting to server (#{e.to_s}).\n" + str << "Server: #{url.host}:#{url.port}" + + return CloudClient::Error.new(str) + rescue Timeout::Error => e + str = "Error timeout while connected to server (#{e.to_s}).\n" + str << "Server: #{url.host}:#{url.port}" + + return CloudClient::Error.new(str) end end diff --git a/src/cloud/occi/bin/occi-compute b/src/cloud/occi/bin/occi-compute index e67f8fa36a..77db4d6317 100755 --- a/src/cloud/occi/bin/occi-compute +++ b/src/cloud/occi/bin/occi-compute @@ -29,51 +29,54 @@ $: << RUBY_LIB_LOCATION $: << RUBY_LIB_LOCATION+"/cloud" COMMANDS_HELP=<<-EOT - ** Synopsis - occi-compute - - Manages compute resources +** Synopsis +occi-compute - ** Usage - occi-compute [OPTIONS] [ARGUMENTS] +Manages compute resources - COMMANDS +** Usage +occi-compute [OPTIONS] [ARGUMENTS] - create - creates a new compute resource described by the provided - +COMMANDS - list - lists available compute resources +create + creates a new compute resource described by the provided + - show - retrieves the OCCI XML representation of the compute resource - identified by +list + lists available compute resources - update - updates the representation of the compute resource represented by the - provided +show + retrieves the OCCI XML representation of the compute resource + identified by - delete - deletes the compute resource idenfitied by +update + updates the representation of the compute resource represented by the + provided + +delete + deletes the compute resource idenfitied by - OPTIONS +OPTIONS - --help, -h: - Show help +--help, -h: + Show help - --username , -U : - The username of the user +--username , -U : + The username of the user - --password , -P : - The password of the user +--password , -P : + The password of the user - --url , -R : - Set url as the web service url to use +--url , -R : + Set url as the web service url to use - --debug, -D - Enables verbosity +--timeout , -T + Sets a timeout for the http connection + +--debug, -D + Enables verbosity EOT @@ -88,6 +91,7 @@ opts = GetoptLong.new( ['--username', '-U',GetoptLong::REQUIRED_ARGUMENT], ['--password', '-P',GetoptLong::REQUIRED_ARGUMENT], ['--url', '-R',GetoptLong::REQUIRED_ARGUMENT], + ['--timeout', '-T',GetoptLong::REQUIRED_ARGUMENT], ['--debug', '-D',GetoptLong::NO_ARGUMENT] ) @@ -95,6 +99,7 @@ url = nil username = nil password = nil auth = nil +timeout = nil debug = false begin @@ -109,6 +114,8 @@ begin password = arg when '--url' url = arg + when '--timeout' + timeout = arg when '--debug' debug = true end @@ -118,7 +125,7 @@ rescue Exception => e end begin - occi_client = OCCIClient::Client.new(url,username,password,debug) + occi_client = OCCIClient::Client.new(url,username,password, timeout, debug) rescue Exception => e puts "#{cmd_name}: #{e.message}" exit(-1) diff --git a/src/cloud/occi/bin/occi-network b/src/cloud/occi/bin/occi-network index e8c1686c26..6508147b77 100755 --- a/src/cloud/occi/bin/occi-network +++ b/src/cloud/occi/bin/occi-network @@ -29,51 +29,54 @@ $: << RUBY_LIB_LOCATION $: << RUBY_LIB_LOCATION+"/cloud" COMMANDS_HELP=<<-EOT - ** Synopsis - occi-network - - Manages virtual networks +** Synopsis +occi-network - ** Usage - occi-network [OPTIONS] [ARGUMENTS] +Manages virtual networks - COMMANDS +** Usage +occi-network [OPTIONS] [ARGUMENTS] - create - creates a new virtual network described by the provided - +COMMANDS - list - lists available virtual networks +create + creates a new virtual network described by the provided + - show - retrieves the OCCI XML representation of the virtual network - identified by +list + lists available virtual networks - delete - deletes the virtual network idenfitied by +show + retrieves the OCCI XML representation of the virtual network + identified by + +delete + deletes the virtual network idenfitied by - OPTIONS +OPTIONS - --help, -h: - Show help +--help, -h: + Show help - --username , -U : - The username of the user +--username , -U : + The username of the user - --password , -P : - The password of the user +--password , -P : + The password of the user - --url , -R : - Set url as the web service url to use +--url , -R : + Set url as the web service url to use + +--timeout , -T + Sets a timeout for the http connection - --debug, -D - Enables verbosity +--debug, -D + Enables verbosity - --multipart, -M: - Use 'multipart-post' library instead of Curb/Curl +--multipart, -M: + Use 'multipart-post' library instead of Curb/Curl EOT @@ -89,6 +92,7 @@ opts = GetoptLong.new( ['--username', '-U',GetoptLong::REQUIRED_ARGUMENT], ['--password', '-P',GetoptLong::REQUIRED_ARGUMENT], ['--url', '-R',GetoptLong::REQUIRED_ARGUMENT], + ['--timeout', '-T',GetoptLong::REQUIRED_ARGUMENT], ['--debug', '-D',GetoptLong::NO_ARGUMENT] ) @@ -96,6 +100,7 @@ url = nil username = nil password = nil auth = nil +timeout = nil debug = false begin @@ -110,6 +115,8 @@ begin password = arg when '--url' url = arg + when '--timeout' + timeout = arg when '--debug' debug = true end @@ -120,7 +127,7 @@ end begin - occi_client = OCCIClient::Client.new(url,username,password,debug) + occi_client = OCCIClient::Client.new(url,username,password,timeout,debug) rescue Exception => e puts "#{cmd_name}: #{e.message}" exit(-1) diff --git a/src/cloud/occi/bin/occi-storage b/src/cloud/occi/bin/occi-storage index 3cb296a713..5f3b50325d 100755 --- a/src/cloud/occi/bin/occi-storage +++ b/src/cloud/occi/bin/occi-storage @@ -67,6 +67,9 @@ OPTIONS --url , -R : Set url as the web service url to use +--timeout , -T + Sets a timeout for the http connection + --debug, -D Enables verbosity @@ -88,6 +91,7 @@ opts = GetoptLong.new( ['--password', '-P',GetoptLong::REQUIRED_ARGUMENT], ['--url', '-R',GetoptLong::REQUIRED_ARGUMENT], ['--debug', '-D',GetoptLong::NO_ARGUMENT], + ['--timeout', '-T',GetoptLong::REQUIRED_ARGUMENT], ['--multipart', '-M',GetoptLong::NO_ARGUMENT] ) @@ -95,6 +99,7 @@ url = nil username = nil password = nil auth = nil +timeout = nil debug = false curb = true @@ -110,6 +115,8 @@ begin password = arg when '--url' url = arg + when '--timeout' + timeout = arg when '--debug' debug = true when '--multipart' @@ -128,7 +135,7 @@ end begin - occi_client = OCCIClient::Client.new(url,username,password,debug) + occi_client = OCCIClient::Client.new(url,username,password,timeout,debug) rescue Exception => e puts "#{cmd_name}: #{e.message}" exit(-1) diff --git a/src/cloud/occi/lib/OCCIClient.rb b/src/cloud/occi/lib/OCCIClient.rb index f54df2abb9..ab94afe059 100755 --- a/src/cloud/occi/lib/OCCIClient.rb +++ b/src/cloud/occi/lib/OCCIClient.rb @@ -33,8 +33,10 @@ module OCCIClient ###################################################################### # Initialize client library ###################################################################### - def initialize(endpoint_str=nil, user=nil, pass=nil, debug_flag=true) - @debug = debug_flag + def initialize(endpoint_str=nil, user=nil, pass=nil, + timeout=nil, debug_flag=true) + @debug = debug_flag + @timeout = timeout # Server location if endpoint_str @@ -78,7 +80,7 @@ module OCCIClient req.basic_auth @occiauth[0], @occiauth[1] - res = CloudClient::http_start(url) do |http| + res = CloudClient::http_start(url, @timeout) do |http| http.request(req) end @@ -98,7 +100,7 @@ module OCCIClient req.basic_auth @occiauth[0], @occiauth[1] - res = CloudClient::http_start(url) {|http| + res = CloudClient::http_start(url, @timeout) {|http| http.request(req) } @@ -123,7 +125,7 @@ module OCCIClient req.basic_auth @occiauth[0], @occiauth[1] - res = CloudClient::http_start(url) do |http| + res = CloudClient::http_start(url, @timeout) do |http| http.request(req) end @@ -143,7 +145,7 @@ module OCCIClient req.basic_auth @occiauth[0], @occiauth[1] - res = CloudClient::http_start(url) {|http| + res = CloudClient::http_start(url, @timeout) {|http| http.request(req) } @@ -203,7 +205,7 @@ module OCCIClient req.basic_auth @occiauth[0], @occiauth[1] - res = CloudClient::http_start(url) do |http| + res = CloudClient::http_start(url, @timeout) do |http| http.request(req) end @@ -226,7 +228,7 @@ module OCCIClient req.basic_auth @occiauth[0], @occiauth[1] - res = CloudClient::http_start(url) {|http| + res = CloudClient::http_start(url, @timeout) {|http| http.request(req) } @@ -250,7 +252,7 @@ module OCCIClient req.basic_auth @occiauth[0], @occiauth[1] - res = CloudClient::http_start(url) {|http| + res = CloudClient::http_start(url, @timeout) {|http| http.request(req) } @@ -276,7 +278,7 @@ module OCCIClient req.basic_auth @occiauth[0], @occiauth[1] - res = CloudClient::http_start(url) do |http| + res = CloudClient::http_start(url, @timeout) do |http| http.request(req) end @@ -296,7 +298,7 @@ module OCCIClient req.basic_auth @occiauth[0], @occiauth[1] - res = CloudClient::http_start(url) {|http| + res = CloudClient::http_start(url, @timeout) {|http| http.request(req) } @@ -317,7 +319,7 @@ module OCCIClient req.basic_auth @occiauth[0], @occiauth[1] - res = CloudClient::http_start(url) {|http| + res = CloudClient::http_start(url, @timeout) {|http| http.request(req) } @@ -337,7 +339,7 @@ module OCCIClient req.basic_auth @occiauth[0], @occiauth[1] - res = CloudClient::http_start(url) {|http| + res = CloudClient::http_start(url, @timeout) {|http| http.request(req) } @@ -358,7 +360,7 @@ module OCCIClient req.basic_auth @occiauth[0], @occiauth[1] - res = CloudClient::http_start(url) {|http| + res = CloudClient::http_start(url, @timeout) {|http| http.request(req) }