Skip to content

Commit a5ef340

Browse files
committed
Merge branch 'chef-33' of git://github.com/outerim/chef into CHEF-44
2 parents 18317d1 + fa39504 commit a5ef340

8 files changed

Lines changed: 520 additions & 1 deletion

File tree

chef/lib/chef/exceptions.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Exec < RuntimeError; end
2121
class FileNotFound < RuntimeError; end
2222
class Package < RuntimeError; end
2323
class Service < RuntimeError; end
24+
class Route < RuntimeError; end
2425
class SearchIndex < RuntimeError; end
2526
class Override < RuntimeError; end
2627
class UnsupportedAction < RuntimeError; end

chef/lib/chef/platform.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ class Platform
3838
:redhat => {},
3939
:gentoo => {
4040
:default => {
41-
:package => Chef::Provider::Package::Portage
41+
:package => Chef::Provider::Package::Portage,
42+
:service => Chef::Provider::Service::Gentoo
4243
}
4344
},
4445
:solaris => {},
@@ -60,6 +61,7 @@ class Platform
6061
:user => Chef::Provider::User::Useradd,
6162
:group => Chef::Provider::Group::Groupadd,
6263
:http_request => Chef::Provider::HttpRequest,
64+
:route => Chef::Provider::Route
6365
}
6466
}
6567

chef/lib/chef/provider/route.rb

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#
2+
# Author:: Bryan McLellan (btm@loftninjas.org)
3+
# Copyright:: Copyright (c) 2009 Bryan McLellan
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 'chef/log'
20+
require 'chef/mixin/command'
21+
require 'chef/provider'
22+
23+
class Chef
24+
class Provider
25+
class Route < Chef::Provider
26+
include Chef::Mixin::Command
27+
28+
def load_current_resource
29+
@current_resource = Chef::Resource::Route.new(@new_resource.name)
30+
31+
Chef::Log.debug("Checking routes for #{@current_resource.target}")
32+
status = popen4("route -n") do |pid, stdin, stdout, stderr|
33+
stdin.close
34+
stdout.each do |line|
35+
case line
36+
# Destination Gateway Genmask Flags Metric Ref Use Iface
37+
when /^#{@new_resource.target}\s+([\d.]+)\s+([\d.]+)\s+(.+)\s+(\d+)\s+(.+)\s+(.+)\s+(\w+)$/
38+
@current_resource.target(@new_resource.target)
39+
@current_resource.gateway($1)
40+
@current_resource.netmask($2)
41+
@current_resource.metric($4.to_i)
42+
@current_resource.device($7)
43+
Chef::Log.debug("Found route ip:#{@current_resource.target} gw:#{@current_resource.gateway} nm:#{@current_resource.netmask} " +
44+
"metric:#{@current_resource.metric} dev:#{@current_resource.device}")
45+
end
46+
end
47+
end
48+
49+
unless status.exitstatus == 0
50+
raise Chef::Exception::Route, "route failed - #{status.inspect}!"
51+
end
52+
53+
@current_resource
54+
end
55+
56+
def action_add
57+
# check to see if load_current_resource found the route
58+
unless @current_resource.gateway
59+
if @new_resource.route_type == :net
60+
command = "route add -net #{@new_resource.target}"
61+
else
62+
command = "route add #{@new_resource.target}"
63+
end
64+
command << " netmask #{@new_resource.netmask}" if @new_resource.netmask
65+
command << " gw #{@new_resource.gateway}" if @new_resource.gateway
66+
command << " metric #{@new_resource.metric}" if @new_resource.metric
67+
command << " dev #{@new_resource.device}" if @new_resource.device
68+
69+
run_command(
70+
:command => command
71+
)
72+
@new_resource.updated = true
73+
else
74+
Chef::Log.debug("Route #{@current_resource} already exists")
75+
end
76+
end
77+
78+
def action_delete
79+
# check to see if load_current_resource found the route
80+
if @current_resource.gateway
81+
command = "route del #{@new_resource.target}"
82+
command << " netmask #{@new_resource.netmask}" if @new_resource.netmask
83+
command << " gw #{@new_resource.gateway}" if @new_resource.gateway
84+
command << " metric #{@new_resource.metric}" if @new_resource.metric
85+
command << " dev #{@new_resource.device}" if @new_resource.device
86+
87+
run_command(
88+
:command => command
89+
)
90+
@new_resource.updated = true
91+
else
92+
Chef::Log.debug("Route #{@current_resource} does not exist")
93+
end
94+
end
95+
end
96+
end
97+
end
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#
2+
# Author:: Lee Jensen (<ljensen@engineyard.com>)
3+
# Copyright:: Copyright (c) 2008 Opscode, Inc.
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 'chef/provider/service'
20+
require 'chef/mixin/command'
21+
22+
class Chef
23+
class Provider
24+
class Service
25+
class Gentoo < Chef::Provider::Service::Init
26+
def load_current_resource
27+
super
28+
29+
raise Chef::Exception::Service unless ::File.exists?("/sbin/rc-update")
30+
31+
IO.popen('/sbin/rc-update -s default') do |io|
32+
io.each_line do |line|
33+
if line.match(/^\s*#{@current_resource.service_name}\s+/)
34+
@current_resource.enabled true
35+
end
36+
end
37+
end
38+
39+
@current_resource
40+
end
41+
42+
def enable_service(name)
43+
run_command(:command => "/sbin/rc-update add #{name} default")
44+
end
45+
46+
def disable_service(name)
47+
run_command(:command => "/sbin/rc-update del #{name} default")
48+
end
49+
end
50+
end
51+
end
52+
end

chef/lib/chef/resource/route.rb

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#
2+
# Author:: Bryan McLellan (btm@loftninjas.org)
3+
# Copyright:: Copyright (c) 2009 Bryan McLellan
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 'chef/resource'
20+
21+
class Chef
22+
class Resource
23+
class Route < Chef::Resource
24+
25+
def initialize(name, collection=nil, node=nil)
26+
super(name, collection, node)
27+
@resource_name = :route
28+
@target = name
29+
@action = :add
30+
@allowed_actions.push(:add, :delete)
31+
@netmask = nil
32+
@gateway = nil
33+
@metric = nil
34+
@device = nil
35+
@route_type = :host
36+
end
37+
38+
def target(arg=nil)
39+
set_or_return(
40+
:target,
41+
arg,
42+
:kind_of => String
43+
)
44+
end
45+
46+
def netmask(arg=nil)
47+
set_or_return(
48+
:netmask,
49+
arg,
50+
:kind_of => String
51+
)
52+
end
53+
54+
def gateway(arg=nil)
55+
set_or_return(
56+
:gateway,
57+
arg,
58+
:kind_of => String
59+
)
60+
end
61+
62+
def metric(arg=nil)
63+
set_or_return(
64+
:metric,
65+
arg,
66+
:kind_of => Integer
67+
)
68+
end
69+
70+
def device(arg=nil)
71+
set_or_return(
72+
:device,
73+
arg,
74+
:kind_of => String
75+
)
76+
end
77+
78+
def route_type(arg=nil)
79+
real_arg = arg.kind_of?(String) ? arg.to_sym : arg
80+
set_or_return(
81+
:route_type,
82+
real_arg,
83+
:equal_to => [ :host, :net ]
84+
)
85+
end
86+
end
87+
end
88+
end
89+
90+
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#
2+
# Author:: Bryan McLellan (btm@loftninjas.org)
3+
# Copyright:: Copyright (c) 2009 Bryan McLellan
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::Provider::Route, "initialize" do
22+
before(:each) do
23+
@node = mock("Chef::Node", :null_object => true)
24+
@new_resource = mock("Chef::Resource", :null_object => true)
25+
end
26+
27+
it "should return a Chef::Provider::Route object" do
28+
provider = Chef::Provider::Route.new(@node, @new_resource)
29+
provider.should be_a_kind_of(Chef::Provider::Route)
30+
end
31+
32+
end
33+
34+
describe Chef::Provider::Route, "action_add" do
35+
before(:each) do
36+
@node = mock("Chef::Node", :null_object => true)
37+
@new_resource = mock("Chef::Resource::Route",
38+
:null_object => true,
39+
:name => "10.0.0.10",
40+
:target => "10.0.0.10",
41+
:gateway => "10.0.0.9"
42+
)
43+
@current_resource = mock("Chef::Resource::Route",
44+
:null_object => true,
45+
:name => "10.0.0.10",
46+
:target => "10.0.0.10",
47+
:gateway => "10.0.0.9"
48+
)
49+
50+
@provider = Chef::Provider::Route.new(@node, @new_resource)
51+
@provider.current_resource = @current_resource
52+
end
53+
54+
it "should add the route if it does not exist" do
55+
@provider.stub!(:run_command).and_return(true)
56+
@current_resource.stub!(:gateway).and_return(nil)
57+
@new_resource.should_recieve(:updated=).with(true)
58+
@provider.action_add
59+
end
60+
61+
it "should not add the route if it exists" do
62+
@provider.stub!(:run_command).and_return(true)
63+
@new_resource.should_not_recieve(:updated=).with(true)
64+
@provider.action_add
65+
end
66+
end
67+
68+
describe Chef::Provider::Route, "action_delete" do
69+
before(:each) do
70+
@node = mock("Chef::Node", :null_object => true)
71+
@new_resource = mock("Chef::Resource::Route",
72+
:null_object => true,
73+
:name => "10.0.0.10",
74+
:target => "10.0.0.10",
75+
:gateway => "10.0.0.9"
76+
)
77+
@current_resource = mock("Chef::Resource::Route",
78+
:null_object => true,
79+
:name => "10.0.0.10",
80+
:target => "10.0.0.10",
81+
:gateway => "10.0.0.9"
82+
)
83+
84+
@provider = Chef::Provider::Route.new(@node, @new_resource)
85+
@provider.current_resource = @current_resource
86+
end
87+
88+
it "should delete the route if it exists" do
89+
@provider.stub!(:run_command).and_return(true)
90+
@new_resource.should_recieve(:updated=).with(true)
91+
@provider.action_delete
92+
end
93+
94+
it "should not delete the route if it does not exist" do
95+
@provider.stub!(:run_command).and_return(true)
96+
@new_resource.should_not_recieve(:updated=).with(true)
97+
@provider.action_delete
98+
end
99+
end

0 commit comments

Comments
 (0)