<commit_before>module Cucumber
  module Chef

    class TestLabError < Error; end

    class TestLab
      attr_reader :connection, :server
      attr_accessor :stdout, :stderr, :stdin

      INVALID_STATES = ['terminated', 'shutting-down', 'starting-up', 'pending']
      RUNNING_STATES = ['running']
      SHUTDOWN_STATES = ['shutdown', 'stopping', 'stopped']
      VALID_STATES = RUNNING_STATES+SHUTDOWN_STATES

      def initialize(stdout=STDOUT, stderr=STDERR, stdin=STDIN)
        @stdout, @stderr, @stdin = stdout, stderr, stdin
        @stdout.sync = true if @stdout.respond_to?(:sync=)

        @connection = Fog::Compute.new(:provider => 'AWS',
                                       :aws_access_key_id => Cucumber::Chef::Config[:aws][:aws_access_key_id],
                                       :aws_secret_access_key => Cucumber::Chef::Config[:aws][:aws_secret_access_key],
                                       :region => Cucumber::Chef::Config[:aws][:region])
        ensure_security_group if Cucumber::Chef::Config[:aws][:security_group] == "cucumber-chef"
      end

################################################################################

      def create
        if labs_exist?
          @stdout.puts("A test lab already exists using the AWS credentials you have supplied; attempting to reprovision it.")
          @server = labs_running.first
        else
          server_definition = {
            :image_id => Cucumber::Chef::Config.aws_image_id,
            :groups => Cucumber::Chef::Config[:aws][:security_group],
            :flavor_id => Cucumber::Chef::Config[:aws][:aws_instance_type],
            :key_name => Cucumber::Chef::Config[:aws][:aws_ssh_key_id],
            :availability_zone => Cucumber::Chef::Config[:aws][:availability_zone],
            :tags => { "purpose" => "cucumber-chef", "cucumber-chef" => Cucumber::Chef::Config[:mode] },
            :identity_file => Cucumber::Chef::Config[:aws][:identity_file]
          }
          @server = @connection.servers.create(server_definition)
          @stdout.puts("Provisioning cucumber-chef test lab platform.")

          @stdout.print("Waiting for instance...")
          @server.wait_for { ready? }
          @stdout.puts("OK!\n")

          tag_server
        end

        info

        @stdout.print("Waiting for sshd...")
        begin
          @stdout.print(".")
          sleep(1)
        end until Cucumber::Chef::SSH.ready?(@server.public_ip_address)
        @stdout.puts("OK!\n")

        @stdout.puts("Instance provisioned!")

        @server
      end


      def destroy
        l = labs
        n = nodes
        c = clients

        if (l.count > 0)
          @stdout.puts("Destroying Servers:")
          l.each do |server|
            @stdout.puts("  * #{server.public_ip_address}")
            server.destroy
          end
        end

        if (n.count > 0)
          @stdout.puts("Destroying Chef Nodes:")
          n.each do |node|
            @stdout.puts("  * #{node.name}")
            node.destroy
          end
        end

        if (c.count > 0)
          @stdout.puts("Destroying Chef Clients:")
          c.each do |client|
            @stdout.puts("  * #{client.name}")
            client.destroy
          end
        end
      end

################################################################################

      def start
        # TODO: Implementation
      end


      def stop
        # TODO: Implementation
      end

################################################################################

      def info
        @stdout.puts("----------------------------------------------------------------------------")
        if labs_exist?
          labs.each do |lab|
            @stdout.puts("Instance ID: #{lab.id}")
            @stdout.puts("State: #{lab.state}")<commit_msg>always ensure secuity group for now<commit_after>module Cucumber
  module Chef

    class TestLabError < Error; end

    class TestLab
      attr_reader :connection, :server
      attr_accessor :stdout, :stderr, :stdin

      INVALID_STATES = ['terminated', 'shutting-down', 'starting-up', 'pending']
      RUNNING_STATES = ['running']
      SHUTDOWN_STATES = ['shutdown', 'stopping', 'stopped']
      VALID_STATES = RUNNING_STATES+SHUTDOWN_STATES

      def initialize(stdout=STDOUT, stderr=STDERR, stdin=STDIN)
        @stdout, @stderr, @stdin = stdout, stderr, stdin
        @stdout.sync = true if @stdout.respond_to?(:sync=)

        @connection = Fog::Compute.new(:provider => 'AWS',
                                       :aws_access_key_id => Cucumber::Chef::Config[:aws][:aws_access_key_id],
                                       :aws_secret_access_key => Cucumber::Chef::Config[:aws][:aws_secret_access_key],
                                       :region => Cucumber::Chef::Config[:aws][:region])
        ensure_security_group
      end

################################################################################

      def create
        if labs_exist?
          @stdout.puts("A test lab already exists using the AWS credentials you have supplied; attempting to reprovision it.")
          @server = labs_running.first
        else
          server_definition = {
            :image_id => Cucumber::Chef::Config.aws_image_id,
            :groups => Cucumber::Chef::Config[:aws][:security_group],
            :flavor_id => Cucumber::Chef::Config[:aws][:aws_instance_type],
            :key_name => Cucumber::Chef::Config[:aws][:aws_ssh_key_id],
            :availability_zone => Cucumber::Chef::Config[:aws][:availability_zone],
            :tags => { "purpose" => "cucumber-chef", "cucumber-chef" => Cucumber::Chef::Config[:mode] },
            :identity_file => Cucumber::Chef::Config[:aws][:identity_file]
          }
          @server = @connection.servers.create(server_definition)
          @stdout.puts("Provisioning cucumber-chef test lab platform.")

          @stdout.print("Waiting for instance...")
          @server.wait_for { ready? }
          @stdout.puts("OK!\n")

          tag_server
        end

        info

        @stdout.print("Waiting for sshd...")
        begin
          @stdout.print(".")
          sleep(1)
        end until Cucumber::Chef::SSH.ready?(@server.public_ip_address)
        @stdout.puts("OK!\n")

        @stdout.puts("Instance provisioned!")

        @server
      end


      def destroy
        l = labs
        n = nodes
        c = clients

        if (l.count > 0)
          @stdout.puts("Destroying Servers:")
          l.each do |server|
            @stdout.puts("  * #{server.public_ip_address}")
            server.destroy
          end
        end

#        if (n.count > 0)
#          @stdout.puts("Destroying Chef Nodes:")
#          n.each do |node|
#            @stdout.puts("  * #{node.name}")
#            node.destroy
#          end
#        end

#        if (c.count > 0)
#          @stdout.puts("Destroying Chef Clients:")
#          c.each do |client|
#            @stdout.puts("  * #{client.name}")
#            client.destroy
#          end
#        end
      end

################################################################################

      def start
        # TODO: Implementation
      end


      def stop
        # TODO: Implementation
      end

################################################################################

      def info
        @stdout.puts("----------------------------------------------------------------------------")
        if labs_exist?
          labs.each do |lab|
            @stdout.puts("Instance ID: #{lab.id}")
            @stdout.puts("State: #{lab.state}")
{'fraction_non_alphanumeric': 0.20301837270341208, 'fraction_numerical': 0.0010498687664041995, 'mean_word_length': 2.0398883127243717, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '790866', 'n_tokens_mistral': 2227, 'n_tokens_neox': 2122, 'n_words': 405}