Instance Attribute Details

#is_running ⇒ Object

Instance Method Details

#action_add ⇒ Object

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141

# File 'lib/chef/provider/route.rb', line 127defaction_add# check to see if load_current_resource found the route
ifis_runningChef::Log.debug("#{new_resource} route already active - nothing to do")elsecommand=generate_command(:add)converge_by("run #{command.join('')} to add route")doshell_out_compact!(command)Chef::Log.info("#{new_resource} added")endend# for now we always write the file (ugly but its what it is)
generate_configend

#action_delete ⇒ Object

143
144
145
146
147
148
149
150
151
152
153
154
155
156

# File 'lib/chef/provider/route.rb', line 143defaction_deleteifis_runningcommand=generate_command(:delete)converge_by("run #{command.join('')} to delete route ")doshell_out_compact!(command)Chef::Log.info("#{new_resource} removed")endelseChef::Log.debug("#{new_resource} route does not exist - nothing to do")end# for now we always write the file (ugly but its what it is)
generate_configend

#generate_config ⇒ Object

# File 'lib/chef/provider/route.rb', line 158defgenerate_configconf={}casenode[:platform]when"centos","redhat","fedora"# walk the collection
run_context.resource_collection.eachdo|resource|nextunlessresource.is_a?Chef::Resource::Route# default to eth0
dev=ifresource.deviceresource.deviceelse"eth0"endconf[dev]=""ifconf[dev].nil?case@actionwhen:addconf[dev]<<config_file_contents(:add,target:resource.target,netmask:resource.netmask,gateway:resource.gateway)ifresource.action==[:add]when:delete# need to do this for the case when the last route on an int
# is removed
conf[dev]<<config_file_contents(:delete)endendconf.eachdo|k,v|network_file_name="/etc/sysconfig/network-scripts/route-#{k}"converge_by("write route route.#{k}\n#{conf[k]} to #{network_file_name}")donetwork_file=::File.new(network_file_name,"w")network_file.puts(conf[k])Chef::Log.debug("#{new_resource} writing route.#{k}\n#{conf[k]}")network_file.closeendendendend

#load_current_resource ⇒ Object

# File 'lib/chef/provider/route.rb', line 85defload_current_resourceself.is_running=false# cidr or quad dot mask
new_ip=ifnew_resource.netmaskIPAddr.new("#{new_resource.target}/#{new_resource.netmask}")elseIPAddr.new(new_resource.target)end# For linux, we use /proc/net/route file to read proc table info
returnifnode[:os]!="linux"route_file=::File.open("/proc/net/route","r")# Read all routes
while(line=route_file.gets)# Get all the fields for a route
_,destination,gateway,_,_,_,_,mask=line.split# Convert hex-encoded values to quad-dotted notation (e.g. 0064A8C0 => 192.168.100.0)
destination=hex2ip(destination)gateway=hex2ip(gateway)mask=hex2ip(mask)# Skip formatting lines (header, etc)
nextunlessdestination&&gateway&&maskChef::Log.debug("#{new_resource} 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_resource} new ip: #{new_ip.inspect} running ip: #{running_ip.inspect}")self.is_running=trueifrunning_ip==new_ip&&gateway==new_resource.gatewayendroute_file.closeend