Module: MovableInk::AWS::EC2

Included in:
MovableInk::AWS
Defined in:
lib/movable_ink/aws/ec2.rb

Instance Method Summary collapse

Instance Method Details

#all_instances(region: my_region, no_filter: false) ⇒ Object



40
41
42
43
# File 'lib/movable_ink/aws/ec2.rb', line 40

def all_instances(region: my_region, no_filter: false)
  @all_instances ||= {}
  @all_instances[region] ||= load_all_instances(region, no_filter: no_filter)
end

#assign_ip_address(role:) ⇒ Object



154
155
156
157
158
159
160
161
# File 'lib/movable_ink/aws/ec2.rb', line 154

def assign_ip_address(role:)
  run_with_backoff do
    ec2.associate_address({
      instance_id: instance_id,
      allocation_id: available_elastic_ips(role: role).sample.allocation_id
    })
  end
end

#available_elastic_ips(role:) ⇒ Object



150
151
152
# File 'lib/movable_ink/aws/ec2.rb', line 150

def available_elastic_ips(role:)
  unassigned_elastic_ips.select { |address| address.tags.detect { |t| t.key == 'mi:roles' && t.value == role } }
end

#default_filter ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/movable_ink/aws/ec2.rb', line 45

def default_filter
  [{
    name: 'instance-state-name',
    values: ['running']
  },
  {
    name: 'tag:mi:env',
    values: [mi_env]
  }]
end

#ec2(region: my_region) ⇒ Object



6
7
8
9
# File 'lib/movable_ink/aws/ec2.rb', line 6

def ec2(region: my_region)
  @ec2_client ||= {}
  @ec2_client[region] ||= Aws::EC2::Client.new(region: region)
end

#elastic_ips ⇒ Object



140
141
142
143
144
# File 'lib/movable_ink/aws/ec2.rb', line 140

def elastic_ips
  @all_elastic_ips ||= run_with_backoff do
    ec2.describe_addresses.addresses
  end
end

#instance_ip_addresses_by_role(role:, exclude_roles: [], region: my_region, availability_zone: nil, exact_match: false, use_cache: true) ⇒ Object



121
122
123
# File 'lib/movable_ink/aws/ec2.rb', line 121

def instance_ip_addresses_by_role(role:, exclude_roles: [], region: my_region, availability_zone: nil, exact_match: false, use_cache: true)
  private_ip_addresses(instances(role: role, exclude_roles: exclude_roles, region: region, availability_zone: availability_zone, exact_match: exact_match, use_cache: use_cache))
end

#instance_ip_addresses_by_role_ordered(role:, exclude_roles: [], region: my_region, exact_match: false) ⇒ Object



125
126
127
128
129
130
# File 'lib/movable_ink/aws/ec2.rb', line 125

def instance_ip_addresses_by_role_ordered(role:, exclude_roles: [], region: my_region, exact_match: false)
  instances = instances(role: role, exclude_roles: exclude_roles, region: region, exact_match: exact_match)
  instances_in_my_az = instances.select { |instance| instance.placement.availability_zone == availability_zone }
  ordered_instances = instances_in_my_az.shuffle + (instances - instances_in_my_az).shuffle
  private_ip_addresses(ordered_instances)
end

#instance_tags ⇒ Object



66
67
68
69
70
71
72
# File 'lib/movable_ink/aws/ec2.rb', line 66

def instance_tags
  @instance_tags ||= run_with_backoff(quiet: true) do
    ec2.describe_tags({
      filters: [{ name: 'resource-id', values: [instance_id] } ]
    }).tags
  end
end

#instances(role:, exclude_roles: [], region: my_region, availability_zone: nil, exact_match: false, use_cache: true) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/movable_ink/aws/ec2.rb', line 78

def instances(role:, exclude_roles: [], region: my_region, availability_zone: nil, exact_match: false, use_cache: true)
  roles = role.split(/\s*,\s*/)
  if use_cache == false
    filter = default_filter.push({
      name: 'tag:mi:roles',
      values: roles
    })
    instances = load_all_instances(region, filter: filter)
  else
    instances = all_instances(region: region).select { |instance|
      instance.tags.select{ |tag| tag.key == 'mi:roles' }.detect { |tag|
        tag_roles = tag.value.split(/\s*,\s*/)
        if exact_match
          tag_roles == roles
        else
          exclude_roles.push('decommissioned')
          tag_roles.any? { |tag_role| roles.include?(tag_role) } && !tag_roles.any? { |role| exclude_roles.include?(role) }
        end
      }
    }
  end

  if availability_zone
    instances.select { |instance|
      instance.placement.availability_zone == availability_zone
    }
  else
    instances
  end
end

#load_all_instances(region, no_filter: false, filter: nil) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/movable_ink/aws/ec2.rb', line 56

def load_all_instances(region, no_filter: false, filter: nil)
  filters = no_filter ? nil : (filter || default_filter)

  run_with_backoff do
    ec2(region: region).describe_instances(filters: filters).flat_map do |resp|
      resp.reservations.flat_map(&:instances)
    end
  end
end

#load_mi_env ⇒ Object



24
25
26
27
28
29
30
# File 'lib/movable_ink/aws/ec2.rb', line 24

def load_mi_env
  instance_tags
    .detect { |tag| tag.key == 'mi:env' }
    .value
rescue NoMethodError
  raise MovableInk::AWS::Errors::NoEnvironmentTagError
end

#me ⇒ Object



74
75
76
# File 'lib/movable_ink/aws/ec2.rb', line 74

def me
  @me ||= all_instances.select{|instance| instance.instance_id == instance_id}.first rescue nil
end

#mi_env ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/movable_ink/aws/ec2.rb', line 15

def mi_env
  @mi_env ||= if File.exist?(mi_env_cache_file_path)
    environments = JSON.parse(File.read(mi_env_cache_file_path))
    environments[my_region] || load_mi_env
  else
    load_mi_env
  end
end

#mi_env_cache_file_path ⇒ Object



11
12
13
# File 'lib/movable_ink/aws/ec2.rb', line 11

def mi_env_cache_file_path
  '/etc/movableink/environments.json'
end

#private_ip_addresses(instances) ⇒ Object



117
118
119
# File 'lib/movable_ink/aws/ec2.rb', line 117

def private_ip_addresses(instances)
  instances.map(&:private_ip_address)
end

#redis_by_role(role, port) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/movable_ink/aws/ec2.rb', line 132

def redis_by_role(role, port)
  instance_ip_addresses_by_role(role: role)
    .shuffle
    .inject([]) { |redii, instance|
      redii.push({"host" => instance, "port" => port})
    }
end

#statsd_host ⇒ Object



113
114
115
# File 'lib/movable_ink/aws/ec2.rb', line 113

def statsd_host
  instance_ip_addresses_by_role(role: 'statsd', availability_zone: availability_zone, use_cache: false).sample
end

#thopter ⇒ Object



109
110
111
# File 'lib/movable_ink/aws/ec2.rb', line 109

def thopter
  private_ip_addresses(thopter_instance).first
end

#thopter_instance ⇒ Object



32
33
34
35
36
37
38
# File 'lib/movable_ink/aws/ec2.rb', line 32

def thopter_instance
  @thopter_instance ||= all_instances(region: 'us-east-1').select do |instance|
    instance.tags.select{ |tag| tag.key == 'mi:roles' }.detect do |tag|
      tag.value.include?('thopter')
    end
  end
end

#unassigned_elastic_ips ⇒ Object



146
147
148
# File 'lib/movable_ink/aws/ec2.rb', line 146

def unassigned_elastic_ips
  @unassigned_elastic_ips ||= elastic_ips.select { |address| address.association_id.nil? }
end