<commit_before>require 'chef/mixin/shell_out'
require 'chef/provisioning/driver'
require 'chef/provisioning/convergence_strategy/install_cached'
require 'chef/provisioning/convergence_strategy/install_sh'
require 'chef/provisioning/convergence_strategy/install_msi'
require 'chef/provisioning/convergence_strategy/no_converge'
require 'chef/provisioning/transport/ssh'
require 'chef/provisioning/transport/winrm'
require 'chef/provisioning/machine/windows_machine'
require 'chef/provisioning/machine/unix_machine'
require 'chef/provisioning/machine_spec'

require 'chef/provider/aws_key_pair'
require 'chef/resource/aws_key_pair'
require 'chef/provisioning/aws_driver/version'
require 'chef/provisioning/aws_driver/credentials'

require 'yaml'
require 'aws-sdk-v1'

# loads the entire aws-sdk
AWS.eager_autoload!

class Chef
module Provisioning
module AWSDriver
  # Provisions machines using the AWS SDK
  class Driver < Chef::Provisioning::Driver

    include Chef::Mixin::ShellOut

    attr_reader :aws_config

    # URL scheme:
    # aws:profilename:region
    # TODO: migration path from fog:AWS - parse that URL
    # canonical URL calls realpath on <path>
    def self.from_url(driver_url, config)
      Driver.new(driver_url, config)
    end

    def initialize(driver_url, config)
      super

      _, profile_name, region = driver_url.split(':')
      profile_name = nil if profile_name && profile_name.empty?
      region = nil if region && region.empty?

      credentials = profile_name ? aws_credentials[profile_name] : aws_credentials.default
      @aws_config = AWS::Core::Configuration.new(
        access_key_id:     credentials[:aws_access_key_id],
        secret_access_key: credentials[:aws_secret_access_key],
        region: region || credentials[:region]
      )
    end

    def self.canonicalize_url(driver_url, config)
      [ driver_url, config ]
    end

    # Load balancer methods
    def allocate_load_balancer(action_handler, lb_spec, lb_options, machine_specs)
      lb_options ||= {}
      if lb_options[:security_group_id]
        security_group = ec2.security_groups[:security_group_id]
      elsif lb_options[:security_group_name]
        security_group = ec2.security_groups.filter('group-name', lb_options[:security_group_name]).first
      end

      availability_zones = lb_options[:availability_zones]
      listeners = lb_options[:listeners]

      validate_listeners(listeners)

      lb_optionals = {}
      lb_optionals[:security_groups] = [security_group] if security_group
      lb_optionals[:availability_zones] = availability_zones if availability_zones
      lb_optionals[:listeners] = listeners if listeners

      actual_elb = load_balancer_for(lb_spec)
      if !actual_elb.exists?
        perform_action = proc { |desc, &block| action_handler.perform_action(desc, &block) }

        updates = [ "Create load balancer #{lb_spec.name} in #{aws_config.region}" ]
        updates << "  enable availability zones #{availability_zones.join(', ')}" if availability_zones && availability_zones.size > 0
        updates << "  with listeners #{listeners.join(', ')}" if listeners && listeners.size > 0
        updates << "  with security group #{security_group.name}" if security_group

        action_handler.perform_action updates do
          actual_elb = elb.load_balancers.create(lb_spec.name, lb_optionals)

          lb_spec.location = {
            'driver_url' => driver_url,
            'driver_version' => Chef::Provisioning::AWSDriver::VERSION,
            'allocated_at' => Time.now.utc.to_s,
          }
        end
      else
        # Header gets printed the first time we make an update
        perform_action = proc do |desc, &block|<commit_msg>Add scheme and subnets options<commit_after>require 'chef/mixin/shell_out'
require 'chef/provisioning/driver'
require 'chef/provisioning/convergence_strategy/install_cached'
require 'chef/provisioning/convergence_strategy/install_sh'
require 'chef/provisioning/convergence_strategy/install_msi'
require 'chef/provisioning/convergence_strategy/no_converge'
require 'chef/provisioning/transport/ssh'
require 'chef/provisioning/transport/winrm'
require 'chef/provisioning/machine/windows_machine'
require 'chef/provisioning/machine/unix_machine'
require 'chef/provisioning/machine_spec'

require 'chef/provider/aws_key_pair'
require 'chef/resource/aws_key_pair'
require 'chef/provisioning/aws_driver/version'
require 'chef/provisioning/aws_driver/credentials'

require 'yaml'
require 'aws-sdk-v1'

# loads the entire aws-sdk
AWS.eager_autoload!

class Chef
module Provisioning
module AWSDriver
  # Provisions machines using the AWS SDK
  class Driver < Chef::Provisioning::Driver

    include Chef::Mixin::ShellOut

    attr_reader :aws_config

    # URL scheme:
    # aws:profilename:region
    # TODO: migration path from fog:AWS - parse that URL
    # canonical URL calls realpath on <path>
    def self.from_url(driver_url, config)
      Driver.new(driver_url, config)
    end

    def initialize(driver_url, config)
      super

      _, profile_name, region = driver_url.split(':')
      profile_name = nil if profile_name && profile_name.empty?
      region = nil if region && region.empty?

      credentials = profile_name ? aws_credentials[profile_name] : aws_credentials.default
      @aws_config = AWS::Core::Configuration.new(
        access_key_id:     credentials[:aws_access_key_id],
        secret_access_key: credentials[:aws_secret_access_key],
        region: region || credentials[:region]
      )
    end

    def self.canonicalize_url(driver_url, config)
      [ driver_url, config ]
    end

    # Load balancer methods
    def allocate_load_balancer(action_handler, lb_spec, lb_options, machine_specs)
      lb_options ||= {}
      if lb_options[:security_group_id]
        security_group = ec2.security_groups[:security_group_id]
      elsif lb_options[:security_group_name]
        security_group = ec2.security_groups.filter('group-name', lb_options[:security_group_name]).first
      end

      availability_zones = lb_options[:availability_zones]
      listeners = lb_options[:listeners]
      subnets = lb_options[:subnets]
      scheme = lb_options[:scheme]

      validate_listeners(listeners)

      lb_optionals = {}
      lb_optionals[:security_groups] = [security_group] if security_group
      lb_optionals[:availability_zones] = availability_zones if availability_zones
      lb_optionals[:listeners] = listeners if listeners
      lb_optionals[:subnets] = subnets if subnets
      lb_optionals[:scheme] = scheme if scheme

      actual_elb = load_balancer_for(lb_spec)
      if !actual_elb.exists?
        perform_action = proc { |desc, &block| action_handler.perform_action(desc, &block) }

        updates = [ "Create load balancer #{lb_spec.name} in #{aws_config.region}" ]
        updates << "  enable availability zones #{availability_zones.join(', ')}" if availability_zones && availability_zones.size > 0
        updates << "  with listeners #{listeners.join(', ')}" if listeners && listeners.size > 0
        updates << "  with security group #{security_group.name}" if security_group

        action_handler.perform_action updates do
          actual_elb = elb.load_balancers.create(lb_spec.name, lb_optionals)

          lb_spec.location = {
            'driver_url' => driver_url,
            'driver_version' => Chef::Provisioning::AWSDriver::VERSION,
            'allocated_at' => Time.now.utc.to_s,
          }
        end
      else
        # Header gets printed the first time we make an update
        perform_action = proc do |desc, &block|
{'fraction_non_alphanumeric': 0.09442398737506576, 'fraction_numerical': 0.0013150973172014729, 'mean_word_length': 3.834710743801653, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1072588', 'n_tokens_mistral': 2371, 'n_tokens_neox': 2319, 'n_words': 467}