class Virt::Guest

Attributes

arch[R]
boot_device[R]
current_memory[R]
interface[RW]
machine[R]
memory[RW]
name[R]
template_path[RW]
type[R]
vcpu[RW]
volume[RW]
xml_desc[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/virt/guest.rb, line 7
def initialize options = {}
  @connection = Virt.connection
  @name = options[:name] || raise("Must provide a name")

  # If our domain exists, we ignore the provided options and defaults
  fetch_guest
  @memory ||= options[:memory] || default_memory_size
  @vcpu   ||= options[:vcpu]   || default_vcpu_count
  @arch   ||= options[:arch]   || default_arch

  @template_path = options[:template_path] || default_template_path
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/virt/guest.rb, line 84
def <=> other
  self.name <=> other.name
end
destroy() click to toggle source
# File lib/virt/guest.rb, line 64
def destroy
  return true if new?
  stop(true) if running?
  @domain = @domain.undefine
  new?
end
new?() click to toggle source
# File lib/virt/guest.rb, line 20
def new?
  @domain.nil?
end
poweroff() click to toggle source
# File lib/virt/guest.rb, line 60
def poweroff
  stop(true)
end
reboot() click to toggle source
# File lib/virt/guest.rb, line 71
def reboot
  raise "Guest not running, can't reboot" if new? or !running?
  @domain.reboot
end
running?() click to toggle source
# File lib/virt/guest.rb, line 36
def running?
  return false if new?
  @domain.active?
rescue
  # some versions of libvirt do not support checking for active state
  @connection.connection.list_domains.each do |did|
    return true if @connection.connection.lookup_domain_by_id(did).name == name
  end
  false
end
save() click to toggle source
# File lib/virt/guest.rb, line 24
def save
  @domain = @connection.connection.define_domain_xml(xml)
  fetch_info
  !new?
end
shutdown() click to toggle source
# File lib/virt/guest.rb, line 56
def shutdown
  stop
end
start() click to toggle source
# File lib/virt/guest.rb, line 30
def start
  raise "Guest not created, can't start" if new?
  @domain.create unless running?
  running?
end
stop(force=false) click to toggle source
# File lib/virt/guest.rb, line 47
def stop(force=false)
  raise "Guest not created, can't stop" if new?
  force ? @domain.destroy : @domain.shutdown
  !running?
rescue Libvirt::Error
  # domain is not running
  true
end
to_s() click to toggle source
# File lib/virt/guest.rb, line 80
def to_s
  name.to_s
end
uuid() click to toggle source
# File lib/virt/guest.rb, line 76
def uuid
  @domain.uuid unless new?
end

Protected Instance Methods

default_arch() click to toggle source
# File lib/virt/guest.rb, line 127
def default_arch
  "x86_64"
end
default_memory_size() click to toggle source
# File lib/virt/guest.rb, line 119
def default_memory_size
  1048576
end
default_template_path() click to toggle source
# File lib/virt/guest.rb, line 131
def default_template_path; end
default_vcpu_count() click to toggle source
# File lib/virt/guest.rb, line 123
def default_vcpu_count
  1
end
fetch_guest() click to toggle source
# File lib/virt/guest.rb, line 90
def fetch_guest
  @domain = @connection.connection.lookup_domain_by_name(name)
  fetch_info
rescue Libvirt::RetrieveError
end
fetch_info() click to toggle source
# File lib/virt/guest.rb, line 96
def fetch_info
  return if @domain.nil?
  @xml_desc       = @domain.xml_desc
  @memory         = @domain.max_memory
  @current_memory = document("domain/currentMemory") if running?
  @type           = document("domain", "type")
  @vcpu           = document("domain/vcpu")
  @arch           = document("domain/os/type", "arch")
  @machine        = document("domain/os/type", "machine")
  @boot_device    = document("domain/os/boot", "dev") rescue nil

  # do we have a NIC?
  network_type = document("domain/devices/interface", "type") rescue nil

  unless network_type.nil?
    @interface       ||= Interface.new
    @interface.type    = network_type
    @interface.mac     = document("domain/devices/interface/mac", "address")
    @interface.device  = document("domain/devices/interface/source", "bridge")  if @interface.type == "bridge"
    @interface.network = document("domain/devices/interface/source", "network") if @interface.type == "network"
 end
end