Class: FPM::Package::RPM

Inherits:
FPM::Package show all
Defined in:
lib/fpm/package/rpm.rb

Overview

RPM Package type.

Build RPMs without having to waste hours reading Maximum-RPM. Well, in case you want to read it, here: www.rpm.org/max-rpm/

The following attributes are supported:

  • :rpm_rpmbuild_define - an array of definitions to give to rpmbuild. These are used, verbatim, each as: –define ITEM

Constant Summary collapse

DIGEST_ALGORITHM_MAP =
{
  "md5" => 1,
  "sha1" => 2,
  "sha256" => 8,
  "sha384" => 9,
  "sha512" => 10
}
COMPRESSION_MAP =
{
  "none" => "w0.gzdio",
  "xz" => "w2.xzdio",
  "gzip" => "w9.gzdio",
  "bzip2" => "w9.bzdio"
}

Instance Attribute Summary

Attributes inherited from FPM::Package

#attributes, #category, #config_files, #conflicts, #dependencies, #description, #directories, #license, #maintainer, #name, #provides, #replaces, #scripts, #url, #vendor

Instance Method Summary collapse

Methods inherited from FPM::Package

apply_options, #cleanup, #cleanup_build, #cleanup_staging, #convert, default_attributes, #edit_file, #files, inherited, #initialize, option, #script, #staging_path, type, #type, types

Methods included from Util

#copy_entry, #mknod_w, #program_in_path?, #safesystem, #safesystemout, #tar_cmd, #with

Constructor Details

This class inherits a constructor from FPM::Package

Instance Method Details

#architectureObject

Handle any architecture naming conversions. For example, debian calls amd64 what redhat calls x86_64, this method fixes those types of things.



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/fpm/package/rpm.rb', line 155

def architecture
  case @architecture
    when nil
      return %x{uname -m}.chomp   # default to current arch
    when "amd64" # debian and redhat disagree on architecture names
      return "x86_64"
    when "native"
      return %x{uname -m}.chomp   # 'native' is current arch
    when "all"
      # Translate fpm "all" arch to what it means in RPM.
      return "noarch"
    else
      return @architecture
  end
end

#build_sub_dirObject

def prefix



407
408
409
410
# File 'lib/fpm/package/rpm.rb', line 407

def build_sub_dir
  return "BUILD"
  #return File.join("BUILD", prefix)
end

#converted_from(origin) ⇒ Object

See FPM::Package#converted_from



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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/fpm/package/rpm.rb', line 177

def converted_from(origin)
  if origin == FPM::Package::Gem
    # Gem dependency operator "~>" is not compatible with rpm. Translate any found.
    fixed_deps = []
    self.dependencies.collect do |dep|
      name, op, version = dep.split(/\s+/)
      if op == "~>"
        # ~> x.y means: > x.y and < (x+1).0
        fixed_deps << "#{name} >= #{version}"
        fixed_deps << "#{name} < #{version.to_i + 1}.0.0"
      else
        fixed_deps << dep
      end
    end
    self.dependencies = fixed_deps

    # Convert 'rubygem-foo' provides values to 'rubygem(foo)'
    # since that's what most rpm packagers seem to do.
    self.provides = self.provides.collect do |provides|
      # Tries to match rubygem_prefix [1], gem_name [2] and version [3] if present
      # and return it in rubygem_prefix(gem_name) form
      if name=/^(#{attributes[:gem_package_name_prefix]})-([^\s]+)\s*(.*)$/.match(provides)
        "#{name[1]}(#{name[2]})#{name[3] ? " #{name[3]}" : ""}"
      else
        provides
      end
    end
    self.dependencies = self.dependencies.collect do |dependency|
      # Tries to match rubygem_prefix [1], gem_name [2] and version [3] if present
      # and return it in rubygem_prefix(gem_name) form
      if name=/^(#{attributes[:gem_package_name_prefix]})-([^\s]+)\s*(.*)$/.match(dependency)
        "#{name[1]}(#{name[2]})#{name[3] ? " #{name[3]}" : ""}"
      else
        dependency
      end
    end
  end

  # Convert != dependency as Conflict =, as rpm doesn't understand !=
  self.dependencies = self.dependencies.select do |dep|
    name, op, version = dep.split(/\s+/)
    dep_ok = true
    if op == '!='
      self.conflicts << "#{name} = #{version}"
      dep_ok = false
    end
    dep_ok
  end

  # Convert any dashes in version strings to underscores.
  self.dependencies = self.dependencies.collect do |dep|
    name, op, version = dep.split(/\s+/)
    if !version.nil? and version.include?("-")
      @logger.warn("Dependency package '#{name}' version '#{version}' " \
                   "includes dashes, converting to underscores")
      version = version.gsub(/-/, "_")
      "#{name} #{op} #{version}"
    else
      dep
    end
  end

  # if --ignore-iteration-in-dependencies is true convert foo = X, to
  # foo >= X , foo < X+1
  if self.attributes[:rpm_ignore_iteration_in_dependencies?]
    self.dependencies = self.dependencies.collect do |dep|
      name, op, version = dep.split(/\s+/)
      if op == '='
        nextversion = version.split('.').collect { |v| v.to_i }
        nextversion[-1] += 1
        nextversion = nextversion.join(".")
        @logger.warn("Converting dependency #{dep} to #{name} >= #{version}, #{name} < #{nextversion}")
        ["#{name} >= #{version}", "#{name} < #{nextversion}"]
      else
        dep
      end
    end.flatten
  end

end

#digest_algorithmObject

def payload_compression



443
444
445
# File 'lib/fpm/package/rpm.rb', line 443

def digest_algorithm
  return DIGEST_ALGORITHM_MAP[attributes[:rpm_digest]]
end

#epochObject

The default epoch value must be nil, see #381



423
424
425
426
427
428
429
430
431
432
# File 'lib/fpm/package/rpm.rb', line 423

def epoch
  return @epoch if @epoch.is_a?(Numeric)

  if @epoch.nil? or @epoch.empty?
    @logger.warn("no value for epoch is set, defaulting to nil")
    return nil
  end

  return @epoch
end

#input(path) ⇒ Object

def converted



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/fpm/package/rpm.rb', line 258

def input(path)
  rpm = ::RPM::File.new(path)

  tags = {}
  rpm.header.tags.each do |tag|
    tags[tag.tag] = tag.value
  end

  self.architecture = tags[:arch]
  self.category = tags[:group]
  self.description = tags[:description]
  self.epoch = (tags[:epoch] || [nil]).first # for some reason epoch is an array
  self.iteration = tags[:release]
  self.license = tags[:license]
  self.maintainer = maintainer
  self.name = tags[:name]
  self.url = tags[:url]
  self.vendor = tags[:vendor]
  self.version = tags[:version]

  self.scripts[:before_install] = tags[:prein]
  self.scripts[:after_install] = tags[:postin]
  self.scripts[:before_remove] = tags[:preun]
  self.scripts[:after_remove] = tags[:postun]
  # TODO(sissel): prefix these scripts above with a shebang line if there isn't one?
  # Also taking into account the value of tags[preinprog] etc, something like:
  #    #!#{tags[:preinprog]}
  #    #{tags[prein]}
  # TODO(sissel): put 'trigger scripts' stuff into attributes

  if !attributes[:no_auto_depends?]
    self.dependencies += rpm.requires.collect do |name, operator, version|
      [name, operator, version].join(" ")
    end
  end

  self.conflicts += rpm.conflicts.collect do |name, operator, version|
    [name, operator, version].join(" ")
  end
  self.provides += rpm.provides.collect do |name, operator, version|
    [name, operator, version].join(" ")
  end
  #input.replaces += replaces
  
  self.config_files += rpm.config_files

  # rpms support '%dir' things for specifying empty directories to package,
  # but the rpm header itself doesn't actually record this information.
  # so there's no 'directories' to copy, so don't try to merge in the
  # 'directories' feature. 
  # TODO(sissel): If you want this feature, we'll have to find scan
  # the extracted rpm for empty directories. I'll wait until someone asks for
  # this feature
  #self.directories += rpm.directories

  # Extract to the staging directory
  rpm.extract(staging_path)
end

#iterationObject

This method ensures a default value for iteration if none is provided.



172
173
174
# File 'lib/fpm/package/rpm.rb', line 172

def iteration
  return @iteration ? @iteration : 1
end

#output(output_path) ⇒ Object

def input



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/fpm/package/rpm.rb', line 317

def output(output_path)
  output_check(output_path)
  %w(BUILD RPMS SRPMS SOURCES SPECS).each { |d| FileUtils.mkdir_p(build_path(d)) }
  args = ["rpmbuild", "-bb"]

  # issue #309
  if !attributes[:rpm_os].nil?
    rpm_target = "#{architecture}-unknown-#{attributes[:rpm_os]}"
    args += ["--target", rpm_target]
  end

  args += [
    "--define", "buildroot #{build_path}/BUILD",
    "--define", "_topdir #{build_path}",
    "--define", "_sourcedir #{build_path}",
    "--define", "_rpmdir #{build_path}/RPMS",
  ]

  args += ["--sign"] if attributes[:rpm_sign?]

  if attributes[:rpm_auto_add_directories?]
    fs_dirs_list = File.join(template_dir, "rpm", "filesystem_list")
    fs_dirs = File.readlines(fs_dirs_list).reject { |x| x =~ /^\s*#/}.map { |x| x.chomp }

    Find.find(staging_path) do |path|
      next if path == staging_path
      if File.directory? path and !File.symlink? path
        add_path = path.gsub(/^#{staging_path}/,'')
        self.directories << add_path if not fs_dirs.include? add_path
      end
    end
  else
    self.directories = self.directories.map { |x| File.join(self.prefix, x) }
    alldirs = []
    self.directories.each do |path|
      Find.find(File.join(staging_path, path)) do |subpath|
        if File.directory? subpath and !File.symlink? subpath
          alldirs << subpath.gsub(/^#{staging_path}/, '')
        end
      end
    end
    self.directories = alldirs
  end

  # scan all conf file paths for files and add them
  allconfigs = []
  self.config_files.each do |path|
    cfg_path = File.expand_path(path, staging_path)
    Find.find(cfg_path) do |p|
      allconfigs << p.gsub("#{staging_path}/", '') if File.file? p
    end
  end
  allconfigs.sort!.uniq!

  self.config_files = allconfigs.map { |x| File.join(self.prefix, x) }

  (attributes[:rpm_rpmbuild_define] or []).each do |define|
    args += ["--define", define]
  end

  # copy all files from staging to BUILD dir
  Find.find(staging_path) do |path|
    src = path.gsub(/^#{staging_path}/, '')
    dst = File.join(build_path, build_sub_dir, src)
    copy_entry(path, dst)
  end

  rpmspec = template("rpm.erb").result(binding)
  specfile = File.join(build_path("SPECS"), "#{name}.spec")
  File.write(specfile, rpmspec)

  edit_file(specfile) if attributes[:edit?]

  args << specfile

  @logger.info("Running rpmbuild", :args => args)
  safesystem(*args)

  ::Dir["#{build_path}/RPMS/**/*.rpm"].each do |rpmpath|
    # This should only output one rpm, should we verify this?
    FileUtils.cp(rpmpath, output_path)
  end

  @logger.log("Created rpm", :path => output_path)
end

#payload_compressionObject

def to_s



439
440
441
# File 'lib/fpm/package/rpm.rb', line 439

def payload_compression
  return COMPRESSION_MAP[attributes[:rpm_compression]]
end

#prefixObject

def output



403
404
405
# File 'lib/fpm/package/rpm.rb', line 403

def prefix
  return (attributes[:prefix] or "/")
end

#to_s(format = nil) ⇒ Object

def epoch



434
435
436
437
# File 'lib/fpm/package/rpm.rb', line 434

def to_s(format=nil)
  return super("NAME-VERSION-ITERATION.ARCH.TYPE") if format.nil?
  return super(format)
end

#versionObject

def prefix



412
413
414
415
416
417
418
419
420
# File 'lib/fpm/package/rpm.rb', line 412

def version
  if @version.kind_of?(String) and @version.include?("-")
    @logger.warn("Package version '#{@version}' includes dashes, converting" \
                 " to underscores")
    @version = @version.gsub(/-/, "_")
  end

  return @version
end