ec2_meta 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 440bc0cad223de4782e2bf03477901e0f6a9be94
4
+ data.tar.gz: 50a1f47e43fd18de836c4b88c64a1147aec8fd18
5
+ SHA512:
6
+ metadata.gz: c6eba6b6168dcc66818f9c50ba5772efb43182f635bd49fd0750d64fb022ce98f98d005f5a30a07277aff3447adee2f7199e98428a3fb48453bfd1c3ea9f1123
7
+ data.tar.gz: bfd026571b5acf6b39912c607dbbe2e8c950c062d5b19a9b18091ad6f9965830d58fff3c712df5f1eec919be05d54dc09750193e05d732e0ce5c34150480b8ce
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -0,0 +1,47 @@
1
+ # Created by https://www.gitignore.io
2
+
3
+ ### Ruby ###
4
+ *.gem
5
+ *.rbc
6
+ /.config
7
+ /coverage/
8
+ /InstalledFiles
9
+ /pkg/
10
+ /spec/reports/
11
+ /test/tmp/
12
+ /test/version_tmp/
13
+ /tmp/
14
+
15
+ ## Specific to RubyMotion:
16
+ .dat*
17
+ .repl_history
18
+ build/
19
+
20
+ ## Documentation cache and generated files:
21
+ /.yardoc/
22
+ /_yardoc/
23
+ /doc/
24
+ /rdoc/
25
+
26
+ ## Environment normalisation:
27
+ /.bundle/
28
+ /vendor/bundle
29
+ /lib/bundler/man/
30
+
31
+ # for a library or gem, you might want to ignore these files since the code is
32
+ # intended to run in multiple environments; otherwise, check them in:
33
+ # Gemfile.lock
34
+ # .ruby-version
35
+ # .ruby-gemset
36
+
37
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
38
+ .rvmrc
39
+
40
+
41
+ ### Vim ###
42
+ [._]*.s[a-w][a-z]
43
+ [._]s[a-w][a-z]
44
+ *.un~
45
+ Session.vim
46
+ .netrwhist
47
+ *~
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ec2_meta.gemspec
4
+ gemspec
5
+
6
+ group :development, :test do
7
+ gem 'pry-byebug'
8
+ gem 'gem-release'
9
+ end
10
+
11
+ group :test do
12
+ gem 'coveralls', require: false
13
+ gem 'codeclimate-test-reporter', require: false
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,112 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ec2_meta (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.3.8)
10
+ ast (2.0.0)
11
+ astrolabe (1.3.0)
12
+ parser (>= 2.2.0.pre.3, < 3.0)
13
+ byebug (4.0.5)
14
+ columnize (= 0.9.0)
15
+ codeclimate-test-reporter (0.4.7)
16
+ simplecov (>= 0.7.1, < 1.0.0)
17
+ coderay (1.1.0)
18
+ columnize (0.9.0)
19
+ coveralls (0.8.0)
20
+ multi_json (~> 1.10)
21
+ rest-client (>= 1.6.8, < 2)
22
+ simplecov (~> 0.9.1)
23
+ term-ansicolor (~> 1.3)
24
+ thor (~> 0.19.1)
25
+ crack (0.4.2)
26
+ safe_yaml (~> 1.0.0)
27
+ diff-lcs (1.2.5)
28
+ docile (1.1.5)
29
+ domain_name (0.5.23)
30
+ unf (>= 0.0.5, < 1.0.0)
31
+ gem-release (0.7.3)
32
+ http-cookie (1.0.2)
33
+ domain_name (~> 0.5)
34
+ method_source (0.8.2)
35
+ mime-types (2.4.3)
36
+ multi_json (1.11.0)
37
+ netrc (0.10.3)
38
+ parser (2.2.0.3)
39
+ ast (>= 1.1, < 3.0)
40
+ powerpack (0.1.0)
41
+ pry (0.10.1)
42
+ coderay (~> 1.1.0)
43
+ method_source (~> 0.8.1)
44
+ slop (~> 3.4)
45
+ pry-byebug (3.1.0)
46
+ byebug (~> 4.0)
47
+ pry (~> 0.10)
48
+ rainbow (2.0.0)
49
+ rake (10.4.2)
50
+ rest-client (1.8.0)
51
+ http-cookie (>= 1.0.2, < 2.0)
52
+ mime-types (>= 1.16, < 3.0)
53
+ netrc (~> 0.7)
54
+ rspec (3.2.0)
55
+ rspec-core (~> 3.2.0)
56
+ rspec-expectations (~> 3.2.0)
57
+ rspec-mocks (~> 3.2.0)
58
+ rspec-core (3.2.3)
59
+ rspec-support (~> 3.2.0)
60
+ rspec-expectations (3.2.1)
61
+ diff-lcs (>= 1.2.0, < 2.0)
62
+ rspec-support (~> 3.2.0)
63
+ rspec-mocks (3.2.1)
64
+ diff-lcs (>= 1.2.0, < 2.0)
65
+ rspec-support (~> 3.2.0)
66
+ rspec-support (3.2.2)
67
+ rubocop (0.30.0)
68
+ astrolabe (~> 1.3)
69
+ parser (>= 2.2.0.1, < 3.0)
70
+ powerpack (~> 0.1)
71
+ rainbow (>= 1.99.1, < 3.0)
72
+ ruby-progressbar (~> 1.4)
73
+ ruby-progressbar (1.7.5)
74
+ safe_yaml (1.0.4)
75
+ simplecov (0.9.2)
76
+ docile (~> 1.1.0)
77
+ multi_json (~> 1.0)
78
+ simplecov-html (~> 0.9.0)
79
+ simplecov-html (0.9.0)
80
+ slop (3.6.0)
81
+ term-ansicolor (1.3.0)
82
+ tins (~> 1.0)
83
+ thor (0.19.1)
84
+ tins (1.3.5)
85
+ unf (0.1.4)
86
+ unf_ext
87
+ unf_ext (0.0.6)
88
+ webmock (1.21.0)
89
+ addressable (>= 2.3.6)
90
+ crack (>= 0.3.2)
91
+ yard (0.8.7.6)
92
+ yardstick (0.9.9)
93
+ yard (~> 0.8, >= 0.8.7.2)
94
+
95
+ PLATFORMS
96
+ ruby
97
+
98
+ DEPENDENCIES
99
+ bundler (~> 1.7)
100
+ codeclimate-test-reporter
101
+ coveralls
102
+ ec2_meta!
103
+ gem-release
104
+ pry-byebug
105
+ rake (~> 10.0)
106
+ rspec (~> 3.2)
107
+ rspec-mocks (~> 3.2)
108
+ rubocop (~> 0.30)
109
+ simplecov (~> 0.9)
110
+ webmock (~> 1.21)
111
+ yard (~> 0.8)
112
+ yardstick (~> 0.9)
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Leonis & Co.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # Ec2Meta
2
+
3
+ [![Build Status](https://travis-ci.org/leonis/ec2_meta.svg?branch=master)](https://travis-ci.org/leonis/ec2_meta)
4
+ [![Coverage Status](https://coveralls.io/repos/leonis/ec2_meta/badge.svg)](https://coveralls.io/r/leonis/ec2_meta)
5
+ [![Code Climate](https://codeclimate.com/github/leonis/ec2_meta/badges/gpa.svg)](https://codeclimate.com/github/leonis/ec2_meta)
6
+
7
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ec2_meta`. To experiment with that code, run `bin/console` for an interactive prompt.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'ec2_meta'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install ec2_meta
24
+
25
+ ## Usage
26
+
27
+ ### Get Client
28
+
29
+ ```ruby
30
+ # without options
31
+ client = Ec2Meta.client
32
+
33
+ # with all options
34
+ client = Ec2Meta.client(
35
+ logger: Logger.new($stdout),
36
+ fail_on_not_found: true
37
+ )
38
+ ```
39
+
40
+ #### options
41
+
42
+ name | default | description
43
+ ---- | ------- | ------------
44
+ logger | Ec2Meta::NullLogger.new | Pass your logger if you want
45
+ fail_on_not_found | false | If set true, raise error on metadata not found.
46
+
47
+ ### Get Meta
48
+
49
+ Currently supports all of `Instance metadata` on [Instance Metadata and User Data - Instance Metadata Categories](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html#instancedata-data-categories) at this time.
50
+
51
+ [Supported Meta Apis](https://github.com/leonis/ec2_meta/wiki/Supported-Meta-Apis)
52
+
53
+ #### Sample Code
54
+
55
+ You can fetch meta data like below.
56
+
57
+ ```ruby
58
+ # fetch ami-id
59
+ client.ami_id
60
+
61
+ # fetch hostname
62
+ client.hostname
63
+
64
+ # fetch vpc_id for first network interface
65
+ client.network.interfaces.macs(0).vpc_id
66
+ ```
67
+
68
+ When meta data not found, return `nil` if `fail_on_not_found` is not true.
69
+
70
+ ## License
71
+
72
+ MIT License
73
+
74
+ ## Contributing
75
+
76
+ 1. Fork it ( https://github.com/[my-github-username]/ec2_meta/fork )
77
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
78
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
79
+ 4. Push to the branch (`git push origin my-new-feature`)
80
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'bundler/gem_tasks'
5
+
6
+ require 'rspec/core/rake_task'
7
+ RSpec::Core::RakeTask.new(:spec)
8
+ task test: :spec
9
+
10
+ require 'rubocop/rake_task'
11
+ RuboCop::RakeTask.new
12
+
13
+ require 'yard'
14
+ YARD::Rake::YardocTask.new
15
+
16
+ require 'yardstick/rake/measurement'
17
+ Yardstick::Rake::Measurement.new do |measurement|
18
+ measurement.output = ' measurement/report.txt'
19
+ end
20
+
21
+ task default: [:spec]
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ec2_meta"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/ec2_meta.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ec2_meta/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'ec2_meta'
8
+ spec.version = Ec2Meta::VERSION
9
+ spec.authors = ['daisuke.hirakiuchi']
10
+ spec.email = ['devops@leonisand.co']
11
+ spec.licenses = ['MIT']
12
+
13
+ spec.summary = 'AWS Metadata API client library'
14
+ spec.description = 'AWS Metadata API client library. This gem is available only on AWS EC2 instance.'
15
+ spec.homepage = 'https://github.com/leonis/ec2_meta'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'bundler', '~> 1.7'
21
+ spec.add_development_dependency 'rake', '~> 10.0'
22
+ spec.add_development_dependency 'rspec', '~> 3.2'
23
+ spec.add_development_dependency 'rspec-mocks', '~> 3.2'
24
+ spec.add_development_dependency 'webmock', '~> 1.21'
25
+ spec.add_development_dependency 'rubocop', '~> 0.30'
26
+ spec.add_development_dependency 'simplecov', '~> 0.9'
27
+ spec.add_development_dependency 'yard', '~> 0.8'
28
+ spec.add_development_dependency 'yardstick', '~> 0.9'
29
+ end
@@ -0,0 +1,9 @@
1
+ module Ec2Meta
2
+ module Api20140225
3
+ module Base
4
+ def self.extended(obj)
5
+ obj.extend Ec2Meta::Api20140225::MetaData
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,238 @@
1
+ module Ec2Meta
2
+ module Api20140225
3
+ module MetaData
4
+ def meta_data
5
+ MetaData.new(fetcher, 'meta-data')
6
+ end
7
+
8
+ class BlockDeviceMapping < Ec2Meta::Api::Path
9
+ # Fetch 'block-device-mapping/ami'
10
+ def ami
11
+ fetch('ami')
12
+ end
13
+
14
+ # Fetch 'block-device-mapping/ebs#{no}'
15
+ def ebs(no)
16
+ fetch("ebs#{no}")
17
+ end
18
+
19
+ # Fetch 'block-device-mapping/ephemeral#{no}'
20
+ def ephemeral(no)
21
+ fetch("ephemeral#{no}")
22
+ end
23
+
24
+ # Fetch 'block-device-mapping/root'
25
+ def root
26
+ fetch('root')
27
+ end
28
+
29
+ # Fetch 'block-device-mapping/swap'
30
+ def swap
31
+ fetch('swap')
32
+ end
33
+ end
34
+
35
+ class Iam < Ec2Meta::Api::Path
36
+ # Fetch 'iam/info'
37
+ def info
38
+ fetch('info')
39
+ end
40
+
41
+ # Fetch 'iam/security-credentials/#{role_name}'
42
+ def security_credentials(role_name)
43
+ fetch("security-credentials/#{role_name}")
44
+ end
45
+ end
46
+
47
+ class Network < Ec2Meta::Api::Path
48
+ def interfaces
49
+ Interfaces.new(fetcher, new_prefix('interfaces'))
50
+ end
51
+ end
52
+
53
+ class Interfaces < Ec2Meta::Api::Path
54
+ def macs(mac = nil)
55
+ case mac
56
+ when ::String
57
+ mac_address_for(mac)
58
+ when ::Integer
59
+ mac_address_at(mac)
60
+ when nil
61
+ fetch_macs
62
+ else
63
+ ::Kernel.raise ::ArgumentError, 'require String or Integer, or nil.'
64
+ end
65
+ end
66
+
67
+ private
68
+
69
+ def fetch_macs
70
+ result = fetch('macs/')
71
+ (result.nil? ? [] : result.split("\n").map { |v| v.chomp('/') })
72
+ end
73
+
74
+ def mac_address_for(address)
75
+ MacAddress.new(fetcher, new_prefix("macs/#{address}"), address)
76
+ end
77
+
78
+ def mac_address_at(position)
79
+ addr = fetch_macs.at(position)
80
+ return nil if addr.nil?
81
+
82
+ mac_address_for(addr)
83
+ end
84
+ end
85
+
86
+ class MacAddress < Ec2Meta::Api::Path
87
+ def initialize(fetcher, prefix, mac_addr)
88
+ super(fetcher, prefix)
89
+ @mac_addr = mac_addr
90
+ end
91
+
92
+ # Fetch network/interfaces/macs/#{mac}/device-number
93
+ def device_number
94
+ fetch('device-number')
95
+ end
96
+
97
+ # Fetch network/interfaces/macs/#{mac}/ipv4-associations/#{public-ip}
98
+ def ipv4_associations(public_ip)
99
+ fetch("ipv4-associations/#{public_ip}")
100
+ end
101
+
102
+ # Fetch network/interfaces/macs/#{mac}/local-hostname
103
+ def local_hostname
104
+ fetch('local-hostname')
105
+ end
106
+
107
+ def local_ipv4s
108
+ fetch('local-ipv4s')
109
+ end
110
+
111
+ def mac
112
+ fetch('mac')
113
+ end
114
+
115
+ def owner_id
116
+ fetch('owner-id')
117
+ end
118
+
119
+ def public_hostname
120
+ fetch('public-hostname')
121
+ end
122
+
123
+ def public_ipv4s
124
+ fetch('public-ipv4s')
125
+ end
126
+
127
+ def security_groups
128
+ fetch('security-groups')
129
+ end
130
+
131
+ def security_group_ids
132
+ fetch('security-group-ids')
133
+ end
134
+
135
+ def subnet_id
136
+ fetch('subnet-id')
137
+ end
138
+
139
+ def subnet_ipv4_cidr_block
140
+ fetch('subnet-ipv4-cidr-block')
141
+ end
142
+
143
+ def vpc_id
144
+ fetch('vpc-id')
145
+ end
146
+
147
+ def vpc_ipv4_cidr_block
148
+ fetch('vpc-ipv4-cidr-block')
149
+ end
150
+ end
151
+
152
+ class Placement < Ec2Meta::Api::Path
153
+ def availability_zone
154
+ fetch('availability-zone')
155
+ end
156
+ end
157
+
158
+ class PublicKeys < Ec2Meta::Api::Path
159
+ def initialize(fetcher, prefix, no)
160
+ super(fetcher, prefix)
161
+ @key_no = no
162
+ end
163
+
164
+ def openssh_key
165
+ fetch('openssh-key')
166
+ end
167
+ end
168
+
169
+ class Services < Ec2Meta::Api::Path
170
+ def domain
171
+ fetch('domain')
172
+ end
173
+ end
174
+
175
+ class Spot < Ec2Meta::Api::Path
176
+ def termination_time
177
+ fetch('termination-time')
178
+ end
179
+ end
180
+
181
+ class MetaData < Ec2Meta::Api::Path
182
+ %w[
183
+ ami-id
184
+ ami-launch-index
185
+ ami-manifest-path
186
+ ancestor-ami-ids
187
+ hostname
188
+ instance-action
189
+ instance-id
190
+ instance-type
191
+ kernel-id
192
+ local-hostname
193
+ local-ipv4
194
+ mac
195
+ product-codes
196
+ public-hostname
197
+ public-ipv4
198
+ ramdisk-id
199
+ reservation-id
200
+ reservation-id
201
+ security-groups
202
+ ].each do |attr|
203
+ define_method(attr.gsub('-', '_')) do
204
+ fetch(attr)
205
+ end
206
+ end
207
+
208
+ def block_device_mapping
209
+ BlockDeviceMapping.new(fetcher, new_prefix('block-device-mapping'))
210
+ end
211
+
212
+ def iam
213
+ Iam.new(fetcher, new_prefix('iam'))
214
+ end
215
+
216
+ def network
217
+ Network.new(fetcher, new_prefix('network'))
218
+ end
219
+
220
+ def placement
221
+ Placement.new(fetcher, new_prefix('placement'))
222
+ end
223
+
224
+ def public_keys(no)
225
+ PublicKeys.new(fetcher, new_prefix("public-keys/#{no}"), no)
226
+ end
227
+
228
+ def services
229
+ Services.new(fetcher, new_prefix('services'))
230
+ end
231
+
232
+ def spot
233
+ Spot.new(fetcher, new_prefix('spot'))
234
+ end
235
+ end
236
+ end
237
+ end
238
+ end
@@ -0,0 +1,31 @@
1
+ module Ec2Meta
2
+ module Api
3
+ class Path < BasicObject
4
+ attr_reader :prefix
5
+
6
+ def initialize(fetcher, prefix = [])
7
+ @_fetcher = fetcher
8
+ @prefix = (prefix.is_a?(::String) ? [prefix] : prefix)
9
+ end
10
+
11
+ private
12
+
13
+ def fetch(path)
14
+ fetcher.fetch(request_path(path))
15
+ end
16
+
17
+ def request_path(path)
18
+ path_prefix = prefix.empty? ? '' : '/' + prefix.join('/')
19
+ path_prefix + '/' + path
20
+ end
21
+
22
+ def new_prefix(part)
23
+ prefix.dup << part
24
+ end
25
+
26
+ def fetcher
27
+ @_fetcher
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,36 @@
1
+ module Ec2Meta
2
+ class Cache
3
+ def initialize
4
+ @cache = {}
5
+ end
6
+
7
+ def write(key, value)
8
+ @cache[key.to_s] = value
9
+ end
10
+
11
+ def read(key)
12
+ @cache[key.to_s]
13
+ end
14
+
15
+ def fetch(key)
16
+ return read(key) if exist?(key)
17
+
18
+ fail ArgumentError, 'require block' unless block_given?
19
+
20
+ value = yield
21
+ write(key, value)
22
+ end
23
+
24
+ def exist?(key)
25
+ @cache.key?(key.to_s)
26
+ end
27
+
28
+ def delete(key)
29
+ @cache.delete(key)
30
+ end
31
+
32
+ def clear
33
+ @cache.clear
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,22 @@
1
+ module Ec2Meta
2
+ class Client
3
+ def initialize(options = {})
4
+ @options = {
5
+ api_version: '2014-02-25'
6
+ }.merge(options)
7
+
8
+ load_api_module
9
+ end
10
+
11
+ private
12
+
13
+ def fetcher
14
+ return @fetcher if @fetcher
15
+ @fetcher = Fetcher.new(@options)
16
+ end
17
+
18
+ def load_api_module
19
+ extend ApiLoader.api_module(@options[:api_version])
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,60 @@
1
+ require 'net/http'
2
+ require 'timeout'
3
+
4
+ module Ec2Meta
5
+ class MetaNotFound < StandardError; end
6
+
7
+ class Fetcher
8
+ API_HOST = '169.254.169.254'.freeze
9
+ API_VERSION = '2014-02-25'.freeze
10
+
11
+ def initialize(options = {})
12
+ @options = options
13
+ @cache = ::Ec2Meta::Cache.new
14
+ end
15
+
16
+ def fetch(path)
17
+ @cache.fetch(path) do
18
+ res = http_client.get(request_path(path))
19
+ break res.body if res.code != '404'
20
+
21
+ fail MetaNotFound, "'#{path}' not found." if fail_on_not_found?
22
+ nil
23
+ end
24
+ rescue Timeout::Error => e
25
+ logger.error 'Can\'t fetch EC2 metadata from EC2 METADATA API.'
26
+ logger.error 'ec2_meta gem is only available on AWS EC2 instance.'
27
+ raise e
28
+ rescue MetaNotFound => e
29
+ raise e
30
+ rescue => e
31
+ logger.error "Can't fetch EC2 metadata from EC2 METADATA API.(#{e.message})"
32
+ logger.error e.backtrace.join("\n")
33
+ raise e
34
+ end
35
+
36
+ private
37
+
38
+ def logger
39
+ @options[:logger]
40
+ end
41
+
42
+ def http_client
43
+ return @http_client unless @http_client.nil?
44
+
45
+ @http_client = Net::HTTP.new(API_HOST)
46
+ @http_client.open_timeout = @options[:open_timeout] || 2.0
47
+ @http_client.read_timeout = @options[:read_timeout] || 2.0
48
+
49
+ @http_client
50
+ end
51
+
52
+ def request_path(path)
53
+ '/' + API_VERSION + path
54
+ end
55
+
56
+ def fail_on_not_found?
57
+ @options[:fail_on_not_found]
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,14 @@
1
+ module Ec2Meta
2
+ class NotSupported < StandardError; end
3
+
4
+ class ApiLoader
5
+ def self.api_module(api_version)
6
+ fail 'require valid api_version' if api_version.nil? || api_version.empty?
7
+
8
+ api_namespace = Ec2Meta.const_get('Api' + api_version.gsub('-', ''))
9
+ api_namespace.const_get('Base')
10
+ rescue NameError
11
+ raise NotSupported, "Not Supported the api_version: #{api_version}"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,30 @@
1
+ module Ec2Meta
2
+ class NullLogger
3
+ def debug(_progname = nil, &_block)
4
+ end
5
+
6
+ def debug?
7
+ false
8
+ end
9
+
10
+ def error(_progname = nil, &_block); end
11
+
12
+ def error?
13
+ false
14
+ end
15
+
16
+ def fatal(_progname = nil, &_block)
17
+ end
18
+
19
+ def fatal?
20
+ false
21
+ end
22
+
23
+ def info(_progname = nil, &_block)
24
+ end
25
+
26
+ def info?
27
+ false
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module Ec2Meta
2
+ VERSION = "0.1.0"
3
+ end
data/lib/ec2_meta.rb ADDED
@@ -0,0 +1,27 @@
1
+ require 'logger'
2
+
3
+ require 'ec2_meta/version'
4
+ require 'ec2_meta/client'
5
+ require 'ec2_meta/loader'
6
+ require 'ec2_meta/cache'
7
+ require 'ec2_meta/fetcher'
8
+ require 'ec2_meta/null_logger'
9
+ require 'ec2_meta/apis/path.rb'
10
+
11
+ require 'ec2_meta/apis/2014_02_25/base'
12
+ require 'ec2_meta/apis/2014_02_25/meta_data'
13
+
14
+ module Ec2Meta
15
+ # Your code goes here...
16
+ class << self
17
+ attr_reader :options
18
+
19
+ def client(options = {})
20
+ opts = {
21
+ logger: ::Ec2Meta::NullLogger.new
22
+ }.merge(options)
23
+
24
+ Client.new(opts)
25
+ end
26
+ end
27
+ end
data/sample.rb ADDED
@@ -0,0 +1,156 @@
1
+ require 'pry'
2
+ require 'rubygems'
3
+ require 'bundler/setup'
4
+
5
+ require 'ec2_meta'
6
+ require 'pry'
7
+
8
+ client = Ec2Meta.client
9
+
10
+ m = client.meta_data
11
+
12
+ puts '#ami-id'
13
+ p m.ami_id
14
+
15
+ puts '#ami-launch-index'
16
+ p m.ami_launch_index
17
+
18
+ puts '#ami-manifest-path'
19
+ p m.ami_manifest_path
20
+
21
+ puts '#ancestor-ami-ids'
22
+ p m.ancestor_ami_ids
23
+
24
+ puts '#block-device-mapping/ami'
25
+ p m.block_device_mapping.ami
26
+
27
+ # TODO: change no
28
+ puts '#block-device-mapping/ebs0'
29
+ p m.block_device_mapping.ebs(0)
30
+
31
+ # TODO: change no
32
+ puts '#block-device-mapping/ephemeral0'
33
+ p m.block_device_mapping.ephemeral(0)
34
+
35
+ puts '#block-device-mapping/root'
36
+ p m.block_device_mapping.root
37
+
38
+ puts '#block-device-mapping/swap'
39
+ p m.block_device_mapping.swap
40
+
41
+ puts '#hostname'
42
+ p m.hostname
43
+
44
+ puts '#iam/info'
45
+ p m.iam.info
46
+
47
+ # TODO: change role-name
48
+ puts '#iam/security-credentials/#{role-name}'
49
+ p m.iam.security_credentials('role-name')
50
+
51
+ puts '#instance-action'
52
+ p m.instance_action
53
+
54
+ puts '#instance-id'
55
+ p m.instance_id
56
+
57
+ puts '#instance-type'
58
+ p m.instance_type
59
+
60
+ puts '#kernel-id'
61
+ p m.kernel_id
62
+
63
+ puts '#local-hostname'
64
+ p m.local_hostname
65
+
66
+ puts '#local-ipv4'
67
+ p m.local_ipv4
68
+
69
+ puts '#mac'
70
+ p m.mac
71
+
72
+ puts '#network/interfaces/macs/0'
73
+ #p m.network.interfaces.macs(0)
74
+
75
+ puts '#network/interfaces/macs/0/device-number'
76
+ p m.network.interfaces.macs(0).device_number
77
+
78
+ p m.network.interfaces.macs(m.mac).device_number
79
+
80
+ ### network/interfaces/macs/#{mac}
81
+ puts '-------------'
82
+ mac = m.network.interfaces.macs('mac')
83
+
84
+ puts '#network/interfaces/macs/#{mac}/device-number'
85
+ p mac.device_number
86
+
87
+ puts '#network/interfaces/macs/#{mac}/ipv4-associations/#{public-ip}'
88
+ p mac.ipv4_associations('public-ip')
89
+
90
+ puts '#network/interfaces/macs/#{mac}/local-hostname'
91
+ p mac.local_hostname
92
+
93
+ puts '#network/interfaces/macs/#{mac}/local-ipv4s'
94
+ p mac.local_ipv4s
95
+
96
+ puts '#network/interfaces/macs/#{mac}/mac'
97
+ p mac.mac
98
+
99
+ puts '#network/interfaces/macs/#{mac}/owner-id'
100
+ p mac.owner_id
101
+
102
+ puts '#network/interfaces/macs/#{mac}/public-hostname'
103
+ p mac.public_hostname
104
+
105
+ puts '#network/interfaces/macs/#{mac}/public-ipv4s'
106
+ p mac.public_ipv4s
107
+
108
+ puts '#network/interfaces/macs/#{mac}/security-groups'
109
+ p mac.security_groups
110
+
111
+ puts '#network/interfaces/macs/#{mac}/security-group-ids'
112
+ p mac.security_group_ids
113
+
114
+ puts '#network/interfaces/macs/#{mac}/subnet-id'
115
+ p mac.subnet_id
116
+
117
+ puts '#network/interfaces/macs/#{mac}/subnet-ipv4-cidr-block'
118
+ p mac.subnet_ipv4_cidr_block
119
+
120
+ puts '#network/interfaces/macs/#{mac}/vpc-id'
121
+ p mac.vpc_id
122
+
123
+ puts '#network/interfaces/macs/#{mac}/vpc-ipv4-cidr-block'
124
+ p mac.vpc_ipv4_cidr_block
125
+
126
+ puts '-------------'
127
+
128
+ puts '#placement/availability-zone'
129
+ p m.placement.availability_zone
130
+
131
+ puts '#product-codes'
132
+ p m.product_codes
133
+
134
+ puts '#public-hostname'
135
+ p m.public_hostname
136
+
137
+ puts '#public-keys/0/openssh-key'
138
+ p m.public_keys(0).openssh_key
139
+
140
+ puts '#ramdisk-id'
141
+ p m.ramdisk_id
142
+
143
+ puts '#security-groups'
144
+ p m.security_groups
145
+
146
+ puts '#reservation_id'
147
+ p m.reservation_id
148
+
149
+ puts '#security-groups'
150
+ p m.security_groups
151
+
152
+ puts '#services/domain'
153
+ p m.services.domain
154
+
155
+ puts '#spot/termination-time'
156
+ p m.spot.termination_time
metadata ADDED
@@ -0,0 +1,195 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ec2_meta
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - daisuke.hirakiuchi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-mocks
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.21'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.21'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.30'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.30'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.9'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.9'
111
+ - !ruby/object:Gem::Dependency
112
+ name: yard
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.8'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.8'
125
+ - !ruby/object:Gem::Dependency
126
+ name: yardstick
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.9'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.9'
139
+ description: AWS Metadata API client library. This gem is available only on AWS EC2
140
+ instance.
141
+ email:
142
+ - devops@leonisand.co
143
+ executables: []
144
+ extensions: []
145
+ extra_rdoc_files: []
146
+ files:
147
+ - ".coveralls.yml"
148
+ - ".gitignore"
149
+ - ".rspec"
150
+ - ".travis.yml"
151
+ - Gemfile
152
+ - Gemfile.lock
153
+ - LICENSE
154
+ - README.md
155
+ - Rakefile
156
+ - bin/console
157
+ - bin/setup
158
+ - ec2_meta.gemspec
159
+ - lib/ec2_meta.rb
160
+ - lib/ec2_meta/apis/2014_02_25/base.rb
161
+ - lib/ec2_meta/apis/2014_02_25/meta_data.rb
162
+ - lib/ec2_meta/apis/path.rb
163
+ - lib/ec2_meta/cache.rb
164
+ - lib/ec2_meta/client.rb
165
+ - lib/ec2_meta/fetcher.rb
166
+ - lib/ec2_meta/loader.rb
167
+ - lib/ec2_meta/null_logger.rb
168
+ - lib/ec2_meta/version.rb
169
+ - sample.rb
170
+ homepage: https://github.com/leonis/ec2_meta
171
+ licenses:
172
+ - MIT
173
+ metadata: {}
174
+ post_install_message:
175
+ rdoc_options: []
176
+ require_paths:
177
+ - lib
178
+ required_ruby_version: !ruby/object:Gem::Requirement
179
+ requirements:
180
+ - - ">="
181
+ - !ruby/object:Gem::Version
182
+ version: '0'
183
+ required_rubygems_version: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ requirements: []
189
+ rubyforge_project:
190
+ rubygems_version: 2.2.2
191
+ signing_key:
192
+ specification_version: 4
193
+ summary: AWS Metadata API client library
194
+ test_files: []
195
+ has_rdoc: