Class: Virt::Guest

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

Direct Known Subclasses

KVM::Guest, VMWare::Guest

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#to_gb, #xml

Constructor Details

#initialize(options = {}) ⇒ Guest

Returns a new instance of Guest.



7
8
9
10
11
12
13
14
15
16
17
18
# 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

Instance Attribute Details

#archObject (readonly)

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



84
85
86
# File 'lib/virt/guest.rb', line 84

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

#destroyObject



64
65
66
67
68
69
# File 'lib/virt/guest.rb', line 64

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

#new?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/virt/guest.rb', line 20

def new?
  @domain.nil?
end

#poweroffObject



60
61
62
# File 'lib/virt/guest.rb', line 60

def poweroff
  stop(true)
end

#rebootObject



71
72
73
74
# File 'lib/virt/guest.rb', line 71

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

#running?Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
# 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

#saveObject



24
25
26
27
28
# File 'lib/virt/guest.rb', line 24

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

#shutdownObject



56
57
58
# File 'lib/virt/guest.rb', line 56

def shutdown
  stop
end

#startObject



30
31
32
33
34
# 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) ⇒ Object



47
48
49
50
51
52
53
54
# 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_sObject



80
81
82
# File 'lib/virt/guest.rb', line 80

def to_s
  name.to_s
end

#uuidObject



76
77
78
# File 'lib/virt/guest.rb', line 76

def uuid
  @domain.uuid unless new?
end