<commit_before># frozen_string_literal: true

require 'log4r'

require 'vagrant-libvirt/util/resolvers'

module VagrantPlugins
  module ProviderLibvirt
    module Action
      class CreateDomain
        include VagrantPlugins::ProviderLibvirt::Util::ErbTemplate
        include VagrantPlugins::ProviderLibvirt::Util::StorageUtil

        def initialize(app, _env)
          @logger = Log4r::Logger.new('vagrant_libvirt::action::create_domain')
          @app = app
        end

        def call(env)
          # Get config.
          config = env[:machine].provider_config

          # Gather some info about domain
          @name = env[:domain_name]
          @title = config.title
          vagrantfile = File.join(env[:root_path], (env[:vagrantfile_name] || "Vagrantfile"))
          @description = !config.description.empty? ? config.description : "Source: #{vagrantfile}"
          @uuid = config.uuid
          @cpus = config.cpus.to_i
          @cpuset = config.cpuset
          @cpu_features = config.cpu_features
          @cpu_topology = config.cpu_topology
          @nodeset = config.nodeset
          @features = config.features
          @features_hyperv = config.features_hyperv
          @clock_offset = config.clock_offset
          @clock_timers = config.clock_timers
          @shares = config.shares
          @cpu_mode = config.cpu_mode
          @cpu_model = config.cpu_model
          @cpu_fallback = config.cpu_fallback
          @numa_nodes = config.numa_nodes
          @loader = config.loader
          @nvram = config.nvram
          @machine_type = config.machine_type
          @machine_arch = config.machine_arch
          @disk_driver_opts = config.disk_driver_opts
          @nested = config.nested
          @memory_size = config.memory.to_i * 1024
          @memory_backing = config.memory_backing
          @management_network_mac = config.management_network_mac
          @domain_volume_cache = config.volume_cache || 'default'
          @kernel = config.kernel
          @cmd_line = config.cmd_line
          @emulator_path = config.emulator_path
          @initrd = config.initrd
          @dtb = config.dtb
          @graphics_type = config.graphics_type
          @graphics_autoport = config.graphics_autoport
          @graphics_port = config.graphics_port
          @graphics_ip = config.graphics_ip
          @graphics_passwd = config.graphics_passwd
          @graphics_gl = config.graphics_gl
          @video_type = config.video_type
          @sound_type = config.sound_type
          @video_vram = config.video_vram
          @video_accel3d = config.video_accel3d
          @keymap = config.keymap
          @kvm_hidden = config.kvm_hidden

          @tpm_model = config.tpm_model
          @tpm_type = config.tpm_type
          @tpm_path = config.tpm_path
          @tpm_version = config.tpm_version

          # Boot order
          @boot_order = config.boot_order

          # Storage
          @storage_pool_name = config.storage_pool_name
          @domain_volumes = env[:domain_volumes] || []
          @disks = env[:disks] || []
          @cdroms = config.cdroms

          # Input
          @inputs = config.inputs

          # Channels
          @channels = config.channels

          # PCI device passthrough
          @pcis = config.pcis

          # Watchdog device
          @watchdog_dev = config.watchdog_dev

          # USB controller
          @usbctl_dev = config.usbctl_dev

          # USB device passthrough
          @usbs = config.usbs

          # Redirected devices
          @redirdevs = config.redirdevs
          @redirfilters = config.redirfilters

          # Additional QEMU commandline arguments
          @qemu_args = config.qemu_args

          # Additional QEMU commandline environment variables
          @qemu_env = config.qemu_env

          # smartcard device
          @smartcard_dev = config.smartcard_dev

          # RNG device passthrough
          @rng = config.rng

          # Memballoon
          @memballoon_enabled = config.memballoon_enabled
          @memballoon_model = config.memballoon_model
          @memballoon_pci_bus = config.memballoon_pci_bus
          @memballoon_pci_slot = config.memballoon_pci_slot

          @domain_type = config.driver

          @os_type = 'hvm'

          env[:domain_volumes].each_with_index do |vol, index|
            suffix_index = index > 0 ? "_#{index}" : ''
            domain_volume = env[:machine].provider.driver.connection.volumes.all(
              name: "#{@name}#{suffix_index}.img"
            ).find { |x| x.pool_name == vol[:pool] }
            raise Errors::NoDomainVolume if domain_volume.nil?
          end

          @disks.each do |disk|
            # make the disk. equivalent to:
            # qemu-img create -f qcow2 <path> 5g
            begin
              env[:machine].provider.driver.connection.volumes.create(
                name: disk[:name],
                format_type: disk[:type],
                path: disk[:absolute_path],
                capacity: disk[:size],
                owner: storage_uid(env),
                group: storage_gid(env),
                #:allocation => ?,
                pool_name: disk[:pool],
              )
            rescue Libvirt::Error => e
              # It is hard to believe that e contains just a string
              # and no useful error code!
              msg = "Call to virStorageVolCreateXML failed: " +
                    "storage volume '#{disk[:absolute_path]}' exists already"
              if e.message == msg and disk[:allow_existing]
                disk[:preexisting] = true
              else
                raise Errors::FogCreateDomainVolumeError,
                      error_message: e.message
              end<commit_msg>Fix allow_existing for disks against newer versions of libvirt. (#1507)<commit_after># frozen_string_literal: true

require 'log4r'

require 'vagrant-libvirt/util/resolvers'

module VagrantPlugins
  module ProviderLibvirt
    module Action
      class CreateDomain
        include VagrantPlugins::ProviderLibvirt::Util::ErbTemplate
        include VagrantPlugins::ProviderLibvirt::Util::StorageUtil

        def initialize(app, _env)
          @logger = Log4r::Logger.new('vagrant_libvirt::action::create_domain')
          @app = app
        end

        def call(env)
          # Get config.
          config = env[:machine].provider_config

          # Gather some info about domain
          @name = env[:domain_name]
          @title = config.title
          vagrantfile = File.join(env[:root_path], (env[:vagrantfile_name] || "Vagrantfile"))
          @description = !config.description.empty? ? config.description : "Source: #{vagrantfile}"
          @uuid = config.uuid
          @cpus = config.cpus.to_i
          @cpuset = config.cpuset
          @cpu_features = config.cpu_features
          @cpu_topology = config.cpu_topology
          @nodeset = config.nodeset
          @features = config.features
          @features_hyperv = config.features_hyperv
          @clock_offset = config.clock_offset
          @clock_timers = config.clock_timers
          @shares = config.shares
          @cpu_mode = config.cpu_mode
          @cpu_model = config.cpu_model
          @cpu_fallback = config.cpu_fallback
          @numa_nodes = config.numa_nodes
          @loader = config.loader
          @nvram = config.nvram
          @machine_type = config.machine_type
          @machine_arch = config.machine_arch
          @disk_driver_opts = config.disk_driver_opts
          @nested = config.nested
          @memory_size = config.memory.to_i * 1024
          @memory_backing = config.memory_backing
          @management_network_mac = config.management_network_mac
          @domain_volume_cache = config.volume_cache || 'default'
          @kernel = config.kernel
          @cmd_line = config.cmd_line
          @emulator_path = config.emulator_path
          @initrd = config.initrd
          @dtb = config.dtb
          @graphics_type = config.graphics_type
          @graphics_autoport = config.graphics_autoport
          @graphics_port = config.graphics_port
          @graphics_ip = config.graphics_ip
          @graphics_passwd = config.graphics_passwd
          @graphics_gl = config.graphics_gl
          @video_type = config.video_type
          @sound_type = config.sound_type
          @video_vram = config.video_vram
          @video_accel3d = config.video_accel3d
          @keymap = config.keymap
          @kvm_hidden = config.kvm_hidden

          @tpm_model = config.tpm_model
          @tpm_type = config.tpm_type
          @tpm_path = config.tpm_path
          @tpm_version = config.tpm_version

          # Boot order
          @boot_order = config.boot_order

          # Storage
          @storage_pool_name = config.storage_pool_name
          @domain_volumes = env[:domain_volumes] || []
          @disks = env[:disks] || []
          @cdroms = config.cdroms

          # Input
          @inputs = config.inputs

          # Channels
          @channels = config.channels

          # PCI device passthrough
          @pcis = config.pcis

          # Watchdog device
          @watchdog_dev = config.watchdog_dev

          # USB controller
          @usbctl_dev = config.usbctl_dev

          # USB device passthrough
          @usbs = config.usbs

          # Redirected devices
          @redirdevs = config.redirdevs
          @redirfilters = config.redirfilters

          # Additional QEMU commandline arguments
          @qemu_args = config.qemu_args

          # Additional QEMU commandline environment variables
          @qemu_env = config.qemu_env

          # smartcard device
          @smartcard_dev = config.smartcard_dev

          # RNG device passthrough
          @rng = config.rng

          # Memballoon
          @memballoon_enabled = config.memballoon_enabled
          @memballoon_model = config.memballoon_model
          @memballoon_pci_bus = config.memballoon_pci_bus
          @memballoon_pci_slot = config.memballoon_pci_slot

          @domain_type = config.driver

          @os_type = 'hvm'

          env[:domain_volumes].each_with_index do |vol, index|
            suffix_index = index > 0 ? "_#{index}" : ''
            domain_volume = env[:machine].provider.driver.connection.volumes.all(
              name: "#{@name}#{suffix_index}.img"
            ).find { |x| x.pool_name == vol[:pool] }
            raise Errors::NoDomainVolume if domain_volume.nil?
          end

          @disks.each do |disk|
            # make the disk. equivalent to:
            # qemu-img create -f qcow2 <path> 5g
            begin
              env[:machine].provider.driver.connection.volumes.create(
                name: disk[:name],
                format_type: disk[:type],
                path: disk[:absolute_path],
                capacity: disk[:size],
                owner: storage_uid(env),
                group: storage_gid(env),
                #:allocation => ?,
                pool_name: disk[:pool],
              )
            rescue Libvirt::Error => e
              # It is hard to believe that e contains just a string
              # and no useful error code!
              msgs = [disk[:name], disk[:absolute_path]].map do |name|
                "Call to virStorageVolCreateXML failed: " +
                "storage volume '#{name}' exists already"
              end
              if msgs.include?(e.message) and disk[:allow_existing]
                disk[:preexisting] = true
              else
                raise Errors::FogCreateDomainVolumeError,
                      error_message: e.message
              end
{'fraction_non_alphanumeric': 0.08471375847566733, 'fraction_numerical': 0.0022315681057419965, 'mean_word_length': 1.8970661362506216, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2129337', 'n_tokens_mistral': 3330, 'n_tokens_neox': 3252, 'n_words': 646}