aws-instmd 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: edc15ee2fa3f841c42e33bc67f8319bf61ea9b9a
4
+ data.tar.gz: 4cbf4b6966c05600ec4cc54b4ffb5288f6bbc8a4
5
+ SHA512:
6
+ metadata.gz: 9e90c96d4a01b4ed270fe373efe4f75668454042112902c61182203ebe9fbf4ec8f76f23c2994ca7f8911b438091c9f58851b72110b1db8c3a906daf08f52631
7
+ data.tar.gz: 602a47c9c23936765fa905cb0155ca4b801d7456913099f15ee0c0d2913960db413783d6fff4c9446b43f123fe68cfc51f7a9f7c0ce2d93c49203f6ec8a3b4e3
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # AWS::InstMD
2
+
3
+ AWS::InstMD exposes the instance metadata information through a Ruby API.
4
+
5
+ ## Why?
6
+
7
+ 169.254.169.254 does not offer a simple view of the whole instance metadata,
8
+ and querying the whole tree can be a bit quirky.
9
+
10
+ We wanted to solve that problem once and for all, expose it in a straightforward
11
+ and maintainable way.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ gem 'aws-instmd'
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install aws-instmd
26
+
27
+ ## Usage
28
+
29
+ ### Command-line utility
30
+
31
+ `awsinstmd` will dump the metadata in JSON.
32
+
33
+ ### Ruby API
34
+
35
+ The Ruby gem should be easy to use (feedback is obviously welcome).
36
+
37
+ Documentation should be improved; in the meantime, here is a simple example:
38
+
39
+ require 'aws/instmd'
40
+ puts AWS::InstMD.meta_data.instance_id
41
+ puts AWS::InstMD.meta_data.instance_type
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs = %w[lib test]
6
+ t.test_files = FileList['test/**/test*.rb']
7
+ end
8
+
9
+ desc 'Run tests'
10
+ task :default => :test
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path '../lib', __FILE__
3
+ $LOAD_PATH.unshift lib unless $LOAD_PATH.include? lib
4
+
5
+ require 'aws/instmd/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'aws-instmd'
9
+ spec.version = AWS::InstMD::VERSION
10
+ spec.authors = ['Pierre Carrier']
11
+ spec.email = ['pierre@gcarrier.fr']
12
+ spec.description = 'AWS instance metadata client'
13
+ spec.summary = 'Query the entire 169.254.169.254 tree in seconds'
14
+ spec.homepage = 'https://github.com/airbnb/gem-aws-instmd'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files`.split $/
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename f }
19
+ spec.test_files = spec.files.grep %r{^(test|spec|features)/}
20
+ spec.require_paths = %w[lib]
21
+
22
+ spec.add_dependency 'json'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.3'
25
+ spec.add_development_dependency 'rake'
26
+ end
data/bin/awsinstmd ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'aws/instmd'
4
+ require 'json'
5
+
6
+ puts JSON::pretty_generate AWS::InstMD.to_hash
data/lib/aws/instmd.rb ADDED
@@ -0,0 +1,71 @@
1
+ require 'net/http'
2
+
3
+ module AWS
4
+ class InstMD
5
+ def self.method_missing name, *args, &block
6
+ @@root ||= InstMD.new
7
+ @@root.method(name).call(*args, &block)
8
+ end
9
+
10
+ # Can't be the first one to make that pun.
11
+ # Still proud.
12
+ class Hashish < Hash
13
+ def method_missing name
14
+ self[name.to_s.gsub('_', '-')]
15
+ end
16
+ end
17
+
18
+ class Treeish < Hashish
19
+ private
20
+ def initialize http, prefix
21
+ entries = InstMD.query http, prefix
22
+ entries.lines.each do |l|
23
+ l.chomp!
24
+ if l.end_with? '/'
25
+ self[l[0..-2]] = Treeish.new http, "#{prefix}#{l}"
26
+ # meta-data/public-keys/ entries have a '0=foo' format
27
+ elsif l =~ /(\d+)=(.*)/
28
+ number, name = $1, $2
29
+ self[name] = Treeish.new http, "#{prefix}#{number}/"
30
+ else
31
+ self[l] = InstMD.query http, "#{prefix}#{l}"
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ attr_accessor :user_data, :meta_data, :dynamic
38
+
39
+ # Amazon, Y U NO trailing slash entries
40
+ # in /, /$version and /$version/dynamic/??
41
+ # There is waaay too much code here.
42
+ def initialize version='latest', host='169.254.169.254', port='80'
43
+ http = Net::HTTP.new host, port
44
+ @meta_data = Treeish.new http, "/#{version}/meta-data/"
45
+ @user_data = InstMD.query http, "/#{version}/user-data"
46
+ @dynamic = Hashish.new
47
+
48
+ begin
49
+ dynamic_stuff = InstMD.query(http, "/#{version}/dynamic/").lines
50
+ rescue
51
+ dynamic_stuff = []
52
+ end
53
+ dynamic_stuff.each do |e|
54
+ e = e.chomp.chomp '/'
55
+ @dynamic[e] = Treeish.new http, "/#{version}/dynamic/#{e}/"
56
+ end
57
+ end
58
+
59
+ def self.query http, path
60
+ rep = http.request Net::HTTP::Get.new path
61
+ unless Net::HTTPOK === rep
62
+ raise Net::HTTPBadResponse, "#{rep.code} #{path}"
63
+ end
64
+ rep.body
65
+ end
66
+
67
+ def to_hash
68
+ {:meta_data => @meta_data, :user_data => @user_data, :dynamic => @dynamic}
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,5 @@
1
+ module AWS
2
+ class InstMD
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aws-instmd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pierre Carrier
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: AWS instance metadata client
56
+ email:
57
+ - pierre@gcarrier.fr
58
+ executables:
59
+ - awsinstmd
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - Gemfile
64
+ - README.md
65
+ - Rakefile
66
+ - aws-instmd.gemspec
67
+ - bin/awsinstmd
68
+ - lib/aws/instmd.rb
69
+ - lib/aws/instmd/version.rb
70
+ homepage: https://github.com/airbnb/gem-aws-instmd
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.0.2
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Query the entire 169.254.169.254 tree in seconds
94
+ test_files: []
95
+ has_rdoc: