lib/vagrant-libvirt/action/start_domain.rb in vagrant-libvirt-0.5.3 vs lib/vagrant-libvirt/action/start_domain.rb in vagrant-libvirt-0.6.0

- old
+ new

@@ -1,5 +1,7 @@ +# frozen_string_literal: true + require 'log4r' require 'rexml/document' module VagrantPlugins module ProviderLibvirt @@ -54,11 +56,11 @@ end # disk_bus REXML::XPath.each(xml_descr, '/domain/devices/disk[@device="disk"]/target[@dev="vda"]') do |disk_target| next unless disk_target.attributes['bus'] != config.disk_bus - @logger.debug "domain disk bus updated from '#{disk_target.attributes['bus']}' to '#{bus}'" + @logger.debug "domain disk bus updated from '#{disk_target.attributes['bus']}' to '#{config.disk_bus}'" descr_changed = true disk_target.attributes['bus'] = config.disk_bus disk_target.parent.delete_element("#{disk_target.parent.xpath}/address") end @@ -97,15 +99,17 @@ end if config.cpu_mode != 'host-passthrough' cpu_model = REXML::XPath.first(xml_descr, '/domain/cpu/model') if cpu_model.nil? - @logger.debug "cpu_model updated from not set to '#{config.cpu_model}'" - descr_changed = true - cpu_model = REXML::Element.new('model', REXML::XPath.first(xml_descr, '/domain/cpu')) - cpu_model.attributes['fallback'] = 'allow' - cpu_model.text = config.cpu_model + if config.cpu_model.strip != '' + @logger.debug "cpu_model updated from not set to '#{config.cpu_model}'" + descr_changed = true + cpu_model = REXML::Element.new('model', REXML::XPath.first(xml_descr, '/domain/cpu')) + cpu_model.attributes['fallback'] = 'allow' + cpu_model.text = config.cpu_model + end else if (cpu_model.text or '').strip != config.cpu_model.strip @logger.debug "cpu_model text updated from #{cpu_model.text} to '#{config.cpu_model}'" descr_changed = true cpu_model.text = config.cpu_model @@ -163,21 +167,21 @@ clock.attributes['offset'] = config.clock_offset end # clock timers - because timers can be added/removed, just rebuild and then compare if !config.clock_timers.empty? || clock.has_elements? - oldclock = '' + oldclock = String.new formatter.write(REXML::XPath.first(xml_descr, '/domain/clock'), oldclock) clock.delete_element('//timer') config.clock_timers.each do |clock_timer| timer = REXML::Element.new('timer', clock) clock_timer.each do |attr, value| timer.attributes[attr.to_s] = value end end - newclock = '' + newclock = String.new formatter.write(clock, newclock) unless newclock.eql? oldclock @logger.debug "clock timers config changed" descr_changed = true end @@ -318,14 +322,16 @@ end end if config.initrd initrd = REXML::XPath.first(xml_descr, '/domain/os/initrd') if initrd.nil? - @logger.debug "initrd updated from not set to '#{config.initrd}'" - descr_changed = true - initrd = REXML::Element.new('initrd', REXML::XPath.first(xml_descr, '/domain/os')) - initrd.text = config.initrd + if config.initrd.strip != '' + @logger.debug "initrd updated from not set to '#{config.initrd}'" + descr_changed = true + initrd = REXML::Element.new('initrd', REXML::XPath.first(xml_descr, '/domain/os')) + initrd.text = config.initrd + end else if (initrd.text or '').strip != config.initrd @logger.debug "initrd updated from '#{initrd.text}' to '#{config.initrd}'" descr_changed = true initrd.text = config.initrd @@ -335,25 +341,28 @@ # Apply if descr_changed begin libvirt_domain.undefine - new_descr = '' + new_descr = String.new xml_descr.write new_descr - server = env[:machine].provider.driver.connection.servers.create(xml: new_descr) + env[:machine].provider.driver.connection.servers.create(xml: new_descr) rescue Fog::Errors::Error => e - server = env[:machine].provider.driver.connection.servers.create(xml: descr) + env[:machine].provider.driver.connection.servers.create(xml: descr) raise Errors::FogCreateServerError, error_message: e.message end end - rescue => e + rescue Errors::VagrantLibvirtError => e env[:ui].error("Error when updating domain settings: #{e.message}") end # Autostart with host if enabled in Vagrantfile libvirt_domain.autostart = config.autostart + @logger.debug { + "Starting Domain with XML:\n#{libvirt_domain.xml_desc}" + } # Actually start the domain domain.start - rescue => e + rescue Fog::Errors::Error, Errors::VagrantLibvirtError => e raise Errors::FogError, message: e.message end @app.call(env) end