Class: Chef::Provider::Route
- Inherits:
-
Chef::Provider
- Object
- Chef::Provider
- Chef::Provider::Route
- 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" }.freeze
Constants included from Mixin::ShellOut
Mixin::ShellOut::DEPRECATED_OPTIONS
Instance Attribute Summary collapse
-
#is_running ⇒ Object
Returns the value of attribute is_running.
Attributes inherited from Chef::Provider
#action, #cookbook_name, #current_resource, #new_resource, #recipe_name, #run_context
Instance Method Summary collapse
- #action_add ⇒ Object
- #action_delete ⇒ Object
- #config_file_contents(action, options = {}) ⇒ Object
- #generate_command(action) ⇒ Object
- #generate_config ⇒ Object
- #hex2ip(hex_data) ⇒ Object
- #load_current_resource ⇒ Object
- #whyrun_supported? ⇒ Boolean
Methods included from Mixin::Command
#chdir_or_tmpdir, #handle_command_failures, #output_of_command, #run_command, #run_command_and_return_stdout_stderr, #run_command_with_systems_locale
Methods included from Mixin::Command::Windows
Methods included from Mixin::Command::Unix
Methods inherited from Chef::Provider
#action_nothing, #check_resource_semantics!, #cleanup_after_converge, #converge_by, #converge_if_changed, #define_resource_requirements, #events, include_resource_dsl, include_resource_dsl_module, #initialize, #node, #process_resource_requirements, provides, provides?, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, supports?, use_inline_resources, #whyrun_mode?
Methods included from Mixin::Provides
#provided_as, #provides, #provides?
Methods included from Mixin::DescendantsTracker
#descendants, descendants, direct_descendants, #direct_descendants, find_descendants_by_name, #find_descendants_by_name, #inherited, store_inherited
Methods included from DeprecatedLWRPClass
#const_missing, #deprecated_constants, #register_deprecated_lwrp_class
Methods included from Mixin::LazyModuleInclude
#descendants, #include, #included
Methods included from Mixin::NotifyingBlock
#notifying_block, #subcontext_block
Methods included from DSL::DeclareResource
#build_resource, #declare_resource, #delete_resource, #delete_resource!, #edit_resource, #edit_resource!, #find_resource, #find_resource!, #with_run_context
Methods included from Mixin::ShellOut
#a_to_s, #clean_array, #run_command_compatible_options, #shell_out, #shell_out!, #shell_out_compact, #shell_out_compact!, #shell_out_with_systems_locale, #shell_out_with_systems_locale!
Methods included from Mixin::PowershellOut
#powershell_out, #powershell_out!
Methods included from Mixin::WindowsArchitectureHelper
#assert_valid_windows_architecture!, #disable_wow64_file_redirection, #forced_32bit_override_required?, #is_i386_process_on_x86_64_windows?, #node_supports_windows_architecture?, #node_windows_architecture, #restore_wow64_file_redirection, #valid_windows_architecture?, #with_os_architecture, #wow64_architecture_override_required?, #wow64_directory
Methods included from DSL::PlatformIntrospection
#docker?, #platform?, #platform_family?, #value_for_platform, #value_for_platform_family
Constructor Details
This class inherits a constructor from Chef::Provider
Instance Attribute Details
#is_running ⇒ Object
Returns the value of attribute is_running.
31 32 33 |
# File 'lib/chef/provider/route.rb', line 31 def is_running @is_running end |
Instance Method Details
#action_add ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/chef/provider/route.rb', line 133 def action_add # check to see if load_current_resource found the route if is_running Chef::Log.debug("#{new_resource} route already active - nothing to do") else command = generate_command(:add) converge_by("run #{command.join(' ')} to add route") do shell_out_compact!(command) Chef::Log.info("#{new_resource} added") end end # for now we always write the file (ugly but its what it is) generate_config end |
#action_delete ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/chef/provider/route.rb', line 149 def action_delete if is_running command = generate_command(:delete) converge_by("run #{command.join(' ')} to delete route ") do shell_out_compact!(command) Chef::Log.info("#{new_resource} removed") end else Chef::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_config end |
#config_file_contents(action, options = {}) ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/chef/provider/route.rb', line 217 def config_file_contents(action, = {}) content = "" case action when :add content << ([:target]).to_s content << "/#{options[:netmask]}" if [:netmask] content << " via #{options[:gateway]}" if [:gateway] content << "\n" end content end |
#generate_command(action) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/chef/provider/route.rb', line 200 def generate_command(action) target = new_resource.target target = "#{target}/#{MASK[new_resource.netmask.to_s]}" if new_resource.netmask case action when :add command = [ "ip", "route", "replace", target ] command += [ "via", new_resource.gateway ] if new_resource.gateway command += [ "dev", new_resource.device ] if new_resource.device when :delete command = [ "ip", "route", "delete", target ] command += [ "via", new_resource.gateway ] if new_resource.gateway end command end |
#generate_config ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/chef/provider/route.rb', line 164 def generate_config conf = {} case node[:platform] when "centos", "redhat", "fedora" # walk the collection run_context.resource_collection.each do |resource| next unless resource.is_a? Chef::Resource::Route # default to eth0 dev = if resource.device resource.device else "eth0" end conf[dev] = "" if conf[dev].nil? case @action when :add conf[dev] << config_file_contents(:add, target: resource.target, netmask: resource.netmask, gateway: resource.gateway) if resource.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) end end conf.each do |k, v| network_file_name = "/etc/sysconfig/network-scripts/route-#{k}" converge_by("write route route.#{k}\n#{conf[k]} to #{network_file_name}") do network_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.close end end end end |
#hex2ip(hex_data) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/chef/provider/route.rb', line 67 def hex2ip(hex_data) # Cleanup hex data hex_ip = hex_data.to_s.downcase.gsub(/[^0-9a-f]/, "") # Check hex data format (IP is a 32bit integer, so should be 8 chars long) return nil if hex_ip.length != hex_data.length || hex_ip.length != 8 # Extract octets from hex data octets = hex_ip.scan(/../).reverse.collect { |octet| [octet].pack("H2").unpack("C").first } # Validate IP ip = octets.join(".") begin IPAddr.new(ip, Socket::AF_INET).to_s rescue ArgumentError Chef::Log.debug("Invalid IP address data: hex=#{hex_ip}, ip=#{ip}") return nil end end |
#load_current_resource ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/chef/provider/route.rb', line 91 def load_current_resource self.is_running = false # cidr or quad dot mask new_ip = if new_resource.netmask IPAddr.new("#{new_resource.target}/#{new_resource.netmask}") else IPAddr.new(new_resource.target) end # For linux, we use /proc/net/route file to read proc table info return if node[: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) next unless destination && gateway && mask Chef::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 = true if running_ip == new_ip && gateway == new_resource.gateway end route_file.close end |
#whyrun_supported? ⇒ Boolean
87 88 89 |
# File 'lib/chef/provider/route.rb', line 87 def whyrun_supported? true end |