ohai 0.3.6 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -124,18 +124,31 @@ iface.keys.each do |ifn|
124
124
  iaddr = nil
125
125
  if iface[ifn][:encapsulation].eql?("Ethernet")
126
126
  iface[ifn][:addresses].keys.each do |addr|
127
- if [ifn][:addresses][addr]["family"].eql?("inet")
127
+ if iface[ifn][:addresses][addr]["family"].eql?("inet")
128
128
  iaddr = addr
129
129
  break
130
130
  end
131
131
  end
132
- iface[ifn][:arp].keys.each do |addr|
133
- if addr.eql?(iaddr)
134
- iface[ifn][:addresses][iface[ifn][:arp][iaddr]] = { "family" => "lladdr" }
135
- break
132
+ if iface[ifn][:arp]
133
+ iface[ifn][:arp].keys.each do |addr|
134
+ if addr.eql?(iaddr)
135
+ iface[ifn][:addresses][iface[ifn][:arp][iaddr]] = { "family" => "lladdr" }
136
+ break
137
+ end
136
138
  end
137
139
  end
138
140
  end
139
141
  end
140
142
 
141
143
  network[:interfaces] = iface
144
+
145
+ popen4("route get default") do |pid, stdin, stdout, stderr|
146
+ stdin.close
147
+ route_get = stdout.string
148
+ matches = /interface: (\S+)/.match(route_get)
149
+ if matches
150
+ Ohai::Log.debug("found gateway device: #{$1}")
151
+ network[:default_interface] = matches[1]
152
+ end
153
+ end
154
+
@@ -6,9 +6,9 @@
6
6
  # Licensed under the Apache License, Version 2.0 (the "License");
7
7
  # you may not use this file except in compliance with the License.
8
8
  # You may obtain a copy of the License at
9
- #
9
+ #
10
10
  # http://www.apache.org/licenses/LICENSE-2.0
11
- #
11
+ #
12
12
  # Unless required by applicable law or agreed to in writing, software
13
13
  # distributed under the License is distributed on an "AS IS" BASIS,
14
14
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -37,46 +37,45 @@ end
37
37
  module Ohai
38
38
  class System
39
39
  attr_accessor :data, :seen_plugins
40
-
40
+
41
41
  include Ohai::Mixin::FromFile
42
42
  include Ohai::Mixin::Command
43
-
43
+
44
44
  def initialize
45
45
  @data = Mash.new
46
46
  @seen_plugins = Hash.new
47
47
  @providers = Mash.new
48
48
  @plugin_path = ""
49
- Ohai::Log.level(Ohai::Config.log_level)
50
49
  end
51
-
50
+
52
51
  def [](key)
53
52
  @data[key]
54
53
  end
55
-
54
+
56
55
  def []=(key, value)
57
56
  @data[key] = value
58
57
  end
59
-
58
+
60
59
  def each(&block)
61
60
  @data.each do |key, value|
62
61
  block.call(key, value)
63
62
  end
64
63
  end
65
-
64
+
66
65
  def attribute?(name)
67
- @data.has_key?(name)
66
+ @data.has_key?(name)
68
67
  end
69
-
68
+
70
69
  def set(name, *value)
71
70
  set_attribute(name, *value)
72
71
  end
73
-
72
+
74
73
  def from(cmd)
75
74
  status, stdout, stderr = run_command(:command => cmd)
76
- return "" if stdout.nil?
75
+ return "" if stdout.nil? || stdout.empty?
77
76
  stdout.chomp!.strip
78
77
  end
79
-
78
+
80
79
  def provides(*paths)
81
80
  paths.each do |path|
82
81
  parts = path.split('/')
@@ -92,34 +91,34 @@ module Ohai
92
91
  h[:_providers] << @plugin_path
93
92
  end
94
93
  end
95
-
94
+
96
95
  # Set the value equal to the stdout of the command, plus run through a regex - the first piece of match data is the value.
97
96
  def from_with_regex(cmd, *regex_list)
98
97
  regex_list.flatten.each do |regex|
99
98
  status, stdout, stderr = run_command(:command => cmd)
100
- return "" if stdout.nil?
99
+ return "" if stdout.nil? || stdout.empty?
101
100
  stdout.chomp!.strip
102
101
  md = stdout.match(regex)
103
102
  return md[1]
104
103
  end
105
104
  end
106
-
105
+
107
106
  def set_attribute(name, *value)
108
107
  @data[name] = *value
109
108
  @data[name]
110
109
  end
111
-
110
+
112
111
  def get_attribute(name)
113
112
  @data[name]
114
113
  end
115
-
114
+
116
115
  def all_plugins
117
116
  require_plugin('os')
118
-
117
+
119
118
  Ohai::Config[:plugin_path].each do |path|
120
- [
121
- Dir[File.join(path, '*')],
122
- Dir[File.join(path, @data[:os], '**', '*')]
119
+ [
120
+ Dir[File.join(path, '*')],
121
+ Dir[File.join(path, @data[:os], '**', '*')]
123
122
  ].flatten.each do |file|
124
123
  file_regex = Regexp.new("#{path}#{File::SEPARATOR}(.+).rb$")
125
124
  md = file_regex.match(file)
@@ -146,7 +145,7 @@ module Ohai
146
145
  end
147
146
  refreshments.flatten.uniq
148
147
  end
149
-
148
+
150
149
  def refresh_plugins(path = '/')
151
150
  parts = path.split('/')
152
151
  if parts.length == 0
@@ -162,24 +161,24 @@ module Ohai
162
161
 
163
162
  refreshments = collect_providers(h)
164
163
  Ohai::Log.debug("Refreshing plugins: #{refreshments.join(", ")}")
165
-
164
+
166
165
  refreshments.each do |r|
167
166
  @seen_plugins.delete(r) if @seen_plugins.has_key?(r)
168
167
  end
169
168
  refreshments.each do |r|
170
- require_plugin(r) unless @seen_plugins.has_key?(r)
169
+ require_plugin(r) unless @seen_plugins.has_key?(r)
171
170
  end
172
171
  end
173
-
172
+
174
173
  def require_plugin(plugin_name, force=false)
175
174
  unless force
176
175
  return true if @seen_plugins[plugin_name]
177
176
  end
178
-
177
+
179
178
  @plugin_path = plugin_name
180
-
179
+
181
180
  filename = "#{plugin_name.gsub("::", File::SEPARATOR)}.rb"
182
-
181
+
183
182
  Ohai::Config[:plugin_path].each do |path|
184
183
  check_path = File.expand_path(File.join(path, filename))
185
184
  begin
@@ -194,26 +193,35 @@ module Ohai
194
193
  end
195
194
  end
196
195
  end
197
-
196
+
198
197
  # Sneaky! Lets us stub out require_plugin when testing plugins, but still
199
198
  # call the real require_plugin to kick the whole thing off.
200
199
  alias :_require_plugin :require_plugin
201
-
202
- # Serialize this object as a hash
200
+
201
+ # Serialize this object as a hash
203
202
  def to_json(*a)
204
203
  output = @data.clone
205
204
  output["json_class"] = self.class.name
206
205
  output.to_json(*a)
207
206
  end
208
-
209
- # Pretty Print this object as JSON
207
+
208
+ # Pretty Print this object as JSON
210
209
  def json_pretty_print
211
210
  JSON.pretty_generate(@data)
212
211
  end
213
-
212
+
214
213
  def attributes_print(a)
215
- JSON.pretty_generate(@data[a])
214
+ raise ArgumentError, "I cannot find an attribute named #{a}!" unless @data.has_key?(a)
215
+ case a
216
+ when Hash,Mash,Array
217
+ JSON.pretty_generate(@data[a])
218
+ when String
219
+ JSON.pretty_generate(@data[a].to_a)
220
+ else
221
+ raise ArgumentError, "I can only generate JSON for Hashes, Mashes, Arrays and Strings. You fed me a #{@data[a].class}!"
222
+ end
216
223
  end
224
+
217
225
  # Create an Ohai::System from JSON
218
226
  def self.json_create(o)
219
227
  ohai = new
@@ -222,16 +230,16 @@ module Ohai
222
230
  end
223
231
  ohai
224
232
  end
225
-
233
+
226
234
  def method_missing(name, *args)
227
- return get_attribute(name) if args.length == 0
228
-
235
+ return get_attribute(name) if args.length == 0
236
+
229
237
  set_attribute(name, *args)
230
238
  end
231
-
239
+
232
240
  private
233
241
  def load_plugin_file
234
-
242
+
235
243
  end
236
244
  end
237
245
  end
@@ -0,0 +1,87 @@
1
+ #
2
+ # Author:: Cary Penniman (<cary@rightscale.com>)
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ require File.join(File.dirname(__FILE__), '..', '..', '/spec_helper.rb')
19
+
20
+ describe Ohai::System, "plugin cloud" do
21
+ before(:each) do
22
+ @ohai = Ohai::System.new
23
+ @ohai.stub!(:require_plugin).and_return(true)
24
+ end
25
+
26
+ describe "no cloud" do
27
+ it "should NOT populate the cloud data" do
28
+ @ohai[:ec2] = nil
29
+ @ohai[:rackspace] = nil
30
+ @ohai._require_plugin("cloud")
31
+ @ohai[:cloud].should be_nil
32
+ end
33
+ end
34
+
35
+ describe "with EC2" do
36
+ before(:each) do
37
+ @ohai[:ec2] = Mash.new()
38
+ end
39
+
40
+ it "should populate cloud public ip" do
41
+ @ohai[:ec2]['public_ipv4'] = "174.129.150.8"
42
+ @ohai._require_plugin("cloud")
43
+ @ohai[:cloud][:public_ips][0].should == @ohai[:ec2]['public_ipv4']
44
+ end
45
+
46
+ it "should populate cloud private ip" do
47
+ @ohai[:ec2]['local_ipv4'] = "10.252.42.149"
48
+ @ohai._require_plugin("cloud")
49
+ @ohai[:cloud][:private_ips][0].should == @ohai[:ec2]['local_ipv4']
50
+ end
51
+
52
+ it "should populate cloud provider" do
53
+ @ohai._require_plugin("cloud")
54
+ @ohai[:cloud][:provider].should == "ec2"
55
+ end
56
+ end
57
+
58
+ describe "with rackspace" do
59
+ before(:each) do
60
+ @ohai[:rackspace] = Mash.new()
61
+ end
62
+
63
+ it "should populate cloud public ip" do
64
+ @ohai[:rackspace]['public_ip'] = "174.129.150.8"
65
+ @ohai._require_plugin("cloud")
66
+ @ohai[:cloud][:public_ips][0].should == @ohai[:rackspace][:public_ip]
67
+ end
68
+
69
+ it "should populate cloud private ip" do
70
+ @ohai[:rackspace]['private_ip'] = "10.252.42.149"
71
+ @ohai._require_plugin("cloud")
72
+ @ohai[:cloud][:private_ips][0].should == @ohai[:rackspace][:private_ip]
73
+ end
74
+
75
+ it "should populate first cloud public ip" do
76
+ @ohai[:rackspace]['public_ip'] = "174.129.150.8"
77
+ @ohai._require_plugin("cloud")
78
+ @ohai[:cloud][:public_ips].first.should == @ohai[:rackspace][:public_ip]
79
+ end
80
+
81
+ it "should populate cloud provider" do
82
+ @ohai._require_plugin("cloud")
83
+ @ohai[:cloud][:provider].should == "rackspace"
84
+ end
85
+ end
86
+
87
+ end
@@ -29,6 +29,9 @@ describe Ohai::System, "Linux plugin platform" do
29
29
  File.stub!(:exists?).with("/etc/debian_version").and_return(false)
30
30
  File.stub!(:exists?).with("/etc/redhat-release").and_return(false)
31
31
  File.stub!(:exists?).with("/etc/gentoo-release").and_return(false)
32
+ File.stub!(:exists?).with("/etc/SuSE-release").and_return(false)
33
+ File.stub!(:exists?).with("/etc/arch-release").and_return(false)
34
+ File.stub!(:exists?).with("/etc/fedora-release").and_return(false)
32
35
  end
33
36
 
34
37
  it "should require the lsb plugin" do
@@ -77,5 +80,69 @@ describe Ohai::System, "Linux plugin platform" do
77
80
 
78
81
  end
79
82
 
80
-
81
- end
83
+ describe "on arch" do
84
+ before(:each) do
85
+ @ohai.lsb = nil
86
+ File.should_receive(:exists?).with("/etc/arch-release").and_return(true)
87
+ end
88
+
89
+ it "should set platform to arch" do
90
+ @ohai._require_plugin("linux::platform")
91
+ @ohai[:platform].should == "arch"
92
+ end
93
+ end
94
+
95
+ describe "on redhat breeds" do
96
+ before(:each) do
97
+ @ohai.lsb = nil
98
+ File.should_receive(:exists?).with("/etc/redhat-release").and_return(true)
99
+ end
100
+
101
+ it "should check for the existance of redhat-release" do
102
+ @ohai._require_plugin("linux::platform")
103
+ end
104
+
105
+ it "should read the platform as centos and version as 5.3" do
106
+ File.should_receive(:read).with("/etc/redhat-release").and_return("CentOS release 5.3")
107
+ @ohai._require_plugin("linux::platform")
108
+ @ohai[:platform].should == "centos"
109
+ end
110
+
111
+ it "may be that someone munged Red Hat to be RedHat" do
112
+ File.should_receive(:read).with("/etc/redhat-release").and_return("RedHat release 5.3")
113
+ @ohai._require_plugin("linux::platform")
114
+ @ohai[:platform].should == "redhat"
115
+ @ohai[:platform_version].should == "5.3"
116
+ end
117
+
118
+ it "should read the platform as redhat and version as 5.3" do
119
+ File.should_receive(:read).with("/etc/redhat-release").and_return("Red Hat release 5.3")
120
+ @ohai._require_plugin("linux::platform")
121
+ @ohai[:platform].should == "redhat"
122
+ @ohai[:platform_version].should == "5.3"
123
+ end
124
+
125
+ it "should read the platform as fedora and version as 13 (rawhide)" do
126
+ File.should_receive(:read).with("/etc/redhat-release").and_return("Fedora release 13 (Rawhide)")
127
+ @ohai._require_plugin("linux::platform")
128
+ @ohai[:platform].should == "fedora"
129
+ @ohai[:platform_version].should == "13 (rawhide)"
130
+ end
131
+
132
+ it "should read the platform as fedora and version as 10" do
133
+ File.should_receive(:read).with("/etc/redhat-release").and_return("Fedora release 10")
134
+ @ohai._require_plugin("linux::platform")
135
+ @ohai[:platform].should == "fedora"
136
+ @ohai[:platform_version].should == "10"
137
+ end
138
+
139
+ it "should read the platform as fedora and version as 13 using to_i" do
140
+ File.should_receive(:read).with("/etc/redhat-release").and_return("Fedora release 13 (Rawhide)")
141
+ @ohai._require_plugin("linux::platform")
142
+ @ohai[:platform].should == "fedora"
143
+ @ohai[:platform_version].to_i.should == 13
144
+ end
145
+ end
146
+
147
+ end
148
+
@@ -96,9 +96,17 @@ describe Ohai::System, "Linux virtualization platform" do
96
96
  end
97
97
 
98
98
  it "should set virtualpc guest if dmidecode detects Microsoft Virtual Machine" do
99
- @stdout.stub!(:each).
100
- and_yield("Manufacturer: Microsoft").
101
- and_yield(" Product Name: Virtual Machine")
99
+ ms_vpc_dmidecode=<<-MSVPC
100
+ System Information
101
+ Manufacturer: Microsoft Corporation
102
+ Product Name: Virtual Machine
103
+ Version: VS2005R2
104
+ Serial Number: 1688-7189-5337-7903-2297-1012-52
105
+ UUID: D29974A4-BE51-044C-BDC6-EFBC4B87A8E9
106
+ Wake-up Type: Power Switch
107
+ MSVPC
108
+ @stdout.stub!(:string).and_return(ms_vpc_dmidecode)
109
+
102
110
  @ohai.stub!(:popen4).with("dmidecode").and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status)
103
111
  @ohai._require_plugin("linux::virtualization")
104
112
  @ohai[:virtualization][:emulator].should == "virtualpc"
@@ -106,9 +114,18 @@ describe Ohai::System, "Linux virtualization platform" do
106
114
  end
107
115
 
108
116
  it "should set vmware guest if dmidecode detects VMware Virtual Platform" do
109
- @stdout.stub!(:each).
110
- and_yield("Manufacturer: VMware").
111
- and_yield("Product Name: VMware Virtual Platform")
117
+ vmware_dmidecode=<<-VMWARE
118
+ System Information
119
+ Manufacturer: VMware, Inc.
120
+ Product Name: VMware Virtual Platform
121
+ Version: None
122
+ Serial Number: VMware-50 3f f7 14 42 d1 f1 da-3b 46 27 d0 29 b4 74 1d
123
+ UUID: a86cc405-e1b9-447b-ad05-6f8db39d876a
124
+ Wake-up Type: Power Switch
125
+ SKU Number: Not Specified
126
+ Family: Not Specified
127
+ VMWARE
128
+ @stdout.stub!(:string).and_return(vmware_dmidecode)
112
129
  @ohai.stub!(:popen4).with("dmidecode").and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status)
113
130
  @ohai._require_plugin("linux::virtualization")
114
131
  @ohai[:virtualization][:emulator].should == "vmware"