Class: Simp::YUM

Inherits:
Object
  • Object
show all
Defined in:
lib/simp/yum.rb

Overview

Various utilities for dealing with YUM repos

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(yum_conf, initialize_cache = false) ⇒ YUM

Returns a new instance of YUM.

Raises:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/simp/yum.rb', line 14

def initialize(yum_conf, initialize_cache = false)
  if File.exist?(yum_conf)
    @yum_conf = File.absolute_path(yum_conf)
  else
    raise(Error, "Could not find yum configuration at '#{yum_conf}'")
  end

  # Only need to look these up once!
  @@yum_cmd ||= %x(which yum).strip
  raise(Error, "Error: Could not find 'yum'. Please install and try again.") if @@yum_cmd.empty?

  tmp_dir = ENV['TMPDIR'] || '/tmp'

  # Ensure that yumdownloader uses a fresh cache directory for each run of a given platform
  @@yum_cache ||= File.join(tmp_dir, 'yum_cache-' +
    Facter.fact('operatingsystem').value + '-' +
    Facter.fact('operatingsystemmajrelease').value + '-' +
    Facter.fact('architecture').value)

  FileUtils.mkdir_p(@@yum_cache)

  @@yum ||= "TMPDIR=#{@@yum_cache} #{@@yum_cmd} -c #{@yum_conf}"

  @@yumdownloader_cmd ||= %x(which yumdownloader).strip
  raise(Error, "Error: Could not find 'yumdownloader'. Please install and try again.") if @@yumdownloader_cmd.empty?

  @@yumdownloader ||= "TMPDIR=#{@@yum_cache} #{@@yumdownloader_cmd} -c #{@yum_conf}"

  @@curl ||= %x(which curl).strip
  raise(Error, "Error: Could not find 'curl'. Please install and try again.") if @@curl.empty?

  @@file ||= %x(which file).strip
  raise(Error, "Error: Could not find 'file'. Please install and try again.") if @@file.empty?

  generate_cache if initialize_cache
end

Instance Attribute Details

#yum_confObject (readonly)

Returns the value of attribute yum_conf.



12
13
14
# File 'lib/simp/yum.rb', line 12

def yum_conf
  @yum_conf
end

Class Method Details

.generate_yum_conf(yum_dir = nil) ⇒ Object

Create a reasonable YUM config file

  • yum_tmp => The directory in which to store the YUM DB and any other temporary files

Returns the location of the YUM configuration

Raises:



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
# File 'lib/simp/yum.rb', line 73

def self.generate_yum_conf(yum_dir=nil)
  yum_dir ||= Dir.pwd

  raise(Error, "Could not find YUM data dir at '#{yum_dir}'") unless File.directory?(yum_dir)

  yum_conf = nil
  Dir.chdir(yum_dir) do
    # Create the target directory
    yum_tmp = File.join('packages','yum_tmp')

    FileUtils.mkdir_p(yum_tmp) unless File.directory?(yum_tmp)

    yum_cache = File.expand_path('yum_cache', yum_tmp)
    FileUtils.mkdir_p(yum_cache) unless File.directory?(yum_cache)

    yum_logfile = File.expand_path('yum.log', yum_tmp)

    repo_dirs = []

    # Add the global directory
    repo_dirs << File.expand_path('../my_repos')

    if File.directory?('my_repos')
      # Add the local user repos if they exist
      repo_dirs << File.expand_path('my_repos')
    else
      # Add the default Internet repos otherwise
      repo_dirs << File.expand_path('repos')
    end

    # Create our YUM config file
    yum_conf = File.expand_path('yum.conf', yum_tmp)

    File.open(yum_conf, 'w') do |fh|
      fh.puts <<-EOM.gsub(/^\s+/,'')
      [main]
      keepcache = 0
      persistdir = #{yum_cache}
      logfile = #{yum_logfile}
      exactarch = 1
      obsoletes = 0
      gpgcheck = 0
      plugins = 1
      reposdir = #{repo_dirs.join(' ')}
      assumeyes = 1
      EOM
    end
  end

  return yum_conf
end

Instance Method Details

#available_package(rpm) ⇒ Object

Returns the full name of the latest package of the given name

Returns nil if nothing found



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/simp/yum.rb', line 128

def available_package(rpm)
  yum_output = %x(#{@@yum} list #{rpm} 2>/dev/null)

  found_rpm = nil
  if $?.success?
    pkg_name, pkg_version = yum_output.lines.last.strip.split(/\s+/)
    pkg_name, pkg_arch = pkg_name.split('.')

    found_rpm = %(#{pkg_name}-#{pkg_version}.#{pkg_arch}.rpm)
  end

  return found_rpm
end

#clean_yum_cache_dirObject



51
52
53
54
55
56
# File 'lib/simp/yum.rb', line 51

def clean_yum_cache_dir
  # Make this as safe as we can
  if @@yum_cache =~ /yum_cache/
    FileUtils.remove_entry(@@yum_cache)
  end
end

#download(rpm, opts = {:target_dir => nil}) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/simp/yum.rb', line 171

def download(rpm, opts={:target_dir => nil})
  rpm.strip!

  downloaded_rpm_name = nil

  target_dir = Dir.pwd

  if opts[:target_dir]
    target_dir = File.absolute_path(opts[:target_dir])
  end

  Dir.mktmpdir do |dir|
    Dir.chdir(dir) do
      # If just passed an RPM name, use yumdownloader
      if rpm !~ %r(://)
        # In case someone passed a path
        rpm_name = rpm.split(File::SEPARATOR).last

        err_msg = %x(#{@@yumdownloader} #{File.basename(rpm_name, '.rpm')} 2>/dev/null)

        downloaded_rpm_name = rpm_name
      else
        # If passed a URL, curl it and fall back to yumdownloader
        rpm_name = rpm.split('/').last

        %x(#{@@curl} -L --max-redirs 10 -s -o #{rpm_name} -k #{rpm})

        # Check what we've just downloaded
        if !(File.exist?(rpm_name) && %x(#{@@file} #{rpm_name}).include?('RPM'))
          # Fall back on yumdownloader
          FileUtils.rm_f(rpm_name)

          err_msg = %x(#{@@yumdownloader} #{File.basename(rpm_name, '.rpm')} 2>/dev/null)
        end

        # We might get a filename that doesn't make sense so we need to
        # move the file appropriately
        rpm_info = Simp::RPM.new(rpm_name)

        unless File.exist?(rpm_info.rpm_name)
          FileUtils.mv(rpm_name, rpm_info.rpm_name)
        end

        downloaded_rpm_name = rpm_info.rpm_name
      end

      rpms = Dir.glob('*.rpm')

      err_msg = ''
      err_msg = "\n-- ERROR MESSAGE --\n" + err_msg if err_msg
      raise(Error, "Could not find any remote RPMs for #{rpm}" + err_msg) if rpms.empty?

      # Copy over all of the RPMs
      rpms.each do |new_rpm|
        FileUtils.mkdir_p(target_dir)
        FileUtils.mv(new_rpm, target_dir)
      end
    end
  end

  return downloaded_rpm_name
end

#generate_cacheObject



58
59
60
61
62
63
64
65
66
67
# File 'lib/simp/yum.rb', line 58

def generate_cache
  puts "Attempting to generate build-specific YUM cache from\n  #{@yum_conf}"

  %x(#{@@yum} clean all 2>/dev/null)
  %x(#{@@yum} makecache 2>/dev/null)

  unless $?.success?
    puts "WARNING: Unable to generate build-specific YUM cache from #{@yum_conf}"
  end
end

#get_source(rpm, arch = nil) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/simp/yum.rb', line 156

def get_source(rpm, arch=nil)
  sources = get_sources(rpm)

  if arch
    native_sources = sources.grep(%r((#{arch}|noarch)\.rpm$))

    if native_sources.size > 1
      # We can't have more than one native source
      raise(Error, "More than one native source found for #{rpm}:\n  * #{native_sources.join("\n  *")}")
    end
  end

  return sources.first
end

#get_sources(rpm) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/simp/yum.rb', line 142

def get_sources(rpm)
  Dir.mktmpdir do |dir|
    output = %x(#{@@yumdownloader} --urls #{File.basename(rpm,'.rpm')} 2>/dev/null).lines
    sources = output.grep(%r(\.rpm$))

    unless (output.grep(/Error/).empty? || sources.empty?)
      err_msg = "\n-- YUMDOWNLOADER ERROR --\n" + output.join("\n")
      raise(Error, "No sources found for '#{rpm}'" + err_msg)
    end

    return sources
  end
end