Class: Chef::Provider::Ifconfig

Inherits:
Chef::Provider show all
Includes:
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

Overview

use the ifconfig resource to manage interfaces on *nix systems

Examples:

set a static ip on eth1

ifconfig '33.33.33.80' do
  device 'eth1'
end

Direct Known Subclasses

Aix, Debian, Redhat

Defined Under Namespace

Classes: Aix, Debian, Redhat

Instance Attribute Summary collapse

Attributes inherited from Chef::Provider

#action, #after_resource, #current_resource, #logger, #new_resource, #recipe_name, #run_context

Instance Method Summary collapse

Methods included from Mixin::ShellOut

apply_default_env, maybe_add_timeout, #shell_out, #shell_out!

Methods inherited from Chef::Provider

action, #action_nothing, #check_resource_semantics!, #cleanup_after_converge, #compile_and_converge_action, #converge_by, #converge_if_changed, #cookbook_name, #description, #events, include_resource_dsl?, include_resource_dsl_module, #introduced, #load_after_resource, #node, #process_resource_requirements, provides, provides?, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, supports?, use, use_inline_resources, #validate_required_properties!, #whyrun_mode?, #whyrun_supported?

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 Mixin::LazyModuleInclude

#descendants, #include, #included

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 Mixin::PowershellExec

#powershell_exec, #powershell_exec!

Methods included from DSL::Powershell

#ps_credential

Methods included from DSL::RegistryHelper

#registry_data_exists?, #registry_get_subkeys, #registry_get_values, #registry_has_subkeys?, #registry_key_exists?, #registry_value_exists?

Methods included from DSL::ChefVault

#chef_vault, #chef_vault_item, #chef_vault_item_for_environment

Methods included from DSL::DataQuery

#data_bag, #data_bag_item, #search, #tagged?

Methods included from EncryptedDataBagItem::CheckEncrypted

#encrypted?

Methods included from DSL::PlatformIntrospection

#older_than_win_2012_or_8?, #platform?, #platform_family?, #value_for_platform, #value_for_platform_family

Methods included from DSL::Recipe

#exec, #have_resource_class_for?, #resource_class_for

Methods included from DSL::Definitions

add_definition, #evaluate_resource_definition, #has_resource_definition?

Methods included from DSL::Resources

add_resource_dsl, remove_resource_dsl

Methods included from DSL::Cheffish

load_cheffish

Methods included from DSL::RebootPending

#reboot_pending?

Methods included from DSL::IncludeRecipe

#include_recipe, #load_recipe

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!, #resources, #with_run_context

Constructor Details

#initialize(new_resource, run_context) ⇒ Ifconfig

Returns a new instance of Ifconfig.



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

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.



40
41
42
# File 'lib/chef/provider/ifconfig.rb', line 40

def config_path
  @config_path
end

#config_templateObject

Returns the value of attribute config_template.



39
40
41
# File 'lib/chef/provider/ifconfig.rb', line 39

def config_template
  @config_template
end

#ifconfig_versionString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the major.minor of the net-tools version as a string.

Returns:

  • (String)

    the major.minor of the net-tools version as a string



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

def ifconfig_version
  @ifconfig_version
end

Instance Method Details

#can_generate_config?Boolean

Returns:

  • (Boolean)


230
231
232
# File 'lib/chef/provider/ifconfig.rb', line 230

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

#define_resource_requirementsObject



164
165
166
167
168
169
170
171
172
# File 'lib/chef/provider/ifconfig.rb', line 164

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



249
250
251
252
253
254
255
# File 'lib/chef/provider/ifconfig.rb', line 249

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



238
239
240
241
242
243
244
245
246
247
# File 'lib/chef/provider/ifconfig.rb', line 238

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



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
81
82
83
84
85
86
87
88
89
90
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/chef/provider/ifconfig.rb', line 52

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

  @ifconfig_success = true
  @interfaces = {}

  @ifconfig_version = nil

  @net_tools_version = shell_out("ifconfig", "--version")
  @net_tools_version.stdout.each_line do |line|
    if line =~ /^net-tools (\d+\.\d+)/
      @ifconfig_version = line.match(/^net-tools (\d+\.\d+)/)[1]
    end
  end
  @net_tools_version.stderr.each_line do |line|
    if line =~ /^net-tools (\d+\.\d+)/
      @ifconfig_version = line.match(/^net-tools (\d+\.\d+)/)[1]
    end
  end

  if @ifconfig_version.nil?
    raise "net-tools not found - this is required for ifconfig"
  elsif @ifconfig_version.to_i < 2
    # Example output for 1.60 is as follows: (sanitized but format intact)
    # eth0      Link encap:Ethernet  HWaddr 00:00:00:00:00:00
    #           inet addr:192.168.1.1  Bcast:192.168.0.1  Mask:255.255.248.0
    #           inet6 addr: 0000::00:0000:0000:0000/64 Scope:Link
    #           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
    #           RX packets:65158911 errors:0 dropped:0 overruns:0 frame:0
    #           TX packets:41723949 errors:0 dropped:0 overruns:0 carrier:0
    #           collisions:0 txqueuelen:1000
    #           RX bytes:42664658792 (39.7 GiB)  TX bytes:52722603938 (49.1 GiB)
    #           Interrupt:30
    @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
  elsif @ifconfig_version.to_i >= 2
    # Example output for 2.10-alpha is as follows: (sanitized but format intact)
    # eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    #       inet 192.168.1.1  netmask 255.255.240.0  broadcast 192.168.0.1
    #       inet6 0000::0000:000:0000:0000  prefixlen 64  scopeid 0x20<link>
    #       ether 00:00:00:00:00:00  txqueuelen 1000  (Ethernet)
    #       RX packets 2383836  bytes 1642630840 (1.5 GiB)
    #       RX errors 0  dropped 0  overruns 0  frame 0
    #       TX packets 1244218  bytes 977339327 (932.0 MiB)
    #       TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    #
    # Permalink for addr_regex : https://rubular.com/r/JrykUpfjRnYeQD
    @status = shell_out("ifconfig")
    @status.stdout.each_line do |line|
      addr_regex = /^((\w|-)+):?(\d*):?\ .+$/
      if line =~ addr_regex
        if line.match(addr_regex).nil?
          @int_name = "nil"
        elsif line.match(addr_regex)[3] == ""
          @int_name = line.match(addr_regex)[1]
          @interfaces[@int_name] = {}
          @interfaces[@int_name]["mtu"] = (line =~ /mtu (\S+)/ ? Regexp.last_match(1) : "nil") if line =~ /mtu/ && @interfaces[@int_name]["mtu"].nil?
        else
          @int_name = "#{line.match(addr_regex)[1]}:#{line.match(addr_regex)[3]}"
          @interfaces[@int_name] = {}
          @interfaces[@int_name]["mtu"] = (line =~ /mtu (\S+)/ ? Regexp.last_match(1) : "nil") if line =~ /mtu/ && @interfaces[@int_name]["mtu"].nil?
        end
      else
        @interfaces[@int_name]["inet_addr"] = (line =~ /inet (\S+)/ ? Regexp.last_match(1) : "nil") if line =~ /inet/ && @interfaces[@int_name]["inet_addr"].nil?
        @interfaces[@int_name]["bcast"] = (line =~ /broadcast (\S+)/ ? Regexp.last_match(1) : "nil") if line =~ /broadcast/ && @interfaces[@int_name]["bcast"].nil?
        @interfaces[@int_name]["mask"] = (line =~ /netmask (\S+)/ ? Regexp.last_match(1) : "nil") if line =~ /netmask/ && @interfaces[@int_name]["mask"].nil?
        @interfaces[@int_name]["hwaddr"] = (line =~ /ether (\S+)/ ? Regexp.last_match(1) : "nil") if line =~ /ether/ && @interfaces[@int_name]["hwaddr"].nil?
        @interfaces[@int_name]["metric"] = (line =~ /Metric:(\S+)/ ? Regexp.last_match(1) : "nil") if line =~ /Metric:/ && @interfaces[@int_name]["metric"].nil?
      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
  end

  current_resource
end

#resource_for_config(path) ⇒ Object



234
235
236
# File 'lib/chef/provider/ifconfig.rb', line 234

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