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.
@@ -99,7 +99,7 @@ module Ohai
99
99
  return status, stdout_string, stderr_string
100
100
  end
101
101
 
102
- def run_comand_windows(command, timeout)
102
+ def run_command_windows(command, timeout)
103
103
  if timeout
104
104
  begin
105
105
  systemu(command)
@@ -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 'open-uri'
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 = "http://#{EC2_METADATA_ADDR}/2008-02-01/meta-data" unless defined?(EC2_METADATA_URL)
29
- EC2_USERDATA_URL = "http://#{EC2_METADATA_ADDR}/2008-02-01/user-data" unless defined?(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
- OpenURI.open_uri("#{EC2_METADATA_URL}/#{id}").read.split("\n").each do |o|
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
- OpenURI.open_uri("#{EC2_METADATA_URL}/#{key}").read.split("\n")
69
+ http_client.get("#{EC2_METADATA_URL}/#{key}").body.split("\n")
66
70
  else
67
- OpenURI.open_uri("#{EC2_METADATA_URL}/#{key}").read
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
- # assumes the only expected error is the 404 if there's no user-data
78
- begin
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
@@ -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
- self.fetch_metadata.each {|k, v| ec2[k] = v }
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")
@@ -18,6 +18,6 @@
18
18
 
19
19
  module Ohai
20
20
  OHAI_ROOT = File.expand_path(File.dirname(__FILE__))
21
- VERSION = '0.6.0'
21
+ VERSION = '0.6.2.rc.0'
22
22
  end
23
23
 
@@ -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 CONDITIONS OF ANY KIND, either express or implied.
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
- OpenURI.should_not_receive(:open)
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
- OpenURI.stub!(:open_uri).
40
- with("http://169.254.169.254/2008-02-01/meta-data/").
41
- and_return(mock(IO, :read => "instance_type\nami_id\nsecurity-groups"))
42
- OpenURI.stub!(:open_uri).
43
- with("http://169.254.169.254/2008-02-01/meta-data/instance_type").
44
- and_return(mock(IO, :read => "c1.medium"))
45
- OpenURI.stub!(:open_uri).
46
- with("http://169.254.169.254/2008-02-01/meta-data/ami_id").
47
- and_return(mock(IO, :read => "ami-5d2dc934"))
48
- OpenURI.stub!(:open_uri).
49
- with("http://169.254.169.254/2008-02-01/meta-data/security-groups").
50
- and_return(mock(IO, :read => "group1\ngroup2"))
51
- OpenURI.stub!(:open_uri).
52
- with("http://169.254.169.254/2008-02-01/user-data/").
53
- and_return(mock(IO, :gets => "By the pricking of my thumb..."))
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
- OpenURI.stub!(:open_uri).
39
- with("http://169.254.169.254/2008-02-01/meta-data/").
40
- and_return(mock(IO, :read => "instance_type\nami_id\nsecurity-groups"))
41
- OpenURI.stub!(:open_uri).
42
- with("http://169.254.169.254/2008-02-01/meta-data/instance_type").
43
- and_return(mock(IO, :read => "c1.medium"))
44
- OpenURI.stub!(:open_uri).
45
- with("http://169.254.169.254/2008-02-01/meta-data/ami_id").
46
- and_return(mock(IO, :read => "ami-5d2dc934"))
47
- OpenURI.stub!(:open_uri).
48
- with("http://169.254.169.254/2008-02-01/meta-data/security-groups").
49
- and_return(mock(IO, :read => "group1\ngroup2"))
50
- OpenURI.stub!(:open_uri).
51
- with("http://169.254.169.254/2008-02-01/user-data/").
52
- and_return(mock(IO, :gets => "By the pricking of my thumb..."))
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: "0"
298
+ version: 1.3.1
299
299
  requirements: []
300
300
 
301
301
  rubyforge_project: