From 6909f4074dbd9cee5906333f693bd282476a6ac5 Mon Sep 17 00:00:00 2001 From: kaustubh-d Date: Tue, 18 Nov 2014 14:56:22 +0530 Subject: fix aix related providers to replace popen4 with mixlib shell_out --- lib/chef/provider/group.rb | 2 ++ lib/chef/provider/group/dscl.rb | 7 ++--- lib/chef/provider/ifconfig.rb | 50 +++++++++++++++---------------- lib/chef/provider/ifconfig/aix.rb | 52 ++++++++++++++++----------------- lib/chef/provider/mount.rb | 4 +-- lib/chef/provider/mount/mount.rb | 7 ++--- lib/chef/provider/package.rb | 5 ++-- lib/chef/provider/package/aix.rb | 55 ++++++++++++++++------------------- lib/chef/provider/package/dpkg.rb | 29 +++++++++--------- lib/chef/provider/package/macports.rb | 11 ++++--- lib/chef/provider/package/pacman.rb | 28 +++++++++--------- lib/chef/provider/package/portage.rb | 7 ++--- lib/chef/provider/package/rpm.rb | 3 +- lib/chef/provider/package/solaris.rb | 38 +++++++++++------------- lib/chef/provider/package/yum.rb | 6 ++-- lib/chef/provider/package/zypper.rb | 35 +++++++++++----------- 16 files changed, 159 insertions(+), 180 deletions(-) (limited to 'lib/chef/provider') diff --git a/lib/chef/provider/group.rb b/lib/chef/provider/group.rb index 35a16c870c..29738cc046 100644 --- a/lib/chef/provider/group.rb +++ b/lib/chef/provider/group.rb @@ -17,12 +17,14 @@ # require 'chef/provider' +require 'chef/mixin/shell_out' require 'chef/mixin/command' require 'etc' class Chef class Provider class Group < Chef::Provider + include Chef::Mixin::ShellOut include Chef::Mixin::Command attr_accessor :group_exists attr_accessor :change_desc diff --git a/lib/chef/provider/group/dscl.rb b/lib/chef/provider/group/dscl.rb index e06c090f50..a59a94aa98 100644 --- a/lib/chef/provider/group/dscl.rb +++ b/lib/chef/provider/group/dscl.rb @@ -24,10 +24,9 @@ class Chef def dscl(*args) host = "." stdout_result = ""; stderr_result = ""; cmd = "dscl #{host} -#{args.join(' ')}" - status = popen4(cmd) do |pid, stdin, stdout, stderr| - stdout.each { |line| stdout_result << line } - stderr.each { |line| stderr_result << line } - end + status = shell_out(cmd) + status.stdout.each_line { |line| stdout_result << line } + status.stderr.each_line { |line| stderr_result << line } return [cmd, status, stdout_result, stderr_result] end diff --git a/lib/chef/provider/ifconfig.rb b/lib/chef/provider/ifconfig.rb index ac52100b56..06080c90c3 100644 --- a/lib/chef/provider/ifconfig.rb +++ b/lib/chef/provider/ifconfig.rb @@ -18,6 +18,7 @@ require 'chef/log' require 'chef/mixin/command' +require 'chef/mixin/shell_out' require 'chef/provider' require 'chef/resource/file' require 'chef/exceptions' @@ -38,6 +39,7 @@ require 'erb' class Chef class Provider class Ifconfig < Chef::Provider + include Chef::Mixin::ShellOut include Chef::Mixin::Command attr_accessor :config_template @@ -59,32 +61,30 @@ class Chef @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 + @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+)/ ? ($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(@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 + if @interfaces.has_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 diff --git a/lib/chef/provider/ifconfig/aix.rb b/lib/chef/provider/ifconfig/aix.rb index 460b1ba7f2..8fead44bc6 100644 --- a/lib/chef/provider/ifconfig/aix.rb +++ b/lib/chef/provider/ifconfig/aix.rb @@ -30,35 +30,33 @@ class Chef found_interface = false interface = {} - @status = popen4("ifconfig -a") do |pid, stdin, stdout, stderr| - stdout.each do |line| - - if !found_interface - if line =~ /^(\S+):\sflags=(\S+)/ - # We have interface name, if this is the interface for @current_resource, load info else skip till next interface is found. - if $1 == @new_resource.device - # Found interface - found_interface = true - @interface_exists = true - @current_resource.target(@new_resource.target) - @current_resource.device($1) - interface[:flags] = $2 - @current_resource.metric($1) if line =~ /metric\s(\S+)/ - end + @status = shell_out("ifconfig -a") + @status.stdout.each_line do |line| + if !found_interface + if line =~ /^(\S+):\sflags=(\S+)/ + # We have interface name, if this is the interface for @current_resource, load info else skip till next interface is found. + if $1 == @new_resource.device + # Found interface + found_interface = true + @interface_exists = true + @current_resource.target(@new_resource.target) + @current_resource.device($1) + interface[:flags] = $2 + @current_resource.metric($1) if line =~ /metric\s(\S+)/ end + end + else + # parse interface related information, stop when next interface is found. + if line =~ /^(\S+):\sflags=(\S+)/ + # we are done parsing interface info and hit another one, so stop. + found_interface = false + break else - # parse interface related information, stop when next interface is found. - if line =~ /^(\S+):\sflags=(\S+)/ - # we are done parsing interface info and hit another one, so stop. - found_interface = false - break - else - if found_interface - # read up interface info - @current_resource.inet_addr($1) if line =~ /inet\s(\S+)\s/ - @current_resource.bcast($1) if line =~ /broadcast\s(\S+)/ - @current_resource.mask(hex_to_dec_netmask($1)) if line =~ /netmask\s(\S+)\s/ - end + if found_interface + # read up interface info + @current_resource.inet_addr($1) if line =~ /inet\s(\S+)\s/ + @current_resource.bcast($1) if line =~ /broadcast\s(\S+)/ + @current_resource.mask(hex_to_dec_netmask($1)) if line =~ /netmask\s(\S+)\s/ end end end diff --git a/lib/chef/provider/mount.rb b/lib/chef/provider/mount.rb index b46604260e..1631d87033 100644 --- a/lib/chef/provider/mount.rb +++ b/lib/chef/provider/mount.rb @@ -18,14 +18,14 @@ # require 'chef/log' -require 'chef/mixin/command' +require 'chef/mixin/shell_out' require 'chef/provider' class Chef class Provider class Mount < Chef::Provider - include Chef::Mixin::Command + include Chef::Mixin::ShellOut attr_accessor :unmount_retries diff --git a/lib/chef/provider/mount/mount.rb b/lib/chef/provider/mount/mount.rb index c1d4fb2223..0a6e269d2d 100644 --- a/lib/chef/provider/mount/mount.rb +++ b/lib/chef/provider/mount/mount.rb @@ -213,10 +213,9 @@ class Chef @real_device = @new_resource.device else @real_device = "" - status = popen4("/sbin/findfs #{device_fstab}") do |pid, stdin, stdout, stderr| - device_line = stdout.first # stdout.first consumes - @real_device = device_line.chomp unless device_line.nil? - end + ret = shell_out("/sbin/findfs #{device_fstab}") + device_line = ret.stdout.lines.first # stdout.first consumes + @real_device = device_line.chomp unless device_line.nil? end end @real_device diff --git a/lib/chef/provider/package.rb b/lib/chef/provider/package.rb index fd317a3a31..de6d399ee3 100644 --- a/lib/chef/provider/package.rb +++ b/lib/chef/provider/package.rb @@ -16,6 +16,7 @@ # limitations under the License. # +require 'chef/mixin/shell_out' require 'chef/mixin/command' require 'chef/log' require 'chef/file_cache' @@ -24,9 +25,7 @@ require 'chef/platform' class Chef class Provider class Package < Chef::Provider - - # @todo: validate no subclasses need this and nuke it - include Chef::Mixin::Command + include Chef::Mixin::ShellOut # # Hook that subclasses use to populate the candidate_version(s) diff --git a/lib/chef/provider/package/aix.rb b/lib/chef/provider/package/aix.rb index 88de4679ba..107f914c05 100644 --- a/lib/chef/provider/package/aix.rb +++ b/lib/chef/provider/package/aix.rb @@ -52,34 +52,30 @@ class Chef @package_source_found = ::File.exists?(@new_resource.source) if @package_source_found Chef::Log.debug("#{@new_resource} checking pkg status") - status = popen4("installp -L -d #{@new_resource.source}") do |pid, stdin, stdout, stderr| - package_found = false - stdout.each do |line| - case line - when /#{@new_resource.package_name}:/ - package_found = true - fields = line.split(":") - @new_resource.version(fields[2]) - end + ret = shell_out("installp -L -d #{@new_resource.source}") + ret.stdout.each_line do | line | + case line + when /#{@new_resource.package_name}:/ + fields = line.split(":") + @new_resource.version(fields[2]) end end end end Chef::Log.debug("#{@new_resource} checking install state") - status = popen4("lslpp -lcq #{@current_resource.package_name}") do |pid, stdin, stdout, stderr| - stdout.each do |line| - case line - when /#{@current_resource.package_name}/ - fields = line.split(":") - Chef::Log.debug("#{@new_resource} version #{fields[2]} is already installed") - @current_resource.version(fields[2]) - end + ret = shell_out("lslpp -lcq #{@current_resource.package_name}") + ret.stdout.each_line do | line | + case line + when /#{@current_resource.package_name}/ + fields = line.split(":") + Chef::Log.debug("#{@new_resource} version #{fields[2]} is already installed") + @current_resource.version(fields[2]) end end - unless status.exitstatus == 0 || status.exitstatus == 1 - raise Chef::Exceptions::Package, "lslpp failed - #{status.inspect}!" + unless ret.exitstatus == 0 || ret.exitstatus == 1 + raise Chef::Exceptions::Package, "lslpp failed - #{ret.format_for_exception}!" end @current_resource @@ -87,19 +83,18 @@ class Chef def candidate_version return @candidate_version if @candidate_version - status = popen4("installp -L -d #{@new_resource.source}") do |pid, stdin, stdout, stderr| - stdout.each_line do |line| - case line - when /\w:#{Regexp.escape(@new_resource.package_name)}:(.*)/ - fields = line.split(":") - @candidate_version = fields[2] - @new_resource.version(fields[2]) - Chef::Log.debug("#{@new_resource} setting install candidate version to #{@candidate_version}") - end + ret = shell_out("installp -L -d #{@new_resource.source}") + ret.stdout.each_line do | line | + case line + when /\w:#{Regexp.escape(@new_resource.package_name)}:(.*)/ + fields = line.split(":") + @candidate_version = fields[2] + @new_resource.version(fields[2]) + Chef::Log.debug("#{@new_resource} setting install candidate version to #{@candidate_version}") end end - unless status.exitstatus == 0 - raise Chef::Exceptions::Package, "installp -L -d #{@new_resource.source} - #{status.inspect}!" + unless ret.exitstatus == 0 + raise Chef::Exceptions::Package, "installp -L -d #{@new_resource.source} - #{ret.format_for_exception}!" end @candidate_version end diff --git a/lib/chef/provider/package/dpkg.rb b/lib/chef/provider/package/dpkg.rb index 3a9cecc660..1500aa9a7e 100644 --- a/lib/chef/provider/package/dpkg.rb +++ b/lib/chef/provider/package/dpkg.rb @@ -61,12 +61,10 @@ class Chef if @source_exists # Get information from the package if supplied Chef::Log.debug("#{@new_resource} checking dpkg status") - status = popen4("dpkg-deb -W #{@new_resource.source}") do |pid, stdin, stdout, stderr| - stdout.each_line do |line| - if pkginfo = DPKG_INFO.match(line) - @current_resource.package_name(pkginfo[1]) - @new_resource.version(pkginfo[2]) - end + shell_out("dpkg-deb -W #{@new_resource.source}").stdout.each_line do |line| + if pkginfo = DPKG_INFO.match(line) + @current_resource.package_name(pkginfo[1]) + @new_resource.version(pkginfo[2]) end end else @@ -79,16 +77,15 @@ class Chef # Check to see if it is installed package_installed = nil Chef::Log.debug("#{@new_resource} checking install state") - status = popen4("dpkg -s #{@current_resource.package_name}") do |pid, stdin, stdout, stderr| - stdout.each_line do |line| - case line - when DPKG_INSTALLED - package_installed = true - when DPKG_VERSION - if package_installed - Chef::Log.debug("#{@new_resource} current version is #{$1}") - @current_resource.version($1) - end + status = shell_out("dpkg -s #{@current_resource.package_name}") + status.stdout.each_line do |line| + case line + when DPKG_INSTALLED + package_installed = true + when DPKG_VERSION + if package_installed + Chef::Log.debug("#{@new_resource} current version is #{$1}") + @current_resource.version($1) end end end diff --git a/lib/chef/provider/package/macports.rb b/lib/chef/provider/package/macports.rb index 248dc75d28..b252344c99 100644 --- a/lib/chef/provider/package/macports.rb +++ b/lib/chef/provider/package/macports.rb @@ -83,12 +83,11 @@ class Chef private def get_response_from_command(command) output = nil - status = popen4(command) do |pid, stdin, stdout, stderr| - begin - output = stdout.read - rescue Exception - raise Chef::Exceptions::Package, "Could not read from STDOUT on command: #{command}" - end + status = shell_out(command) + begin + output = status.stdout + rescue Exception + raise Chef::Exceptions::Package, "Could not read from STDOUT on command: #{command}" end unless status.exitstatus == 0 || status.exitstatus == 1 raise Chef::Exceptions::Package, "#{command} failed - #{status.insect}!" diff --git a/lib/chef/provider/package/pacman.rb b/lib/chef/provider/package/pacman.rb index 45edda5c5d..f16fc811f5 100644 --- a/lib/chef/provider/package/pacman.rb +++ b/lib/chef/provider/package/pacman.rb @@ -34,13 +34,12 @@ class Chef @current_resource.version(nil) Chef::Log.debug("#{@new_resource} checking pacman for #{@new_resource.package_name}") - status = popen4("pacman -Qi #{@new_resource.package_name}") do |pid, stdin, stdout, stderr| - stdout.each do |line| - case line - when /^Version(\s?)*: (.+)$/ - Chef::Log.debug("#{@new_resource} current version is #{$2}") - @current_resource.version($2) - end + status = shell_out("pacman -Qi #{@new_resource.package_name}") + status.stdout.each_line do |line| + case line + when /^Version(\s?)*: (.+)$/ + Chef::Log.debug("#{@new_resource} current version is #{$2}") + @current_resource.version($2) end end @@ -63,14 +62,13 @@ class Chef package_repos = repos.map {|r| Regexp.escape(r) }.join('|') - status = popen4("pacman -Sl") do |pid, stdin, stdout, stderr| - stdout.each do |line| - case line - when /^(#{package_repos}) #{Regexp.escape(@new_resource.package_name)} (.+)$/ - # $2 contains a string like "4.4.0-1" or "3.10-4 [installed]" - # simply split by space and use first token - @candidate_version = $2.split(" ").first - end + status = shell_out("pacman -Sl") + status.stdout.each_line do |line| + case line + when /^(#{package_repos}) #{Regexp.escape(@new_resource.package_name)} (.+)$/ + # $2 contains a string like "4.4.0-1" or "3.10-4 [installed]" + # simply split by space and use first token + @candidate_version = $2.split(" ").first end end diff --git a/lib/chef/provider/package/portage.rb b/lib/chef/provider/package/portage.rb index 816e262ab0..bb047ad2fa 100644 --- a/lib/chef/provider/package/portage.rb +++ b/lib/chef/provider/package/portage.rb @@ -92,10 +92,9 @@ class Chef def candidate_version return @candidate_version if @candidate_version - status = popen4("emerge --color n --nospinner --search #{@new_resource.package_name.split('/').last}") do |pid, stdin, stdout, stderr| - available, installed = parse_emerge(@new_resource.package_name, stdout.read) - @candidate_version = available - end + status = shell_out("emerge --color n --nospinner --search #{@new_resource.package_name.split('/').last}") + available, installed = parse_emerge(@new_resource.package_name, status.stdout) + @candidate_version = available unless status.exitstatus == 0 raise Chef::Exceptions::Package, "emerge --search failed - #{status.inspect}!" diff --git a/lib/chef/provider/package/rpm.rb b/lib/chef/provider/package/rpm.rb index c96261bcd1..fa784c2943 100644 --- a/lib/chef/provider/package/rpm.rb +++ b/lib/chef/provider/package/rpm.rb @@ -60,8 +60,7 @@ class Chef end Chef::Log.debug("#{@new_resource} checking rpm status") - status = shell_out!("rpm -qp --queryformat '%{NAME} %{VERSION}-%{RELEASE}\n' #{@new_resource.source}") - status.stdout.each_line do |line| + shell_out!("rpm -qp --queryformat '%{NAME} %{VERSION}-%{RELEASE}\n' #{@new_resource.source}").stdout.each_line do |line| case line when /^([\w\d+_.-]+)\s([\w\d_.-]+)$/ @current_resource.package_name($1) diff --git a/lib/chef/provider/package/solaris.rb b/lib/chef/provider/package/solaris.rb index 53dd00dd07..a2cfd93ef6 100644 --- a/lib/chef/provider/package/solaris.rb +++ b/lib/chef/provider/package/solaris.rb @@ -55,25 +55,22 @@ class Chef @package_source_found = ::File.exists?(@new_resource.source) if @package_source_found Chef::Log.debug("#{@new_resource} checking pkg status") - status = popen4("pkginfo -l -d #{@new_resource.source} #{@new_resource.package_name}") do |pid, stdin, stdout, stderr| - stdout.each do |line| - case line - when /VERSION:\s+(.+)/ - @new_resource.version($1) - end + shell_out("pkginfo -l -d #{@new_resource.source} #{@new_resource.package_name}").stdout.each_line do |line| + case line + when /VERSION:\s+(.+)/ + @new_resource.version($1) end end end end Chef::Log.debug("#{@new_resource} checking install state") - status = popen4("pkginfo -l #{@current_resource.package_name}") do |pid, stdin, stdout, stderr| - stdout.each do |line| - case line - when /VERSION:\s+(.+)/ - Chef::Log.debug("#{@new_resource} version #{$1} is already installed") - @current_resource.version($1) - end + status = shell_out("pkginfo -l #{@current_resource.package_name}") + status.stdout.each_line do |line| + case line + when /VERSION:\s+(.+)/ + Chef::Log.debug("#{@new_resource} version #{$1} is already installed") + @current_resource.version($1) end end @@ -90,14 +87,13 @@ class Chef def candidate_version return @candidate_version if @candidate_version - status = popen4("pkginfo -l -d #{@new_resource.source} #{new_resource.package_name}") do |pid, stdin, stdout, stderr| - stdout.each_line do |line| - case line - when /VERSION:\s+(.+)/ - @candidate_version = $1 - @new_resource.version($1) - Chef::Log.debug("#{@new_resource} setting install candidate version to #{@candidate_version}") - end + status = shell_out("pkginfo -l -d #{@new_resource.source} #{new_resource.package_name}") + status.stdout.each_line do |line| + case line + when /VERSION:\s+(.+)/ + @candidate_version = $1 + @new_resource.version($1) + Chef::Log.debug("#{@new_resource} setting install candidate version to #{@candidate_version}") end end unless status.exitstatus == 0 diff --git a/lib/chef/provider/package/yum.rb b/lib/chef/provider/package/yum.rb index 05fb76c4f5..405e4177ab 100644 --- a/lib/chef/provider/package/yum.rb +++ b/lib/chef/provider/package/yum.rb @@ -18,7 +18,7 @@ require 'chef/config' require 'chef/provider/package' -require 'chef/mixin/command' +require 'chef/mixin/command' # handle_command_failures require 'chef/mixin/shell_out' require 'chef/resource/package' require 'singleton' @@ -1010,7 +1010,7 @@ class Chef if status.exitstatus > 0 command_output = "STDOUT: #{stdout}" command_output << "STDERR: #{stderr}" - handle_command_failures(status, command_output, {}) + Chef::Mixin::Command.handle_command_failures(status, command_output, {}) end end @@ -1327,4 +1327,4 @@ class Chef end end end -end \ No newline at end of file +end diff --git a/lib/chef/provider/package/zypper.rb b/lib/chef/provider/package/zypper.rb index b00bef0f92..2cd321660b 100644 --- a/lib/chef/provider/package/zypper.rb +++ b/lib/chef/provider/package/zypper.rb @@ -38,24 +38,23 @@ class Chef version='' oud_version='' Chef::Log.debug("#{@new_resource} checking zypper") - status = popen4("zypper --non-interactive info #{@new_resource.package_name}") do |pid, stdin, stdout, stderr| - stdout.each do |line| - case line - when /^Version: (.+)$/ - version = $1 - Chef::Log.debug("#{@new_resource} version #{$1}") - when /^Installed: Yes$/ - is_installed=true - Chef::Log.debug("#{@new_resource} is installed") - - when /^Installed: No$/ - is_installed=false - Chef::Log.debug("#{@new_resource} is not installed") - when /^Status: out-of-date \(version (.+) installed\)$/ - is_out_of_date=true - oud_version=$1 - Chef::Log.debug("#{@new_resource} out of date version #{$1}") - end + status = shell_out("zypper --non-interactive info #{@new_resource.package_name}") + status.stdout.each_line do |line| + case line + when /^Version: (.+)$/ + version = $1 + Chef::Log.debug("#{@new_resource} version #{$1}") + when /^Installed: Yes$/ + is_installed=true + Chef::Log.debug("#{@new_resource} is installed") + + when /^Installed: No$/ + is_installed=false + Chef::Log.debug("#{@new_resource} is not installed") + when /^Status: out-of-date \(version (.+) installed\)$/ + is_out_of_date=true + oud_version=$1 + Chef::Log.debug("#{@new_resource} out of date version #{$1}") end end -- cgit v1.2.1