Class: Chef::Provider::Ifconfig

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

Instance Attribute Summary

Attributes inherited from Chef::Provider

#current_resource, #new_resource, #run_context

Instance Method Summary collapse

Methods included from Mixin::Command

#chdir_or_tmpdir, #handle_command_failures, #output_of_command, #run_command, #run_command_with_systems_locale

Methods included from Mixin::Command::Unix

#popen4

Methods included from Mixin::Command::Windows

#popen4

Methods inherited from Chef::Provider

#action_nothing, build_from_file, #cookbook_name, #initialize, #node, #resource_collection

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

#action_addObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/chef/provider/ifconfig.rb', line 82

def action_add
  # check to see if load_current_resource found ifconfig
  unless @current_resource.inet_addr
    unless @new_resource.device == "lo"
      command = "ifconfig #{@new_resource.device} #{@new_resource.name}"
      command << " netmask #{@new_resource.mask}" if @new_resource.mask
      command << " metric #{@new_resource.metric}" if @new_resource.metric
      command << " mtu #{@new_resource.mtu}" if @new_resource.mtu
    end

    run_command(
      :command => command
    )
    Chef::Log.info("#{@new_resource} added")
    @new_resource.updated_by_last_action(true)
  end

  # Write out the config files
  generate_config
end

#action_deleteObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/chef/provider/ifconfig.rb', line 122

def action_delete
  # check to see if load_current_resource found the interface
  if @current_resource.device
    command = "ifconfig #{@new_resource.device} down"
    run_command(
      :command => command
    )
    delete_config
    Chef::Log.info("#{@new_resource} deleted")
    @new_resource.updated_by_last_action(true)
  else
    Chef::Log.debug("#{@new_resource} does not exist - nothing to do")
  end
end

#action_disableObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/chef/provider/ifconfig.rb', line 137

def action_disable
  # check to see if load_current_resource found the interface
  # disables, but leaves config files in place.
  if @current_resource.device
    command = "ifconfig #{@new_resource.device} down"
    run_command(
      :command => command
    )
    Chef::Log.info("#{@new_resource} disabled")
    @new_resource.updated_by_last_action(true)
  else
    Chef::Log.debug("#{@new_resource} does not exist - nothing to do")
  end
end

#action_enableObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/chef/provider/ifconfig.rb', line 103

def action_enable
  # check to see if load_current_resource found ifconfig
  # enables, but does not manage config files
  unless @current_resource.inet_addr
    unless @new_resource.device == "lo"
      command = "ifconfig #{@new_resource.device} #{@new_resource.name}"
      command << " netmask #{@new_resource.mask}" if @new_resource.mask
      command << " metric #{@new_resource.metric}" if @new_resource.metric
      command << " mtu #{@new_resource.mtu}" if @new_resource.mtu
    end

    run_command(
      :command => command
    )
    Chef::Log.info("#{@new_resource} enabled")
    @new_resource.updated_by_last_action(true)
  end
end

#delete_configObject



178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/chef/provider/ifconfig.rb', line 178

def delete_config
  require 'fileutils'
  case node[:platform]
  when "centos","redhat","fedora"
    ifcfg_file = "/etc/sysconfig/network-scripts/ifcfg-#{@new_resource.device}"
    if ::File.exist?(ifcfg_file)
      FileUtils.rm_f(ifcfg_file, :verbose => false, :force => true)
    end
  when "debian","ubuntu"
    # delete configs
  when "slackware"
    # delete configs
  end
end

#generate_configObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/chef/provider/ifconfig.rb', line 152

def generate_config
  b = binding
  case node[:platform]
  when "centos","redhat","fedora"
    content = %{
<% if @new_resource.device %>DEVICE=<%= @new_resource.device %><% end %>
<% if @new_resource.onboot %>ONBOOT=<%= @new_resource.onboot %><% end %>
<% if @new_resource.bootproto %>BOOTPROTO=<%= @new_resource.bootproto %><% end %>
<% if @new_resource.target %>IPADDR=<%= @new_resource.target %><% end %>
<% if @new_resource.mask %>NETMASK=<%= @new_resource.mask %><% end %>
<% if @new_resource.network %>NETWORK=<%= @new_resource.network %><% end %>
<% if @new_resource.bcast %>BROADCAST=<%= @new_resource.bcast %><% end %>
<% if @new_resource.onparent %>ONPARENT=<%= @new_resource.onparent %><% end %>
    }
    template = ::ERB.new(content)
    network_file = ::File.new("/etc/sysconfig/network-scripts/ifcfg-#{@new_resource.device}", "w")
    network_file.puts(template.result(b))
    network_file.close
    Chef::Log.info("#{@new_resource} created configuration file")
  when "debian","ubuntu"
    # template
  when "slackware"
    # template
  end
end

#load_current_resourceObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/chef/provider/ifconfig.rb', line 41

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

  @interfaces = {}

  status = popen4("ifconfig") do |pid, stdin, stdout, stderr|
    stdout.each do |line|

      if !line[0..9].strip.empty?
        @int_name = line[0..9].strip
        @interfaces[@int_name] = {"hwaddr" => (line =~ /(HWaddr)/ ? ($') : "nil").strip.chomp }
      else
        @interfaces[@int_name]["inet_addr"] = (line =~ /inet addr:(\S+)/ ? ($1) : "nil") if line =~ /inet addr:/
        @interfaces[@int_name]["bcast"] = (line =~ /Bcast:(\S+)/ ? ($1) : "nil") if line =~ /Bcast:/
        @interfaces[@int_name]["mask"] = (line =~ /Mask:(\S+)/ ? ($1) : "nil") if line =~ /Mask:/
        @interfaces[@int_name]["mtu"] = (line =~ /MTU:(\S+)/ ? ($1) : "nil") if line =~ /MTU:/
        @interfaces[@int_name]["metric"] = (line =~ /Metric:(\S+)/ ? ($1) : "nil") if line =~ /Metric:/
      end

      if @interfaces.has_key?(@new_resource.device)
        @interface = @interfaces.fetch(@new_resource.device)

        @current_resource.target(@new_resource.target)
        @current_resource.device(@int_name)
        @current_resource.inet_addr(@interface["inet_addr"])
        @current_resource.hwaddr(@interface["hwaddr"])
        @current_resource.bcast(@interface["bcast"])
        @current_resource.mask(@interface["mask"])
        @current_resource.mtu(@interface["mtu"])
        @current_resource.metric(@interface["metric"])
      end
    end
  end

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

  @current_resource
end