You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltacloud.apache.org by lu...@apache.org on 2010/07/09 01:46:09 UTC

svn commit: r962321 - in /incubator/deltacloud/trunk/server: ./ lib/deltacloud/base_driver/ lib/deltacloud/drivers/ec2/ lib/deltacloud/drivers/gogrid/ lib/deltacloud/helpers/ views/instances/

Author: lutter
Date: Thu Jul  8 23:46:09 2010
New Revision: 962321

URL: http://svn.apache.org/viewvc?rev=962321&view=rev
Log:
Added authentication features to EC2 and GoGrid drivers

Modified:
    incubator/deltacloud/trunk/server/lib/deltacloud/base_driver/features.rb
    incubator/deltacloud/trunk/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
    incubator/deltacloud/trunk/server/lib/deltacloud/drivers/gogrid/gogrid_driver.rb
    incubator/deltacloud/trunk/server/lib/deltacloud/helpers/application_helper.rb
    incubator/deltacloud/trunk/server/server.rb
    incubator/deltacloud/trunk/server/views/instances/new.html.haml
    incubator/deltacloud/trunk/server/views/instances/show.xml.haml

Modified: incubator/deltacloud/trunk/server/lib/deltacloud/base_driver/features.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/deltacloud/base_driver/features.rb?rev=962321&r1=962320&r2=962321&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/deltacloud/base_driver/features.rb (original)
+++ incubator/deltacloud/trunk/server/lib/deltacloud/base_driver/features.rb Thu Jul  8 23:46:09 2010
@@ -136,6 +136,21 @@ module Deltacloud
       end
     end
 
+    declare_feature :instances, :authentication_key do
+      operation :create do
+        param :keyname, :string,  :optional, nil
+        "EC2 key authentification method"
+      end
+      operation :show do
+      end
+    end
+
+    declare_feature :instances, :authentication_password do
+      operation :create do
+        param :password, :string, :optional
+      end
+    end
+
     declare_feature :instances, :hardware_profiles do
       description "Size instances according to changes to a hardware profile"
       # The parameters are filled in from the hardware profiles

Modified: incubator/deltacloud/trunk/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/deltacloud/drivers/ec2/ec2_driver.rb?rev=962321&r1=962320&r2=962321&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/deltacloud/drivers/ec2/ec2_driver.rb (original)
+++ incubator/deltacloud/trunk/server/lib/deltacloud/drivers/ec2/ec2_driver.rb Thu Jul  8 23:46:09 2010
@@ -19,12 +19,23 @@
 require 'deltacloud/base_driver'
 require 'AWS'
 
+class Instance
+  attr_accessor :keyname
+  attr_accessor :authn_error
+
+  def authn_feature_failed?
+    return true unless authn_error.nil?
+  end
+
+end
+
 module Deltacloud
   module Drivers
     module EC2
 class EC2Driver < Deltacloud::BaseDriver
 
   feature :instances, :user_data
+  feature :instances, :authentication_key
 
   define_hardware_profile('m1-small') do
     cpu              1
@@ -156,7 +167,7 @@ class EC2Driver < Deltacloud::BaseDriver
     ec2_instances = ec2.run_instances(
       :image_id => image.id,
       :user_data => opts[:user_data],
-      :key_name => opts[:key_name],
+      :key_name => opts[:keyname],
       :availability_zone => realm_id,
       :monitoring_enabled => true,
       :instance_type => hwp.name.tr('-', '.'),
@@ -267,7 +278,7 @@ class EC2Driver < Deltacloud::BaseDriver
     realm_id = ec2_instance['placement']['availabilityZone']
     (realm_id = nil ) if ( realm_id == '' )
     hwp_name = ec2_instance['instanceType'].gsub( /\./, '-')
-    Instance.new( {
+    instance = Instance.new( {
       :id=>ec2_instance['instanceId'],
       :name => ec2_instance['imageId'],
       :state=>state,
@@ -279,7 +290,10 @@ class EC2Driver < Deltacloud::BaseDriver
       :flavor_id=>ec2_instance['instanceType'].gsub( /\./, '-'),
       :instance_profile =>InstanceProfile.new(hwp_name),
       :actions=>instance_actions_for( state ),
+      :keyname => ec2_instance['keyName']
     } )
+    instance.authn_error = "Key not set for instance" unless ec2_instance['keyName']
+    return instance
   end
 
   def convert_volume(ec2_volume)

Modified: incubator/deltacloud/trunk/server/lib/deltacloud/drivers/gogrid/gogrid_driver.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/deltacloud/drivers/gogrid/gogrid_driver.rb?rev=962321&r1=962320&r2=962321&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/deltacloud/drivers/gogrid/gogrid_driver.rb (original)
+++ incubator/deltacloud/trunk/server/lib/deltacloud/drivers/gogrid/gogrid_driver.rb Thu Jul  8 23:46:09 2010
@@ -18,12 +18,24 @@
 require 'deltacloud/base_driver'
 require 'deltacloud/drivers/gogrid/gogrid_client'
 
+class Instance
+  attr_accessor :username
+  attr_accessor :password
+  attr_accessor :authn_error
+
+  def authn_feature_failed?
+    return true unless auth_error.nil?
+  end
+end
+
 module Deltacloud
   module Drivers
     module Gogrid
 
 class GogridDriver < Deltacloud::BaseDriver
 
+  feature :instances, :authentication_password
+
   define_hardware_profile 'server' do
     cpu            2
     memory         [512, 1024, 2048, 4096, 8192]
@@ -76,14 +88,29 @@ class GogridDriver < Deltacloud::BaseDri
     else
       server_ram = "512MB"
     end
+    client = new_client(credentials)
     name = (opts[:name] && opts[:name]!='') ? opts[:name] : get_random_instance_name
     safely do
-      convert_instance(new_client(credentials).request('grid/server/add', {
+      instance = client.request('grid/server/add', {
         'name' => name,
         'image' => image_id,
         'server.ram' => server_ram,
         'ip' => get_next_free_ip(credentials)
-      })['list'].first, credentials.user)
+      })['list'].first
+      if instance
+        login_data = get_login_data(client, instance[:id])
+        if login_data['username'] and login_data['password']
+          instance['username'] = login_data['username']
+          instance['password'] = login_data['password']
+          inst = convert_instance(instance, credentials.user)
+        else
+          inst = convert_instance(instance, credentials.user)
+          inst.authn_error = "Unable to fetch password"
+        end
+        return inst
+      else
+        return nil
+      end
     end
   end
 
@@ -91,8 +118,18 @@ class GogridDriver < Deltacloud::BaseDri
     instances = []
     if opts and opts[:id]
       safely do
-        instance = new_client(credentials).request('grid/server/get', { 'id' => opts[:id]})['list'].first
-        instances = [convert_instance(instance, credentials.user)]
+        client = new_client(credentials)
+        instance = client.request('grid/server/get', { 'id' => opts[:id] })['list'].first
+        login_data = get_login_data(client, instance['id'])
+        if login_data['username'] and login_data['password']
+          instance['username'] = login_data['username']
+          instance['password'] = login_data['password']
+          inst = convert_instance(instance, credentials.user)
+        else
+          inst = convert_instance(instance, credentials.user)
+          inst.authn_error = "Unable to fetch password"
+        end
+        instances = [inst]
       end
     else
       safely do
@@ -111,15 +148,21 @@ class GogridDriver < Deltacloud::BaseDri
     end
   end
 
+  def destroy_instance(credentials, id)
+    safely do
+      new_client(credentials).request('grid/server/delete', { 'id' => id})
+    end
+  end
+
   def stop_instance(credentials, id)
     safely do
       new_client(credentials).request('grid/server/power', { 'id' => id, 'power' => 'off'})
     end
   end
 
-  def destroy_instance(credentials, id)
+  def start_instance(credentials, id)
     safely do
-      new_client(credentials).request('grid/server/delete', { 'id' => id})
+      new_client(credentials).request('grid/server/power', { 'id' => id, 'power' => 'on'})
     end
   end
 
@@ -128,7 +171,8 @@ class GogridDriver < Deltacloud::BaseDri
     pending.to( :running )       .automatically
     running.to( :stopped )       .on( :stop )
     stopped.to( :running )       .on( :start )
-    stopped.to( :finish )        .automatically
+    running.to( :finish )       .on( :destroy )
+    stopped.to( :finish )       .on( :destroy )
   end
 
   private
@@ -137,6 +181,22 @@ class GogridDriver < Deltacloud::BaseDri
     GoGridClient.new('https://api.gogrid.com/api', credentials.user, credentials.password)
   end
 
+  def get_login_data(client, instance_id)
+    login_data = {}
+    begin
+      client.request('support/password/list')['list'].each do |passwd|
+        next unless passwd['server']
+        if passwd['server']['id'] == instance_id
+          login_data['username'], login_data['password'] = passwd['username'], passwd['password']
+          break
+        end
+      end
+    rescue Exception => e
+      login_data[:error] = e.message
+    end
+    return login_data
+  end
+
   def convert_image(gg_image, owner_id=nil)
     Image.new( {
       :id=>gg_image['id'],
@@ -190,7 +250,7 @@ class GogridDriver < Deltacloud::BaseDri
     prof = InstanceProfile.new("server", opts)
 
     Instance.new(
-      :id => instance['name'],
+      :id => instance['id'],
       :owner_id => owner_id,
       :image_id => instance['image']['id'],
       :flavor_id => instance['ram']['id'],
@@ -200,7 +260,9 @@ class GogridDriver < Deltacloud::BaseDri
       :state => convert_server_state(instance['state']['name'], instance['id']),
       :actions => instance_actions_for(convert_server_state(instance['state']['name'], instance['id'])),
       :public_addresses => [ instance['ip']['ip'] ],
-      :private_addresses => []
+      :private_addresses => [],
+      :username => instance['username'],
+      :password => instance['password']
     )
   end
 

Modified: incubator/deltacloud/trunk/server/lib/deltacloud/helpers/application_helper.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/deltacloud/helpers/application_helper.rb?rev=962321&r1=962320&r2=962321&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/deltacloud/helpers/application_helper.rb (original)
+++ incubator/deltacloud/trunk/server/lib/deltacloud/helpers/application_helper.rb Thu Jul  8 23:46:09 2010
@@ -44,4 +44,13 @@ module ApplicationHelper
     not driver.features(:instances).select{ |f| f.name.eql?(feature_name) }.empty?
   end
 
+  def driver_has_auth_features?
+    driver_has_feature?(:authentication_password) || driver_has_feature?(:authentication_key)
+  end
+
+  def driver_auth_feature_name
+    return 'key' if driver_has_feature?(:authentication_key)
+    return 'password' if driver_has_feature?(:authentication_password)
+  end
+
 end

Modified: incubator/deltacloud/trunk/server/server.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/server.rb?rev=962321&r1=962320&r2=962321&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/server.rb (original)
+++ incubator/deltacloud/trunk/server/server.rb Thu Jul  8 23:46:09 2010
@@ -256,7 +256,10 @@ collection :instances do
           @instance = instance
           haml :"instances/show"
         end
-        format.html { redirect instance_url(instance.id) }
+        format.html do
+          redirect instance_url(instance.id) if instance and instance.id
+          redirect instances_url
+        end
       end
     end
   end

Modified: incubator/deltacloud/trunk/server/views/instances/new.html.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/views/instances/new.html.haml?rev=962321&r1=962320&r2=962321&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/views/instances/new.html.haml (original)
+++ incubator/deltacloud/trunk/server/views/instances/new.html.haml Thu Jul  8 23:46:09 2010
@@ -5,9 +5,15 @@
 
 %form{ :action => instances_url, :method => :post }
   %input{ :name => :image_id, :type => :hidden, :value => @instance.image_id }/
-  %label
-    Instance Name:
-  %input{ :name => 'name', :size => 30 }/
+  %p
+    %label
+      Instance Name:
+      %input{ :name => 'name', :size => 30 }/
+  -if driver_has_feature?(:authentication_key)
+    %p
+      %label
+        Instance Keyname:
+        %input{:name => 'keyname', :size => 30 }
   - if !@hardware_profiles.empty?
     %h3 What size machine?
     - for hwp in @hardware_profiles

Modified: incubator/deltacloud/trunk/server/views/instances/show.xml.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/views/instances/show.xml.haml?rev=962321&r1=962320&r2=962321&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/views/instances/show.xml.haml (original)
+++ incubator/deltacloud/trunk/server/views/instances/show.xml.haml Thu Jul  8 23:46:09 2010
@@ -26,3 +26,16 @@
     - @instance.private_addresses.each do |address|
       %address<
         =address
+  - if driver_has_auth_features?
+    %authentication{ :type => driver_auth_feature_name }
+      - if @instance.authn_feature_failed?
+        %error  #{@instance.auth_error}
+      - else
+        - if driver_auth_feature_name == 'password'
+          %login
+            %username #{@instance.username}
+            %password #{@instance.password}
+        - if driver_auth_feature_name == 'key'
+          %login
+            %keyname #{@instance.keyname}
+