class RubyPackager::Tasks

Attributes

after_install_script[RW]
application_name[R]
architecture[RW]
autoreq[RW]
before_install_script[RW]
bundle_standalone[RW]
bundle_with_binstubs[RW]
bundle_without[RW]
dependencies[RW]
description[RW]
epoch[RW]
install_path[RW]
iteration[RW]
package_name[RW]
rpm_group[RW]
rpm_os[RW]
rpm_user[RW]
staged_files[R]
vendorize[RW]
version[RW]

Public Class Methods

new(application_name, prefix) { |self| ... } click to toggle source
# File lib/ruby_packager.rb, line 6
def initialize(application_name, prefix)
  @application_name = application_name
  @prefix = prefix
  @package_name = "#{@prefix}-#{application_name}"
  @version = '1.0'
  @dependencies = []
  @autoreq = true
  @install_path = "opt/#{@prefix}/apps/#{application_name}"
  @links = {}
  @bundle_standalone = false
  @bundle_with_binstubs = false
  @bundle_without = ["development", "test"]
  @vendorize = true
  install(FileList["Gemfile*"])
  if block_given?
    yield self
    define_tasks
  end
end

Public Instance Methods

define_tasks() click to toggle source
# File lib/ruby_packager.rb, line 72
def define_tasks
  define_show_config_task
  define_show_staging_list_task
  define_bundle_file_task
  define_stage_task
  define_rpm_task
  define_install_rpm_task
  define_clean_task
end
filename() click to toggle source
# File lib/ruby_packager.rb, line 82
def filename
  full_version = "#{version}"
  full_version += "-#{iteration}" if iteration
  "pkg/#{package_name}-#{full_version}.x86_64.rpm"
end
generate(filename, &block) click to toggle source
# File lib/ruby_packager.rb, line 88
def generate(filename, &block)
  target_file = staged_app(filename)
  file(target_file) do
    block.call(target_file)
  end
  @staged_files << target_file
end
install(file_list, options = {}) click to toggle source
# File lib/ruby_packager.rb, line 49
def install(file_list, options = {})
  @staged_files ||= []
  prefix = options[:prefix] ? staged_file_path(options[:prefix]) : staged_app
  @staged_files += map(file_list => prefix)
end

Private Instance Methods

define_bundle_file_task() click to toggle source
# File lib/ruby_packager.rb, line 139
def define_bundle_file_task
  file staged_app("bundle") => [staged_app("Gemfile"), staged_app("Gemfile.lock")] do
    Bundler.with_clean_env do
      Dir.chdir(staged_app) do
        links.each { |source, target| ln_sf target, source }
        if vendorize
          sh("bundle package --all")
          gem_cache_dir = Pathname.pwd + "vendor/cache"
          mkpath "vendor"
          rm_rf "vendor/cache"
          ln_s gem_cache_dir, "vendor/cache"
        
          begin
            rm_rf ".bundle"

            bundle_command = %w(bundle install)
            bundle_command += ["--local"]
            bundle_command += ["--path", "vendor/bundle"]
            bundle_command += ["--standalone"] if bundle_standalone
            bundle_command += ["--binstubs"] if bundle_with_binstubs
            if bundle_without
              bundle_command += ["--without"]
              bundle_command += bundle_without
            end

            sh(*bundle_command)
            rm_rf "vendor/cache"
            touch "vendor/bundle"
          rescue => e
            rm_rf "vendor/bundle"
            raise e
          end
        end
      end
    end
  end
end
define_clean_task() click to toggle source
# File lib/ruby_packager.rb, line 249
def define_clean_task
  desc "Clean the previous build directory"
  task :clean do
    sh("rm -rf tmp pkg")
  end
end
define_install_rpm_task() click to toggle source
# File lib/ruby_packager.rb, line 213
def define_install_rpm_task
  desc "Install the generated RPM package"
  task :install_rpm => :rpm do
    sh("sudo rpm --upgrade --force -p #{filename}")
  end
end
define_rpm_task() click to toggle source
# File lib/ruby_packager.rb, line 182
def define_rpm_task
  desc "Create an RPM package"
  task :rpm => filename

  file filename => target_files do
    mkpath("pkg")
    fpm_cmd = %w(fpm -s dir -t rpm)
    fpm_cmd += [
      "-n", package_name,
      "--version", version
    ]
    fpm_cmd += [ "--iteration", iteration ] if iteration
    fpm_cmd += [ "--epoch", epoch ] if epoch
    dependencies.each do |dep|
      fpm_cmd += ['-d', dep]
    end
    fpm_cmd += ['--rpm-autoreq'] if autoreq
    fpm_cmd += ['--before-install', before_install_script] if before_install_script
    fpm_cmd += ['--after-install', after_install_script] if after_install_script
    fpm_cmd += ['-p', filename]
    fpm_cmd += ['-C', staging_dir]
    fpm_cmd += ['-a', architecture] if architecture
    fpm_cmd += ['--description', description] if description
    fpm_cmd += ['--rpm-os', rpm_os] if rpm_os
    fpm_cmd += ['--rpm-user', rpm_user] if rpm_user
    fpm_cmd += ['--rpm-group', rpm_group] if rpm_group
    fpm_cmd += ['.']
    sh(*fpm_cmd)
  end
end
define_show_config_task() click to toggle source
# File lib/ruby_packager.rb, line 220
    def define_show_config_task
      desc "Show configuration for the RPM packager"
      task :show_config do
        puts <<-END_CONTENT
        You can override any of these in your packager config.
        application_name      : #{application_name}
        package_name          : #{package_name}
        dependencies          : #{dependencies}
        architecture          : #{architecture}
        rpm_user              : #{rpm_user}
        rpm_group             : #{rpm_group}
        before_install_script : #{before_install_script}
        after_install_script  : #{after_install_script}
        version               : #{version}
        links                 : #{links}
        install_path          : #{install_path}
        staged_files          : #{staged_files}

        END_CONTENT
      end
    end
define_show_staging_list_task() click to toggle source
# File lib/ruby_packager.rb, line 242
def define_show_staging_list_task
  desc "Show a list of files to be included in the RPM"
  task :show_staging_list do
    staged_files.each { |path| puts path }
  end
end
define_stage_task() click to toggle source
# File lib/ruby_packager.rb, line 177
def define_stage_task
  desc "Stage RPM package contents into #{staging_dir}"
  task :stage => target_files
end
map(mappings) click to toggle source
# File lib/ruby_packager.rb, line 109
def map(mappings)
  target_files = []
  mappings.collect do |sources, prefix|
    sources.each do |source|
      next if File.directory?(source)
      target = File.join(prefix, source)
      file(target => source) do
        mkpath(File.dirname(target))
        cp(source, target)
      end
      target_files << target
    end
  end
  target_files
end
staged_app(path = nil) click to toggle source
# File lib/ruby_packager.rb, line 125
def staged_app(path = nil)
  components = [staging_dir, install_path, path].compact
  File.join(*components)
end
staged_file_path(prefix) click to toggle source
# File lib/ruby_packager.rb, line 130
def staged_file_path(prefix)
  components = [staging_dir, prefix].compact
  File.join(*components)
end
staging_dir() click to toggle source
# File lib/ruby_packager.rb, line 105
def staging_dir
  'tmp/stage'
end
target_files() click to toggle source
# File lib/ruby_packager.rb, line 135
def target_files
  staged_files + [staged_app("bundle")]
end