Skip to content

Commit 18dc35a

Browse files
committed
Replace xml_element(s) with direct inline Nokogiri use
This uses the Nokogiri feature to directly query an attribute by its path to avoid needing to retrieve it. Especially in list_domains this avoids the need to parse the same XML over and over again.
1 parent 860af54 commit 18dc35a

File tree

5 files changed

+20
-27
lines changed

5 files changed

+20
-27
lines changed

lib/fog/libvirt.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'fog/xml'
33
require 'fog/json'
44
require 'libvirt'
5+
require 'nokogiri'
56

67
require File.expand_path('../libvirt/version', __FILE__)
78

lib/fog/libvirt/models/compute/util/util.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
1-
require 'nokogiri'
21
require 'securerandom'
32

43
module Fog
54
module Libvirt
65
module Util
7-
def xml_element(xml, path, attribute=nil)
8-
xml = Nokogiri::XML(xml)
9-
attribute.nil? ? (xml/path).first.text : (xml/path).first[attribute.to_sym]
10-
end
11-
12-
def xml_elements(xml, path, attribute=nil)
13-
xml = Nokogiri::XML(xml)
14-
attribute.nil? ? (xml/path).map : (xml/path).map{|element| element[attribute.to_sym]}
15-
end
16-
176
def randomized_name
187
"fog-#{(SecureRandom.random_number*10E14).to_i.round}"
198
end

lib/fog/libvirt/requests/compute/list_domains.rb

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,41 +31,41 @@ def catchLibvirtExceptions
3131
def domain_display xml
3232
attrs = {}
3333
[:type, :port, :password, :listen].each do |element|
34-
attrs[element] = xml_element(xml, "domain/devices/graphics",element.to_s) rescue nil
34+
attrs[element] = (xml / "domain/devices/graphics/@#{element}").text
3535
end
36-
attrs.reject{|k,v| v.nil? or v == ""}
36+
attrs.reject! { |k, v| v.empty? }
3737
end
3838

3939
def domain_volumes xml
40-
xml_elements(xml, "domain/devices/disk/source").map do |element|
40+
(xml / "domain/devices/disk/source").map do |element|
4141
element[:file] || element[:dev] || element[:name]
4242
end
4343
end
4444

4545
def boot_order xml
46-
xml_elements(xml, "domain/os/boot", "dev")
46+
(xml / "domain/os/boot/@dev").map(&:text)
4747
end
4848

4949
def firmware(xml)
50-
firmware_from_loader = xml_elements(xml, "domain/os/loader", "type").first
50+
firmware_from_loader = (xml / "domain/os/loader/@type").text
5151

5252
case firmware_from_loader
5353
when 'pflash'
5454
'efi'
5555
when 'rom'
5656
'bios'
5757
else
58-
xml_elements(xml, "domain/os", "firmware").first || 'bios'
58+
(xml / "domain/os/@firmware").first&.text || 'bios'
5959
end
6060
end
6161

6262
# we rely on the fact that the secure attribute is only present when secure boot is enabled
6363
def secure_boot_enabled?(xml)
64-
xml_elements(xml, "domain/os/loader", "secure").first == 'yes'
64+
(xml / "domain/os/loader/@secure").text == 'yes'
6565
end
6666

6767
def domain_interfaces xml
68-
ifs = xml_elements(xml, "domain/devices/interface")
68+
ifs = xml / "domain/devices/interface"
6969
ifs.map { |i|
7070
nics.new({
7171
:type => i['type'],
@@ -80,6 +80,8 @@ def domain_interfaces xml
8080
def domain_to_attributes(dom)
8181
states= %w(nostate running blocked paused shutting-down shutoff crashed pmsuspended)
8282

83+
xml = Nokogiri::XML(dom.xml_desc)
84+
8385
begin
8486
{
8587
:id => dom.uuid,
@@ -92,13 +94,13 @@ def domain_to_attributes(dom)
9294
:autostart => dom.autostart?,
9395
:os_type => dom.os_type,
9496
:active => dom.active?,
95-
:display => domain_display(dom.xml_desc),
96-
:boot_order => boot_order(dom.xml_desc),
97-
:nics => domain_interfaces(dom.xml_desc),
98-
:volumes_path => domain_volumes(dom.xml_desc),
97+
:display => domain_display(xml),
98+
:boot_order => boot_order(xml),
99+
:nics => domain_interfaces(xml),
100+
:volumes_path => domain_volumes(xml),
99101
:state => states[dom.info.state],
100-
:firmware => firmware(dom.xml_desc),
101-
:secure_boot => secure_boot_enabled?(dom.xml_desc),
102+
:firmware => firmware(xml),
103+
:secure_boot => secure_boot_enabled?(xml),
102104
}
103105
rescue ::Libvirt::RetrieveError, ::Libvirt::Error
104106
# Catch libvirt exceptions to avoid race conditions involving

lib/fog/libvirt/requests/compute/list_volumes.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ def list_volumes(filter = { })
2525
private
2626

2727
def volume_to_attributes(vol)
28-
format_type = xml_element(vol.xml_desc, "/volume/target/format", "type") rescue nil # not all volumes have types, e.g. LVM
28+
xml = Nokogiri::XML(vol.xml_desc)
29+
format_type = (xml / "/volume/target/format/@type").first&.text&.strip
2930
return nil if format_type == "dir"
3031

3132
begin

lib/fog/libvirt/requests/compute/update_display.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def update_display(options = { })
1313
display[:listen] = options[:listen].to_s if options[:listen]
1414
display[:passwd] = options[:password].to_s if options[:password]
1515
display[:autoport] = 'yes' if display[:port] == '-1'
16-
new_keymap = options[:keymap] || xml_elements(domain.xml_desc, "graphics", "keymap")[0]
16+
new_keymap = options.fetch(:keymap) { (Nokogiri::XML(domain.xml_desc) / "graphics/@keymap").first&.text }
1717
display[:keymap] = new_keymap unless new_keymap.nil?
1818

1919
builder = Nokogiri::XML::Builder.new { graphics_ (display) }

0 commit comments

Comments
 (0)