ohai 0.6.0 → 0.6.2.rc.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ohai/mixin/command.rb +1 -1
- data/lib/ohai/mixin/ec2_metadata.rb +16 -16
- data/lib/ohai/plugins/ec2.rb +1 -1
- data/lib/ohai/version.rb +1 -1
- data/spec/ohai/plugins/ec2_spec.rb +20 -17
- data/spec/ohai/plugins/eucalyptus_spec.rb +18 -15
- metadata +4 -4
data/lib/ohai/mixin/command.rb
CHANGED
@@ -17,7 +17,7 @@
|
|
17
17
|
# See the License for the specific language governing permissions and
|
18
18
|
# limitations under the License.
|
19
19
|
|
20
|
-
require '
|
20
|
+
require 'net/http'
|
21
21
|
require 'socket'
|
22
22
|
|
23
23
|
module Ohai
|
@@ -25,17 +25,17 @@ module Ohai
|
|
25
25
|
module Ec2Metadata
|
26
26
|
|
27
27
|
EC2_METADATA_ADDR = "169.254.169.254" unless defined?(EC2_METADATA_ADDR)
|
28
|
-
EC2_METADATA_URL = "
|
29
|
-
EC2_USERDATA_URL = "
|
28
|
+
EC2_METADATA_URL = "/2008-02-01/meta-data" unless defined?(EC2_METADATA_URL)
|
29
|
+
EC2_USERDATA_URL = "/2008-02-01/user-data" unless defined?(EC2_USERDATA_URL)
|
30
30
|
EC2_ARRAY_VALUES = %w(security-groups)
|
31
31
|
|
32
32
|
def can_metadata_connect?(addr, port, timeout=2)
|
33
33
|
t = Socket.new(Socket::Constants::AF_INET, Socket::Constants::SOCK_STREAM, 0)
|
34
34
|
saddr = Socket.pack_sockaddr_in(port, addr)
|
35
35
|
connected = false
|
36
|
-
|
36
|
+
|
37
37
|
begin
|
38
|
-
t.connect_nonblock(saddr)
|
38
|
+
t.connect_nonblock(saddr)
|
39
39
|
rescue Errno::EINPROGRESS
|
40
40
|
r,w,e = IO::select(nil,[t],nil,timeout)
|
41
41
|
if !w.nil?
|
@@ -55,16 +55,20 @@ module Ohai
|
|
55
55
|
connected
|
56
56
|
end
|
57
57
|
|
58
|
+
def http_client
|
59
|
+
Net::HTTP.start(EC2_METADATA_ADDR).tap {|h| h.read_timeout = 600}
|
60
|
+
end
|
61
|
+
|
58
62
|
def fetch_metadata(id='')
|
59
63
|
metadata = Hash.new
|
60
|
-
|
64
|
+
http_client.get("#{EC2_METADATA_URL}/#{id}").body.split("\n").each do |o|
|
61
65
|
key = "#{id}#{o.gsub(/\=.*$/, '/')}"
|
62
66
|
if key[-1..-1] != '/'
|
63
|
-
metadata[key.gsub(/\-|\//, '_').to_sym] =
|
67
|
+
metadata[key.gsub(/\-|\//, '_').to_sym] =
|
64
68
|
if EC2_ARRAY_VALUES.include? key
|
65
|
-
|
69
|
+
http_client.get("#{EC2_METADATA_URL}/#{key}").body.split("\n")
|
66
70
|
else
|
67
|
-
|
71
|
+
http_client.get("#{EC2_METADATA_URL}/#{key}").body
|
68
72
|
end
|
69
73
|
else
|
70
74
|
next
|
@@ -72,14 +76,10 @@ module Ohai
|
|
72
76
|
end
|
73
77
|
metadata
|
74
78
|
end
|
75
|
-
|
79
|
+
|
76
80
|
def fetch_userdata()
|
77
|
-
|
78
|
-
|
79
|
-
OpenURI.open_uri("#{EC2_USERDATA_URL}/").read
|
80
|
-
rescue OpenURI::HTTPError
|
81
|
-
nil
|
82
|
-
end
|
81
|
+
response = http_client.get("#{EC2_USERDATA_URL}/")
|
82
|
+
response.code == "200" ? response.body : nil
|
83
83
|
end
|
84
84
|
end
|
85
85
|
end
|
data/lib/ohai/plugins/ec2.rb
CHANGED
@@ -48,7 +48,7 @@ end
|
|
48
48
|
if looks_like_ec2?
|
49
49
|
Ohai::Log.debug("looks_like_ec2? == true")
|
50
50
|
ec2 Mash.new
|
51
|
-
|
51
|
+
fetch_metadata.each {|k, v| ec2[k] = v }
|
52
52
|
ec2[:userdata] = self.fetch_userdata
|
53
53
|
else
|
54
54
|
Ohai::Log.debug("looks_like_ec2? == false")
|
data/lib/ohai/version.rb
CHANGED
@@ -12,7 +12,7 @@
|
|
12
12
|
#
|
13
13
|
# Unless required by applicable law or agreed to in writing, software
|
14
14
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
-
# WITHOUT WARRANTIES OR
|
15
|
+
# WITHOUT WARRANTIES OR CONDIT"Net::HTTP Response"NS OF ANY KIND, either express or implied.
|
16
16
|
# See the License for the specific language governing permissions and
|
17
17
|
# limitations under the License.
|
18
18
|
#
|
@@ -29,28 +29,31 @@ describe Ohai::System, "plugin ec2" do
|
|
29
29
|
|
30
30
|
shared_examples_for "!ec2" do
|
31
31
|
it "should NOT attempt to fetch the ec2 metadata" do
|
32
|
-
|
32
|
+
@ohai.should_not_receive(:http_client)
|
33
33
|
@ohai._require_plugin("ec2")
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
37
|
shared_examples_for "ec2" do
|
38
38
|
before(:each) do
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
with("
|
44
|
-
and_return(mock(
|
45
|
-
|
46
|
-
with("
|
47
|
-
and_return(mock(
|
48
|
-
|
49
|
-
with("
|
50
|
-
and_return(mock(
|
51
|
-
|
52
|
-
with("
|
53
|
-
and_return(mock(
|
39
|
+
@http_client = mock("Net::HTTP client")
|
40
|
+
@ohai.stub!(:http_client).and_return(@http_client)
|
41
|
+
|
42
|
+
@http_client.should_receive(:get).
|
43
|
+
with("/2008-02-01/meta-data/").
|
44
|
+
and_return(mock("Net::HTTP Response", :body => "instance_type\nami_id\nsecurity-groups"))
|
45
|
+
@http_client.should_receive(:get).
|
46
|
+
with("/2008-02-01/meta-data/instance_type").
|
47
|
+
and_return(mock("Net::HTTP Response", :body => "c1.medium"))
|
48
|
+
@http_client.should_receive(:get).
|
49
|
+
with("/2008-02-01/meta-data/ami_id").
|
50
|
+
and_return(mock("Net::HTTP Response", :body => "ami-5d2dc934"))
|
51
|
+
@http_client.should_receive(:get).
|
52
|
+
with("/2008-02-01/meta-data/security-groups").
|
53
|
+
and_return(mock("Net::HTTP Response", :body => "group1\ngroup2"))
|
54
|
+
@http_client.should_receive(:get).
|
55
|
+
with("/2008-02-01/user-data/").
|
56
|
+
and_return(mock("Net::HTTP Response", :body => "By the pricking of my thumb...", :code => "200"))
|
54
57
|
end
|
55
58
|
|
56
59
|
it "should recursively fetch all the ec2 metadata" do
|
@@ -35,21 +35,24 @@ describe Ohai::System, "plugin eucalyptus" do
|
|
35
35
|
|
36
36
|
shared_examples_for "eucalyptus" do
|
37
37
|
before(:each) do
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
with("
|
43
|
-
and_return(mock(
|
44
|
-
|
45
|
-
with("
|
46
|
-
and_return(mock(
|
47
|
-
|
48
|
-
with("
|
49
|
-
and_return(mock(
|
50
|
-
|
51
|
-
with("
|
52
|
-
and_return(mock(
|
38
|
+
@http_client = mock("Net::HTTP client")
|
39
|
+
@ohai.stub!(:http_client).and_return(@http_client)
|
40
|
+
|
41
|
+
@http_client.should_receive(:get).
|
42
|
+
with("/2008-02-01/meta-data/").
|
43
|
+
and_return(mock("Net::HTTP Response", :body => "instance_type\nami_id\nsecurity-groups"))
|
44
|
+
@http_client.should_receive(:get).
|
45
|
+
with("/2008-02-01/meta-data/instance_type").
|
46
|
+
and_return(mock("Net::HTTP Response", :body => "c1.medium"))
|
47
|
+
@http_client.should_receive(:get).
|
48
|
+
with("/2008-02-01/meta-data/ami_id").
|
49
|
+
and_return(mock("Net::HTTP Response", :body => "ami-5d2dc934"))
|
50
|
+
@http_client.should_receive(:get).
|
51
|
+
with("/2008-02-01/meta-data/security-groups").
|
52
|
+
and_return(mock("Net::HTTP Response", :body => "group1\ngroup2"))
|
53
|
+
@http_client.should_receive(:get).
|
54
|
+
with("/2008-02-01/user-data/").
|
55
|
+
and_return(mock("Net::HTTP Response", :body => "By the pricking of my thumb...", :code => "200"))
|
53
56
|
end
|
54
57
|
|
55
58
|
it "should recursively fetch all the eucalyptus metadata" do
|
metadata
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ohai
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
version: 0.6.0
|
4
|
+
prerelease: 6
|
5
|
+
version: 0.6.2.rc.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Adam Jacob
|
@@ -293,9 +293,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
293
293
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
294
294
|
none: false
|
295
295
|
requirements:
|
296
|
-
- - "
|
296
|
+
- - ">"
|
297
297
|
- !ruby/object:Gem::Version
|
298
|
-
version:
|
298
|
+
version: 1.3.1
|
299
299
|
requirements: []
|
300
300
|
|
301
301
|
rubyforge_project:
|