mirror of
https://github.com/vagrant-libvirt/vagrant-libvirt.git
synced 2025-02-25 18:55:27 -06:00
Return early from set_boot_order if not needed (#1578)
Switch to calling the returning the next middleware in the chain as soon as possible in the set boot order action. Makes the overall remaining logic tidier. Include basic tests to check existing behaviour.
This commit is contained in:
@@ -16,6 +16,12 @@ module VagrantPlugins
|
||||
end
|
||||
|
||||
def call(env)
|
||||
# Only execute specific boot ordering if this is defined
|
||||
# in the Vagrant file
|
||||
unless @boot_order.count >= 1
|
||||
return @app.call(env)
|
||||
end
|
||||
|
||||
# Get domain first
|
||||
begin
|
||||
domain = env[:machine].provider
|
||||
@@ -30,53 +36,49 @@ module VagrantPlugins
|
||||
error_message: e.message
|
||||
end
|
||||
|
||||
# Only execute specific boot ordering if this is defined
|
||||
# in the Vagrant file
|
||||
if @boot_order.count >= 1
|
||||
# If a domain is initially defined with no box or disk or
|
||||
# with an explicit boot order, Libvirt adds <boot dev="foo">
|
||||
# This conflicts with an explicit boot_order configuration,
|
||||
# so we need to remove it from the domain xml and feed it back.
|
||||
# Also see https://bugzilla.redhat.com/show_bug.cgi?id=1248514
|
||||
# as to why we have to do this after all devices have been defined.
|
||||
xml = Nokogiri::XML(domain.xml_desc)
|
||||
xml.search('/domain/os/boot').each(&:remove)
|
||||
|
||||
# If a domain is initially defined with no box or disk or
|
||||
# with an explicit boot order, Libvirt adds <boot dev="foo">
|
||||
# This conflicts with an explicit boot_order configuration,
|
||||
# so we need to remove it from the domain xml and feed it back.
|
||||
# Also see https://bugzilla.redhat.com/show_bug.cgi?id=1248514
|
||||
# as to why we have to do this after all devices have been defined.
|
||||
xml = Nokogiri::XML(domain.xml_desc)
|
||||
xml.search('/domain/os/boot').each(&:remove)
|
||||
# Parse the XML and find each defined drive and network interfacee
|
||||
hd = xml.search("/domain/devices/disk[@device='disk']")
|
||||
cdrom = xml.search("/domain/devices/disk[@device='cdrom']")
|
||||
# implemented only for 1 network
|
||||
nets = @boot_order.flat_map do |x|
|
||||
x.class == Hash ? x : nil
|
||||
end.compact
|
||||
raise 'Defined only for 1 network for boot' if nets.size > 1
|
||||
network = search_network(nets, xml)
|
||||
|
||||
# Parse the XML and find each defined drive and network interfacee
|
||||
hd = xml.search("/domain/devices/disk[@device='disk']")
|
||||
cdrom = xml.search("/domain/devices/disk[@device='cdrom']")
|
||||
# implemented only for 1 network
|
||||
nets = @boot_order.flat_map do |x|
|
||||
x.class == Hash ? x : nil
|
||||
end.compact
|
||||
raise 'Defined only for 1 network for boot' if nets.size > 1
|
||||
network = search_network(nets, xml)
|
||||
# Generate an array per device group and a flattened
|
||||
# array from all of those
|
||||
devices = { 'hd' => hd,
|
||||
'cdrom' => cdrom,
|
||||
'network' => network }
|
||||
|
||||
# Generate an array per device group and a flattened
|
||||
# array from all of those
|
||||
devices = { 'hd' => hd,
|
||||
'cdrom' => cdrom,
|
||||
'network' => network }
|
||||
|
||||
final_boot_order = final_boot_order(@boot_order, devices)
|
||||
# Loop over the entire defined boot order array and
|
||||
# create boot order entries in the domain XML
|
||||
final_boot_order.each_with_index do |node, index|
|
||||
boot = "<boot order='#{index + 1}'/>"
|
||||
node.add_child(boot)
|
||||
logger_msg(node, index)
|
||||
end
|
||||
|
||||
# Finally redefine the domain XML through Libvirt
|
||||
# to apply the boot ordering
|
||||
env[:machine].provider
|
||||
.driver
|
||||
.connection
|
||||
.client
|
||||
.define_domain_xml(xml.to_s)
|
||||
final_boot_order = final_boot_order(@boot_order, devices)
|
||||
# Loop over the entire defined boot order array and
|
||||
# create boot order entries in the domain XML
|
||||
final_boot_order.each_with_index do |node, index|
|
||||
next if node.nil?
|
||||
boot = "<boot order='#{index + 1}'/>"
|
||||
node.add_child(boot)
|
||||
logger_msg(node, index)
|
||||
end
|
||||
|
||||
# Finally redefine the domain XML through Libvirt
|
||||
# to apply the boot ordering
|
||||
env[:machine].provider
|
||||
.driver
|
||||
.connection
|
||||
.client
|
||||
.define_domain_xml(xml.to_s)
|
||||
|
||||
@app.call(env)
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user