Class: InstanceSelector::Providers::AWS
- Inherits:
-
Object
- Object
- InstanceSelector::Providers::AWS
- Defined in:
- lib/instance_selector/providers/aws.rb
Instance Method Summary collapse
- #args_to_filters(args) ⇒ Object
- #connect ⇒ Object
- #dns_from_instance_reservation_set(reservation_set) ⇒ Object
-
#initialize(options = {}) ⇒ AWS
constructor
A new instance of AWS.
- #instances(filters = {}) ⇒ Object
- #on_demand_instances(filters = {}) ⇒ Object
- #parse_tags(tags) ⇒ Object
- #spot_instances(filters = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ AWS
Returns a new instance of AWS.
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/instance_selector/providers/aws.rb', line 4 def initialize(={}) @fog = .delete(:fog_client) unless @fog = { region: 'us-east-1', }.merge() connect end end |
Instance Method Details
#args_to_filters(args) ⇒ Object
75 76 77 78 |
# File 'lib/instance_selector/providers/aws.rb', line 75 def args_to_filters(args) filters = {} filters.merge! (args[:tags]) end |
#connect ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/instance_selector/providers/aws.rb', line 57 def connect begin @fog = Fog::Compute.new(provider: 'AWS', region: [:region], aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'], aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']) rescue @fog = Fog::Compute.new(provider: 'AWS', region: [:region]) ensure unless @fog abort " Could not authenticate with AWS.\n Please create a .fog file or set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY\n EOS\n end\n end\nend\n" |
#dns_from_instance_reservation_set(reservation_set) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/instance_selector/providers/aws.rb', line 17 def dns_from_instance_reservation_set(reservation_set) reservation_set.inject({}) do |memo, i| # Each instancesSet can have multiple instances # Odd, but explains why it's plural. i['instancesSet'].each do |instance| # instance = i['instancesSet'][0] key = instance['dnsName'].empty? ? instance['ipAddress'] : instance['dnsName'] memo[key] = instance['tagSet']['Name'] end memo end end |
#instances(filters = {}) ⇒ Object
51 52 53 54 55 |
# File 'lib/instance_selector/providers/aws.rb', line 51 def instances(filters={}) on_demand = on_demand_instances(filters) spot = spot_instances(filters) on_demand.merge(spot) end |
#on_demand_instances(filters = {}) ⇒ Object
31 32 33 34 35 |
# File 'lib/instance_selector/providers/aws.rb', line 31 def on_demand_instances(filters={}) instances = @fog.describe_instances({"instance-state-name" => "running"}.merge(filters)) dns_from_instance_reservation_set instances.body['reservationSet'] end |
#parse_tags(tags) ⇒ Object
80 81 82 83 84 85 |
# File 'lib/instance_selector/providers/aws.rb', line 80 def () .inject({}) do |memo, tag| memo["tag:#{tag[0]}"] = tag[1] memo end end |
#spot_instances(filters = {}) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/instance_selector/providers/aws.rb', line 37 def spot_instances(filters={}) requests = @fog.describe_spot_instance_requests({'state' => 'active'}.merge(filters)) requests.body['spotInstanceRequestSet'].inject({}) do |memo, req| if req['instanceId'] && !req['instanceId'].empty? instances = @fog.describe_instances('instance-id' => req['instanceId']) memo.merge! dns_from_instance_reservation_set(instances.body['reservationSet']) end memo end end |