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

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

Instance Method Summary collapse

Methods included from Mixin::Command

#chdir_or_tmpdir, #handle_command_failures, #not_if, #only_if, #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.



33
34
35
# File 'lib/chef/provider/package/yum.rb', line 33

def initialize
  load_data
end

Instance Method Details

#candidate_version(package_name, arch) ⇒ Object



144
145
146
# File 'lib/chef/provider/package/yum.rb', line 144

def candidate_version(package_name, arch)
  version(package_name, :available, arch)
end

#flushObject



148
149
150
# File 'lib/chef/provider/package/yum.rb', line 148

def flush
  @data.clear
end

#installed_version(package_name, arch) ⇒ Object



140
141
142
# File 'lib/chef/provider/package/yum.rb', line 140

def installed_version(package_name, arch)
  version(package_name, :installed, arch)
end

#load_dataObject Also known as: reload



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
# File 'lib/chef/provider/package/yum.rb', line 58

def load_data
  @data = Hash.new
  error = String.new

  helper = ::File.join(::File.dirname(__FILE__), 'yum-dump.py')
  status = popen4("python #{helper}", :waitlast => true) do |pid, stdin, stdout, stderr|
    stdout.each do |line|
      line.chomp!
      name, type, epoch, version, release, arch = line.split(',')
      type_sym = type.to_sym
      if !@data.has_key?(name)
        @data[name] = Hash.new
      end
      if !@data[name].has_key?(type_sym)
        @data[name][type_sym] = Hash.new
      end
      @data[name][type_sym][arch] = { :epoch => epoch, :version => version,
                                      :release => release }
    end
    
    error = stderr.readlines
  end

  unless status.exitstatus == 0
    raise Chef::Exceptions::Package, "yum failed - #{status.inspect} - returns: #{error}"
  end

  @updated_at = Time.now
end

#refreshObject



50
51
52
53
54
55
56
# File 'lib/chef/provider/package/yum.rb', line 50

def refresh
  if @data.empty?
    reload
  elsif stale?
     reload
  end
end

#stale?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/chef/provider/package/yum.rb', line 37

def stale?
  interval = Chef::Config[:interval].to_f

  # run once mode
  if interval == 0
    return false
  elsif (Time.now - @updated_at) > interval
    return true
  end

  false
end

#version(package_name, type, arch) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/chef/provider/package/yum.rb', line 89

def version(package_name, type, arch)
  if (x = @data[package_name])
    if (y = x[type])
      if arch
        if (z = y[arch])
          return "#{z[:version]}-#{z[:release]}"
        end
      else
        # no arch specified - take the first match
        z = y.to_a[0][1]
        return "#{z[:version]}-#{z[:release]}"
      end
    end
  end

  nil
end

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

Returns:

  • (Boolean)


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
# File 'lib/chef/provider/package/yum.rb', line 107

def version_available?(package_name, desired_version, arch)
  if (package_data = @data[package_name])
    if (available_versions = package_data[:available])
      if arch
        # arch gets passed like ".x86_64"
        matching_versions = [ available_versions[arch.sub(/^./, '')]]
      else
        matching_versions = available_versions.values
      end

      if matching_versions.nil?
        if arch.empty?
          arch_msg = ""
        else
          arch_msg = "with arch #{arch.sub(/^./, '')} "
        end

        raise ArgumentError, "#{package_name}: Found no available versions #{arch_msg}to match"
      end

      # Expect [ { :version => "ver", :release => "rel" }, { :version => "ver", :release => "rel" }, { :version => "ver", :release => "rel" } ] ???
      matching_versions.each do |ver|
        Chef::Log.debug("#{package_name}: trying to match #{desired_version} to version #{ver[:version]} and release #{ver[:release]}")
        if (desired_version == "#{ver[:version]}-#{ver[:release]}")
          return true
        end
      end
    end
  end

  nil
end