Class: Libvirt::Spec::Domain

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/libvirt/spec/domain.rb,
lib/libvirt/spec/domain/clock.rb,
lib/libvirt/spec/domain/memtune.rb,
lib/libvirt/spec/domain/os_booting.rb

Overview

A specification of a domain. This translates directly down to XML which can be used to define and launch domains on a node by libvirt.

Defined Under Namespace

Classes: Clock, Memtune, OSBooting

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#raise_if_unparseables, #try

Constructor Details

#initialize(xml = nil) ⇒ Domain

Initializes a domain specification. If a valid XML string for a domain is given, the it will attempt to be parsed into the structure. This is still very experimental. As such, if there is something which is found which is not parseable, an Exception::UnparseableSpec exception will be raised. Catch this and inspect the message for more information.

Parameters:

  • xml (String) (defaults to: nil)

    XML spec to attempt to parse into the structure.



38
39
40
41
42
43
44
45
46
# File 'lib/libvirt/spec/domain.rb', line 38

def initialize(xml=nil)
  @os = OSBooting.new
  @memtune = Memtune.new
  @features = []
  @clock = Clock.new
  @devices = []

  load!(xml) if xml
end

Instance Attribute Details

#clockObject

Returns the value of attribute clock.



28
29
30
# File 'lib/libvirt/spec/domain.rb', line 28

def clock
  @clock
end

#current_memoryObject

Returns the value of attribute current_memory.



18
19
20
# File 'lib/libvirt/spec/domain.rb', line 18

def current_memory
  @current_memory
end

#descriptionObject

Returns the value of attribute description.



15
16
17
# File 'lib/libvirt/spec/domain.rb', line 15

def description
  @description
end

#devicesObject

Returns the value of attribute devices.



29
30
31
# File 'lib/libvirt/spec/domain.rb', line 29

def devices
  @devices
end

#featuresObject

Returns the value of attribute features.



22
23
24
# File 'lib/libvirt/spec/domain.rb', line 22

def features
  @features
end

#hypervisorObject

Returns the value of attribute hypervisor.



12
13
14
# File 'lib/libvirt/spec/domain.rb', line 12

def hypervisor
  @hypervisor
end

#memoryObject

Returns the value of attribute memory.



17
18
19
# File 'lib/libvirt/spec/domain.rb', line 17

def memory
  @memory
end

#memory_backingObject

Returns the value of attribute memory_backing.



19
20
21
# File 'lib/libvirt/spec/domain.rb', line 19

def memory_backing
  @memory_backing
end

#memtuneObject

Returns the value of attribute memtune.



20
21
22
# File 'lib/libvirt/spec/domain.rb', line 20

def memtune
  @memtune
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/libvirt/spec/domain.rb', line 13

def name
  @name
end

#on_crashObject

Returns the value of attribute on_crash.



26
27
28
# File 'lib/libvirt/spec/domain.rb', line 26

def on_crash
  @on_crash
end

#on_poweroffObject

Returns the value of attribute on_poweroff.



24
25
26
# File 'lib/libvirt/spec/domain.rb', line 24

def on_poweroff
  @on_poweroff
end

#on_rebootObject

Returns the value of attribute on_reboot.



25
26
27
# File 'lib/libvirt/spec/domain.rb', line 25

def on_reboot
  @on_reboot
end

#osObject

Returns the value of attribute os.



16
17
18
# File 'lib/libvirt/spec/domain.rb', line 16

def os
  @os
end

#uuidObject

Returns the value of attribute uuid.



14
15
16
# File 'lib/libvirt/spec/domain.rb', line 14

def uuid
  @uuid
end

#vcpuObject

Returns the value of attribute vcpu.



21
22
23
# File 'lib/libvirt/spec/domain.rb', line 21

def vcpu
  @vcpu
end

Instance Method Details

#load!(xml) ⇒ Object

Attempts to load the attributes from an XML specification. Warning: this will overwrite any already set attributes which exist in the XML.

Parameters:

  • xml (String)

    XML spec to attempt to parse into the structure.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/libvirt/spec/domain.rb', line 52

def load!(xml)
  root = Nokogiri::XML(xml).root
  try(root.xpath("//domain[@type]"), :preserve => true) { |result| self.hypervisor = result["type"].to_sym }
  try(root.xpath("//domain/name")) { |result| self.name = result.text }
  try(root.xpath("//domain/uuid")) { |result| self.uuid = result.text }
  try(root.xpath("//domain/memory")) { |result| self.memory = result.text }
  try(root.xpath("//domain/currentMemory")) { |result| self.current_memory = result.text }
  try(root.xpath("//domain/vcpu")) { |result| self.vcpu = result.text }

  try(root.xpath("//domain/on_poweroff")) { |result| self.on_poweroff = result.text.to_sym }
  try(root.xpath("//domain/on_reboot")) { |result| self.on_reboot = result.text.to_sym }
  try(root.xpath("//domain/on_crash")) { |result| self.on_crash = result.text.to_sym }

  try(root.xpath("//domain/clock")) { |result| self.clock = Clock.new(result) }
  try(root.xpath("//domain/os")) { |result| self.os = OSBooting.new(result) }

  try(root.xpath("//domain/devices")) do |result|
    self.devices = []

    result.element_children.each do |device|
      self.devices << Device.load!(device)
    end
  end

  try(root.xpath("//domain/features")) do |result|
    self.features = []

    result.element_children.each do |feature|
      self.features << feature.name.to_sym
    end
  end

  raise_if_unparseables(root.xpath("//domain/*"))
end

#to_xmlString

Returns the XML for this specification. This XML may be passed into libvirt to create a domain. This is actually the method which should be used for validation of this XML, since libvirt has great validation built in. If you define a domain and an error occurs, then it will notify you what is missing or wrong with the specification.

Returns:

  • (String)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/libvirt/spec/domain.rb', line 94

def to_xml
  Nokogiri::XML::Builder.new do |xml|
    xml.domain(:type => hypervisor) do
      # Name and description
      xml.name name if name
      xml.uuid uuid if uuid
      xml.description description if description

      # Operating system boot information
      os.to_xml(xml)

      # Basic resources
      xml.memory memory if memory
      xml.currentMemory current_memory if current_memory
      xml.vcpu vcpu if vcpu

      if memory_backing == :huge_pages
        xml.memoryBacking do
          xml.hugepages
        end
      end

      # Memtune handles whether or not to render itself
      memtune.to_xml(xml)

      if !features.empty?
        xml.features do
          features.each do |feature|
            xml.send(feature)
          end
        end
      end

      # Lifecycle control
      xml.on_poweroff on_poweroff if on_poweroff
      xml.on_reboot on_reboot if on_reboot
      xml.on_crash on_crash if on_crash

      # Clock
      clock.to_xml(xml)

      # Devices
      if !devices.empty?
        xml.devices do
          devices.map { |d| d.to_xml(xml) }
        end
      end
    end
  end.to_xml
end