Skip to content

Commit ceec2c8

Browse files
committed
add basic working route resource and provider
1 parent b057d05 commit ceec2c8

3 files changed

Lines changed: 188 additions & 0 deletions

File tree

chef/lib/chef/platform.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class Platform
6060
:user => Chef::Provider::User::Useradd,
6161
:group => Chef::Provider::Group::Groupadd,
6262
:http_request => Chef::Provider::HttpRequest,
63+
:route => Chef::Provider::Route
6364
}
6465
}
6566

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::Package, "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.target} 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.target} does not exist")
93+
end
94+
end
95+
end
96+
end
97+
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+

0 commit comments

Comments
 (0)