MovableInkAWS 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0539bfb11df57a94a89383fd54ba31df82b1fbee4980262d1ea35c023fad9664
4
- data.tar.gz: 6684d49314bbb159b52b88d35a0783e8c917bccbd2bd273cf3fe0adf320128b6
3
+ metadata.gz: 590ea003298d992808c1b6556bccbb7b25caac6826951a6414c9d2886603a4b5
4
+ data.tar.gz: 7029aa3fef07dac4b45858f0182c1d5091356937efcd4a01f73b0baec1c4b6cd
5
5
  SHA512:
6
- metadata.gz: 26464d435dc1f1306967b4166c9d970c6ca9dab58e22b3ec1f8f3b1a94b1b40259f004f2141cfb63364a29280cf36cbafc253709cf4ad39ed70e2ee77b2de2de
7
- data.tar.gz: ea438262feb93ca615aff818c417d117651d4b017250c9bf6b0b0f930ae86f16e01ff3d855481db7ce9a4650a590966cfc978ec847d2f883340cc42de134b683
6
+ metadata.gz: 71bf9f3e2afd8e95375ae0a45321094672f46fe563ffb0f116b8dae9936f86ddca638d013392005602a906f76f5f41318f4a43e75e02ad8861643256806f090e
7
+ data.tar.gz: e153c1ee20f4318d9d0dc017d13bfc36caec6d2d22d7904f0527bf53a5d301fd0ce50ea174838432aa1684ae8008d75d727129f437d6972fc7c4ce94287c26c5
data/Gemfile.lock CHANGED
@@ -1,19 +1,19 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- MovableInkAWS (0.1.4)
4
+ MovableInkAWS (0.1.6)
5
5
  aws-sdk (~> 2.10, >= 2.10.0)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- aws-sdk (2.11.28)
11
- aws-sdk-resources (= 2.11.28)
12
- aws-sdk-core (2.11.28)
10
+ aws-sdk (2.11.21)
11
+ aws-sdk-resources (= 2.11.21)
12
+ aws-sdk-core (2.11.21)
13
13
  aws-sigv4 (~> 1.0)
14
14
  jmespath (~> 1.0)
15
- aws-sdk-resources (2.11.28)
16
- aws-sdk-core (= 2.11.28)
15
+ aws-sdk-resources (2.11.21)
16
+ aws-sdk-core (= 2.11.21)
17
17
  aws-sigv4 (1.0.2)
18
18
  diff-lcs (1.3)
19
19
  jmespath (1.3.1)
@@ -139,6 +139,29 @@ module MovableInk
139
139
  redii.push({"host" => instance, "port" => port})
140
140
  }
141
141
  end
142
+
143
+ def elastic_ips
144
+ @all_elastic_ips ||= run_with_backoff do
145
+ ec2.describe_addresses.flat_map(&:addresses)
146
+ end
147
+ end
148
+
149
+ def unassigned_elastic_ips
150
+ @unassigned_elastic_ips ||= elastic_ips.select { |address| address.association_id.nil? }
151
+ end
152
+
153
+ def available_elastic_ips(role:)
154
+ unassigned_elastic_ips.select { |address| address.tags.detect { |t| t.key == 'mi:roles' && t.value == role } }
155
+ end
156
+
157
+ def assign_ip_address(role:)
158
+ run_with_backoff do
159
+ ec2.associate_address({
160
+ instance_id: instance_id,
161
+ allocation_id: available_elastic_ips(role: role).sample.allocation_id
162
+ })
163
+ end
164
+ end
142
165
  end
143
166
  end
144
167
  end
@@ -5,42 +5,6 @@ module MovableInk
5
5
  @route53_client ||= Aws::Route53::Client.new(region: 'us-east-1')
6
6
  end
7
7
 
8
- def elastic_ips
9
- @all_elastic_ips ||= load_all_elastic_ips
10
- end
11
-
12
- def load_all_elastic_ips
13
- run_with_backoff do
14
- ec2.describe_addresses.flat_map(&:addresses)
15
- end
16
- end
17
-
18
- def unassigned_elastic_ips
19
- @unassigned_elastic_ips ||= elastic_ips.select { |address| address.association_id.nil? }
20
- end
21
-
22
- def reserved_elastic_ips
23
- @reserved_elastic_ips ||= s3.get_object({bucket: 'movableink-chef', key: 'reserved_ips.json'}).body
24
- .map { |line| JSON.parse(line) }
25
- end
26
-
27
- def available_elastic_ips(role:)
28
- reserved_elastic_ips.select do |ip|
29
- ip["datacenter"] == datacenter &&
30
- ip["role"] == role &&
31
- unassigned_elastic_ips.map(&:allocation_id).include?(ip["allocation_id"])
32
- end
33
- end
34
-
35
- def assign_ip_address(role:)
36
- run_with_backoff do
37
- ec2.associate_address({
38
- instance_id: instance_id,
39
- allocation_id: available_elastic_ips(role: role).sample["allocation_id"]
40
- })
41
- end
42
- end
43
-
44
8
  def resource_record_sets(hosted_zone_id)
45
9
  @resource_record_sets ||= {}
46
10
  @resource_record_sets[hosted_zone_id] ||= list_all_r53_resource_record_sets(hosted_zone_id)
@@ -1,5 +1,5 @@
1
1
  module MovableInk
2
2
  class AWS
3
- VERSION = '0.1.5'
3
+ VERSION = '0.1.6'
4
4
  end
5
5
  end
data/spec/ec2_spec.rb CHANGED
@@ -219,5 +219,80 @@ describe MovableInk::AWS::EC2 do
219
219
  expect(aws.redis_by_role('visitor_redis', port)).to match_array(redii)
220
220
  end
221
221
  end
222
+
223
+ context "elastic IPs" do
224
+ let(:elastic_ip_data) { ec2.stub_data(:describe_addresses, addresses: [
225
+ {
226
+ allocation_id: "eipalloc-1",
227
+ association_id: "eipassoc-1",
228
+ domain: "vpc",
229
+ public_ip: "1.1.1.1",
230
+ tags: [{
231
+ key: 'mi:roles',
232
+ value: 'some_role'
233
+ }]
234
+ },
235
+ {
236
+ allocation_id: "eipalloc-2",
237
+ association_id: "eipassoc-2",
238
+ domain: "vpc",
239
+ public_ip: "1.1.1.2",
240
+ tags: [{
241
+ key: 'mi:roles',
242
+ value: 'some_role'
243
+ }]
244
+ },
245
+ {
246
+ allocation_id: "eipalloc-3",
247
+ association_id: nil,
248
+ domain: "vpc",
249
+ public_ip: "1.1.1.3",
250
+ tags: [{
251
+ key: 'mi:roles',
252
+ value: 'some_role'
253
+ }]
254
+ }
255
+ ])
256
+ }
257
+ let(:associate_address_data) { ec2.stub_data(:associate_address, association_id: 'eipassoc-3') }
258
+
259
+ it "should find all elastic IPs" do
260
+ ec2.stub_responses(:describe_addresses, elastic_ip_data)
261
+ allow(aws).to receive(:my_region).and_return('us-east-1')
262
+ allow(aws).to receive(:ec2).and_return(ec2)
263
+
264
+ expect(aws.elastic_ips.count).to eq(3)
265
+ expect(aws.elastic_ips.first.public_ip).to eq('1.1.1.1')
266
+ expect(aws.elastic_ips.last.public_ip).to eq('1.1.1.3')
267
+ end
268
+
269
+ it "should find unassigned elastic IPs" do
270
+ ec2.stub_responses(:describe_addresses, elastic_ip_data)
271
+ allow(aws).to receive(:my_region).and_return('us-east-1')
272
+ allow(aws).to receive(:ec2).and_return(ec2)
273
+
274
+ expect(aws.unassigned_elastic_ips.count).to eq(1)
275
+ expect(aws.unassigned_elastic_ips.first.public_ip).to eq('1.1.1.3')
276
+ end
277
+
278
+ it "should filter reserved IPs to get available IPs" do
279
+ ec2.stub_responses(:describe_addresses, elastic_ip_data)
280
+ allow(aws).to receive(:my_region).and_return('us-east-1')
281
+ allow(aws).to receive(:ec2).and_return(ec2)
282
+
283
+ expect(aws.available_elastic_ips(role: 'some_role').count).to eq(1)
284
+ expect(aws.available_elastic_ips(role: 'some_role').first['public_ip']).to eq('1.1.1.3')
285
+ end
286
+
287
+ it "should assign an elastic IP" do
288
+ ec2.stub_responses(:describe_addresses, elastic_ip_data)
289
+ ec2.stub_responses(:associate_address, associate_address_data)
290
+ allow(aws).to receive(:my_region).and_return('us-east-1')
291
+ allow(aws).to receive(:instance_id).and_return('i-12345')
292
+ allow(aws).to receive(:ec2).and_return(ec2)
293
+
294
+ expect(aws.assign_ip_address(role: 'some_role').association_id).to eq('eipassoc-3')
295
+ end
296
+ end
222
297
  end
223
298
  end
data/spec/route53_spec.rb CHANGED
@@ -4,93 +4,6 @@ require_relative '../lib/movable_ink/aws'
4
4
  describe MovableInk::AWS::Route53 do
5
5
  let(:aws) { MovableInk::AWS.new }
6
6
 
7
- context "elastic IPs" do
8
- let(:ec2) { Aws::EC2::Client.new(stub_responses: true) }
9
- let(:s3) { Aws::S3::Client.new(stub_responses: true) }
10
- let(:elastic_ip_data) { ec2.stub_data(:describe_addresses, addresses: [
11
- {
12
- allocation_id: "eipalloc-1",
13
- association_id: "eipassoc-1",
14
- domain: "vpc",
15
- public_ip: "1.1.1.1"
16
- },
17
- {
18
- allocation_id: "eipalloc-2",
19
- association_id: "eipassoc-2",
20
- domain: "vpc",
21
- public_ip: "1.1.1.2"
22
- },
23
- {
24
- allocation_id: "eipalloc-3",
25
- association_id: nil,
26
- domain: "vpc",
27
- public_ip: "1.1.1.3"
28
- }
29
- ])
30
- }
31
- let(:associate_address_data) { ec2.stub_data(:associate_address, association_id: 'eipassoc-3') }
32
- let(:reserved_ips) { StringIO.new(
33
- "{\"datacenter\":\"iad\",\"allocation_id\":\"eipalloc-1\",\"public_ip\":\"1.1.1.1\",\"role\":\"some_role\"}
34
- {\"datacenter\":\"iad\",\"allocation_id\":\"eipalloc-2\",\"public_ip\":\"1.1.1.2\",\"role\":\"some_role\"}
35
- {\"datacenter\":\"iad\",\"allocation_id\":\"eipalloc-3\",\"public_ip\":\"1.1.1.3\",\"role\":\"some_role\"}"
36
- )}
37
- let(:s3_reserved_ips_data) { s3.stub_data(:get_object, body: reserved_ips) }
38
-
39
- it "should find all elastic IPs" do
40
- ec2.stub_responses(:describe_addresses, elastic_ip_data)
41
- allow(aws).to receive(:my_region).and_return('us-east-1')
42
- allow(aws).to receive(:instance_id).and_return('i-12345')
43
- allow(aws).to receive(:ec2).and_return(ec2)
44
-
45
- expect(aws.elastic_ips.count).to eq(3)
46
- expect(aws.elastic_ips.first.public_ip).to eq('1.1.1.1')
47
- expect(aws.elastic_ips.last.public_ip).to eq('1.1.1.3')
48
- end
49
-
50
- it "should find unassigned elastic IPs" do
51
- ec2.stub_responses(:describe_addresses, elastic_ip_data)
52
- allow(aws).to receive(:my_region).and_return('us-east-1')
53
- allow(aws).to receive(:instance_id).and_return('i-12345')
54
- allow(aws).to receive(:ec2).and_return(ec2)
55
-
56
- expect(aws.unassigned_elastic_ips.count).to eq(1)
57
- expect(aws.unassigned_elastic_ips.first.public_ip).to eq('1.1.1.3')
58
- end
59
-
60
- it "should load reserved elastic IPs from S3" do
61
- s3.stub_responses(:get_object, s3_reserved_ips_data)
62
- allow(aws).to receive(:my_region).and_return('us-east-1')
63
- allow(aws).to receive(:instance_id).and_return('i-12345')
64
- allow(aws).to receive(:s3).and_return(s3)
65
-
66
- expect(aws.reserved_elastic_ips.count).to eq(3)
67
- end
68
-
69
- it "should filter reserved IPs to get available IPs" do
70
- ec2.stub_responses(:describe_addresses, elastic_ip_data)
71
- s3.stub_responses(:get_object, s3_reserved_ips_data)
72
- allow(aws).to receive(:my_region).and_return('us-east-1')
73
- allow(aws).to receive(:instance_id).and_return('i-12345')
74
- allow(aws).to receive(:ec2).and_return(ec2)
75
- allow(aws).to receive(:s3).and_return(s3)
76
-
77
- expect(aws.available_elastic_ips(role: 'some_role').count).to eq(1)
78
- expect(aws.available_elastic_ips(role: 'some_role').first['public_ip']).to eq('1.1.1.3')
79
- end
80
-
81
- it "should assign an elastic IP" do
82
- ec2.stub_responses(:describe_addresses, elastic_ip_data)
83
- ec2.stub_responses(:associate_address, associate_address_data)
84
- s3.stub_responses(:get_object, s3_reserved_ips_data)
85
- allow(aws).to receive(:my_region).and_return('us-east-1')
86
- allow(aws).to receive(:instance_id).and_return('i-12345')
87
- allow(aws).to receive(:ec2).and_return(ec2)
88
- allow(aws).to receive(:s3).and_return(s3)
89
-
90
- expect(aws.assign_ip_address(role: 'some_role').association_id).to eq('eipassoc-3')
91
- end
92
- end
93
-
94
7
  context "resource record sets" do
95
8
  let(:route53) { Aws::Route53::Client.new(stub_responses: true) }
96
9
  let(:rrset_data) { route53.stub_data(:list_resource_record_sets, resource_record_sets: [
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: MovableInkAWS
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Chesler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-29 00:00:00.000000000 Z
11
+ date: 2018-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk