class Pennyworth::ImageRunner

Constants

DOMAIN_TEMPLATE

Attributes

name[RW]

Public Class Methods

new(image, username) click to toggle source
# File lib/image_runner.rb, line 24
def initialize(image, username)
  @image = image
  @name = File.basename(image)
  @username = username

  @connection = Libvirt::open("qemu:///system")
end

Public Instance Methods

cleanup_directory(_dir) click to toggle source
# File lib/image_runner.rb, line 46
def cleanup_directory(_dir)
  # The machine will be reset anyway after the tests, so this is is a NOP
end
start() click to toggle source
# File lib/image_runner.rb, line 32
def start
  cleanup

  ip = start_built_image
  @command_runner = RemoteCommandRunner.new(ip, @username)

  ip
end
stop() click to toggle source
# File lib/image_runner.rb, line 41
def stop
  system = @connection.lookup_domain_by_name(@name)
  system.destroy
end

Private Instance Methods

cleanup() click to toggle source
# File lib/image_runner.rb, line 52
def cleanup
  system = @connection.lookup_domain_by_name(@name)
  system.destroy
rescue
end
start_built_image() click to toggle source

Creates a transient kvm domain from the predefined image_test-domain.xml file and returns the ip address for further interaction.

# File lib/image_runner.rb, line 60
def start_built_image
  domain_config = File.read(DOMAIN_TEMPLATE)
  domain_config.gsub!("@@image@@", @image)
  domain_config.gsub!("@@name@@", @name)

  @connection.create_domain_xml(domain_config)
  system = @connection.lookup_domain_by_name(@name)

  domain_xml = Nokogiri::XML(system.xml_desc)
  mac = domain_xml.xpath("//domain/devices/interface/mac").attr("address")
  ip_address = nil

  # Loop until the VM has got an IP address we can return
  lease_file = "/var/lib/libvirt/dnsmasq/default.leases"
  300.times do
    match = File.readlines(lease_file).grep(/#{mac}/).first
    if match
      ip_address = match.split[2]
      break
    end

    sleep 1
  end

  ip_address
end