|
| 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 |
0 commit comments