aws-eni 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 613d8b521f5ea3db65f0420e90dfe188c6b42e53
4
- data.tar.gz: 0a1eebe7c2b86a128a282f9edd8d61d5c0c39d2c
3
+ metadata.gz: 5bd70f82e45aa8e1c89003fefe1118b3c3e7a0ff
4
+ data.tar.gz: 8f7c1cbbb2136f722d34f8d414a167327e6920f0
5
5
  SHA512:
6
- metadata.gz: 092fd301932d4521a4c7b1fda6c1d04f21160d9585bd5e0b1c11e5385a87b97b21bb33bd3a55001fafa3da4e2b39d67b12b5cd759c7f00aa2064ce9dfe733edd
7
- data.tar.gz: c10cb8481dc1f9a544e3ebedb674bd434d4e30f1a9cdc1e02d022d2195016f5f16813919a387a82e27b4aa206d29b092cbbb51bc568e1a4f3ba7e135007f976c
6
+ metadata.gz: 3d7d4e8422d6232c893375f284b4cbc33b232c2cbc2af731be6f679540c017ab97e6162a22ce52ca011a49ca11dac653f862b51bba9893ebf5ed6a78ca280b89
7
+ data.tar.gz: 09dafcec8f9c13900c99754f0dfad80b7a912da55512de34f163a8a546fc4cdd2efaba04b8eb34032f5dd6857a95e35c9cce5c686272818cf32093c81af0ec24
data/bin/aws-eni CHANGED
@@ -361,9 +361,10 @@ command [:unassign] do |c|
361
361
  c.switch [:r,:release], negatable: false
362
362
 
363
363
  c.action do |global,opts,args|
364
- help_now! "Missing argument" if args.empty?
365
364
  params = parse_args args, :interface_id, :device_name, :private_ip
365
+ help_now! "Missing argument" unless params[:private_ip]
366
366
  params[:release] = opts[:release]
367
+
367
368
  unassign = Aws::ENI.unassign_secondary_ip(params[:private_ip], params)
368
369
  if unassign[:released]
369
370
  puts "EIP #{unassign[:public_ip]} (#{unassign[:allocation_id]}) dissociated from #{unassign[:private_ip]} and released"
@@ -416,10 +417,10 @@ command [:dissociate] do |c|
416
417
  c.switch [:r,:release], negatable: false
417
418
 
418
419
  c.action do |global,opts,args|
419
- help_now! "Missing argument" if args.empty?
420
420
  params = parse_args args, :private_ip, :public_ip, :allocation_id, :association_id, :interface_id, :device_name
421
- ip = params[:private_ip] || params[:public_ip] || params[:association_id] || params[:allocation_id]
422
- dissoc = Aws::ENI.dissociate_elastic_ip(ip, params)
421
+ address = params[:private_ip] || params[:public_ip] || params[:association_id] || params[:allocation_id]
422
+ help_now! "Missing argument" unless address
423
+ dissoc = Aws::ENI.dissociate_elastic_ip(address, params)
423
424
  if dissoc[:released]
424
425
  puts "EIP #{dissoc[:public_ip]} (#{dissoc[:allocation_id]}) dissociated from #{dissoc[:private_ip]} on #{dissoc[:device_name]} (#{dissoc[:interface_id]}) and released"
425
426
  else
data/lib/aws-eni.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require 'time'
2
- require 'resolv'
3
2
  require 'aws-sdk'
4
3
  require 'aws-eni/version'
5
4
  require 'aws-eni/errors'
@@ -258,15 +257,44 @@ module Aws
258
257
 
259
258
  # remove a private ip using the AWS api and remove it from local config
260
259
  def unassign_secondary_ip(private_ip, options = {})
261
- raise NoMethodError, "unassign_secondary_ip not yet implemented"
260
+ do_release = !!options[:release]
261
+
262
+ find = options[:device_name] || options[:device_number] || options[:interface_id] || private_ip
263
+ device = IFconfig[find].assert(
264
+ exists: true,
265
+ device_name: options[:device_name],
266
+ interface_id: options[:interface_id],
267
+ device_number: options[:device_number]
268
+ )
269
+
270
+ resp = client.describe_network_interfaces(network_interface_ids: [device.interface_id])
271
+ interface = resp[:network_interfaces].first
272
+ raise UnknownInterfaceError, "Interface attachment could not be located" unless interface
273
+
274
+ unless addr_info = interface[:private_ip_addresses].find { |addr| addr[:private_ip_address] == private_ip }
275
+ raise InvalidParameterError, "IP #{private_ip} not found on #{device.name}"
276
+ end
277
+ if addr_info[:primary]
278
+ raise InvalidParameterError, "The primary IP address of an interface cannot be unassigned"
279
+ end
280
+
281
+ if assoc = addr_info[:association]
282
+ client.release_address(allocation_id: assoc[:allocation_id]) if do_release
283
+ end
284
+
285
+ device.remove_alias(private_ip)
286
+ client.unassign_private_ip_addresses(
287
+ network_interface_id: interface[:network_interface_id],
288
+ private_ip_addresses: [private_ip]
289
+ )
262
290
  {
263
- private_ip: '0.0.0.0',
264
- device_name: 'eth0',
265
- interface_id: 'eni-1a2b3c4d',
266
- public_ip: '0.0.0.0',
267
- allocation_id: 'eipalloc-1a2b3c4d',
268
- association_id: 'eipassoc-1a2b3c4d',
269
- released: true
291
+ private_ip: private_ip,
292
+ device_name: device.name,
293
+ interface_id: device.interface_id,
294
+ public_ip: assoc[:public_ip],
295
+ allocation_id: assoc[:allocation_id],
296
+ association_id: assoc[:association_id],
297
+ released: assoc && do_release
270
298
  }
271
299
  end
272
300
 
@@ -318,15 +346,36 @@ module Aws
318
346
  # dissociate a public ip from a private ip through the AWS api and
319
347
  # optionally release the public ip
320
348
  def dissociate_elastic_ip(ip, options = {})
321
- raise NoMethodError, "dissociate_elastic_ip not yet implemented"
349
+ do_release = !!options[:release]
350
+ eip = describe_address(ip)
351
+
352
+ if find = options[:device_name] || options[:device_number] || options[:interface_id]
353
+ device = IFconfig[find].assert(
354
+ device_name: options[:device_name],
355
+ device_number: options[:device_number],
356
+ interface_id: options[:interface_id]
357
+ )
358
+ if device.interface_id != eip[:network_interface_id]
359
+ raise UnknownInterfaceError, "EIP #{public_ip} is not associated with interface #{device.name} (#{device.interface_id})"
360
+ end
361
+ else
362
+ begin
363
+ device = IFconfig[eip[:network_interface_id]]
364
+ rescue UnknownInterfaceError
365
+ raise UnknownInterfaceError, "EIP #{public_ip} is not associated with an interface on this machine"
366
+ end
367
+ end
368
+
369
+ client.disassociate_address(association_id: eip[:association_id])
370
+ client.release_address(allocation_id: eip[:allocation_id]) if do_release
322
371
  {
323
- private_ip: '0.0.0.0',
324
- device_name: 'eth0',
325
- interface_id: 'eni-1a2b3c4d',
326
- public_ip: '0.0.0.0',
327
- allocation_id: 'eipalloc-1a2b3c4d',
328
- association_id: 'eipassoc-1a2b3c4d',
329
- released: true
372
+ private_ip: eip[:private_ip_address],
373
+ device_name: device.name,
374
+ interface_id: eip[:network_interface_id],
375
+ public_ip: eip[:public_ip],
376
+ allocation_id: eip[:allocation_id],
377
+ association_id: eip[:association_id],
378
+ released: do_release
330
379
  }
331
380
  end
332
381
 
@@ -425,7 +474,18 @@ module Aws
425
474
 
426
475
  # use either an ip address or allocation id
427
476
  def describe_address(address)
428
- filter_by = address =~ Resolv::IPv4::Regex ? 'public-ip' : 'allocation-id'
477
+ filter_by = case address
478
+ when /^eipalloc-/
479
+ 'allocation-id'
480
+ when /^eipassoc-/
481
+ 'allocation-id'
482
+ else
483
+ if IPAddr.new(environment[:vpc_cidr]) === IPAddr.new(address)
484
+ 'private-ip-address'
485
+ else
486
+ 'public-ip'
487
+ end
488
+ end
429
489
  resp = client.describe_addresses(filters: [
430
490
  { name: 'domain', values: ['vpc'] },
431
491
  { name: filter_by, values: [address] }
@@ -22,7 +22,7 @@ module Aws
22
22
  when /^(?:eth)?([0-9]+)$/
23
23
  self[$1.to_i]
24
24
  when /^eni-/
25
- find { |dev| dev.instance_id == index }
25
+ find { |dev| dev.interface_id == index }
26
26
  when /^[0-9a-f:]+$/i
27
27
  find { |dev| dev.hwaddr.casecmp(index) == 0 }
28
28
  when /^[0-9\.]+$/
@@ -314,7 +314,7 @@ module Aws
314
314
  end
315
315
 
316
316
  # Remove a secondary ip from this interface
317
- def remove_alias
317
+ def remove_alias(ip)
318
318
  cmd("addr del #{ip}/#{prefix} dev #{name}")
319
319
  unless name == 'eth0' || !cmd("rule list").match(/([0-9]+):\s+from #{ip} lookup #{route_table}/)
320
320
  cmd("rule delete pref #{$1}")
@@ -1,5 +1,5 @@
1
1
  module Aws
2
2
  module ENI
3
- VERSION = "0.1.3"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws-eni
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Greiling
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-30 00:00:00.000000000 Z
11
+ date: 2015-05-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gli