Class: Chef::Provider::Route

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

Instance Attribute Summary

Attributes inherited from Chef::Provider

#current_resource, #new_resource, #node

Instance Method Summary collapse

Methods included from Mixin::Command

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, #filename_to_qualified_string

Methods included from Mixin::RecipeDefinitionDSLCore

#method_missing

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 Method Details

#action_addObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/chef/provider/route.rb', line 56

def action_add
  # check to see if load_current_resource found the route
  unless @current_resource.gateway
    if @new_resource.route_type == :net
      command = "route add -net #{@new_resource.target}"
    else
      command = "route add #{@new_resource.target}"
    end
    command << " netmask #{@new_resource.netmask}" if @new_resource.netmask
    command << " gw #{@new_resource.gateway}" if @new_resource.gateway
    command << " metric #{@new_resource.metric}" if @new_resource.metric
    command << " dev #{@new_resource.device}" if @new_resource.device
  
    run_command(
      :command => command
    )
    @new_resource.updated = true
  else
    Chef::Log.debug("Route #{@current_resource} already exists")
  end
  # Write out the config files
  generate_config
end

#action_deleteObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/chef/provider/route.rb', line 80

def action_delete
  # check to see if load_current_resource found the route
  if @current_resource.gateway 
    command = "route del #{@new_resource.target}"
    command << " netmask #{@new_resource.netmask}" if @new_resource.netmask
    command << " gw #{@new_resource.gateway}" if @new_resource.gateway
    command << " metric #{@new_resource.metric}" if @new_resource.metric
    command << " dev #{@new_resource.device}" if @new_resource.device
  
    run_command(
      :command => command
    )
    @new_resource.updated = true
  else
    Chef::Log.debug("Route #{@current_resource} does not exist")
  end
end

#generate_configObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/chef/provider/route.rb', line 98

def generate_config
  b = binding
  case node[:platform]
  when ("centos" || "redhat" || "fedora")
    content = %{
<% if @new_resource.networking %>NETWORKING=<%= @new_resource.networking %><% end %>
<% if @new_resource.networking_ipv6 %>NETWORKING_IPV6=<%= @new_resource.networking_ipv6 %><% end %>
<% if @new_resource.hostname %>HOSTNAME=<%= @new_resource.hostname %><% end %>
<% if @new_resource.name %>GATEWAY=<%= @new_resource.name %><% end %>
<% if @new_resource.domainname %>DOMAINNAME=<%= @new_resource.domainname %><% end %>
<% if @new_resource.domainname %>DOMAIN=<%= @new_resource.domainname %><% end %>
    }
    template = ::ERB.new(content)
    network_file = ::File.new("/etc/sysconfig/network", "w")
    network_file.puts(template.result(b))
    network_file.close
  end
end

#load_current_resourceObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/chef/provider/route.rb', line 29

def load_current_resource
  @current_resource = Chef::Resource::Route.new(@new_resource.name)

  Chef::Log.debug("Checking routes for #{@current_resource.target}")
  status = popen4("route -n") do |pid, stdin, stdout, stderr|
    stdout.each do |line|
      case line
      # Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
      when /^#{@new_resource.target}\s+([\d.]+)\s+([\d.]+)\s+(.+)\s+(\d+)\s+(.+)\s+(.+)\s+(\w+)$/
        @current_resource.target(@new_resource.target)
        @current_resource.gateway($1)
        @current_resource.netmask($2)
        @current_resource.metric($4.to_i)
        @current_resource.device($7)
        Chef::Log.debug("Found route ip:#{@current_resource.target} gw:#{@current_resource.gateway} nm:#{@current_resource.netmask} " +
            "metric:#{@current_resource.metric} dev:#{@current_resource.device}")
      end
    end
  end

  unless status.exitstatus == 0
    raise Chef::Exception::Route, "route failed - #{status.inspect}!"
  end

  @current_resource
end