Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #64 from seomoz/spot_cleanup

Spot cleanup
  • Loading branch information...
commit 52627b6228a29243b22ffeb188546f33aec95343 2 parents e305c61 + c0d732b
@delano delano authored
View
5 lib/rudy/aws/ec2/spot_request.rb
@@ -89,6 +89,11 @@ def self.from_hash(hash)
end
end
end
+
+ def cancel(requests)
+ opts = requests ? {:spot_instance_request_id => requests.map(&:id)} : {}
+ Rudy::AWS::EC2.execute_request({}) { @@ec2.cancel_spot_instance_requests(opts) }
+ end
private
View
2  lib/rudy/exceptions.rb
@@ -68,5 +68,7 @@ class UnsupportedOS < Rudy::Error; end
class NotImplemented < Rudy::Error; end
+ class SpotRequestCancelled < Rudy::Error; end
+
end
View
7 lib/rudy/metadata.rb
@@ -88,6 +88,9 @@ def self.select(fields={})
# Generates a default criteria for all metadata based on
# region, zone, environment, and role. If a position has
# been specified in the globals it will also be included.
+ # The project name will also be removed if empty, to
+ # maintain backwards compatability with metadata entries
+ # that do not contain a projet field.
# * +rtype+ is the record type. One of: m, disk, or back.
# * +fields+ replaces and adds values to this criteria
# * +less+ removes keys from the default criteria.
@@ -102,6 +105,7 @@ def self.build_criteria(rtype, fields={}, less=[])
mixer = names.zip(values).flatten
criteria = Hash[*mixer].merge(fields)
criteria.reject! { |n,v| less.member?(n) }
+ criteria.delete(:project) if criteria[:project].to_s.empty?
Rudy::Huxtable.ld "CRITERIA: #{criteria.inspect}"
criteria
end
@@ -182,7 +186,8 @@ def initialize(rtype, opts={})
end
def name(*other)
- parts = [@rtype, @zone, @project, @environment, @role, @position, *other]
+ project = (@project.is_a?(Array) && @project.first.empty?) ? nil : @project
+ parts = [@rtype, @zone, project, @environment, @role, @position, *other]
parts.compact.flatten.join Rudy::DELIM
end
View
3  lib/rudy/routines/handlers/spot_request.rb
@@ -21,6 +21,9 @@ def create
request = Rudy::AWS::EC2::SpotRequests.create(opts)
raise NoMachines unless wait_for_fulfillment_of(request)
Rudy::AWS::EC2::SpotRequests.list(request)
+ rescue NoMachines
+ Rudy::AWS::EC2::SpotRequests.cancel(request)
+ raise SpotRequestCancelled
end
def wait_for_fulfillment_of(spot_requests)
Please sign in to comment.
Something went wrong with that request. Please try again.