Skip to content

Commit 65192fd

Browse files
committed
Merge branch 'CHEF-1507'
2 parents 73493e7 + 4b46a6f commit 65192fd

6 files changed

Lines changed: 207 additions & 0 deletions

File tree

‎chef/lib/chef/provider/ohai.rb‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#
2+
# Author:: Michael Leianrtas (<mleinartas@gmail.com>)
3+
# Copyright:: Copyright (c) 2010 Michael Leinartas
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
require 'ohai'
20+
21+
class Chef
22+
class Provider
23+
class Ohai < Chef::Provider
24+
25+
def load_current_resource
26+
true
27+
end
28+
29+
def action_reload
30+
ohai = ::Ohai::System.new
31+
if @new_resource.plugin
32+
ohai.require_plugin @new_resource.plugin
33+
else
34+
ohai.all_plugins
35+
end
36+
37+
node.automatic_attrs.merge! ohai.data
38+
end
39+
end
40+
end
41+
end

‎chef/lib/chef/providers.rb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
require 'chef/provider/ifconfig'
3333
require 'chef/provider/link'
3434
require 'chef/provider/log'
35+
require 'chef/provider/ohai'
3536
require 'chef/provider/mdadm'
3637
require 'chef/provider/mount'
3738
require 'chef/provider/package'

‎chef/lib/chef/resource/ohai.rb‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#
2+
# Author:: Michael Leinartas (<mleinartas@gmail.com>)
3+
# Copyright:: Copyright (c) 2010 Michael Leinartas
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
class Chef
20+
class Resource
21+
class Ohai < Chef::Resource
22+
23+
def initialize(name, run_context=nil)
24+
super
25+
@resource_name = :ohai
26+
@allowed_actions.push(:reload)
27+
@action = :reload
28+
@plugin = nil
29+
end
30+
31+
def plugin(arg=nil)
32+
set_or_return(
33+
:plugin,
34+
arg,
35+
:kind_of => [ String ]
36+
)
37+
end
38+
end
39+
end
40+
end

‎chef/lib/chef/resources.rb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
require 'chef/resource/macports_package'
4343
require 'chef/resource/mdadm'
4444
require 'chef/resource/mount'
45+
require 'chef/resource/ohai'
4546
require 'chef/resource/package'
4647
require 'chef/resource/pacman_package'
4748
require 'chef/resource/perl'
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#
2+
# Author:: Michael Leinartas (<mleinartas@gmail.com>)
3+
# Copyright:: Copyright (c) 2010 Michael Leinartas
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
20+
21+
require 'chef/run_context'
22+
23+
describe Chef::Provider::Ohai do
24+
before(:each) do
25+
# Copied from client_spec
26+
FQDN = "hostname.domainname"
27+
HOSTNAME = "hostname"
28+
PLATFORM = "example-platform"
29+
PLATFORM_VERSION = "example-platform"
30+
Chef::Config[:node_name] = FQDN
31+
mock_ohai = {
32+
:fqdn => FQDN,
33+
:hostname => HOSTNAME,
34+
:platform => PLATFORM,
35+
:platform_version => PLATFORM_VERSION,
36+
:data => {
37+
:origdata => "somevalue"
38+
},
39+
:data2 => {
40+
:origdata => "somevalue",
41+
:newdata => "somevalue"
42+
}
43+
}
44+
mock_ohai.stub!(:all_plugins).and_return(true)
45+
mock_ohai.stub!(:require_plugin).and_return(true)
46+
mock_ohai.stub!(:data).and_return(mock_ohai[:data],
47+
mock_ohai[:data2])
48+
Ohai::System.stub!(:new).and_return(mock_ohai)
49+
Chef::Platform.stub!(:find_platform_and_version).and_return({ "platform" => PLATFORM,
50+
"platform_version" => PLATFORM_VERSION})
51+
# Fake node with a dummy save
52+
@node = Chef::Node.new(HOSTNAME)
53+
@node.name(FQDN)
54+
@node.stub!(:save).and_return(@node)
55+
@run_context = Chef::RunContext.new(@node, {})
56+
@new_resource = Chef::Resource::Ohai.new("ohai_reload")
57+
ohai = Ohai::System.new
58+
ohai.all_plugins
59+
@node.process_external_attrs(ohai.data,{})
60+
61+
@provider = Chef::Provider::Ohai.new(@new_resource, @run_context)
62+
end
63+
64+
describe "when reloading ohai" do
65+
it "should cause node to pick up new values" do
66+
@node[:origdata].should == 'somevalue'
67+
@node[:newdata].should == nil
68+
@provider.action_reload
69+
@node[:origdata].should == 'somevalue'
70+
@node[:newdata].should == 'somevalue'
71+
end
72+
73+
it "should reload a specific plugin and cause node to pick up new values" do
74+
@new_resource.plugin "someplugin"
75+
@provider.action_reload
76+
@node[:origdata].should == 'somevalue'
77+
@node[:newdata].should == 'somevalue'
78+
end
79+
end
80+
end
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#
2+
# Author:: Michael Leinartas (<mleinartas@gmail.com>)
3+
# Copyright:: Copyright (c) 2010 Michael Leinartas
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
20+
21+
describe Chef::Resource::Ohai do
22+
23+
before(:each) do
24+
@resource = Chef::Resource::Ohai.new("ohai_reload")
25+
end
26+
27+
it "should create a new Chef::Resource::Ohai" do
28+
@resource.should be_a_kind_of(Chef::Resource)
29+
@resource.should be_a_kind_of(Chef::Resource::Ohai)
30+
end
31+
32+
it "should have a resource name of :ohai" do
33+
@resource.resource_name.should eql(:ohai)
34+
end
35+
36+
it "should have a default action of create" do
37+
@resource.action.should eql(:reload)
38+
end
39+
40+
it "should allow you to set the plugin attribute" do
41+
@resource.plugin "passwd"
42+
@resource.plugin.should eql("passwd")
43+
end
44+
end

0 commit comments

Comments
 (0)