Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Handle multiple instances with spot request #787

Closed
wants to merge 5 commits into from

2 participants

@rwjblue

Previously (current HEAD) you could instantiate many spot requests by using instance_count, but only the first was added to the SpotRequests collection and returned.

This pull request updates Fog::Compute::AWS::SpotRequest#save so that it initializes and adds all newly created spot requests to the Fog::Compute::AWS::SpotRequests collection. If instance_count is 1 (which is the default) it returns the generated spot_request. If instance_count is greater than 1 it will return an array of the newly created spot requests.

(Please note: this does not change the result of spot_requests.create because Fog::Collection#create automatically returns the object created when SpotRequest.new is called (not the result of SpotRequest#save).

This pull request also updates Fog::Compute::AWS::SpotRequests#bootstrap to allow it to bootstrap multiple servers at once if instance_count is more than one. Fog::Compute::AWS::SpotRequests#bootstrap will then return an array of Fog::Compute::AWS::Server objects (when instance_count == 1) or it will return the first Fog::Compute::AWS::Server (when instance_count == 1).

I do not see any tests specific to Fog::Compute::AWS::SpotRequests or Fog::Compute::AWS::SpotRequest, but I certainly can try to add some testing with a bit of direction.

In the default case of instance_count == 1 both Fog::Compute::AWS::SpotRequests#bootstrap and Fog::Compute::AWS::SpotRequest#save return the same value as current HEAD.

@geemus
Owner

@rjackson thanks for this. I think we should add some more testing as well though. Probably should go in fog/tests/aws/models/compute. Not sure how/why we ended up in a state where there don't appear to be any tests for spot stuff, but if you could add some there that would be awesome. Thanks!

@rwjblue rwjblue closed this
@geemus
Owner

@rjackson - sorry this got misplaced. Anything we should still do related to this? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 9, 2012
  1. Update SpotRequest#save to create additional collection entries with …

    Robert Jackson authored
    …instance_count > 1.
  2. Make SpotRequests#bootstrap handle instance_count > 1.

    Robert Jackson authored
  3. Simplify iteration in SpotRequest#save.

    Robert Jackson authored
  4. Reload spot_request before getting instance_id.

    Robert Jackson authored
Commits on Mar 14, 2012
  1. Merge branch 'master' of git://github.com/fog/fog into handle_multipl…

    Robert Jackson authored
    …e_instances_with_spot_request
This page is out of date. Refresh to see the latest.
View
30 lib/fog/aws/models/compute/spot_request.rb
@@ -3,7 +3,7 @@
module Fog
module Compute
class AWS
-
+
class SpotRequest < Fog::Model
identity :id, :aliases => 'spotInstanceRequestId'
@@ -95,16 +95,30 @@ def save
options.delete_if {|key, value| value.nil?}
data = connection.request_spot_instances(image_id, flavor_id, price, options).body
- spot_instance_request = data['spotInstanceRequestSet'].first
- spot_instance_request['launchSpecification'].each do |name,value|
- spot_instance_request['LaunchSpecification.' + name[0,1].upcase + name[1..-1]] = value
+
+ spot_requests = []
+ data['spotInstanceRequestSet'].each_with_index do |spot_instance_request, index|
+ spot_instance_request['launchSpecification'].each do |name,value|
+ spot_instance_request['LaunchSpecification.' + name[0,1].upcase + name[1..-1]] = value
+ end
+ spot_instance_request.merge(:groups => spot_instance_request['LaunchSpecification.GroupSet'])
+ spot_instance_request.merge(options)
+
+ if index == 0
+ spot_requests << merge_attributes( spot_instance_request )
+ else
+ spot_requests << SpotRequest.new(
+ spot_instance_request.merge(
+ :collection => self.collection,
+ :connection => self.connection)
+ )
+ end
end
- spot_instance_request.merge(:groups => spot_instance_request['LaunchSpecification.GroupSet'])
- spot_instance_request.merge(options)
- merge_attributes( spot_instance_request )
+
+ spot_requests.length == 1 ? self : spot_requests
end
end
end
end
-end
+end
View
40 lib/fog/aws/models/compute/spot_requests.rb
@@ -5,9 +5,9 @@ module Fog
module Compute
class AWS
class SpotRequests < Fog::Collection
-
+
attribute :filters
-
+
model Fog::Compute::AWS::SpotRequest
def initialize(attributes)
@@ -29,7 +29,7 @@ def all(filters = self.filters)
spot_instance_request['LaunchSpecification.' + name[0,1].upcase + name[1..-1]] = value
end
spot_instance_request.merge(:groups => spot_instance_request['LaunchSpecification.GroupSet'])
- spot_instance_request
+ spot_instance_request
end.flatten
)
end
@@ -60,22 +60,28 @@ def bootstrap(new_attributes = {})
security_group.authorize_port_range(22..22)
end
- spot_request.save
- spot_request.wait_for { ready? }
- Fog.wait_for { server = connection.servers.get(spot_request.instance_id) }
- server = connection.servers.get(spot_request.instance_id)
- if spot_request.tags
- for key, value in spot_request.tags
- connection.tags.create(
- :key => key,
- :resource_id => spot_request.instance_id,
- :value => value
- )
+ spot_requests = Array(spot_request.save)
+ servers = []
+ spot_requests.each do |spot_request|
+ spot_request.wait_for { ready? }
+ spot_request.reload
+ Fog.wait_for { connection.servers.get(spot_request.instance_id) }
+ server = connection.servers.get(spot_request.instance_id)
+ if spot_request.tags
+ for key, value in spot_request.tags
+ connection.tags.create(
+ :key => key,
+ :resource_id => spot_request.instance_id,
+ :value => value
+ )
+ end
end
+ server.wait_for { ready? }
+ server.setup(:key_data => [server.private_key])
+ servers << server
end
- server.wait_for { ready? }
- server.setup(:key_data => [server.private_key])
- server
+
+ servers.length == 1 ? servers.first : servers
end
def get(spot_request_id)
Something went wrong with that request. Please try again.