<commit_before>require 'log4r'
require 'vagrant/util/network_ip'
require 'vagrant/util/scoped_hash_override'
require 'ipaddr'
require 'thread'

module VagrantPlugins
  module ProviderLibvirt
    module Action
      # Prepare all networks needed for domain connections.
      class CreateNetworks
        include Vagrant::Util::NetworkIP
        include Vagrant::Util::ScopedHashOverride
        include VagrantPlugins::ProviderLibvirt::Util::ErbTemplate
        include VagrantPlugins::ProviderLibvirt::Util::NetworkUtil

        @@lock = Mutex.new

        def initialize(app, env)
          mess = 'vagrant_libvirt::action::create_networks'
          @logger = Log4r::Logger.new(mess)
          @app = app

          @available_networks = []
          @options = {}
          @libvirt_client = env[:libvirt_compute].client
        end

        def call(env)
          # only one vm at a time should try to set up networks
          # otherwise they'll have inconsitent views of current state
          # and conduct redundant operations that cause errors
          @@lock.synchronize do
            # Iterate over networks If some network is not
            # available, create it if possible. Otherwise raise an error.
            configured_networks(env, @logger).each do |options|
              # Only need to create private networks
              next if options[:iface_type] != :private_network or
                options.fetch(:tcp_tunnel_type, nil)
              @logger.debug "Searching for network with options #{options}"

              # should fix other methods so this doesn't have to be instance var
              @options = options

              # Get a list of all (active and inactive) libvirt networks. This
              # list is used throughout this class and should be easier to
              # process than libvirt API calls.
              @available_networks = libvirt_networks(
                                      env[:libvirt_compute].client)

              # Prepare a hash describing network for this specific interface.
              @interface_network = {
                name:             nil,
                ip_address:       nil,
                netmask:          @options[:netmask],
                network_address:  nil,
                bridge_name:      nil,
                created:          false,
                active:           false,
                autostart:        false,
                libvirt_network:  nil
              }

              if @options[:ip]
                handle_ip_option(env)
              # in vagrant 1.2.3 and later it is not possible to take this branch
              # because cannot have name without ip
              # https://github.com/mitchellh/vagrant/commit/cf2f6da4dbcb4f57c9cdb3b94dcd0bba62c5f5fd
              elsif @options[:network_name]
                handle_network_name_option(env)
              end

              autostart_network if @interface_network[:autostart]
              activate_network if !@interface_network[:active]
            end
          end

          @app.call(env)
        end

        private

        def lookup_network_by_ip(ip)
          @logger.debug "looking up network with ip == #{ip}"
          @available_networks.each do |network|
            if network[:network_address] == ip
              @logger.debug "found existing network by ip: #{network}"
              return network
            end
          end
          nil
        end

        # Return hash of network for specified name, or nil if not found.
        def lookup_network_by_name(network_name)<commit_msg>Removed comment<commit_after>require 'log4r'
require 'vagrant/util/network_ip'
require 'vagrant/util/scoped_hash_override'
require 'ipaddr'
require 'thread'

module VagrantPlugins
  module ProviderLibvirt
    module Action
      # Prepare all networks needed for domain connections.
      class CreateNetworks
        include Vagrant::Util::NetworkIP
        include Vagrant::Util::ScopedHashOverride
        include VagrantPlugins::ProviderLibvirt::Util::ErbTemplate
        include VagrantPlugins::ProviderLibvirt::Util::NetworkUtil

        @@lock = Mutex.new

        def initialize(app, env)
          mess = 'vagrant_libvirt::action::create_networks'
          @logger = Log4r::Logger.new(mess)
          @app = app

          @available_networks = []
          @options = {}
          @libvirt_client = env[:libvirt_compute].client
        end

        def call(env)
          # only one vm at a time should try to set up networks
          # otherwise they'll have inconsitent views of current state
          # and conduct redundant operations that cause errors
          @@lock.synchronize do
            # Iterate over networks If some network is not
            # available, create it if possible. Otherwise raise an error.
            configured_networks(env, @logger).each do |options|
              # Only need to create private networks
              next if options[:iface_type] != :private_network or
                options.fetch(:tcp_tunnel_type, nil)
              @logger.debug "Searching for network with options #{options}"

              # should fix other methods so this doesn't have to be instance var
              @options = options

              # Get a list of all (active and inactive) libvirt networks. This
              # list is used throughout this class and should be easier to
              # process than libvirt API calls.
              @available_networks = libvirt_networks(
                                      env[:libvirt_compute].client)

              # Prepare a hash describing network for this specific interface.
              @interface_network = {
                name:             nil,
                ip_address:       nil,
                netmask:          @options[:netmask],
                network_address:  nil,
                bridge_name:      nil,
                created:          false,
                active:           false,
                autostart:        false,
                libvirt_network:  nil
              }

              if @options[:ip]
                handle_ip_option(env)
              elsif @options[:network_name]
                handle_network_name_option(env)
              end

              autostart_network if @interface_network[:autostart]
              activate_network if !@interface_network[:active]
            end
          end

          @app.call(env)
        end

        private

        def lookup_network_by_ip(ip)
          @logger.debug "looking up network with ip == #{ip}"
          @available_networks.each do |network|
            if network[:network_address] == ip
              @logger.debug "found existing network by ip: #{network}"
              return network
            end
          end
          nil
        end

        # Return hash of network for specified name, or nil if not found.
        def lookup_network_by_name(network_name)
{'fraction_non_alphanumeric': 0.06513299784327822, 'fraction_numerical': 0.0031631919482386773, 'mean_word_length': 1.727843137254902, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7049998', 'n_tokens_mistral': 1759, 'n_tokens_neox': 1687, 'n_words': 527}