incubator-deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lut...@apache.org
Subject svn commit: r962270 - in /incubator/deltacloud/trunk/server: features/support/ features/support/gogrid/ features/support/gogrid/fixtures/ lib/deltacloud/drivers/gogrid/
Date Thu, 08 Jul 2010 23:42:24 GMT
Author: lutter
Date: Thu Jul  8 23:42:24 2010
New Revision: 962270

URL: http://svn.apache.org/viewvc?rev=962270&view=rev
Log:
Added GoGrid driver

Added:
    incubator/deltacloud/trunk/server/features/support/gogrid/
    incubator/deltacloud/trunk/server/features/support/gogrid/config.yaml
    incubator/deltacloud/trunk/server/features/support/gogrid/fixtures/
    incubator/deltacloud/trunk/server/features/support/gogrid/fixtures/flavors.yaml
    incubator/deltacloud/trunk/server/features/support/gogrid/fixtures/images.yaml
    incubator/deltacloud/trunk/server/features/support/gogrid/fixtures/instances.yaml
    incubator/deltacloud/trunk/server/features/support/gogrid/fixtures/ips.yaml
    incubator/deltacloud/trunk/server/features/support/gogrid/servers.yaml
    incubator/deltacloud/trunk/server/lib/deltacloud/drivers/gogrid/
    incubator/deltacloud/trunk/server/lib/deltacloud/drivers/gogrid/gogrid_client.rb
    incubator/deltacloud/trunk/server/lib/deltacloud/drivers/gogrid/gogrid_driver.rb
Modified:
    incubator/deltacloud/trunk/server/features/support/env.rb

Modified: incubator/deltacloud/trunk/server/features/support/env.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/features/support/env.rb?rev=962270&r1=962269&r2=962270&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/features/support/env.rb (original)
+++ incubator/deltacloud/trunk/server/features/support/env.rb Thu Jul  8 23:42:24 2010
@@ -14,6 +14,7 @@ World do
   def app
     @app = Rack::Builder.new do
       set :logging, true
+      set :raise_errors, true
       run Sinatra::Application
     end
  end

Added: incubator/deltacloud/trunk/server/features/support/gogrid/config.yaml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/features/support/gogrid/config.yaml?rev=962270&view=auto
==============================================================================
--- incubator/deltacloud/trunk/server/features/support/gogrid/config.yaml (added)
+++ incubator/deltacloud/trunk/server/features/support/gogrid/config.yaml Thu Jul  8 23:42:24
2010
@@ -0,0 +1,26 @@
+--- 
+:username: mockuser
+:password: mockpassword
+:storage_snapshot_id: snap-72a5401b
+:driver_name: gogrid
+:instances_count: 2
+:flavor_arch: i386
+:storage_snapshot_state: AVAILABLE
+:realm_id: us
+:instance_1_name: ami-e4b6538d
+:image_owner: "mockuser"
+:storage_snapshot_count: "2"
+:realm_state: AVAILABLE
+:instance_2_name: ami-e4b6538d
+:image_arch: i386
+:storage_volume_id: vol-60957009
+:storage_volume_instance: i-123f1234
+:realm_count: 1
+:instance_image_id: 1
+:image_id: 1
+:storage_volume_state: AVAILABLE
+:flavor_id: server-with-512mb-ram
+:instance_realm: us
+:image_count: 2
+:storage_volume_count: 0
+:flavor_count: 3

Added: incubator/deltacloud/trunk/server/features/support/gogrid/fixtures/flavors.yaml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/features/support/gogrid/fixtures/flavors.yaml?rev=962270&view=auto
==============================================================================
--- incubator/deltacloud/trunk/server/features/support/gogrid/fixtures/flavors.yaml (added)
+++ incubator/deltacloud/trunk/server/features/support/gogrid/fixtures/flavors.yaml Thu Jul
 8 23:42:24 2010
@@ -0,0 +1,7 @@
+---
+- name:  512MB
+  description: Server with 512MB RAM
+- name:  1GB
+  description: Server with 1GB RAM
+- name:  2GB
+  description: Server with 2GB RAM

Added: incubator/deltacloud/trunk/server/features/support/gogrid/fixtures/images.yaml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/features/support/gogrid/fixtures/images.yaml?rev=962270&view=auto
==============================================================================
--- incubator/deltacloud/trunk/server/features/support/gogrid/fixtures/images.yaml (added)
+++ incubator/deltacloud/trunk/server/features/support/gogrid/fixtures/images.yaml Thu Jul
 8 23:42:24 2010
@@ -0,0 +1,13 @@
+---
+- id: 1
+  name: w2k8_32_iis_php_mysql
+  description: Windows 2008 (32-bit) w/ IIS 7.0 + PHP 5.2 + MySQL 5.0
+  friendlyName: Windows 2008 IIS
+- id: 2
+  name: GSI-95aa2817-7267-3325-ad01-1387a25f31ff
+  description: My second GSI
+  friendlyName: Test second GSI
+- id: 3
+  name: RHEL 5.1 (64-bit)
+  description: RHEL Linux 5.1 (64-bit)
+  friendlyName: RHEL

Added: incubator/deltacloud/trunk/server/features/support/gogrid/fixtures/instances.yaml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/features/support/gogrid/fixtures/instances.yaml?rev=962270&view=auto
==============================================================================
--- incubator/deltacloud/trunk/server/features/support/gogrid/fixtures/instances.yaml (added)
+++ incubator/deltacloud/trunk/server/features/support/gogrid/fixtures/instances.yaml Thu
Jul  8 23:42:24 2010
@@ -0,0 +1,42 @@
+---
+- name: Example Web Server
+  type:
+    description: This server has a public connection to the Internet.
+    id: 1
+    name: Web Server
+    object: option
+  id: 5075
+  description: Some more info here
+  ip: 
+    id: 499
+    ip: 111.111.111.111
+    object: ip
+    public: true
+    state:
+      description: IP is reserved
+      id: 2
+      name: Assigned
+      object: option
+    subnet: "111.111.111.110/255.255.255.240"
+  os:
+    description: Windows 2008 Server (32-bit)
+    id: 11
+    name: Windows 2008 Server (32-bit)
+    object: option
+  ram:
+    description: Server with 2GB RAM
+    id: 7
+    name: 2GB
+    object: option
+  image:
+    id: 1
+    name: w2k8_32_iis_php_mysql
+    description: Windows 2008 (32-bit) w/ IIS 7.0 + PHP 5.2 + MySQL 5.0
+    object: option
+  object: server
+  state:
+    description: 'null'
+    id: 3
+    name: 'Off'
+    object: option
+

Added: incubator/deltacloud/trunk/server/features/support/gogrid/fixtures/ips.yaml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/features/support/gogrid/fixtures/ips.yaml?rev=962270&view=auto
==============================================================================
--- incubator/deltacloud/trunk/server/features/support/gogrid/fixtures/ips.yaml (added)
+++ incubator/deltacloud/trunk/server/features/support/gogrid/fixtures/ips.yaml Thu Jul  8
23:42:24 2010
@@ -0,0 +1,11 @@
+---
+- public: true
+  id: '138273'
+  ip: '216.121.60.160'
+  subnet: "216.121.60.160/255.255.255.240"
+  object: 'ip'
+- public: true
+  id: '138273'
+  ip: '216.121.60.161'
+  subnet: "216.121.60.161/255.255.255.240"
+  object: 'ip'

Added: incubator/deltacloud/trunk/server/features/support/gogrid/servers.yaml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/features/support/gogrid/servers.yaml?rev=962270&view=auto
==============================================================================
--- incubator/deltacloud/trunk/server/features/support/gogrid/servers.yaml (added)
+++ incubator/deltacloud/trunk/server/features/support/gogrid/servers.yaml Thu Jul  8 23:42:24
2010
@@ -0,0 +1,4 @@
+---
+- name: 'From API'
+  os:
+    name: RHEL

Added: incubator/deltacloud/trunk/server/lib/deltacloud/drivers/gogrid/gogrid_client.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/deltacloud/drivers/gogrid/gogrid_client.rb?rev=962270&view=auto
==============================================================================
--- incubator/deltacloud/trunk/server/lib/deltacloud/drivers/gogrid/gogrid_client.rb (added)
+++ incubator/deltacloud/trunk/server/lib/deltacloud/drivers/gogrid/gogrid_client.rb Thu Jul
 8 23:42:24 2010
@@ -0,0 +1,45 @@
+require 'digest/md5'
+require 'cgi'
+require 'open-uri'
+require 'json'
+
+class GoGridClient
+
+  def initialize(server='https://api.gogrid.com/api',
+                 apikey='YOUR API KEY',
+                 secret='YOUR SHARED SECRET', 
+                 format='json',
+                 version='1.4')
+    @server = server
+    @secret = secret
+    @default_params = {'format'=>format, 'v'=>version,'api_key' => apikey}
+  end    
+  
+  def getRequestURL(method,params)
+    requestURL = @server+'/'+method+'?'
+  	call_params = @default_params.merge(params)
+  	call_params['sig']=getSignature(@default_params['api_key'],@secret)
+  	requestURL = requestURL+encode_params(call_params)
+  end
+  
+  def getSignature(key,secret)
+    Digest::MD5.hexdigest(key+secret+"%.0f"%Time.new.to_f)
+  end
+  
+  def sendAPIRequest(method,params={})
+    open(getRequestURL(method,params)).read
+  end
+
+  def request(method, params={})
+    begin
+      JSON::parse(sendAPIRequest(method, params))
+    rescue Exception => e
+      STDERR.puts("ERROR: #{e.message}")
+    end
+  end
+  
+  def encode_params(params)
+    params.map {|k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join("&")
+  end
+    
+end

Added: 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=962270&view=auto
==============================================================================
--- incubator/deltacloud/trunk/server/lib/deltacloud/drivers/gogrid/gogrid_driver.rb (added)
+++ incubator/deltacloud/trunk/server/lib/deltacloud/drivers/gogrid/gogrid_driver.rb Thu Jul
 8 23:42:24 2010
@@ -0,0 +1,253 @@
+#
+# Copyright (C) 2009  Red Hat, Inc.
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+
+require 'deltacloud/base_driver'
+require 'deltacloud/drivers/gogrid/gogrid_client'
+
+module Deltacloud
+  module Drivers
+    module Gogrid
+
+class GogridDriver < Deltacloud::BaseDriver
+
+  # Storage capacity is same on all machines (10gb), it could be extended using 'Cloud Storage'
+  define_hardware_profile('server-with-512mb-ram') do
+    cpu              2
+    memory         0.5
+    storage        10
+    architecture 'i386'
+  end
+
+  define_hardware_profile('server-with-1gb-ram') do
+    cpu            2
+    memory         1
+    storage        10
+    architecture   'i386'
+  end
+
+  define_hardware_profile('server-with-2gb-ram') do
+    cpu            2
+    memory         2
+    storage        10
+    architecture   'i386'
+  end
+
+  define_hardware_profile('server-with-4gb-ram') do
+    cpu            2
+    memory         4
+    storage        10
+    architecture   'i386'
+  end
+
+  define_hardware_profile('server-with-8gb-ram') do
+    cpu            2
+    memory         8
+    storage        10
+    architecture   'i386'
+  end
+
+  # The only valid option for flavors is server RAM for now
+  def flavors(credentials, opts=nil)
+    flavors = []
+    safely do
+      flavors=new_client(credentials).request('common/lookup/list', { 'lookup' => 'server.ram'
})['list'].collect do |flavor|
+        convert_flavor(flavor)
+      end
+    end
+    flavors = filter_on( flavors, :id, opts )
+    flavors = filter_on( flavors, :architecture, opts )
+    flavors
+  end
+
+  def images(credentials, opts=nil)
+    imgs = []
+    if opts and opts[:id]
+      safely do
+        imgs = [convert_image(new_client(credentials).request('grid/image/get', { 'id' =>
opts[:id] })['list'].first)]
+      end
+    else
+      safely do
+        imgs = new_client(credentials).request('grid/image/list', { 'state' => 'Available'})['list'].collect
do |image|
+          convert_image(image, credentials.user)
+        end
+      end
+    end
+    imgs = filter_on( imgs, :architecture, opts )
+    imgs.sort_by{|e| [e.owner_id, e.description]}
+  end
+
+  def realms(credentials, opts=nil)
+    safely do
+      new_client(credentials).request('common/lookup/list', { 'lookup' => 'image.type'
})['list'].collect do |realm|
+        convert_realm(realm)
+      end
+    end
+  end
+
+  def create_instance(credentials, image_id, opts=nil)
+    flavor_id = opts[:flavor_id] || '1'
+    name = (opts[:name] && opts[:name]!='') ? opts[:name] : get_random_instance_name
+    safely do
+      convert_instance(new_client(credentials).request('grid/server/add', { 
+        'name' => name,
+        'image' => image_id,
+        'server.ram' => flavor_id,
+        'ip' => get_next_free_ip(credentials)
+      })['list'].first, credentials.user)
+    end
+  end
+
+  def instances(credentials, opts=nil)
+    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)]
+      end
+    else
+      safely do
+        instances = new_client(credentials).request('grid/server/list')['list'].collect do
|instance|
+          convert_instance(instance, credentials.user)
+        end 
+      end
+    end
+    instances = filter_on( instances, :state, opts )
+    instances
+  end
+
+  def reboot_instance(credentials, id)
+    safely do
+      new_client(credentials).request('grid/server/power', { 'id' => id, 'power' =>
'reboot'})
+    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)
+    safely do
+      new_client(credentials).request('grid/server/delete', { 'id' => id})
+    end
+  end
+
+  define_instance_states do
+    start.to( :pending )         .automatically
+    pending.to( :running )       .automatically
+    running.to( :stopped )       .on( :stop )
+    stopped.to( :running )       .on( :start )
+    stopped.to( :finish )        .automatically
+  end
+
+  private
+
+  def new_client(credentials)
+    GoGridClient.new('https://api.gogrid.com/api', credentials.user, credentials.password)
+  end
+
+  def convert_image(gg_image, owner_id=nil)
+    Image.new( {
+      :id=>gg_image['id'],
+      :name => gg_image['friendlyName'],
+      :description=> convert_description(gg_image),
+      :owner_id=>gg_image['owner']['name'],
+      :architecture=>convert_arch(gg_image['description']),
+    } )
+  end
+
+  def convert_description(image)
+    if image['price'].eql?(0)
+      image['description']
+    else
+      "#{image['description']} (#{image['price']}$)"
+    end
+  end
+
+  def convert_flavor(flavor)
+    Flavor.new(
+      :id => flavor['id'],
+      :architecture => 'x86',
+      :memory => flavor['name'].tr('G', ''),
+      :storage => '1'
+    )
+  end
+
+  def convert_realm(realm)
+    Realm.new(
+      :id => realm['id'],
+      :name => realm['name'],
+      :state => :unlimited,
+      :storage => :unlimited
+    )
+  end
+
+  def convert_arch(description)
+    description.include?('64-bit') ? 'x86_64' : 'i386'
+  end
+
+  def convert_instance(instance, owner_id)
+    Instance.new(
+      :id => instance['id'],
+      :owner_id => owner_id,
+      :image_id => instance['image']['id'],
+      :flavor_id => instance['ram']['id'],
+      :name => instance['name'],
+      :realm_id => instance['type']['id'],
+      :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 => []
+    )
+  end
+
+  def get_random_instance_name
+    "Server #{Time.now.to_i.to_s.reverse[0..3]}#{rand(9)}"
+  end
+
+  def convert_server_state(state, id)
+    return 'PENDING' unless id
+    state.eql?('Off') ? 'STOPPED' : 'RUNNING'
+  end
+
+  def get_next_free_ip(credentials)
+    ip = ""
+    safely do
+      ip = new_client(credentials).request('grid/ip/list', { 
+        'ip.type' => '1',
+        'ip.state' => '1'
+      })['list'].first['ip']
+    end
+    return ip
+  end
+
+  def safely(&block)
+    begin
+      block.call
+    rescue Exception => e
+      puts "ERROR: #{e.message}"
+    end
+  end
+
+end
+
+    end
+  end
+end
+
+



Mime
View raw message