Class: Fog::Compute::AWS::SpotRequests

Inherits:
Fog::Collection show all
Defined in:
lib/fog/aws/models/compute/spot_requests.rb

Instance Attribute Summary

Attributes inherited from Fog::Collection

#service

Instance Method Summary collapse

Methods inherited from Fog::Collection

#clear, #create, #destroy, #inspect, #load, model, #model, #new, #reload, #table, #to_json

Methods included from Attributes::ClassMethods

#_load, #aliases, #attribute, #attributes, #identity, #ignore_attributes, #ignored_attributes

Methods included from Fog::Core::DeprecatedConnectionAccessors

#connection, #connection=, #prepare_service_value

Methods included from Attributes::InstanceMethods

#_dump, #attributes, #dup, #identity, #identity=, #merge_attributes, #new_record?, #persisted?, #requires, #requires_one

Constructor Details

#initialize(attributes) ⇒ SpotRequests

Returns a new instance of SpotRequests.



13
14
15
16
# File 'lib/fog/aws/models/compute/spot_requests.rb', line 13

def initialize(attributes)
  self.filters ||= {}
  super
end

Instance Method Details

#all(filters = self.filters) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/aws/models/compute/spot_requests.rb', line 18

def all(filters = self.filters)
  unless filters.is_a?(Hash)
    Fog::Logger.deprecation("all with #{filters.class} param is deprecated, use all('spot-instance-request-id' => []) instead [light_black](#{caller.first})[/]")
    filters = {'spot-instance-request-id' => [*filters]}
  end
  self.filters = filters
  data = service.describe_spot_instance_requests(filters).body
  load(
    data['spotInstanceRequestSet'].map do |spot_instance_request|
      spot_instance_request['LaunchSpecification.Placement.AvailabilityZone'] = spot_instance_request['launchedAvailabilityZone']
      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

#bootstrap(new_attributes = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fog/aws/models/compute/spot_requests.rb', line 37

def bootstrap(new_attributes = {})
  spot_request = service.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 = service.key_pairs.get("fog_#{name}")
      spot_request.key_pair = service.key_pairs.create(
        :name => "fog_#{name}",
        :public_key => spot_request.public_key
      )
    end
  end

  # make sure port 22 is open in the first security group
  security_group = service.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
  Fog.wait_for { spot_request.reload.ready? rescue nil }
  server = service.servers.get(spot_request.instance_id)
  if spot_request.tags
    for key, value in spot_request.tags
      service.tags.create(
        :key          => key,
        :resource_id  => spot_request.instance_id,
        :value        => value
      )
    end
  end
  server.wait_for { ready? }
  server.setup(:key_data => [spot_request.private_key])
  server
end

#get(spot_request_id) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/fog/aws/models/compute/spot_requests.rb', line 80

def get(spot_request_id)
  if spot_request_id
    self.class.new(:service => service).all('spot-instance-request-id' => spot_request_id).first
  end
rescue Fog::Errors::NotFound
  nil
end