Class: Chef::Provider::Ifconfig

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

Direct Known Subclasses

Debian, Redhat

Defined Under Namespace

Classes: Debian, Redhat

Instance Attribute Summary collapse

Attributes inherited from Chef::Provider

#action, #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::Windows

#popen4

Methods included from Mixin::Command::Unix

#popen4

Methods inherited from Chef::Provider

#action_nothing, #cleanup_after_converge, #converge_by, #cookbook_name, #events, #node, #process_resource_requirements, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, #whyrun_mode?

Methods included from DSL::Recipe

#method_missing

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Constructor Details

#initialize(new_resource, run_context) ⇒ Ifconfig

Returns a new instance of Ifconfig.



45
46
47
48
49
# File 'lib/chef/provider/ifconfig.rb', line 45

def initialize(new_resource, run_context)
  super(new_resource, run_context)
  @config_template = nil
  @config_path = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Chef::DSL::Recipe

Instance Attribute Details

#config_pathObject

Returns the value of attribute config_path.



43
44
45
# File 'lib/chef/provider/ifconfig.rb', line 43

def config_path
  @config_path
end

#config_templateObject

Returns the value of attribute config_template.



42
43
44
# File 'lib/chef/provider/ifconfig.rb', line 42

def config_template
  @config_template
end

Instance Method Details

#action_addObject



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

def action_add
  # check to see if load_current_resource found interface in 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
    converge_by ("run #{command} to add #{@new_resource}") do
      run_command(
        :command => command
      )
      Chef::Log.info("#{@new_resource} added")
    end
  end

  # Write out the config files
  generate_config
end

#action_deleteObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/chef/provider/ifconfig.rb', line 143

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

#action_disableObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/chef/provider/ifconfig.rb', line 159

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"
    converge_by ("run #{command} to disable #{@new_resource}") do
      run_command(
        :command => command
      )
      Chef::Log.info("#{@new_resource} disabled")
    end
  else
    Chef::Log.debug("#{@new_resource} does not exist - nothing to do")
  end
end

#action_enableObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/chef/provider/ifconfig.rb', line 123

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

    converge_by ("run #{command} to enable #{@new_resource}") do
      run_command(
        :command => command
      )
      Chef::Log.info("#{@new_resource} enabled")
    end
  end
end

#can_generate_config?Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/chef/provider/ifconfig.rb', line 175

def can_generate_config?
  ! @config_template.nil? and ! @config_path.nil?
end

#define_resource_requirementsObject



92
93
94
95
96
97
98
99
100
# File 'lib/chef/provider/ifconfig.rb', line 92

def define_resource_requirements 
  requirements.assert(:all_actions) do |a| 
    a.assertion { @status.exitstatus == 0 }
    a.failure_message Chef::Exceptions::Ifconfig, "ifconfig failed - #{@status.inspect}!"
    # no whyrun - if the base ifconfig used in load_current_resource fails
    # there's no reasonable action that could have been taken in the course of 
    # a chef run to fix it.
  end
end

#delete_configObject



191
192
193
194
195
196
197
198
199
200
# File 'lib/chef/provider/ifconfig.rb', line 191

def delete_config
  return unless can_generate_config?
  require 'fileutils'
  if ::File.exist?(@config_path)
    converge_by ("delete the #{@config_path}") do
      FileUtils.rm_f(@config_path, :verbose => false)
    end
  end
  Chef::Log.info("#{@new_resource} deleted configuration file")
end

#generate_configObject



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

def generate_config
  return unless can_generate_config?
  b = binding
  template = ::ERB.new(@config_template)
  converge_by ("generate configuration file : #{@config_path}") do
    network_file = ::File.new(@config_path, "w")
    network_file.puts(template.result(b))
    network_file.close
  end
  Chef::Log.info("#{@new_resource} created configuration file")
end

#load_current_resourceObject



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
81
82
83
84
85
86
87
88
89
90
# File 'lib/chef/provider/ifconfig.rb', line 55

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

  @ifconfig_success = true
  @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
  @current_resource
end

#whyrun_supported?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/chef/provider/ifconfig.rb', line 51

def whyrun_supported?
  true
end