Class: Virt::Guest

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/virt/guest.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#xml

Constructor Details

#initialize(options = {}) ⇒ Guest

Returns a new instance of Guest.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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
  @volume        = Volume.new options
  @interface   ||= Interface.new options
end

Instance Attribute Details

#archObject

Returns the value of attribute arch.



4
5
6
# File 'lib/virt/guest.rb', line 4

def arch
  @arch
end

#boot_deviceObject (readonly)

Returns the value of attribute boot_device.



4
5
6
# File 'lib/virt/guest.rb', line 4

def boot_device
  @boot_device
end

#current_memoryObject (readonly)

Returns the value of attribute current_memory.



4
5
6
# File 'lib/virt/guest.rb', line 4

def current_memory
  @current_memory
end

#interfaceObject

Returns the value of attribute interface.



5
6
7
# File 'lib/virt/guest.rb', line 5

def interface
  @interface
end

#machineObject (readonly)

Returns the value of attribute machine.



4
5
6
# File 'lib/virt/guest.rb', line 4

def machine
  @machine
end

#memoryObject

Returns the value of attribute memory.



5
6
7
# File 'lib/virt/guest.rb', line 5

def memory
  @memory
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/virt/guest.rb', line 4

def name
  @name
end

#template_pathObject

Returns the value of attribute template_path.



5
6
7
# File 'lib/virt/guest.rb', line 5

def template_path
  @template_path
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/virt/guest.rb', line 4

def type
  @type
end

#vcpuObject

Returns the value of attribute vcpu.



5
6
7
# File 'lib/virt/guest.rb', line 5

def vcpu
  @vcpu
end

#volumeObject

Returns the value of attribute volume.



5
6
7
# File 'lib/virt/guest.rb', line 5

def volume
  @volume
end

#xml_descObject (readonly)

Returns the value of attribute xml_desc.



4
5
6
# File 'lib/virt/guest.rb', line 4

def xml_desc
  @xml_desc
end

Instance Method Details

#<=>(other) ⇒ Object



90
91
92
# File 'lib/virt/guest.rb', line 90

def <=> other
  self.name <=> other.name
end

#destroyObject



66
67
68
69
70
71
# File 'lib/virt/guest.rb', line 66

def destroy
  return true if new?
  stop(true) if running?
  @domain = @domain.undefine
  new?
end

#new?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/virt/guest.rb', line 22

def new?
  @domain.nil?
end

#poweroffObject



62
63
64
# File 'lib/virt/guest.rb', line 62

def poweroff
  stop(true)
end

#rebootObject



73
74
75
76
# File 'lib/virt/guest.rb', line 73

def reboot
  raise "Guest not running, can't reboot" if new? or !running?
  @domain.reboot
end

#running?Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
47
# File 'lib/virt/guest.rb', line 38

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

#saveObject



26
27
28
29
30
# File 'lib/virt/guest.rb', line 26

def save
  @domain = @connection.connection.define_domain_xml(xml)
  fetch_info
  !new?
end

#shutdownObject



58
59
60
# File 'lib/virt/guest.rb', line 58

def shutdown
  stop
end

#startObject



32
33
34
35
36
# File 'lib/virt/guest.rb', line 32

def start
  raise "Guest not created, can't start" if new?
  @domain.create unless running?
  running?
end

#stop(force = false) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/virt/guest.rb', line 49

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_sObject



86
87
88
# File 'lib/virt/guest.rb', line 86

def to_s
  name.to_s
end

#uuidObject



78
79
80
# File 'lib/virt/guest.rb', line 78

def uuid
  @domain.uuid unless new?
end