[PATCH aeolus-cli 2/2] BZ#749376: Handle conductor being down/configuration errors

mzatko at redhat.com mzatko at redhat.com
Fri Dec 16 18:06:56 UTC 2011


From: Maros Zatko <mzatko at redhat.com>

---
 lib/aeolus_cli/command/config_parser.rb |   41 +++++++++++++++++++++++++++++++
 1 files changed, 41 insertions(+), 0 deletions(-)

diff --git a/lib/aeolus_cli/command/config_parser.rb b/lib/aeolus_cli/command/config_parser.rb
index 2be3e61..1968238 100644
--- a/lib/aeolus_cli/command/config_parser.rb
+++ b/lib/aeolus_cli/command/config_parser.rb
@@ -14,6 +14,7 @@
 
 require 'optparse'
 require 'logger'
+require 'rest_client'
 
 module Aeolus
   module CLI
@@ -35,6 +36,13 @@ module Aeolus
         # File.expand_path("~")
         if COMMANDS.include?(@command)
           parse(@command)
+
+          if not ping_conductor
+            puts "ERROR: Can't connect to Conductor instance. \
+Either Conductor is not running or there is incorrect URL/credentials in configuration file (~/.aeolus-cli)."
+            exit(1)
+          end
+
           self.send(@command.to_sym)
         else
           @args << "-h"
@@ -231,6 +239,39 @@ module Aeolus
         end
       end
 
+      def ping_conductor
+        bc = BaseCommand.new
+        config = bc.instance_variable_get(:@config)
+
+        url = config[:conductor][:url]
+        usr = config[:conductor][:username]
+        pwd = config[:conductor][:password]
+
+        begin
+          resp = RestClient::Request.new(
+            :method => :get,
+            :url => url,
+            :user => usr,
+            :password => pwd
+          ).execute
+
+        rescue SocketError,
+               RestClient::ResourceNotFound,
+               OpenSSL::SSL::SSLError,
+               RestClient::Request::Unauthorized,
+               RestClient::ServiceUnavailable,
+               Errno::ECONNREFUSED => e
+          puts "ERROR: " + e.message
+          return false
+
+        rescue => e
+          puts "ERROR: " + e.message
+          raise e
+        end
+
+        return true
+      end
+
       # TODO: Remove all this boilerplate and replace with some metaprogramming,
       # perhaps method_missing
       def list
-- 
1.7.7.4




More information about the aeolus-devel mailing list