Skip to content

Instantly share code, notes, and snippets.

@andrewpsp
Last active August 29, 2015 14:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewpsp/c2a018de45d177d19539 to your computer and use it in GitHub Desktop.
Save andrewpsp/c2a018de45d177d19539 to your computer and use it in GitHub Desktop.
Converting GEM to RPM
#!/usr/bin/env ruby
require 'fpm'
# FIXME: set these from the command line
target = 'system'
iteration = 1
src_dir = '/var/tmp/gems/cache';
dst_lib_dir = {
'puppet' => '/opt/puppet/lib/ruby/gems/1.9.1',
'system' => '/usr/lib64/ruby/gems/1.9.1'
}
rpm_rubygem_prefix = {
'puppet' => 'pe-',
'system' => ''
}
def gem2rpm(opts)
package = FPM::Package::Gem.new
package.input(opts['src_gem'])
# get the dependencies from the gem
dependencies = package.dependencies
# clear dependencies and provides from the gem
package.dependencies = []
package.provides = []
# convert the ruby deps to rpm deps (using code copied from fpm)
fixed_deps = []
dependencies.each do |dep|
dep.gsub!(/rubygem-/, "#{opts['prefix']}rubygem-")
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
# Add in ruby and rubygems deps (with appropriate prefix)
fixed_deps << "#{opts['prefix']}ruby"
fixed_deps << "#{opts['prefix']}rubygems"
rpm = package.convert(FPM::Package::RPM)
rpm.iteration = opts['iteration']
rpm.name = opts['rpm_name']
rpm.attributes[:prefix] = opts['dst_lib_dir']
# set the rpm deps
rpm.dependencies = fixed_deps
begin
output = "NAME-VERSION.ARCH.rpm"
rpm.output(rpm.to_s(output))
ensure
rpm.cleanup
end
end
gemlist = Dir.glob("#{src_dir}/*.gem")
gemlist.each{ |gem|
# strip the path
gem_file = File.basename(gem)
# get the name of the gem, ie. strip the version and .gem
if gem_file =~ /(.*)-(?:[0-9]+\.)+[0-9]+\.gem/
gem_name = $1
# add the RPM name prefix
rpm_name = "#{rpm_rubygem_prefix[target]}rubygem-#{gem_name}"
# convert the gem to RPM
gem2rpm({
'src_gem' => gem,
'rpm_name' => rpm_name,
'dst_lib_dir' => dst_lib_dir[target],
'iteration' => iteration,
'prefix' => rpm_rubygem_prefix[target]
})
else
puts "Couldn't extract gem name from: #{gem_file}"
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment