Class: Vmit::LibvirtVM
- Inherits:
-
Object
- Object
- Vmit::LibvirtVM
- Defined in:
- lib/vmit/libvirt_vm.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
-
#workspace ⇒ Object
readonly
Returns the value of attribute workspace.
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #assert_down ⇒ Object
- #assert_up ⇒ Object
- #destroy ⇒ Object
-
#domain ⇒ Libvirt::Domain
Returns the libvirt domain or creates one for the workspace if it does not exist.
- #down? ⇒ Boolean
-
#initialize(workspace, opts = {}) ⇒ LibvirtVM
constructor
A new instance of LibvirtVM.
- #ip_address ⇒ Object
- #reboot ⇒ Object
- #shutdown ⇒ Object
-
#spice ⇒ Object
synchronus spice viewer.
- #spice_address ⇒ Object
- #state ⇒ Object
- #to_libvirt_xml ⇒ Object
- #up ⇒ Object
- #up? ⇒ Boolean
-
#vnc ⇒ Object
synchronus vnc viewer.
- #vnc_address ⇒ Object
-
#wait_until_shutdown!(&block) ⇒ Object
Waits until the machine is shutdown executing the passed block.
Constructor Details
#initialize(workspace, opts = {}) ⇒ LibvirtVM
Returns a new instance of LibvirtVM.
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/vmit/libvirt_vm.rb', line 22 def initialize(workspace, opts={}) @workspace = workspace @config = Confstruct::Configuration.new(@workspace.config) @config.configure(opts) @conn = ::Libvirt::open("qemu:///system") if not @conn raise 'Can\'t initialize hypervisor' end end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
11 12 13 |
# File 'lib/vmit/libvirt_vm.rb', line 11 def config @config end |
#conn ⇒ Object (readonly)
Returns the value of attribute conn.
12 13 14 |
# File 'lib/vmit/libvirt_vm.rb', line 12 def conn @conn end |
#workspace ⇒ Object (readonly)
Returns the value of attribute workspace.
10 11 12 |
# File 'lib/vmit/libvirt_vm.rb', line 10 def workspace @workspace end |
Class Method Details
Instance Method Details
#[](key) ⇒ Object
194 195 196 197 198 199 200 |
# File 'lib/vmit/libvirt_vm.rb', line 194 def [](key) if @runtime_opts.has_key?(key) @runtime_opts[key] else workspace[key] end end |
#assert_down ⇒ Object
88 89 90 91 92 |
# File 'lib/vmit/libvirt_vm.rb', line 88 def assert_down if up? raise "VM is running. Try 'vmit down'..." end end |
#assert_up ⇒ Object
82 83 84 85 86 |
# File 'lib/vmit/libvirt_vm.rb', line 82 def assert_up unless up? raise "VM is not running. Try 'vmit up'..." end end |
#destroy ⇒ Object
108 109 110 111 112 |
# File 'lib/vmit/libvirt_vm.rb', line 108 def destroy if domain domain.destroy end end |
#domain ⇒ Libvirt::Domain
Returns the libvirt domain or creates one for the workspace if it does not exist
35 36 37 |
# File 'lib/vmit/libvirt_vm.rb', line 35 def domain conn.lookup_domain_by_name(workspace.name) rescue nil end |
#down? ⇒ Boolean
94 95 96 |
# File 'lib/vmit/libvirt_vm.rb', line 94 def down? !up? end |
#ip_address ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/vmit/libvirt_vm.rb', line 150 def ip_address File.open('/var/lib/libvirt/dnsmasq/default.leases') do |f| f.each_line do |line| parts = line.split(' ') if parts[1] == config.mac_address return parts[2] end end end nil end |
#reboot ⇒ Object
98 99 100 101 |
# File 'lib/vmit/libvirt_vm.rb', line 98 def reboot assert_up domain.reboot end |
#shutdown ⇒ Object
103 104 105 106 |
# File 'lib/vmit/libvirt_vm.rb', line 103 def shutdown assert_up domain.shutdown end |
#spice ⇒ Object
synchronus spice viewer
179 180 181 182 183 184 |
# File 'lib/vmit/libvirt_vm.rb', line 179 def spice assert_up addr, port = spice_address raise "Can't get the SPICE information from the VM" unless addr system("spicec --host #{addr} --port #{port}") end |
#spice_address ⇒ Object
162 163 164 165 166 167 168 |
# File 'lib/vmit/libvirt_vm.rb', line 162 def spice_address assert_up doc = Nokogiri::XML(domain.xml_desc) port = doc.xpath("//graphics[@type='spice']/@port") listen = doc.xpath("//graphics[@type='spice']/listen[@type='address']/@address") return listen, port end |
#state ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/vmit/libvirt_vm.rb', line 62 def state assert_up st, reason = domain.state st_sym = case st when Libvirt::Domain::NOSTATE then :unknown when Libvirt::Domain::RUNNING then :running when Libvirt::Domain::BLOCKED then :blocked when Libvirt::Domain::PAUSED then :paused when Libvirt::Domain::SHUTDOWN then :shutdown when Libvirt::Domain::SHUTOFF then :shutoff when Libvirt::Domain::CRASHED then :crashed when Libvirt::Domain::PMSUSPENDED then :pmsuspended end return st_sym, reason end |
#to_libvirt_xml ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/vmit/libvirt_vm.rb', line 202 def to_libvirt_xml builder = Nokogiri::XML::Builder.new do |xml| xml.domain(:type => 'kvm') { xml.name workspace.name xml.uuid config.uuid match = /([0-9+])([^0-9+])/.match config.memory xml.memory(match[1], :unit => match[2]) xml.vcpu 1 xml.os { xml.type('hvm', :arch => 'x86_64') if config.lookup!('kernel') xml.kernel config.kernel if config.lookup!('kernel_cmdline') xml.cmdline config.kernel_cmdline.join(' ') end end xml.initrd config.initrd if config.lookup!('initrd') xml.boot(:dev => 'cdrom') if config.lookup!('cdrom') } # for shutdown to work xml.features { xml.acpi } #xml.on_poweroff 'destroy' unless config.lookup!('reboot').nil? || config.lookup!('reboot') xml.on_reboot 'destroy' end #xml.on_crash 'destroy' #xml.on_lockfailure 'poweroff' xml.devices { xml.emulator '/usr/bin/qemu-kvm' xml.channel(:type => 'spicevmc') do xml.target(:type => 'virtio', :name => 'com.redhat.spice.0') end xml.disk(:type => 'file', :device => 'disk') { xml.driver(:name => 'qemu', :type => 'qcow2') xml.source(:file => workspace.current_image) if config.virtio xml.target(:dev => 'sda', :bus => 'virtio') else xml.target(:dev => 'sda', :bus => 'ide') end } if config.lookup!('cdrom') xml.disk(:type => 'file', :device => 'cdrom') { xml.source(:file => config.cdrom) xml.target(:dev => 'hdc') xml.readonly } end if config.lookup!('floppy') xml.disk(:type => 'dir', :device => 'floppy') { xml.source(:dir => config.floppy) xml.target(:dev => 'fda') xml.readonly } end xml.graphics(:type => 'vnc', :autoport => 'yes') xml.graphics(:type => 'spice', :autoport => 'yes') xml.interface(:type => 'network') { xml.source(:network => 'default') xml.mac(:address => config.mac_address) } } } end builder.to_xml end |
#up ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/vmit/libvirt_vm.rb', line 39 def up unless down? Vmit.logger.error "#{workspace.name} is already up. Run 'vmit ssh' or 'vmit vnc' to access it." return end Vmit.logger.debug "\n#{conn.capabilities}" Vmit.logger.info "Starting VM..." network = conn.lookup_network_by_name('default') Vmit.logger.debug "\n#{network.xml_desc}" if not network.active? network.create end Vmit.logger.debug "\n#{self.to_libvirt_xml}" puts domain.inspect domain.destroy if domain if domain.nil? conn.create_domain_xml(self.to_libvirt_xml) end end |
#up? ⇒ Boolean
78 79 80 |
# File 'lib/vmit/libvirt_vm.rb', line 78 def up? !domain.nil? && domain.active? end |
#vnc ⇒ Object
synchronus vnc viewer
187 188 189 190 191 192 |
# File 'lib/vmit/libvirt_vm.rb', line 187 def vnc assert_up addr = vnc_address raise "Can't get the VNC information from the VM" unless addr system("vncviewer #{addr}") end |
#vnc_address ⇒ Object
170 171 172 173 174 175 176 |
# File 'lib/vmit/libvirt_vm.rb', line 170 def vnc_address assert_up doc = Nokogiri::XML(domain.xml_desc) port = doc.xpath("//graphics[@type='vnc']/@port") listen = doc.xpath("//graphics[@type='vnc']/listen[@type='address']/@address") "#{listen}:#{port}" end |
#wait_until_shutdown!(&block) ⇒ Object
Waits until the machine is shutdown executing the passed block.
If the machine is shutdown, the block will be killed. If the block exits, the machine will be stopped immediately (domain destroyed)
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/vmit/libvirt_vm.rb', line 127 def wait_until_shutdown!(&block) chars = %w{ | / - \\ } thread = Thread.new(&block) thread.abort_on_exception = true Vmit.logger.info "Waiting for machine..." while true print chars[0] if down? Thread.kill(thread) return end if not thread.alive? domain.destroy end sleep(1) print "\b" chars.push chars.shift end end |