diff options
author | Thom May <thom@may.lt> | 2017-11-18 09:13:58 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-18 09:13:58 +0000 |
commit | b09075ee9a7a8eaa5993ad555d6acd2f803ea092 (patch) | |
tree | 37328b01919d61a1faad04bd76f96017ced9da83 | |
parent | 493947028e8560e86a014aced4273f509a678c31 (diff) | |
parent | edf5c9c8e69975841ccda623bf43fdb2b418135d (diff) | |
download | chef-b09075ee9a7a8eaa5993ad555d6acd2f803ea092.tar.gz |
Merge pull request #6587 from chef/shain/fix_centos_rhel_test
Fix mount test, also update ifconfig to work with both common versions.
-rw-r--r-- | lib/chef/provider/ifconfig.rb | 112 | ||||
-rw-r--r-- | spec/functional/resource/ifconfig_spec.rb | 2 | ||||
-rw-r--r-- | spec/functional/resource/mount_spec.rb | 10 |
3 files changed, 98 insertions, 26 deletions
diff --git a/lib/chef/provider/ifconfig.rb b/lib/chef/provider/ifconfig.rb index 16bbe8124b..cdca0db10b 100644 --- a/lib/chef/provider/ifconfig.rb +++ b/lib/chef/provider/ifconfig.rb @@ -57,30 +57,98 @@ class Chef @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:/ + @ifconfig_version = nil + + @net_tools_version = shell_out("ifconfig --version") + @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_f < 2.0 + # 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_f >= 2.0 + # 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 + @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)[2] == "" + @int_name = line.match(addr_regex)[1] + @interfaces[@int_name] = Hash.new + @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)[2]}" + @interfaces[@int_name] = Hash.new + @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"]) + 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 diff --git a/spec/functional/resource/ifconfig_spec.rb b/spec/functional/resource/ifconfig_spec.rb index 0298dbcf45..fbb8995d52 100644 --- a/spec/functional/resource/ifconfig_spec.rb +++ b/spec/functional/resource/ifconfig_spec.rb @@ -20,7 +20,7 @@ require "functional/resource/base" require "chef/mixin/shell_out" # run this test only for following platforms. -include_flag = !(%w{ubuntu centos aix}.include?(ohai[:platform])) +include_flag = !(%w{amazon debian aix}.include?(ohai[:platform_family]) || (ohai[:platform_family] == "rhel" && ohai[:platform_version].to_i < 7)) describe Chef::Resource::Ifconfig, :requires_root, :skip_travis, :external => include_flag do # This test does not work in travis because there is no eth0 diff --git a/spec/functional/resource/mount_spec.rb b/spec/functional/resource/mount_spec.rb index c756b0d3d4..c98d6cec25 100644 --- a/spec/functional/resource/mount_spec.rb +++ b/spec/functional/resource/mount_spec.rb @@ -22,7 +22,7 @@ require "chef/mixin/shell_out" require "tmpdir" # run this test only for following platforms. -include_flag = !(%w{ubuntu centos aix solaris2}.include?(ohai[:platform])) +include_flag = !(%w{debian rhel amazon aix solaris2}.include?(ohai[:platform_family])) describe Chef::Resource::Mount, :requires_root, :skip_travis, :external => include_flag do # Disabled in travis because it refuses to let us mount a ramdisk. /dev/ramX does not @@ -35,15 +35,19 @@ describe Chef::Resource::Mount, :requires_root, :skip_travis, :external => inclu def setup_device_for_mount # use ramdisk for creating a test device for mount. # This can cleaner if we have chef resource/provider for ramdisk. - case ohai[:platform] + case ohai[:platform_family] when "aix" # On AIX, we can't create a ramdisk inside a WPAR, so we use # a "namefs" mount against / to test # https://www-304.ibm.com/support/knowledgecenter/ssw_aix_71/com.ibm.aix.performance/namefs_file_sys.htm device = "/" fstype = "namefs" - when "ubuntu", "centos" + when "debian", "rhel", "amazon" device = "/dev/ram1" + unless File.exist?(device) + shell_out("mknod -m 660 #{device} b 1 0") + shell_out("chown root:disk #{device}") + end shell_out("ls -1 /dev/ram*").stdout.each_line do |d| if shell_out("mount | grep #{d}").exitstatus == "1" # this device is not mounted, so use it. |