Class: Chef::Provider::Package::Yum::YumCache

Inherits:
Object
  • Object
show all
Includes:
Mixin::Command, Singleton
Defined in:
lib/chef/provider/package/yum.rb

Overview

Cache for our installed and available packages, pulled in from yum-dump.py

Instance Attribute Summary collapse

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

Constructor Details

#initializeYumCache

Returns a new instance of YumCache.



650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
# File 'lib/chef/provider/package/yum.rb', line 650

def initialize
  @rpmdb = RPMDb.new

  # Next time @rpmdb is accessed:
  #  :all       - Trigger a run of "yum-dump.py --options --installed-provides", updates
  #               yum's cache and parses options from /etc/yum.conf. Pulls in Provides
  #               dependency data for installed packages only - this data is slow to
  #               gather.
  #  :provides  - Same as :all but pulls in Provides data for available packages as well.
  #               Used as a last resort when we can't find a Provides match.
  #  :installed - Trigger a run of "yum-dump.py --installed", only reads the local rpm
  #               db. Used between client runs for a quick refresh.
  #  :none      - Do nothing, a call to one of the reload methods is required.
  @next_refresh = :all

  @allow_multi_install = []

  @extra_repo_control = nil 

  # these are for subsequent runs if we are on an interval
  Chef::Client.when_run_starts do
    YumCache.instance.reload
  end
end

Instance Attribute Details

#extra_repo_controlObject (readonly)

Returns the value of attribute extra_repo_control.



675
676
677
# File 'lib/chef/provider/package/yum.rb', line 675

def extra_repo_control
  @extra_repo_control
end

Instance Method Details

#allow_multi_installObject

Return an array of packages allowed to be installed multiple times, such as the kernel



854
855
856
857
# File 'lib/chef/provider/package/yum.rb', line 854

def allow_multi_install
  refresh
  @allow_multi_install
end

#available_version(package_name, arch = nil) ⇒ Object Also known as: candidate_version

Return the latest available version for a package.arch



843
844
845
# File 'lib/chef/provider/package/yum.rb', line 843

def available_version(package_name, arch=nil)
  version(package_name, arch, true, false)
end

#disable_extra_repo_controlObject



867
868
869
870
871
872
873
# File 'lib/chef/provider/package/yum.rb', line 867

def disable_extra_repo_control
  # Only force reload when set
  if @extra_repo_control
    @extra_repo_control = nil
    reload
  end
end

#enable_extra_repo_control(arg) ⇒ Object



859
860
861
862
863
864
865
# File 'lib/chef/provider/package/yum.rb', line 859

def enable_extra_repo_control(arg)
  # Don't touch cache if it's the same repos as the last load
  unless @extra_repo_control == arg
    @extra_repo_control = arg
    reload
  end
end

#installed_version(package_name, arch = nil) ⇒ Object

Return the currently installed version for a package.arch



849
850
851
# File 'lib/chef/provider/package/yum.rb', line 849

def installed_version(package_name, arch=nil)
  version(package_name, arch, false, true)
end

#package_available?(package_name) ⇒ Boolean

Check for package by name or name+arch

Returns:

  • (Boolean)


797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
# File 'lib/chef/provider/package/yum.rb', line 797

def package_available?(package_name)
  refresh

  if @rpmdb.lookup(package_name)
    return true
  else
    if package_name =~ %r{^(.*)\.(.*)$}
      pkg_name = $1
      pkg_arch = $2

      if matches = @rpmdb.lookup(pkg_name)
        matches.each do |m|
          return true if m.arch == pkg_arch
        end
      end
    end
  end

  return false
end

#package_repository(package_name, desired_version, arch = nil) ⇒ Object

Return the source repository for a package-version.arch



834
835
836
837
838
839
840
# File 'lib/chef/provider/package/yum.rb', line 834

def package_repository(package_name, desired_version, arch=nil)
  package(package_name, arch, true, false) do |pkg|
    return pkg.repoid if desired_version == pkg.version.to_s
  end

  return nil
end

#packages_from_require(rpmdep) ⇒ Object

Returns a array of packages satisfying an RPMDependency



819
820
821
822
# File 'lib/chef/provider/package/yum.rb', line 819

def packages_from_require(rpmdep)
  refresh
  @rpmdb.whatprovides(rpmdep)
end

#refreshObject

Cache management



680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
# File 'lib/chef/provider/package/yum.rb', line 680

def refresh
  case @next_refresh
  when :none
    return nil
  when :installed
    reset_installed
    # fast
    opts=" --installed"
  when :all
    reset
    # medium
    opts=" --options --installed-provides"
  when :provides
    reset
    # slow!
    opts=" --options --all-provides"
  else
    raise ArgumentError, "Unexpected value in next_refresh: #{@next_refresh}"
  end

  if @extra_repo_control
    opts << " #{@extra_repo_control}"
  end

  one_line = false
  error = nil

  helper = ::File.join(::File.dirname(__FILE__), 'yum-dump.py')

  status = popen4("/usr/bin/python #{helper}#{opts}", :waitlast => true) do |pid, stdin, stdout, stderr|
    stdout.each do |line|
      one_line = true

      line.chomp!

      if line =~ %r{\[option (.*)\] (.*)}
        if $1 == "installonlypkgs"
          @allow_multi_install = $2.split
        else
          raise Chef::Exceptions::Package, "Strange, unknown option line '#{line}' from yum-dump.py"
        end
        next
      end

      if line =~ %r{^(\S+) ([0-9]+) (\S+) (\S+) (\S+) \[(.*)\] ([i,a,r]) (\S+)$}
        name     = $1
        epoch    = $2
        version  = $3
        release  = $4
        arch     = $5
        provides = parse_provides($6)
        type     = $7
        repoid   = $8
      else
        Chef::Log.warn("Problem parsing line '#{line}' from yum-dump.py! " +
                       "Please check your yum configuration.")
        next
      end

      case type
      when "i"
        # if yum-dump was called with --installed this may not be true, but it's okay
        # since we don't touch the @available Set in reload_installed
        available = false
        installed = true
      when "a"
        available = true
        installed = false
      when "r"
        available = true
        installed = true
      end

      pkg = RPMDbPackage.new(name, epoch, version, release, arch, provides, installed, available, repoid)
      @rpmdb << pkg
    end

    error = stderr.readlines
  end

  if status.exitstatus != 0
    raise Chef::Exceptions::Package, "Yum failed - #{status.inspect} - returns: #{error}"
  else
    unless one_line
      Chef::Log.warn("Odd, no output from yum-dump.py. Please check " +
                     "your yum configuration.")
    end
  end

  # A reload method must be called before the cache is altered
  @next_refresh = :none
end

#reloadObject



773
774
775
# File 'lib/chef/provider/package/yum.rb', line 773

def reload
  @next_refresh = :all
end

#reload_installedObject



777
778
779
# File 'lib/chef/provider/package/yum.rb', line 777

def reload_installed
  @next_refresh = :installed
end

#reload_providesObject



781
782
783
# File 'lib/chef/provider/package/yum.rb', line 781

def reload_provides
  @next_refresh = :provides
end

#resetObject



785
786
787
# File 'lib/chef/provider/package/yum.rb', line 785

def reset
  @rpmdb.clear
end

#reset_installedObject



789
790
791
# File 'lib/chef/provider/package/yum.rb', line 789

def reset_installed
  @rpmdb.clear_installed
end

#version_available?(package_name, desired_version, arch = nil) ⇒ Boolean

Check if a package-version.arch is available to install

Returns:

  • (Boolean)


825
826
827
828
829
830
831
# File 'lib/chef/provider/package/yum.rb', line 825

def version_available?(package_name, desired_version, arch=nil)
  version(package_name, arch, true, false) do |v|
    return true if desired_version == v
  end

  return false
end