2011-07-02 00:30:32 -04:00
require 'fog/core/collection'
2011-08-24 21:37:00 -04:00
require 'fog/aws/models/compute/spot_request'
2011-07-02 00:30:32 -04:00
module Fog
module Compute
class AWS
class SpotRequests < Fog :: Collection
attribute :filters
model Fog :: Compute :: AWS :: SpotRequest
def initialize ( attributes )
self . filters || = { }
super
end
def all ( filters = self . filters )
unless filters . is_a? ( Hash )
2011-10-19 15:49:34 -04:00
Fog :: Logger . deprecation ( " all with #{ filters . class } param is deprecated, use all('spot-instance-request-id' => []) instead [light_black]( #{ caller . first } )[/] " )
2011-07-02 00:30:32 -04:00
filters = { 'spot-instance-request-id' = > [ * filters ] }
end
self . filters = filters
data = connection . describe_spot_instance_requests ( filters ) . body
load (
data [ 'spotInstanceRequestSet' ] . map do | spot_instance_request |
2011-07-02 03:54:37 -04:00
spot_instance_request [ 'LaunchSpecification.Placement.AvailabilityZone' ] = spot_instance_request [ 'launchedAvailabilityZone' ]
2011-07-02 00:30:32 -04:00
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
end . flatten
)
end
2011-11-01 12:17:25 -04:00
def bootstrap ( new_attributes = { } )
spot_request = connection . spot_requests . new ( new_attributes )
unless new_attributes [ :key_name ]
# first or create fog_#{credential} keypair
name = Fog . respond_to? ( :credential ) && Fog . credential || :default
unless spot_request . key_pair = connection . key_pairs . get ( " fog_ #{ name } " )
spot_request . key_pair = connection . key_pairs . create (
:name = > " fog_ #{ name } " ,
:public_key = > server . public_key
)
end
end
# make sure port 22 is open in the first security group
security_group = connection . security_groups . get ( spot_request . groups . first )
authorized = security_group . ip_permissions . detect do | ip_permission |
ip_permission [ 'ipRanges' ] . first && ip_permission [ 'ipRanges' ] . first [ 'cidrIp' ] == '0.0.0.0/0' &&
ip_permission [ 'fromPort' ] == 22 &&
ip_permission [ 'ipProtocol' ] == 'tcp' &&
ip_permission [ 'toPort' ] == 22
end
unless authorized
security_group . authorize_port_range ( 22 .. 22 )
end
spot_request . save
spot_request . wait_for { ready? }
2012-04-06 17:10:11 -04:00
Fog . wait_for { server = connection . servers . get ( spot_request . reload . instance_id ) }
2011-11-01 12:17:25 -04:00
server = connection . servers . get ( spot_request . instance_id )
2011-11-02 10:00:49 -04:00
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
2011-11-01 12:17:25 -04:00
server . wait_for { ready? }
2012-04-06 17:10:11 -04:00
server . setup ( :key_data = > [ spot_request . private_key ] )
2011-11-02 09:17:36 -04:00
server
2011-11-01 12:17:25 -04:00
end
2011-07-02 00:30:32 -04:00
def get ( spot_request_id )
if spot_request_id
self . class . new ( :connection = > connection ) . all ( 'spot-instance-request-id' = > spot_request_id ) . first
end
rescue Fog :: Errors :: NotFound
nil
end
end
end
end
end