class Pennyworth::BuildBaseCommand

Public Class Methods

new(boxes_dir) click to toggle source
Calls superclass method Pennyworth::BaseCommand::new
# File lib/pennyworth/commands/build_base_command.rb, line 21
def initialize(boxes_dir)
  super
end

Public Instance Methods

execute(tmp_dir, image_name = nil) click to toggle source
# File lib/pennyworth/commands/build_base_command.rb, line 25
def execute(tmp_dir, image_name = nil)
  Pennyworth::Libvirt.ensure_libvirt_env_started

  box_state = read_local_box_state_file
  images = process_base_image_parameter(local_base_images, image_name)
  log "Creating base images..."
  images.each do |image|
    Dir.chdir(boxes_dir) do
      log
      log "--- #{image} ---"
      source_state = read_box_sources_state(image)
      if box_state[image] && box_state[image]["sources"] &&
         source_state && box_state[image]["sources"] == source_state
        log "  Sources not changed, skipping build"
      else
        log "  Building base image..."
        base_image_create(image, tmp_dir)

        box_state[image] = {
          "sources" => source_state,
          "target" => read_box_target_state(image)
        }
        write_box_state_file(box_state)
      end
    end
  end
end

Private Instance Methods

base_image_cleanup_build(tmp_dir) click to toggle source
# File lib/pennyworth/commands/build_base_command.rb, line 112
def base_image_cleanup_build(tmp_dir)
  if tmp_dir.start_with?("/tmp/")
    begin
      Cheetah.run "sudo", "rm", "-r", tmp_dir, :stdout => :capture
      rescue Cheetah::ExecutionFailed => e
        raise ExecutionFailed.new(e)
      end
  else
    log " Warning: The KIWI tmp dir #{tmp_dir} was outside of '/tmp' so it" \
      " wasn't removed!"
  end
end
base_image_create(image, tmp_dir) click to toggle source

Creates a KVM image from the according Kiwi or Veewe description.

# File lib/pennyworth/commands/build_base_command.rb, line 56
def base_image_create(image, tmp_dir)
  description_dir = File.join(boxes_dir, "definitions", image)

  if File.exists?(File.join(description_dir, "config.xml"))
    build_kiwi(image, tmp_dir)
  elsif File.exists?(File.join(description_dir, "definition.rb"))
    build_veewee(image)
  else
    raise BuildFailed, "Unknown definition format in '#{description_dir}'. " \
      "Supported are Kiwi and Veewee definitions"
  end
end
base_image_export(name, tmp_dir) click to toggle source
# File lib/pennyworth/commands/build_base_command.rb, line 95
def base_image_export(name, tmp_dir)
  Dir.chdir(tmp_dir) do
    image = Dir.glob("*.box").first
    if image
      from_file = File.join(tmp_dir, image)
      to_file = File.join(boxes_dir, name).gsub(/\/$/, "") + ".box"
      begin
        Cheetah.run "sudo", "mv", from_file, to_file, :stdout => :capture
      rescue Cheetah::ExecutionFailed => e
        raise ExecutionFailed.new(e)
      end
    else
      raise BuildFailed, "The built image couldn't be found."
    end
  end
end
build_kiwi(image, tmp_dir) click to toggle source
# File lib/pennyworth/commands/build_base_command.rb, line 78
def build_kiwi(image, tmp_dir)
  description_dir = File.join(boxes_dir, "definitions", image)
  FileUtils.mkdir_p(tmp_dir)
  logfile = "#{tmp_dir}/kiwi-terminal-output.log"
  log "    The build log is available under #{logfile}"
  begin
    Cheetah.run "sudo", "/usr/sbin/kiwi", "--build", description_dir,
      "--destdir", tmp_dir, "--logfile", "#{logfile}",
      :stdout => :capture
  rescue Cheetah::ExecutionFailed => e
    raise ExecutionFailed.new(e)
  end
  log "  Exporting image as box for vagrant..."
  base_image_export(image, tmp_dir)
  base_image_cleanup_build(tmp_dir)
end
build_veewee(image) click to toggle source
# File lib/pennyworth/commands/build_base_command.rb, line 69
def build_veewee(image)
  Cheetah.run "veewee", "kvm", "build", image, "--force", "--auto"
  Cheetah.run "veewee", "kvm", "halt", image
  log "  Exporting image as box for vagrant..."
  Cheetah.run "veewee", "kvm", "export", image, "--force"
rescue Cheetah::ExecutionFailed => e
  raise ExecutionFailed.new(e)
end