ec2-metadata 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.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +70 -0
- data/VERSION +1 -0
- data/lib/ec2_metadata.rb +48 -0
- data/lib/ec2_metadata/base.rb +90 -0
- data/lib/ec2_metadata/named_base.rb +17 -0
- data/lib/ec2_metadata/revision.rb +23 -0
- data/lib/ec2_metadata/root.rb +21 -0
- data/spec/ec2_metadata/base_spec.rb +65 -0
- data/spec/ec2_metadata/revision_spec.rb +31 -0
- data/spec/ec2_metadata/root_spec.rb +41 -0
- data/spec/ec2_metadata_spec.rb +39 -0
- data/spec/introduction_spec.rb +110 -0
- data/spec/spec.opts +3 -0
- data/spec/spec_helper.rb +33 -0
- metadata +98 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Takeshi AKIMA
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
= ec2-metadata
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2010 Takeshi AKIMA. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "ec2-metadata"
|
8
|
+
gem.summary = %Q{ec2-metadata provides to access metadata}
|
9
|
+
gem.description = %Q{ec2-metadata provides to access metadata, and you can use in outside of ec2 like in ec2}
|
10
|
+
gem.email = "akm2000@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/akm/ec2-metadata"
|
12
|
+
gem.authors = ["Takeshi AKIMA"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'spec/rake/spectask'
|
22
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
spec.libs << 'lib' << 'spec'
|
24
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
spec.rcov = true
|
31
|
+
end
|
32
|
+
|
33
|
+
task :spec => :check_dependencies
|
34
|
+
|
35
|
+
begin
|
36
|
+
require 'reek/adapters/rake_task'
|
37
|
+
Reek::RakeTask.new do |t|
|
38
|
+
t.fail_on_error = true
|
39
|
+
t.verbose = false
|
40
|
+
t.source_files = 'lib/**/*.rb'
|
41
|
+
end
|
42
|
+
rescue LoadError
|
43
|
+
task :reek do
|
44
|
+
abort "Reek is not available. In order to run reek, you must: sudo gem install reek"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
begin
|
49
|
+
require 'roodi'
|
50
|
+
require 'roodi_task'
|
51
|
+
RoodiTask.new do |t|
|
52
|
+
t.verbose = false
|
53
|
+
end
|
54
|
+
rescue LoadError
|
55
|
+
task :roodi do
|
56
|
+
abort "Roodi is not available. In order to run roodi, you must: sudo gem install roodi"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
task :default => :spec
|
61
|
+
|
62
|
+
require 'rake/rdoctask'
|
63
|
+
Rake::RDocTask.new do |rdoc|
|
64
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
65
|
+
|
66
|
+
rdoc.rdoc_dir = 'rdoc'
|
67
|
+
rdoc.title = "ec2-metadata #{version}"
|
68
|
+
rdoc.rdoc_files.include('README*')
|
69
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
70
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/ec2_metadata.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
|
3
|
+
module Ec2Metadata
|
4
|
+
DEFAULT_HOST = "169.254.169.254".freeze
|
5
|
+
|
6
|
+
autoload :Base, 'ec2_metadata/base'
|
7
|
+
autoload :NamedBase, 'ec2_metadata/named_base'
|
8
|
+
autoload :Root, 'ec2_metadata/root'
|
9
|
+
autoload :Revision, 'ec2_metadata/revision'
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def instance
|
13
|
+
@instance ||= Root.new
|
14
|
+
end
|
15
|
+
|
16
|
+
def clear_instance
|
17
|
+
@instance = nil
|
18
|
+
end
|
19
|
+
|
20
|
+
def [](key)
|
21
|
+
instance[key]
|
22
|
+
end
|
23
|
+
|
24
|
+
def get(path)
|
25
|
+
logging("Ec2Metadata.get(#{path.inspect})") do
|
26
|
+
Net::HTTP.get(DEFAULT_HOST, path)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def logging(msg)
|
31
|
+
@indent ||= 0
|
32
|
+
disp = (" " * @indent) << msg
|
33
|
+
# puts(disp)
|
34
|
+
@indent += 2
|
35
|
+
begin
|
36
|
+
result = yield
|
37
|
+
ensure
|
38
|
+
@indent -= 2
|
39
|
+
end
|
40
|
+
# puts "#{disp} => #{result.inspect}"
|
41
|
+
result
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class NotFoundError < StandardError
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'ec2_metadata'
|
2
|
+
|
3
|
+
module Ec2Metadata
|
4
|
+
class Base
|
5
|
+
|
6
|
+
attr_reader :path
|
7
|
+
attr_reader :default_child_key
|
8
|
+
|
9
|
+
def initialize(path, default_child_key = nil)
|
10
|
+
@path = path
|
11
|
+
@default_child_key = default_child_key
|
12
|
+
end
|
13
|
+
|
14
|
+
def children
|
15
|
+
@children ||= {}
|
16
|
+
end
|
17
|
+
|
18
|
+
def child_keys
|
19
|
+
unless defined?(@child_keys)
|
20
|
+
lines = Ec2Metadata.get("#{path}").split(/$/).map(&:strip)
|
21
|
+
if lines.all?{|line| line =~ /^[\w]+\=.*?/}
|
22
|
+
@child_keys = []
|
23
|
+
@child_names = {}
|
24
|
+
lines.each do |line|
|
25
|
+
key, name = line.split(/\=/, 2)
|
26
|
+
@child_keys << key
|
27
|
+
@child_names[key] = name
|
28
|
+
end
|
29
|
+
else
|
30
|
+
@child_keys = lines
|
31
|
+
end
|
32
|
+
end
|
33
|
+
@child_keys
|
34
|
+
end
|
35
|
+
alias_method :keys, :child_keys
|
36
|
+
|
37
|
+
def get(child_key)
|
38
|
+
logging("#{self.class.name}.get(#{child_key.inspect})") do
|
39
|
+
child_key = child_key.to_s.gsub(/_/, '-')
|
40
|
+
if children.has_key?(child_key)
|
41
|
+
result = children[child_key]
|
42
|
+
else
|
43
|
+
if is_child_key?(child_key)
|
44
|
+
result = is_struct?(child_key) ?
|
45
|
+
new_child(child_key) :
|
46
|
+
Ec2Metadata.get("#{path}#{child_key}")
|
47
|
+
else
|
48
|
+
raise NotFoundError, "#{path}#{child_key} not found" unless default_child
|
49
|
+
result = default_child.get(child_key)
|
50
|
+
end
|
51
|
+
children[child_key] = result
|
52
|
+
end
|
53
|
+
result
|
54
|
+
end
|
55
|
+
end
|
56
|
+
alias_method :[], :get
|
57
|
+
|
58
|
+
def is_child_key?(key)
|
59
|
+
exp = /^#{key}\/?$/
|
60
|
+
child_keys.any?{|child_key| child_key =~ exp}
|
61
|
+
end
|
62
|
+
|
63
|
+
def is_struct?(child_key)
|
64
|
+
k = child_key.to_s.gsub(/_/, '-') << '/'
|
65
|
+
child_keys.include?(k) || (defined?(@child_names) && @child_names.keys.include?(child_key))
|
66
|
+
end
|
67
|
+
|
68
|
+
def new_child(child_key)
|
69
|
+
if defined?(@child_names) && (name = @child_names[child_key])
|
70
|
+
NamedBase.new(name, "#{path}#{child_key}/")
|
71
|
+
else
|
72
|
+
Base.new("#{path}#{child_key}/")
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
alias_method :[], :get
|
77
|
+
|
78
|
+
def default_child
|
79
|
+
logging("default_child") do
|
80
|
+
@default_child ||= get(default_child_key) if default_child_key
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
private
|
85
|
+
def logging(msg, &block)
|
86
|
+
Ec2Metadata.logging(msg, &block)
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'ec2_metadata'
|
2
|
+
|
3
|
+
module Ec2Metadata
|
4
|
+
class NamedBase < Base
|
5
|
+
attr_reader :name
|
6
|
+
|
7
|
+
def initialize(name, path, default_child_key = nil)
|
8
|
+
super(path, default_child_key)
|
9
|
+
@name = name
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_s
|
13
|
+
@name
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'ec2_metadata'
|
2
|
+
|
3
|
+
module Ec2Metadata
|
4
|
+
class Revision < Base
|
5
|
+
def initialize(path)
|
6
|
+
@path = path
|
7
|
+
@default_child_key = 'meta-data'
|
8
|
+
end
|
9
|
+
|
10
|
+
def new_child(child_key)
|
11
|
+
logging("new_child(#{child_key.inspect})") do
|
12
|
+
child_path = "#{path}#{child_key}"
|
13
|
+
child_path << '/' if is_struct?(child_key)
|
14
|
+
Base.new(child_path)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def is_struct?(child_key)
|
19
|
+
child_key =~ /^meta-data\/?$/
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'ec2_metadata'
|
2
|
+
|
3
|
+
module Ec2Metadata
|
4
|
+
class Root < Base
|
5
|
+
def initialize(path = '/')
|
6
|
+
@path = path
|
7
|
+
@default_child_key = 'latest'
|
8
|
+
end
|
9
|
+
|
10
|
+
def new_child(child_key)
|
11
|
+
logging("new_child(#{child_key.inspect})") do
|
12
|
+
Revision.new("#{path}#{child_key}/")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def is_struct?(child_key)
|
17
|
+
true
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '../spec_helper'))
|
2
|
+
|
3
|
+
describe Ec2Metadata::Base do
|
4
|
+
|
5
|
+
describe :[] do
|
6
|
+
|
7
|
+
REVISIONS.each do |revision|
|
8
|
+
describe revision do
|
9
|
+
before do
|
10
|
+
@meta_data = Ec2Metadata::Base.new("/#{revision}/meta-data/")
|
11
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/#{revision}/meta-data/").once.
|
12
|
+
and_return(ALL_ATTR_KEYS.join("\n"))
|
13
|
+
end
|
14
|
+
|
15
|
+
SIMPLE_ATTR_KEYS.each do |attr_key|
|
16
|
+
it "(#{attr_key.gsub(/-/, '_').inspect}) should return #{attr_key}" do
|
17
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/#{revision}/meta-data/#{attr_key}").once.
|
18
|
+
and_return("#{revision}_#{attr_key}")
|
19
|
+
@meta_data[attr_key].should == "#{revision}_#{attr_key}"
|
20
|
+
@meta_data[attr_key.to_sym].should == "#{revision}_#{attr_key}"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
it "('placement') should return object like Hash" do
|
25
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/#{revision}/meta-data/placement/").and_return("availability-zone")
|
26
|
+
obj = @meta_data[:placement]
|
27
|
+
obj.child_keys.should == ["availability-zone"]
|
28
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/#{revision}/meta-data/placement/availability-zone").and_return("us-east-1a")
|
29
|
+
obj[:availability_zone].should == "us-east-1a"
|
30
|
+
end
|
31
|
+
|
32
|
+
it "('block_device_mapping') should return object like Hash" do
|
33
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/#{revision}/meta-data/block-device-mapping/").and_return(["ami", "ephemeral0", "root", "swap"].join("\n"))
|
34
|
+
obj = @meta_data[:block_device_mapping]
|
35
|
+
obj.child_keys.should == ["ami", "ephemeral0", "root", "swap"]
|
36
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/#{revision}/meta-data/block-device-mapping/ami").and_return("sda1")
|
37
|
+
obj[:ami].should == "sda1"
|
38
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/#{revision}/meta-data/block-device-mapping/ephemeral0").and_return("sda2")
|
39
|
+
obj[:ephemeral0].should == "sda2"
|
40
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/#{revision}/meta-data/block-device-mapping/root").and_return("/dev/sda1")
|
41
|
+
obj[:root].should == "/dev/sda1"
|
42
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/#{revision}/meta-data/block-device-mapping/swap").and_return("sda3")
|
43
|
+
obj[:swap].should == "sda3"
|
44
|
+
end
|
45
|
+
|
46
|
+
it "('public_keys') should return object like Hash" do
|
47
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/#{revision}/meta-data/public-keys/").and_return("0=keypair0")
|
48
|
+
obj = @meta_data[:public_keys]
|
49
|
+
obj.child_keys.should == ["0"]
|
50
|
+
obj.class.should == Ec2Metadata::Base
|
51
|
+
key0 = obj["0"]
|
52
|
+
key0.to_s.should == "keypair0"
|
53
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/#{revision}/meta-data/public-keys/0/").and_return("openssh-key")
|
54
|
+
key0.child_keys.should == ["openssh-key"]
|
55
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/#{revision}/meta-data/public-keys/0/openssh-key").and_return("ssh-rsa 1234567890")
|
56
|
+
key0[:openssh_key].should == "ssh-rsa 1234567890"
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '../spec_helper'))
|
2
|
+
|
3
|
+
describe Ec2Metadata::Root do
|
4
|
+
|
5
|
+
describe :[] do
|
6
|
+
REVISIONS.each do |revision|
|
7
|
+
|
8
|
+
describe revision do
|
9
|
+
before do
|
10
|
+
@rev_obj = Ec2Metadata::Revision.new("/#{revision}/")
|
11
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/#{revision}/").once.
|
12
|
+
and_return(DATA_TYPES.join("\n"))
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should return Ec2Metadata::Base for meta_data" do
|
16
|
+
meta_data = @rev_obj[:meta_data]
|
17
|
+
meta_data.class.should == Ec2Metadata::Base
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should return Ec2Metadata::Base for user_data" do
|
21
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/#{revision}/user-data").once.
|
22
|
+
and_return("#{revision}-user_data")
|
23
|
+
user_data = @rev_obj[:user_data]
|
24
|
+
user_data.should == "#{revision}-user_data"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '../spec_helper'))
|
2
|
+
|
3
|
+
describe Ec2Metadata::Root do
|
4
|
+
|
5
|
+
describe :[] do
|
6
|
+
before do
|
7
|
+
@root = Ec2Metadata::Root.new
|
8
|
+
end
|
9
|
+
|
10
|
+
REVISIONS.each do |rev|
|
11
|
+
it "should return Revision for #{rev}" do
|
12
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/").once.
|
13
|
+
and_return(REVISIONS.join("\n"))
|
14
|
+
revision = @root[rev]
|
15
|
+
revision.class.should == Ec2Metadata::Revision
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should return latest DataType for user-data" do
|
20
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/").once.
|
21
|
+
and_return(REVISIONS.join("\n"))
|
22
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/latest/").once.
|
23
|
+
and_return(DATA_TYPES.join("\n"))
|
24
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/latest/user-data").once.
|
25
|
+
and_return("test-user-data1")
|
26
|
+
obj = @root['user-data']
|
27
|
+
obj.should == "test-user-data1"
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should return latest DataType for meta-data" do
|
31
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/").once.
|
32
|
+
and_return(REVISIONS.join("\n"))
|
33
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/latest/").once.
|
34
|
+
and_return(DATA_TYPES.join("\n"))
|
35
|
+
Net::HTTP.should_not_receive(:get).with("169.254.169.254", "/latest/meta-data/")
|
36
|
+
obj = @root['meta-data']
|
37
|
+
obj.class.should == Ec2Metadata::Base
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe Ec2Metadata do
|
4
|
+
|
5
|
+
describe :[] do
|
6
|
+
before do
|
7
|
+
Ec2Metadata.clear_instance
|
8
|
+
end
|
9
|
+
|
10
|
+
SIMPLE_ATTR_KEYS.each do |attr_key|
|
11
|
+
it "(#{attr_key.gsub(/-/, '_')}) should return value of respose for http://169.254.169.254/latest/meta-data/#{attr_key}" do
|
12
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/").once.and_return(REVISIONS.join("\n"))
|
13
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/latest/").once.and_return(DATA_TYPES.join("\n"))
|
14
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/latest/meta-data/").once.and_return(ALL_ATTR_KEYS.join("\n"))
|
15
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/latest/meta-data/#{attr_key}").once.and_return("latest_#{attr_key}")
|
16
|
+
Ec2Metadata[attr_key].should == "latest_#{attr_key}"
|
17
|
+
Ec2Metadata[attr_key.to_sym].should == "latest_#{attr_key}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
REVISIONS.each do |rev|
|
22
|
+
describe "with revision #{rev}" do
|
23
|
+
it "('#{rev}')[attr_key] should return value of respose for http://169.254.169.254/#{rev}/meta-data/attr_key" do
|
24
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/").and_return(REVISIONS.join("\n"))
|
25
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/#{rev}/").once.and_return(DATA_TYPES.join("\n"))
|
26
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/#{rev}/meta-data/").once.and_return(ALL_ATTR_KEYS.join("\n"))
|
27
|
+
|
28
|
+
SIMPLE_ATTR_KEYS.each do |attr_key|
|
29
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/#{rev}/meta-data/#{attr_key}").once.and_return("#{rev}_#{attr_key}")
|
30
|
+
Ec2Metadata[rev][attr_key].should == "#{rev}_#{attr_key}"
|
31
|
+
Ec2Metadata[rev.to_sym][attr_key.to_sym].should == "#{rev}_#{attr_key}"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe Ec2Metadata do
|
4
|
+
|
5
|
+
before do
|
6
|
+
Ec2Metadata.clear_instance
|
7
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/").once.and_return(REVISIONS.join("\n"))
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "normal usage" do
|
11
|
+
before do
|
12
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/latest/").once.and_return(DATA_TYPES.join("\n"))
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "user-data" do
|
16
|
+
it "should access user-data" do
|
17
|
+
msg = "message when instance was launched"
|
18
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/latest/user-data").once.and_return(msg)
|
19
|
+
Ec2Metadata[:user_data].should == msg
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "default path /latest/meta-data/" do
|
24
|
+
before do
|
25
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/latest/meta-data/").once.and_return(ALL_ATTR_KEYS.join("\n"))
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "should return public-hostname" do
|
29
|
+
before do
|
30
|
+
@public_hostname = "ec2-75-101-241-136.compute-1.amazonaws.com".freeze
|
31
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/latest/meta-data/public-hostname").once.and_return(@public_hostname)
|
32
|
+
end
|
33
|
+
it("with underscore Symbol") {Ec2Metadata[:public_hostname].should == @public_hostname}
|
34
|
+
it("with underscore String") {Ec2Metadata['public_hostname'].should == @public_hostname}
|
35
|
+
it("with hyphen Symbol") {Ec2Metadata[:'public-hostname'].should == @public_hostname}
|
36
|
+
it("with hyphen String") {Ec2Metadata['public-hostname'].should == @public_hostname}
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should access placement data such as 'availavility zone'" do
|
40
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/latest/meta-data/placement/").
|
41
|
+
and_return("availability-zone\nanother-placement-data")
|
42
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/latest/meta-data/placement/availability-zone").
|
43
|
+
and_return("us-east-1a")
|
44
|
+
Ec2Metadata[:placement].keys.should == ["availability-zone", "another-placement-data"]
|
45
|
+
Ec2Metadata[:placement][:availability_zone].should == "us-east-1a"
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should access block-device-mapping" do
|
49
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/latest/meta-data/block-device-mapping/").and_return(["ami", "ephemeral0", "root", "swap"].join("\n"))
|
50
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/latest/meta-data/block-device-mapping/ami").and_return("sda1")
|
51
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/latest/meta-data/block-device-mapping/ephemeral0").and_return("sda2")
|
52
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/latest/meta-data/block-device-mapping/root").and_return("/dev/sda1")
|
53
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/latest/meta-data/block-device-mapping/swap").and_return("sda3")
|
54
|
+
Ec2Metadata[:block_device_mapping].keys.should == ["ami", "ephemeral0", "root", "swap"]
|
55
|
+
Ec2Metadata[:block_device_mapping][:ami].should == "sda1"
|
56
|
+
Ec2Metadata[:block_device_mapping][:ephemeral0].should == "sda2"
|
57
|
+
Ec2Metadata[:block_device_mapping][:root].should == "/dev/sda1"
|
58
|
+
Ec2Metadata[:block_device_mapping][:swap].should == "sda3"
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should access some public-keys" do
|
62
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/latest/meta-data/public-keys/").and_return("0=keypair0\n1=keypair1")
|
63
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/latest/meta-data/public-keys/0/").and_return("openssh-key")
|
64
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/latest/meta-data/public-keys/0/openssh-key").and_return("ssh-rsa 1234567890")
|
65
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/latest/meta-data/public-keys/1/").and_return("another-key")
|
66
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/latest/meta-data/public-keys/1/another-key").and_return("xxxxxxx abcdefghij")
|
67
|
+
Ec2Metadata[:public_keys].keys.should == ["0", '1']
|
68
|
+
Ec2Metadata[:public_keys][0].name.should == "keypair0"
|
69
|
+
Ec2Metadata[:public_keys][0].keys.should == ["openssh-key"]
|
70
|
+
Ec2Metadata[:public_keys][0][:openssh_key].should == "ssh-rsa 1234567890"
|
71
|
+
Ec2Metadata[:public_keys][1].name.should == "keypair1"
|
72
|
+
Ec2Metadata[:public_keys][1].keys.should == ["another-key"]
|
73
|
+
Ec2Metadata[:public_keys][1][:another_key].should == "xxxxxxx abcdefghij"
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe "revision? '2007-01-19' specified" do
|
80
|
+
before do
|
81
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/2007-01-19/").once.and_return(DATA_TYPES.join("\n"))
|
82
|
+
end
|
83
|
+
|
84
|
+
describe "user-data" do
|
85
|
+
it "should access user-data" do
|
86
|
+
msg = "message when instance was launched"
|
87
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/2007-01-19/user-data").once.and_return(msg)
|
88
|
+
Ec2Metadata['2007-01-19'][:user_data].should == msg
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe "default path /2007-01-19/meta-data/" do
|
93
|
+
before do
|
94
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/2007-01-19/meta-data/").once.and_return(ALL_ATTR_KEYS.join("\n"))
|
95
|
+
end
|
96
|
+
|
97
|
+
describe "should return public-hostname" do
|
98
|
+
before do
|
99
|
+
@public_hostname = "ec2-75-101-241-136.compute-1.amazonaws.com".freeze
|
100
|
+
Net::HTTP.should_receive(:get).with("169.254.169.254", "/2007-01-19/meta-data/public-hostname").once.and_return(@public_hostname)
|
101
|
+
end
|
102
|
+
it("with underscore Symbol") {Ec2Metadata['2007-01-19'][:public_hostname].should == @public_hostname}
|
103
|
+
it("with underscore String") {Ec2Metadata['2007-01-19']['public_hostname'].should == @public_hostname}
|
104
|
+
it("with hyphen Symbol") {Ec2Metadata['2007-01-19'][:'public-hostname'].should == @public_hostname}
|
105
|
+
it("with hyphen String") {Ec2Metadata['2007-01-19']['public-hostname'].should == @public_hostname}
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
require 'ec2_metadata'
|
4
|
+
require 'spec'
|
5
|
+
require 'spec/autorun'
|
6
|
+
|
7
|
+
Spec::Runner.configure do |config|
|
8
|
+
|
9
|
+
end
|
10
|
+
|
11
|
+
REVISIONS = [
|
12
|
+
'1.0',
|
13
|
+
'2007-01-19',
|
14
|
+
'2007-03-01',
|
15
|
+
'2007-08-29',
|
16
|
+
'2007-10-10',
|
17
|
+
'2007-12-15',
|
18
|
+
'2008-02-01',
|
19
|
+
'2008-09-01',
|
20
|
+
'2009-04-04',
|
21
|
+
'latest'
|
22
|
+
]
|
23
|
+
|
24
|
+
DATA_TYPES = %w(dynamic user-data meta-data)
|
25
|
+
|
26
|
+
SIMPLE_ATTR_KEYS = %w(ami-id ami-launch-index ami-manifest-path) +
|
27
|
+
%w(hostname instance-action instance-id instance-type kernel-id) +
|
28
|
+
%w(local-hostname local-ipv4) +
|
29
|
+
%w(public-hostname public-ipv4) +
|
30
|
+
%w(ramdisk-id reservation-id security-groups)
|
31
|
+
|
32
|
+
ALL_ATTR_KEYS = SIMPLE_ATTR_KEYS +
|
33
|
+
%w(block-device-mapping/ placement/ public-keys/)
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ec2-metadata
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Takeshi AKIMA
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-04-04 00:00:00 +09:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 2
|
30
|
+
- 9
|
31
|
+
version: 1.2.9
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
description: ec2-metadata provides to access metadata, and you can use in outside of ec2 like in ec2
|
35
|
+
email: akm2000@gmail.com
|
36
|
+
executables: []
|
37
|
+
|
38
|
+
extensions: []
|
39
|
+
|
40
|
+
extra_rdoc_files:
|
41
|
+
- LICENSE
|
42
|
+
- README.rdoc
|
43
|
+
files:
|
44
|
+
- .document
|
45
|
+
- .gitignore
|
46
|
+
- LICENSE
|
47
|
+
- README.rdoc
|
48
|
+
- Rakefile
|
49
|
+
- VERSION
|
50
|
+
- lib/ec2_metadata.rb
|
51
|
+
- lib/ec2_metadata/base.rb
|
52
|
+
- lib/ec2_metadata/named_base.rb
|
53
|
+
- lib/ec2_metadata/revision.rb
|
54
|
+
- lib/ec2_metadata/root.rb
|
55
|
+
- spec/ec2_metadata/base_spec.rb
|
56
|
+
- spec/ec2_metadata/revision_spec.rb
|
57
|
+
- spec/ec2_metadata/root_spec.rb
|
58
|
+
- spec/ec2_metadata_spec.rb
|
59
|
+
- spec/introduction_spec.rb
|
60
|
+
- spec/spec.opts
|
61
|
+
- spec/spec_helper.rb
|
62
|
+
has_rdoc: true
|
63
|
+
homepage: http://github.com/akm/ec2-metadata
|
64
|
+
licenses: []
|
65
|
+
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options:
|
68
|
+
- --charset=UTF-8
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
version: "0"
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
segments:
|
83
|
+
- 0
|
84
|
+
version: "0"
|
85
|
+
requirements: []
|
86
|
+
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 1.3.6
|
89
|
+
signing_key:
|
90
|
+
specification_version: 3
|
91
|
+
summary: ec2-metadata provides to access metadata
|
92
|
+
test_files:
|
93
|
+
- spec/ec2_metadata/base_spec.rb
|
94
|
+
- spec/ec2_metadata/revision_spec.rb
|
95
|
+
- spec/ec2_metadata/root_spec.rb
|
96
|
+
- spec/ec2_metadata_spec.rb
|
97
|
+
- spec/introduction_spec.rb
|
98
|
+
- spec/spec_helper.rb
|