aws-eni 0.1.0

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.
@@ -0,0 +1,76 @@
1
+ require 'time'
2
+ require 'net/http'
3
+
4
+ module Aws
5
+ module ENI
6
+ module Meta
7
+
8
+ # EC2 instance meta-data connection settings
9
+ HOST = '169.254.169.254'
10
+ PORT = '80'
11
+ BASE = '/latest/meta-data/'
12
+
13
+ # Custom exception classes
14
+ class BadResponse < RuntimeError; end
15
+ class ConnectionFailed < RuntimeError; end
16
+
17
+ # These are the errors we trap when attempting to talk to the instance
18
+ # metadata service. Any of these imply the service is not present, no
19
+ # responding or some other non-recoverable error.
20
+ FAILURES = [
21
+ Errno::EHOSTUNREACH,
22
+ Errno::ECONNREFUSED,
23
+ Errno::EHOSTDOWN,
24
+ Errno::ENETUNREACH,
25
+ SocketError,
26
+ Timeout::Error,
27
+ BadResponse,
28
+ ]
29
+
30
+ # Open connection and run a single GET request on the instance metadata
31
+ # endpoint. Same options as open_connection.
32
+ def self.get(path, options = {})
33
+ open_connection options do |conn|
34
+ http_get(conn, path)
35
+ end
36
+ end
37
+
38
+ # Open a connection and attempt to execute the block `retries` times.
39
+ # Can specify open and read timeouts in addition to the number of retries.
40
+ def self.open_connection(options = {})
41
+ retries = options[:retries] || 5
42
+ failed_attempts = 0
43
+ begin
44
+ http = Net::HTTP.new(HOST, PORT, nil)
45
+ http.open_timeout = options[:open_timeout] || 5
46
+ http.read_timeout = options[:read_timeout] || 5
47
+ http.start
48
+ yield(http).tap { http.finish }
49
+ rescue *FAILURES => e
50
+ if failed_attempts < retries
51
+ # retry after an ever increasing cooldown time with each failure
52
+ Kernel.sleep(1.2 ** failed_attempts)
53
+ failed_attempts += 1
54
+ retry
55
+ else
56
+ raise ConnectionFailed, "Connection failed after #{retries} retries."
57
+ end
58
+ end
59
+ end
60
+
61
+ # Perform a GET request on an open connection to the instance metadata
62
+ # endpoint and return the body of any 200 response.
63
+ def self.http_get(connection, path)
64
+ response = connection.request(Net::HTTP::Get.new(BASE + path))
65
+ case response.code.to_i
66
+ when 200
67
+ response.body
68
+ when 404
69
+ nil
70
+ else
71
+ raise BadResponse
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,5 @@
1
+ module Aws
2
+ module ENI
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aws-eni
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mike Greiling
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: gli
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '2.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '2.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-sdk
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description: A command line tool and client library to manage AWS Elastic Network
70
+ Interfaces from within an EC2 instance
71
+ email:
72
+ - mike@pixelcog.com
73
+ executables:
74
+ - aws-eni
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - .gitignore
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - aws-eni.gemspec
84
+ - bin/aws-eni
85
+ - lib/aws-eni.rb
86
+ - lib/aws-eni/errors.rb
87
+ - lib/aws-eni/ifconfig.rb
88
+ - lib/aws-eni/meta.rb
89
+ - lib/aws-eni/version.rb
90
+ homepage: http://pixelcog.com/
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.0.14
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Manage and sync local network config with AWS Elastic Network Interfaces
114
+ test_files: []