Class: Chef::Provider::Ifconfig

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

Direct Known Subclasses

Aix, Debian, Redhat

Defined Under Namespace

Classes: Aix, Debian, Redhat

Constant Summary

Constants included from Mixin::ShellOut

Mixin::ShellOut::DEPRECATED_OPTIONS

Instance Attribute Summary collapse

Attributes inherited from Chef::Provider

#action, #cookbook_name, #current_resource, #new_resource, #recipe_name, #run_context

Instance Method Summary collapse

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

#popen4

Methods included from Mixin::Command::Unix

#popen4

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 inherited from Chef::Provider

#action_nothing, #check_resource_semantics!, #cleanup_after_converge, #converge_by, #converge_if_changed, #events, include_resource_dsl, include_resource_dsl_module, #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::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

#initialize(new_resource, run_context) ⇒ Ifconfig

Returns a new instance of Ifconfig.



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

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

Instance Attribute Details

#config_pathObject

Returns the value of attribute config_path.



48
49
50
# File 'lib/chef/provider/ifconfig.rb', line 48

def config_path
  @config_path
end

#config_templateObject

Returns the value of attribute config_template.



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

def config_template
  @config_template
end

Instance Method Details

#action_addObject



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

def action_add
  # check to see if load_current_resource found interface in ifconfig
  unless current_resource.inet_addr
    unless new_resource.device == loopback_device
      command = add_command
      converge_by("run #{command.join(' ')} to add #{new_resource}") do
        shell_out_compact!(command)
        Chef::Log.info("#{new_resource} added")
      end
    end
  end
  # Write out the config files
  generate_config
end

#action_deleteObject



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/chef/provider/ifconfig.rb', line 131

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

#action_disableObject



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

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

#action_enableObject



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/chef/provider/ifconfig.rb', line 119

def action_enable
  # check to see if load_current_resource found ifconfig
  # enables, but does not manage config files
  return if current_resource.inet_addr
  return if new_resource.device == loopback_device
  command = enable_command
  converge_by("run #{command.join(' ')} to enable #{new_resource}") do
    shell_out_compact!(command)
    Chef::Log.info("#{new_resource} enabled")
  end
end

#can_generate_config?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/chef/provider/ifconfig.rb', line 159

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

#define_resource_requirementsObject



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

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



177
178
179
180
181
182
# File 'lib/chef/provider/ifconfig.rb', line 177

def delete_config
  return unless can_generate_config?
  config = resource_for_config(@config_path)
  config.run_action(:delete)
  new_resource.updated_by_last_action(true) if config.updated?
end

#generate_configObject



167
168
169
170
171
172
173
174
175
# File 'lib/chef/provider/ifconfig.rb', line 167

def generate_config
  return unless can_generate_config?
  b = binding
  template = ::ERB.new(@config_template)
  config = resource_for_config(@config_path)
  config.content(template.result(b))
  config.run_action(:create)
  new_resource.updated_by_last_action(true) if config.updated?
end

#load_current_resourceObject



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
91
92
# File 'lib/chef/provider/ifconfig.rb', line 60

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

  @ifconfig_success = true
  @interfaces = {}

  @status = shell_out("ifconfig")
  @status.stdout.each_line 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+)/ ? Regexp.last_match(1) : "nil") if line =~ /inet addr:/
      @interfaces[@int_name]["bcast"] = (line =~ /Bcast:(\S+)/ ? Regexp.last_match(1) : "nil") if line =~ /Bcast:/
      @interfaces[@int_name]["mask"] = (line =~ /Mask:(\S+)/ ? Regexp.last_match(1) : "nil") if line =~ /Mask:/
      @interfaces[@int_name]["mtu"] = (line =~ /MTU:(\S+)/ ? Regexp.last_match(1) : "nil") if line =~ /MTU:/
      @interfaces[@int_name]["metric"] = (line =~ /Metric:(\S+)/ ? Regexp.last_match(1) : "nil") if line =~ /Metric:/
    end

    next unless @interfaces.key?(new_resource.device)
    @interface = @interfaces.fetch(new_resource.device)

    current_resource.target(new_resource.target)
    current_resource.device(new_resource.device)
    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
  current_resource
end

#resource_for_config(path) ⇒ Object



163
164
165
# File 'lib/chef/provider/ifconfig.rb', line 163

def resource_for_config(path)
  Chef::Resource::File.new(path, run_context)
end

#whyrun_supported?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/chef/provider/ifconfig.rb', line 56

def whyrun_supported?
  true
end