incubator-deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core] Error reporting from backend (rev 2)
Date Thu, 29 Jul 2010 09:00:10 GMT
---
 server/lib/deltacloud/base_driver/base_driver.rb   |   19 +++++
 server/lib/deltacloud/drivers/ec2/ec2_driver.rb    |   47 ++++++------
 .../lib/deltacloud/drivers/gogrid/gogrid_driver.rb |    2 +-
 .../drivers/rackspace/rackspace_driver.rb          |   66 +++++++++++------
 .../drivers/rimuhosting/rimuhosting_driver.rb      |   74 ++++++++++++-------
 .../drivers/terremark/terremark_driver.rb          |   80 +++++++++++++-------
 server/views/errors/backend_error.html.haml        |    4 +-
 7 files changed, 190 insertions(+), 102 deletions(-)

diff --git a/server/lib/deltacloud/base_driver/base_driver.rb b/server/lib/deltacloud/base_driver/base_driver.rb
index e4b5a7b..1dd158a 100644
--- a/server/lib/deltacloud/base_driver/base_driver.rb
+++ b/server/lib/deltacloud/base_driver/base_driver.rb
@@ -195,6 +195,25 @@ module Deltacloud
       return false
     end
 
+    def catched_exceptions_list
+      { :error => [], :auth => [], :glob => [] }
+    end
+
+    def safely(&block)
+      begin
+        block.call
+      rescue *catched_exceptions_list[:error] => e
+        raise Deltacloud::BackendError.new(502, e.class.to_s, e.message, e.backtrace)
+      rescue *catched_exceptions_list[:auth] => e
+        raise Deltacloud::AuthException.new
+      rescue => e
+        catched_exceptions_list[:glob].each do |ex|
+          raise Deltacloud::BackendError.new(502, e.class.to_s, e.message, e.backtrace) if
e.class.name =~ ex
+        end
+        raise e
+      end
+    end
+
   end
 
 end
diff --git a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
index 890caa7..ed3f7f0 100644
--- a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
+++ b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
@@ -166,22 +166,23 @@ class EC2Driver < Deltacloud::BaseDriver
     instances
   end
 
-
-  def create_instance(credentials, image_id, opts)
+ def create_instance(credentials, image_id, opts)
     ec2 = new_client( credentials )
     realm_id = opts[:realm_id]
-    image = image(credentials, :id => image_id )
-    hwp = find_hardware_profile(credentials, opts[:hwp_id], image.id)
-    ec2_instances = ec2.run_instances(
-      :image_id => image.id,
-      :user_data => opts[:user_data],
-      :key_name => opts[:keyname],
-      :availability_zone => realm_id,
-      :monitoring_enabled => true,
-      :instance_type => hwp.name,
-      :disable_api_termination => false,
-      :instance_initiated_shutdown_behavior => 'terminate'
-    )
+    safely do
+      image = image(credentials, :id => image_id )
+      hwp = find_hardware_profile(credentials, opts[:hwp_id], image.id)
+      ec2_instances = ec2.run_instances(
+        :image_id => image.id,
+        :user_data => opts[:user_data],
+        :key_name => opts[:keyname],
+        :availability_zone => realm_id,
+        :monitoring_enabled => true,
+        :instance_type => hwp.name,
+        :disable_api_termination => false,
+        :instance_initiated_shutdown_behavior => 'terminate'
+      )
+    end
     convert_instance( ec2_instances.instancesSet.item.first, credentials.user )
   end
 
@@ -278,7 +279,9 @@ class EC2Driver < Deltacloud::BaseDriver
       :secret_access_key => credentials.password
     }
     opts[:server] = ENV['DCLOUD_EC2_URL'] if ENV['DCLOUD_EC2_URL']
-    AWS::EC2::Base.new(opts)
+    safely do
+      AWS::EC2::Base.new(opts)
+    end
   end
 
   def convert_image(ec2_image)
@@ -344,14 +347,12 @@ class EC2Driver < Deltacloud::BaseDriver
     } )
   end
 
-  def safely(&block)
-    begin
-      block.call
-    rescue AWS::AuthFailure => e
-        raise Deltacloud::AuthException.new
-    rescue Exception => e
-        puts "ERROR: #{e.message}\n#{e.backtrace.join("\n")}"
-    end
+  def catched_exceptions_list
+    {
+      :auth => [ AWS::AuthFailure ],
+      :error => [],
+      :glob => [ /AWS::(\w+)/ ]
+    }
   end
 
 end
diff --git a/server/lib/deltacloud/drivers/gogrid/gogrid_driver.rb b/server/lib/deltacloud/drivers/gogrid/gogrid_driver.rb
index e8d21ca..ff44b44 100644
--- a/server/lib/deltacloud/drivers/gogrid/gogrid_driver.rb
+++ b/server/lib/deltacloud/drivers/gogrid/gogrid_driver.rb
@@ -293,7 +293,7 @@ class GogridDriver < Deltacloud::BaseDriver
     begin
       block.call
     rescue Exception => e
-      puts "ERROR: #{e.message}"
+      raise Deltacloud::BackendError.new(500, e.class.to_s, e.message, e.backtrace)
     end
   end
 
diff --git a/server/lib/deltacloud/drivers/rackspace/rackspace_driver.rb b/server/lib/deltacloud/drivers/rackspace/rackspace_driver.rb
index 6a9c8d2..0dc1733 100644
--- a/server/lib/deltacloud/drivers/rackspace/rackspace_driver.rb
+++ b/server/lib/deltacloud/drivers/rackspace/rackspace_driver.rb
@@ -29,11 +29,13 @@ class RackspaceDriver < Deltacloud::BaseDriver
 
   def hardware_profiles(credentials, opts = nil)
     racks = new_client( credentials )
-    results = racks.list_flavors.map do |flav|
-      HardwareProfile.new(flav["id"].to_s) do
-        architecture 'x86_64'
-        memory flav["ram"].to_i
-        storage flav["disk"].to_i
+    safely do
+      results = racks.list_flavors.map do |flav|
+        HardwareProfile.new(flav["id"].to_s) do
+          architecture 'x86_64'
+          memory flav["ram"].to_i
+          storage flav["disk"].to_i
+        end
       end
     end
     filter_hardware_profiles(results, opts)
@@ -41,14 +43,16 @@ class RackspaceDriver < Deltacloud::BaseDriver
 
   def images(credentials, opts=nil)
     racks = new_client( credentials )
-    results = racks.list_images.map do |img|
-      Image.new( {
-                   :id=>img["id"].to_s,
-                   :name=>img["name"],
-                   :description => img["name"] + " " + img["status"] + "",
-                   :owner_id=>"root",
-                   :architecture=>'x86_64'
-                 } )
+    safely do
+      results = racks.list_images.map do |img|
+        Image.new( {
+                     :id=>img["id"].to_s,
+                     :name=>img["name"],
+                     :description => img["name"] + " " + img["status"] + "",
+                     :owner_id=>"root",
+                     :architecture=>'x86_64'
+                   } )
+      end
     end
     results.sort_by{|e| [e.description]}
     results = filter_on( results, :id, opts )
@@ -66,7 +70,9 @@ class RackspaceDriver < Deltacloud::BaseDriver
 
   def reboot_instance(credentials, id)
     racks = new_client(credentials)
-    racks.reboot_server(id)
+    safely do
+      racks.reboot_server(id)
+    end
   end
 
   def stop_instance(credentials, id)
@@ -75,7 +81,9 @@ class RackspaceDriver < Deltacloud::BaseDriver
 
   def destroy_instance(credentials, id)
     racks = new_client(credentials)
-    racks.delete_server(id)
+    safely do
+      racks.delete_server(id)
+    end
   end
 
 
@@ -88,7 +96,9 @@ class RackspaceDriver < Deltacloud::BaseDriver
     hwp_id = opts[:hwp_id] || 1
     name = Time.now.to_s
     if (opts[:name]) then name = opts[:name] end
-    convert_srv_to_instance(racks.start_server(image_id, hwp_id, name))
+    safely do
+      return convert_srv_to_instance(racks.start_server(image_id, hwp_id, name))
+    end
   end
 
   #
@@ -97,12 +107,14 @@ class RackspaceDriver < Deltacloud::BaseDriver
   def instances(credentials, opts=nil)
     racks = new_client(credentials)
     instances = []
-    if (opts.nil?)
-      instances = racks.list_servers.map do |srv|
-        convert_srv_to_instance(srv)
+    safely do
+      if (opts.nil?)
+        instances = racks.list_servers.map do |srv|
+          convert_srv_to_instance(srv)
+        end
+      else
+        instances << convert_srv_to_instance(racks.load_server_details(opts[:id]))
       end
-    else
-      instances << convert_srv_to_instance(racks.load_server_details(opts[:id]))
     end
     instances = filter_on( instances, :id, opts )
     instances = filter_on( instances, :state, opts )
@@ -128,7 +140,9 @@ class RackspaceDriver < Deltacloud::BaseDriver
   end
 
   def new_client(credentials)
-    RackspaceClient.new(credentials.user, credentials.password)
+    safely do
+      return RackspaceClient.new(credentials.user, credentials.password)
+    end
   end
 
   define_instance_states do
@@ -144,6 +158,14 @@ class RackspaceDriver < Deltacloud::BaseDriver
     stopped.to( :finish )         .automatically
   end
 
+  def safely(&block)
+    begin
+      block.call
+    rescue Exception => e
+      raise Deltacloud::BackendError.new(500, e.class.to_s, e.message, e.backtrace)
+    end
+  end
+
 end
 
     end
diff --git a/server/lib/deltacloud/drivers/rimuhosting/rimuhosting_driver.rb b/server/lib/deltacloud/drivers/rimuhosting/rimuhosting_driver.rb
index 6b0c8a9..f141506 100755
--- a/server/lib/deltacloud/drivers/rimuhosting/rimuhosting_driver.rb
+++ b/server/lib/deltacloud/drivers/rimuhosting/rimuhosting_driver.rb
@@ -29,15 +29,17 @@ class RimuHostingDriver < Deltacloud::BaseDriver
   feature :instances, :user_name
 
   def images(credentails, opts=nil)
-    rh = RimuHostingClient.new(credentails)
-    images = rh.list_images.map do | image |
-      Image.new({
-              :id => image["distro_code"].gsub(/\./,"-"),
-              :name => image["distro_code"],
-              :description => image["distro_description"],
-              :owner_id => "root",
-              :architecture => "x86"
-      })
+    safely do
+      rh = RimuHostingClient.new(credentails)
+      images = rh.list_images.map do | image |
+        Image.new({
+                :id => image["distro_code"].gsub(/\./,"-"),
+                :name => image["distro_code"],
+                :description => image["distro_description"],
+                :owner_id => "root",
+                :architecture => "x86"
+        })
+      end
     end
     images.sort_by{|e| [e.description]}
     images = filter_on( images, :id, opts)
@@ -45,15 +47,17 @@ class RimuHostingDriver < Deltacloud::BaseDriver
   end
 
   def hardware_profiles(credentials, opts = nil)
-    rh = RimuHostingClient.new(credentials)
-    results = rh.list_plans.map do |plan|
-      # FIXME: x86 is not a valid architecture; what is Rimu offering ?
-      # FIXME: VPS plans offer a range of memory/storage, but that's
-      #        not contained in hte pricing_plan_infos
-      HardwareProfile.new(plan["pricing_plan_code"]) do
-        memory plan["minimum_memory_mb"].to_f
-        storage plan["minimum_disk_gb"].to_i
-        architecture "x86"
+    safely do
+      rh = RimuHostingClient.new(credentials)
+      results = rh.list_plans.map do |plan|
+        # FIXME: x86 is not a valid architecture; what is Rimu offering ?
+        # FIXME: VPS plans offer a range of memory/storage, but that's
+        #        not contained in hte pricing_plan_infos
+        HardwareProfile.new(plan["pricing_plan_code"]) do
+          memory plan["minimum_memory_mb"].to_f
+          storage plan["minimum_disk_gb"].to_i
+          architecture "x86"
+        end
       end
     end
     filter_hardware_profiles(results, opts)
@@ -68,9 +72,11 @@ class RimuHostingDriver < Deltacloud::BaseDriver
   end
 
   def instances(credentials, opts=nil)
-     rh = RimuHostingClient.new(credentials)
-    instances = rh.list_nodes.map do | inst |
-      convert_srv_to_instance(inst)
+    safely do
+      rh = RimuHostingClient.new(credentials)
+      instances = rh.list_nodes.map do | inst |
+        convert_srv_to_instance(inst)
+      end
     end
     instances = filter_on( instances, :id, opts)
     instances = filter_on( instances, :state, opts )
@@ -78,13 +84,17 @@ class RimuHostingDriver < Deltacloud::BaseDriver
   end
 
   def reboot_instance(credentials, id)
-     rh = RimuHostingClient.new(credentials)
-    rh.set_server_state(id, :RESTARTING)
+    safely do
+      rh = RimuHostingClient.new(credentials)
+      rh.set_server_state(id, :RESTARTING)
+    end
   end
 
   def start_instance(credentials, id)
-    rh = RimuHostingClient.new(credentials)
-    rh.set_server_state(id, :STARTED)
+    safely do
+      rh = RimuHostingClient.new(credentials)
+      rh.set_server_state(id, :STARTED)
+    end
   end
 
   def stop_instance(credentials, id)
@@ -92,8 +102,10 @@ class RimuHostingDriver < Deltacloud::BaseDriver
   end
 
   def destroy_instance(credentials, id)
-     rh = RimuHostingClient.new(credentials)
-    rh.delete_server(id)
+    safely do
+      rh = RimuHostingClient.new(credentials)
+      return rh.delete_server(id)
+    end
   end
 
   def create_instance(credentials, image_id, opts)
@@ -138,6 +150,14 @@ class RimuHostingDriver < Deltacloud::BaseDriver
     stopped.to( :finish )         .automatically
   end
 
+  def safely(&block)
+    begin
+      block.call
+    rescue Exception => e
+      raise Deltacloud::BackendError.new(500, e.class.to_s, e.message, e.backtrace)
+    end
+  end
+
 
 end
 
diff --git a/server/lib/deltacloud/drivers/terremark/terremark_driver.rb b/server/lib/deltacloud/drivers/terremark/terremark_driver.rb
index 5f5fbe3..ddf548f 100644
--- a/server/lib/deltacloud/drivers/terremark/terremark_driver.rb
+++ b/server/lib/deltacloud/drivers/terremark/terremark_driver.rb
@@ -57,15 +57,17 @@ VAPP_STATE_MAP = { "0" =>  "PENDING", "1" =>  "PENDING", "2" =>
 "STOPPED", "4"
   def images(credentials, opts=nil)
       image_list = []
       terremark_client = new_client(credentials)
-      vdc_id = terremark_client.default_vdc_id
-      catalogItems = terremark_client.get_catalog(vdc_id).body['CatalogItems']
-      catalogItems.each{ |catalog_item|
-        current_item_id = catalog_item['href'].split('/').last
-        current_item = terremark_client.get_catalog_item(current_item_id).body['Entity']
-          if(current_item['type'] == 'application/vnd.vmware.vcloud.vAppTemplate+xml')
-            image_list << convert_image(current_item, credentials.user)
-          end
-      } #end of catalogItems.each
+      safely do
+        vdc_id = terremark_client.default_vdc_id
+        catalogItems = terremark_client.get_catalog(vdc_id).body['CatalogItems']
+        catalogItems.each{ |catalog_item|
+          current_item_id = catalog_item['href'].split('/').last
+          current_item = terremark_client.get_catalog_item(current_item_id).body['Entity']
+            if(current_item['type'] == 'application/vnd.vmware.vcloud.vAppTemplate+xml')
+              image_list << convert_image(current_item, credentials.user)
+            end
+        } #end of catalogItems.each
+      end
       image_list = filter_on( image_list, :id, opts )
       image_list = filter_on( image_list, :architecture, opts )
       image_list = filter_on( image_list, :owner_id, opts )
@@ -91,14 +93,16 @@ VAPP_STATE_MAP = { "0" =>  "PENDING", "1" =>  "PENDING", "2" =>
 "STOPPED", "4"
   def instances(credentials, opts=nil)
       instances = []
       terremark_client = new_client(credentials)
-      vdc_items = terremark_client.get_vdc(terremark_client.default_vdc_id()).body['ResourceEntities']
-      vdc_items.each{|current_item|
-        if(current_item['type'] == 'application/vnd.vmware.vcloud.vApp+xml')
-           vapp_id =  current_item['href'].split('/').last
-           vapp = terremark_client.get_vapp(vapp_id)
-           instances  << convert_instance(vapp, terremark_client, credentials.user)
-        end
-      }#end vdc_items.each
+      safely do
+        vdc_items = terremark_client.get_vdc(terremark_client.default_vdc_id()).body['ResourceEntities']
+        vdc_items.each{|current_item|
+          if(current_item['type'] == 'application/vnd.vmware.vcloud.vApp+xml')
+             vapp_id =  current_item['href'].split('/').last
+             vapp = terremark_client.get_vapp(vapp_id)
+             instances  << convert_instance(vapp, terremark_client, credentials.user)
+          end
+        }#end vdc_items.each
+      end
       instances = filter_on( instances, :id, opts )
       instances
   end
@@ -135,37 +139,45 @@ VAPP_STATE_MAP = { "0" =>  "PENDING", "1" =>  "PENDING", "2" =>
 "STOPPED", "4"
     end
     vapp_opts['cpus'] = opts[:hwp_cpu]
     vapp_opts['memory'] =  opts[:hwp_memory]
-    terremark_client = new_client(credentials)
+    safely do
+      terremark_client = new_client(credentials)
 #######
 #FIXME#  what happens if there is an issue getting the new vapp id? (eg even though created
succesfully)
 #######
-    vapp_id = terremark_client.instantiate_vapp_template(name, image_id, vapp_opts).body['href'].split('/').last
-    new_vapp = terremark_client.get_vapp(vapp_id)
-    return convert_instance(new_vapp, terremark_client, credentials.user) #return an Instance
object
+      vapp_id = terremark_client.instantiate_vapp_template(name, image_id, vapp_opts).body['href'].split('/').last
+      new_vapp = terremark_client.get_vapp(vapp_id)
+      return convert_instance(new_vapp, terremark_client, credentials.user) #return an Instance
object
+    end
   end
 
 #--
 # REBOOT INSTANCE
 #--
   def reboot_instance(credentials, id)
-    terremark_client =  new_client(credentials)
-    terremark_client.power_reset(id)
+    safely do
+      terremark_client =  new_client(credentials)
+      return terremark_client.power_reset(id)
+    end
   end
 
 #--
 # START INSTANCE
 #--
 def start_instance(credentials, id)
+  safely do
     terremark_client =  new_client(credentials)
-    terremark_client.power_on(id)
+    return terremark_client.power_on(id)
+  end
 end
 
 #--
 # STOP INSTANCE
 #--
 def stop_instance(credentials, id)
+  safely do
     terremark_client = new_client(credentials)
-    terremark_client.power_shutdown(id)
+    return terremark_client.power_shutdown(id)
+  end
 end
 
 #--
@@ -173,8 +185,10 @@ end
 #--
 #shuts down... in terremark need to do a futher delete to get rid of a vapp entirely
 def destroy_instance(credentials, id)
+  safely do
     terremark_client = new_client(credentials)
-    terremark_client.delete_vapp(id)
+    return terremark_client.delete_vapp(id)
+  end
 end
 
 #--
@@ -246,14 +260,24 @@ end
   def new_client(credentials)
     #Fog constructor expecting  credentials[:terremark_password] and credentials[:terremark_username]
     terremark_credentials = {:terremark_vcloud_username => "#{credentials.user}", :terremark_vcloud_password
=> "#{credentials.password}" }
-    terremark_client = Fog::Terremark::Vcloud.new(terremark_credentials)
-    vdc_id = terremark_client.default_vdc_id
+    safely do
+      terremark_client = Fog::Terremark::Vcloud.new(terremark_credentials)
+      vdc_id = terremark_client.default_vdc_id
+    end
     if (vdc_id.nil?)
        raise DeltaCloud::AuthException.new
     end
     terremark_client
   end
 
+  def safely(&block)
+    begin
+      block.call
+    rescue Exception => e
+      raise Deltacloud::BackendError.new(500, e.class.to_s, e.message, e.backtrace)
+    end
+  end
+
 
 end
 
diff --git a/server/views/errors/backend_error.html.haml b/server/views/errors/backend_error.html.haml
index a65ff34..3c70754 100644
--- a/server/views/errors/backend_error.html.haml
+++ b/server/views/errors/backend_error.html.haml
@@ -14,4 +14,6 @@
     %dd= @error.cause
   %di
     %dt Details
-    %dd= @error.details
+    %dd
+      %pre
+        =@error.details.join("\n")
-- 
1.7.2


Mime
View raw message