<commit_before>require "log4r"

require 'vagrant/util/retryable'

require 'vagrant-aws/util/timer'

module VagrantPlugins
  module AWS
    module Action
      # This runs the configured instance.
      class RunInstance
        include Vagrant::Util::Retryable

        def initialize(app, env)
          @app    = app
          @logger = Log4r::Logger.new("vagrant_aws::action::run_instance")
        end

        def call(env)
          # Initialize metrics if they haven't been
          env[:metrics] ||= {}

          # Get the region we're going to booting up in
          region = env[:machine].provider_config.region

          # Get the configs
          region_config      = env[:machine].provider_config.get_region_config(region)
          ami                = region_config.ami
          availability_zone  = region_config.availability_zone
          instance_type      = region_config.instance_type
          keypair            = region_config.keypair_name
          private_ip_address = region_config.private_ip_address
          security_groups    = region_config.security_groups
          subnet_id          = region_config.subnet_id
          tags               = region_config.tags

          # If there is no keypair then warn the user
          if !keypair
            env[:ui].warn(I18n.t("vagrant_aws.launch_no_keypair"))
          end

          # If there is a subnet ID then warn the user
          if subnet_id
            env[:ui].warn(I18n.t("vagrant_aws.launch_vpc_warning"))
          end

          # Launch!
          env[:ui].info(I18n.t("vagrant_aws.launching_instance"))
          env[:ui].info(" -- Type: #{instance_type}")
          env[:ui].info(" -- AMI: #{ami}")
          env[:ui].info(" -- Region: #{region}")
          env[:ui].info(" -- Availability Zone: #{availability_zone}") if availability_zone
          env[:ui].info(" -- Keypair: #{keypair}") if keypair
          env[:ui].info(" -- Subnet ID: #{subnet_id}") if subnet_id
          env[:ui].info(" -- Private IP: #{private_ip_address}") if private_ip_address
          env[:ui].info(" -- Security Groups: #{security_groups.inspect}") if !security_groups.empty?

          begin
            options = {
              :availability_zone  => availability_zone,
              :flavor_id          => instance_type,
              :image_id           => ami,
              :key_name           => keypair,
              :private_ip_address => private_ip_address,
              :subnet_id          => subnet_id,
              :tags               => tags
            }

            if !security_groups.empty?
              security_group_key = options[:subnet_id].nil? ? :groups : :security_group_ids
              options[security_group_key] = security_groups
            end

            server = env[:aws_compute].servers.create(options)
          rescue Fog::Compute::AWS::NotFound => e
            # Invalid subnet doesn't have its own error so we catch and
            # check the error message here.
            if e.message =~ /subnet ID/
              raise Errors::FogError,
                :message => "Subnet ID not found: #{subnet_id}"
            end

            raise<commit_msg>Passing region to server connection<commit_after>require "log4r"

require 'vagrant/util/retryable'

require 'vagrant-aws/util/timer'

module VagrantPlugins
  module AWS
    module Action
      # This runs the configured instance.
      class RunInstance
        include Vagrant::Util::Retryable

        def initialize(app, env)
          @app    = app
          @logger = Log4r::Logger.new("vagrant_aws::action::run_instance")
        end

        def call(env)
          # Initialize metrics if they haven't been
          env[:metrics] ||= {}

          # Get the region we're going to booting up in
          region = env[:machine].provider_config.region

          # Get the configs
          region_config      = env[:machine].provider_config.get_region_config(region)
          ami                = region_config.ami
          availability_zone  = region_config.availability_zone
          instance_type      = region_config.instance_type
          keypair            = region_config.keypair_name
          private_ip_address = region_config.private_ip_address
          security_groups    = region_config.security_groups
          subnet_id          = region_config.subnet_id
          tags               = region_config.tags

          # If there is no keypair then warn the user
          if !keypair
            env[:ui].warn(I18n.t("vagrant_aws.launch_no_keypair"))
          end

          # If there is a subnet ID then warn the user
          if subnet_id
            env[:ui].warn(I18n.t("vagrant_aws.launch_vpc_warning"))
          end

          # Launch!
          env[:ui].info(I18n.t("vagrant_aws.launching_instance"))
          env[:ui].info(" -- Type: #{instance_type}")
          env[:ui].info(" -- AMI: #{ami}")
          env[:ui].info(" -- Region: #{region}")
          env[:ui].info(" -- Availability Zone: #{availability_zone}") if availability_zone
          env[:ui].info(" -- Keypair: #{keypair}") if keypair
          env[:ui].info(" -- Subnet ID: #{subnet_id}") if subnet_id
          env[:ui].info(" -- Private IP: #{private_ip_address}") if private_ip_address
          env[:ui].info(" -- Security Groups: #{security_groups.inspect}") if !security_groups.empty?

          begin
            options = {
              :availability_zone  => availability_zone,
              :flavor_id          => instance_type,
              :image_id           => ami,
              :region             => region,
              :key_name           => keypair,
              :private_ip_address => private_ip_address,
              :subnet_id          => subnet_id,
              :tags               => tags
            }

            if !security_groups.empty?
              security_group_key = options[:subnet_id].nil? ? :groups : :security_group_ids
              options[security_group_key] = security_groups
            end

            server = env[:aws_compute].servers.create(options)
          rescue Fog::Compute::AWS::NotFound => e
            # Invalid subnet doesn't have its own error so we catch and
            # check the error message here.
            if e.message =~ /subnet ID/
              raise Errors::FogError,
                :message => "Subnet ID not found: #{subnet_id}"
            end

            raise
{'fraction_non_alphanumeric': 0.10091600683123739, 'fraction_numerical': 0.0024840863219996894, 'mean_word_length': 1.8081952920662598, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, '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': '5548945', 'n_tokens_mistral': 1810, 'n_tokens_neox': 1761, 'n_words': 399}