<commit_before># Utility class to translate ovf definition to libvirt XML
# and manage XML formatting for libvirt interaction
# Not a full OVF converter, only the minimal needed definition
require "nokogiri"

module VagrantPlugins
  module ProviderKvm
    module Util
      class VmDefinition
        # Attributes of the VM
        attr_reader :name
        attr_reader :cpus
        attr_accessor :disk
        attr_reader :mac
        attr_reader :arch
        attr_reader :network

        def self.list_interfaces(definition)
          nics = {}
          ifcount = 0
          doc = Nokogiri::XML(definition)
          # look for user mode interfaces
          doc.css("devices interface[type='user']").each do |item|
            ifcount += 1
            adapter = ifcount
            nics[adapter] ||= {}
            nics[adapter][:type] = :user
          end
          # look for interfaces on virtual network
          doc.css("devices interface[type='netwok']").each do |item|
            ifcount += 1
            adapter = ifcount
            nics[adapter] ||= {}
            nics[adapter][:network] = item.at_css("source")["network"]
            nics[adapter][:type] = :network
          end
          nics
        end

        def initialize(definition, source_type='libvirt')
          @uuid = nil
          @network = 'default'
          if source_type == 'ovf'
            create_from_ovf(definition)
          else
            create_from_libvirt(definition)
          end
        end

        def create_from_ovf(definition)
          doc = Nokogiri::XML(definition)
          # we don't need no namespace
          doc.remove_namespaces!
          @name = doc.at_css("VirtualSystemIdentifier").content
          devices = doc.css("VirtualHardwareSection Item")
          for device in devices
            case device.at_css("ResourceType").content
              # CPU
            when "3"
              @cpus = device.at_css("VirtualQuantity").content
              # Memory
            when "4"
              @memory = size_in_bytes(device.at_css("VirtualQuantity").content,
                                      device.at_css("AllocationUnits").content)
            end
          end

          # disk volume
          diskref = doc.at_css("DiskSection Disk")["fileRef"]
          @disk = doc.at_css("References File[id='#{diskref}']")["href"]

          # mac address
          # XXX we use only the first nic
          @mac = format_mac(doc.at_css("Machine Hardware Adapter[enabled='true']")['MACAddress'])

          # the architecture is not defined in the ovf file
          # we try to guess from OSType
          # see https://www.virtualbox.org/browser/vbox/trunk/src/VBox/Main/include/ovfreader.h
          @arch = doc.at_css("VirtualSystemIdentifier").
            content[-2..-1] == '64' ? "x86_64" : "i686"
        end

        def create_from_libvirt(definition)
          doc = Nokogiri::XML(definition)
          @name = doc.at_css("domain name").content
          @uuid = doc.at_css("domain uuid").content if doc.at_css("domain uuid")
          memory_unit = doc.at_css("domain memory")["unit"]
          @memory = size_in_bytes(doc.at_css("domain memory").content,
                                  memory_unit)
          @cpus = doc.at_css("domain vcpu").content
          @arch = doc.at_css("domain os type")["arch"]
          @disk = doc.at_css("devices disk source")["file"]
          @mac = doc.at_css("devices interface mac")["address"]
          @network = doc.at_css("devices interface source")["network"]
        end

        def as_libvirt
          # RedHat and Debian-based systems have different executable names
          # depending on version/architectures
          qemu_bin = [ '/usr/bin/qemu-kvm' ]
          qemu_bin << '/usr/bin/qemu-system-x86_64' if @arch.match(/64$/)
          qemu_bin << '/usr/bin/qemu-system-i386'   if @arch.match(/^i.86$/)

          xml = KvmTemplateRenderer.render("libvirt_domain", {
            :name => @name,
            :uuid => @uuid,
            :memory => size_from_bytes(@memory, "KiB"),
            :cpus => @cpus,
            :arch => @arch,<commit_msg>add "/usr/bin/kvm" to search options<commit_after># Utility class to translate ovf definition to libvirt XML
# and manage XML formatting for libvirt interaction
# Not a full OVF converter, only the minimal needed definition
require "nokogiri"

module VagrantPlugins
  module ProviderKvm
    module Util
      class VmDefinition
        # Attributes of the VM
        attr_reader :name
        attr_reader :cpus
        attr_accessor :disk
        attr_reader :mac
        attr_reader :arch
        attr_reader :network

        def self.list_interfaces(definition)
          nics = {}
          ifcount = 0
          doc = Nokogiri::XML(definition)
          # look for user mode interfaces
          doc.css("devices interface[type='user']").each do |item|
            ifcount += 1
            adapter = ifcount
            nics[adapter] ||= {}
            nics[adapter][:type] = :user
          end
          # look for interfaces on virtual network
          doc.css("devices interface[type='netwok']").each do |item|
            ifcount += 1
            adapter = ifcount
            nics[adapter] ||= {}
            nics[adapter][:network] = item.at_css("source")["network"]
            nics[adapter][:type] = :network
          end
          nics
        end

        def initialize(definition, source_type='libvirt')
          @uuid = nil
          @network = 'default'
          if source_type == 'ovf'
            create_from_ovf(definition)
          else
            create_from_libvirt(definition)
          end
        end

        def create_from_ovf(definition)
          doc = Nokogiri::XML(definition)
          # we don't need no namespace
          doc.remove_namespaces!
          @name = doc.at_css("VirtualSystemIdentifier").content
          devices = doc.css("VirtualHardwareSection Item")
          for device in devices
            case device.at_css("ResourceType").content
              # CPU
            when "3"
              @cpus = device.at_css("VirtualQuantity").content
              # Memory
            when "4"
              @memory = size_in_bytes(device.at_css("VirtualQuantity").content,
                                      device.at_css("AllocationUnits").content)
            end
          end

          # disk volume
          diskref = doc.at_css("DiskSection Disk")["fileRef"]
          @disk = doc.at_css("References File[id='#{diskref}']")["href"]

          # mac address
          # XXX we use only the first nic
          @mac = format_mac(doc.at_css("Machine Hardware Adapter[enabled='true']")['MACAddress'])

          # the architecture is not defined in the ovf file
          # we try to guess from OSType
          # see https://www.virtualbox.org/browser/vbox/trunk/src/VBox/Main/include/ovfreader.h
          @arch = doc.at_css("VirtualSystemIdentifier").
            content[-2..-1] == '64' ? "x86_64" : "i686"
        end

        def create_from_libvirt(definition)
          doc = Nokogiri::XML(definition)
          @name = doc.at_css("domain name").content
          @uuid = doc.at_css("domain uuid").content if doc.at_css("domain uuid")
          memory_unit = doc.at_css("domain memory")["unit"]
          @memory = size_in_bytes(doc.at_css("domain memory").content,
                                  memory_unit)
          @cpus = doc.at_css("domain vcpu").content
          @arch = doc.at_css("domain os type")["arch"]
          @disk = doc.at_css("devices disk source")["file"]
          @mac = doc.at_css("devices interface mac")["address"]
          @network = doc.at_css("devices interface source")["network"]
        end

        def as_libvirt
          # RedHat and Debian-based systems have different executable names
          # depending on version/architectures
          qemu_bin = [ '/usr/bin/qemu-kvm', '/usr/bin/kvm' ]
          qemu_bin << '/usr/bin/qemu-system-x86_64' if @arch.match(/64$/)
          qemu_bin << '/usr/bin/qemu-system-i386'   if @arch.match(/^i.86$/)

          xml = KvmTemplateRenderer.render("libvirt_domain", {
            :name => @name,
            :uuid => @uuid,
            :memory => size_from_bytes(@memory, "KiB"),
            :cpus => @cpus,
            :arch => @arch,
{'fraction_non_alphanumeric': 0.11239054815881012, 'fraction_numerical': 0.006477150053976251, 'mean_word_length': 1.9113128491620113, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5509857', 'n_tokens_mistral': 2402, 'n_tokens_neox': 2331, 'n_words': 542}