Class: Chef::Provider::Route

Inherits:
Chef::Provider show all
Includes:
Mixin::Command
Defined in:
lib/chef/provider/route.rb

Constant Summary collapse

MASK =
{ 
    '0.0.0.0'          => '0',  
    '128.0.0.0'        => '1',  
    '192.0.0.0'        => '2',  
    '224.0.0.0'        => '3',  
    '240.0.0.0'        => '4',  
    '248.0.0.0'        => '5',  
    '252.0.0.0'        => '6',  
    '254.0.0.0'        => '7',  
    '255.0.0.0'        => '8',  
    '255.128.0.0'      => '9',  
    '255.192.0.0'      => '10', 
    '255.224.0.0'      => '11', 
    '255.240.0.0'      => '12', 
    '255.248.0.0'      => '13', 
    '255.252.0.0'      => '14', 
    '255.254.0.0'      => '15', 
    '255.255.0.0'      => '16', 
    '255.255.128.0'    => '17', 
    '255.255.192.0'    => '18', 
    '255.255.224.0'    => '19', 
    '255.255.240.0'    => '20', 
    '255.255.248.0'    => '21', 
    '255.255.252.0'    => '22', 
    '255.255.254.0'    => '23', 
    '255.255.255.0'    => '24', 
    '255.255.255.128'  => '25', 
    '255.255.255.192'  => '26', 
    '255.255.255.224'  => '27', 
    '255.255.255.240'  => '28', 
    '255.255.255.248'  => '29', 
    '255.255.255.252'  => '30', 
    '255.255.255.254'  => '31', 
    '255.255.255.255'  => '32' 
}

Instance Attribute Summary collapse

Attributes inherited from Chef::Provider

#current_resource, #new_resource, #node

Instance Method Summary collapse

Methods included from Mixin::Command

chdir_or_tmpdir, handle_command_failures, not_if, only_if, output_of_command, popen4, run_command, run_command_with_systems_locale

Methods inherited from Chef::Provider

#action_nothing, build_from_file, #initialize

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Methods included from Mixin::RecipeDefinitionDSLCore

#method_missing

Methods included from Mixin::Language

#data_bag, #data_bag_item, #platform?, #search, #value_for_platform

Constructor Details

This class inherits a constructor from Chef::Provider

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Chef::Mixin::RecipeDefinitionDSLCore

Instance Attribute Details

#is_runningObject

Returns the value of attribute is_running.



27
28
29
# File 'lib/chef/provider/route.rb', line 27

def is_running
  @is_running
end

Instance Method Details

#action_addObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/chef/provider/route.rb', line 106

def action_add
    # check to see if load_current_resource found the route
    if  is_running
        Chef::Log.debug("Route #{@new_resource.name} already active ")
    else
        command =  "ip route replace #{@new_resource.target}"
        command << "/#{MASK[@new_resource.netmask.to_s]}" if @new_resource.netmask 
        command << " via #{@new_resource.gateway} "
        command << " dev #{@new_resource.device} " if @new_resource.device
        
        Chef::Log.info("Adding route: #{command} ")  
        run_command( :command => command )
        @new_resource.updated = true
    end
    
    #for now we always write the file (ugly but its what it is)
    generate_config

end

#action_deleteObject



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/chef/provider/route.rb', line 126

def action_delete
    if is_running
        command = "ip route delete #{@new_resource.target}"
        command << "/#{MASK[@new_resource.netmask.to_s]}" if @new_resource.netmask 
        command << " via #{@new_resource.gateway} " 
        
        Chef::Log.info("Removing route: #{command}")  
        run_command( :command => command )
        @new_resource.updated = true
    else
        Chef::Log.debug("Route #{@new_resource.name} does not exist")
    end
end

#generate_configObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/chef/provider/route.rb', line 140

def generate_config
    conf = Hash.new
    case node[:platform]
    when ("centos" || "redhat" || "fedora")
        # walk the collection 
        @collection.each do |resource|
            if resource.is_a? Chef::Resource::Route
                # default to eth0 
                if resource.device
                  dev = resource.device
                else
                  dev = "eth0"
                end
               
                conf[dev] = String.new if conf[dev].nil? 
                if resource.action == :add
                    conf[dev] << "#{resource.target}"
                    conf[dev] << "/#{resource.netmask}" if resource.netmask
                    conf[dev] << " via #{resource.gateway}\n" 
                else 
                    # need to do this for the case when the last route on an int
                    # is removed
                    conf[dev] = ""
                end
            end
        end
        conf.each do |k, v|
           network_file = ::File.new("/etc/sysconfig/network-scripts/route-#{k}", "w")
           network_file.puts(conf[k])
           Chef::Log.debug("writing route.#{k}\n#{conf[k]}")
           network_file.close
        end 
    end
end

#load_current_resourceObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/chef/provider/route.rb', line 65

def load_current_resource
    is_running = nil
    
    Chef::Log.debug("Configuring Route #{@new_resource.name}")

    # cidr or quad dot mask
    if @new_resource.netmask 
        new_ip = IPAddr.new("#{@new_resource.target}/#{@new_resource.netmask}") 
    else
        new_ip = IPAddr.new(@new_resource.target)
    end 

    # pull routes from proc
    if node[:os] == "linux" 
        route_file = ::File.open("/proc/net/route", "r") 
        while (line = route_file.gets)
            # proc layout
            iface,destination,gateway,flags,refcnt,use,metric,mask,mtu,window,irtt = line.split
          
            # need to convert packed adresses int quad dot
            #  the addrs are reversed hex packed decimal addrs. so this unwraps them. tho you could 
            #  do this without ipaddr using unpack. ipaddr has no htoa method.
            #
            destination = IPAddr.new(destination.scan(/../).reverse.to_s.hex, Socket::AF_INET).to_s
            gateway = IPAddr.new(gateway.scan(/../).reverse.to_s.hex, Socket::AF_INET).to_s
            mask = IPAddr.new(mask.scan(/../).reverse.to_s.hex, Socket::AF_INET).to_s  
            Chef::Log.debug( "System has route:  dest=#{destination} mask=#{mask} gw=#{gateway}")
            
            # check if what were trying to configure is already there
            # use an ipaddr object with ip/mask this way we can have 
            # a new resource be in cidr format (i don't feel like 
            # expanding bitmask by hand.
            #
            running_ip = IPAddr.new("#{destination}/#{mask}")
            Chef::Log.debug( "new ip: #{new_ip.inspect} running ip: #{running_ip.inspect} ")
            is_running = true if running_ip == new_ip 
        end
        route_file.close
    end
end