<commit_before>require 'benchmark'
require 'securerandom'
require 'bosh/director/config'
require 'bosh/dev/sandbox/service'
require 'bosh/dev/sandbox/socket_connector'
require 'bosh/dev/sandbox/postgresql'
require 'bosh/dev/sandbox/mysql'
require 'bosh/dev/sandbox/nginx'
require 'bosh/dev/sandbox/workspace'
require 'bosh/dev/sandbox/director_config'
require 'bosh/dev/sandbox/port_provider'
require 'bosh/dev/sandbox/services/director_service'
require 'bosh/dev/sandbox/services/nginx_service'
require 'bosh/dev/sandbox/services/connection_proxy_service'
require 'cloud/dummy'
require 'logging'

module Bosh::Dev::Sandbox
  class Main
    REPO_ROOT = File.expand_path('../../../../../', File.dirname(__FILE__))

    ASSETS_DIR = File.expand_path('bosh-dev/assets/sandbox', REPO_ROOT)

    UAA_ASSETS_DIR = File.expand_path('spec/assets/uaa', REPO_ROOT)
    UAA_CONFIG_DIR = File.expand_path('spec/assets', REPO_ROOT)

    DIRECTOR_CONFIG = 'director_test.yml'

    REDIS_CONFIG = 'redis_test.conf'
    REDIS_CONF_TEMPLATE = File.join(ASSETS_DIR, 'redis_test.conf.erb')

    HM_CONFIG = 'health_monitor.yml'
    HM_CONF_TEMPLATE = File.join(ASSETS_DIR, 'health_monitor.yml.erb')

    EXTERNAL_CPI = 'cpi'
    EXTERNAL_CPI_TEMPLATE = File.join(ASSETS_DIR, 'cpi.erb')

    attr_reader :name
    attr_reader :health_monitor_process
    attr_reader :scheduler_process

    attr_reader :director_service
    attr_reader :port_provider

    attr_reader :postgres_proxy

    alias_method :db_name, :name
    attr_reader :blobstore_storage_dir

    attr_reader :logs_path

    attr_reader :cpi

    attr_reader :nats_log_path

    def self.from_env
      db_opts = {
        type: ENV['DB'] || 'postgresql',
        user: ENV['TRAVIS'] ? 'travis' : 'root',
        password: ENV['TRAVIS'] ? '' : 'password',
      }

      new(
        db_opts,
        ENV['DEBUG'],
        ENV['TEST_ENV_NUMBER'].to_i,
        Logging.logger(STDOUT),
      )
    end

    def initialize(db_opts, debug, test_env_number, logger)
      @debug = debug
      @logger = logger
      @name = SecureRandom.uuid.gsub('-', '')

      @port_provider = PortProvider.new(test_env_number)

      @logs_path = sandbox_path('logs')
      @dns_db_path = sandbox_path('director-dns.sqlite')
      @task_logs_dir = sandbox_path('boshdir/tasks')
      @blobstore_storage_dir = sandbox_path('bosh_test_blobstore')

      FileUtils.mkdir_p(@logs_path)

      setup_redis
      setup_nats

      @nginx_service = NginxService.new(sandbox_root, director_port, director_ruby_port, uaa_port, @logger)

      # all postgres connections go through this proxy (for testing automatic reconnect)
      @postgres_proxy = ConnectionProxyService.new("127.0.0.1", 5432, @port_provider.get_port(:postgres), @logger)

      director_config = sandbox_path(DirectorService::DIRECTOR_CONFIG)
      director_tmp_path = sandbox_path('boshdir')
      @director_service = DirectorService.new(director_ruby_port, redis_port, base_log_path, director_tmp_path, director_config, @logger)
      setup_heath_monitor

      @scheduler_process = Service.new(
        %W[bosh-director-scheduler -c #{director_config}],
        {output: "#{base_log_path}.scheduler.out"},
        @logger,
      )

      setup_database(db_opts)

      setup_uaa

      # Note that this is not the same object
      # as dummy cpi used inside bosh-director process
      @cpi = Bosh::Clouds::Dummy.new(
        'dir' => cloud_storage_dir
      )
      reconfigure({})
    end

    def agent_tmp_path
      cloud_storage_dir
    end

    def sandbox_path(path)
      File.join(sandbox_root, path)
    end

    def start
      @logger.info("Debug logs are saved to #{saved_logs_path}")
      setup_sandbox_root

      FileUtils.mkdir_p(cloud_storage_dir)
      FileUtils.rm_rf(logs_path)
      FileUtils.mkdir_p(logs_path)

      @postgres_proxy.start

      @redis_process.start
      @redis_socket_connector.try_to_connect

      @nginx_service.start

      @nats_process.start
      @nats_socket_connector.try_to_connect

      @database.create_db
      @database_created = true

      start_uaa if @user_authentication == 'uaa'

      @director_service.start(director_config)
    end<commit_msg>Increase number of attempts for NATs connection in integration suite.<commit_after>require 'benchmark'
require 'securerandom'
require 'bosh/director/config'
require 'bosh/dev/sandbox/service'
require 'bosh/dev/sandbox/socket_connector'
require 'bosh/dev/sandbox/postgresql'
require 'bosh/dev/sandbox/mysql'
require 'bosh/dev/sandbox/nginx'
require 'bosh/dev/sandbox/workspace'
require 'bosh/dev/sandbox/director_config'
require 'bosh/dev/sandbox/port_provider'
require 'bosh/dev/sandbox/services/director_service'
require 'bosh/dev/sandbox/services/nginx_service'
require 'bosh/dev/sandbox/services/connection_proxy_service'
require 'cloud/dummy'
require 'logging'

module Bosh::Dev::Sandbox
  class Main
    REPO_ROOT = File.expand_path('../../../../../', File.dirname(__FILE__))

    ASSETS_DIR = File.expand_path('bosh-dev/assets/sandbox', REPO_ROOT)

    UAA_ASSETS_DIR = File.expand_path('spec/assets/uaa', REPO_ROOT)
    UAA_CONFIG_DIR = File.expand_path('spec/assets', REPO_ROOT)

    DIRECTOR_CONFIG = 'director_test.yml'

    REDIS_CONFIG = 'redis_test.conf'
    REDIS_CONF_TEMPLATE = File.join(ASSETS_DIR, 'redis_test.conf.erb')

    HM_CONFIG = 'health_monitor.yml'
    HM_CONF_TEMPLATE = File.join(ASSETS_DIR, 'health_monitor.yml.erb')

    EXTERNAL_CPI = 'cpi'
    EXTERNAL_CPI_TEMPLATE = File.join(ASSETS_DIR, 'cpi.erb')

    attr_reader :name
    attr_reader :health_monitor_process
    attr_reader :scheduler_process

    attr_reader :director_service
    attr_reader :port_provider

    attr_reader :postgres_proxy

    alias_method :db_name, :name
    attr_reader :blobstore_storage_dir

    attr_reader :logs_path

    attr_reader :cpi

    attr_reader :nats_log_path

    def self.from_env
      db_opts = {
        type: ENV['DB'] || 'postgresql',
        user: ENV['TRAVIS'] ? 'travis' : 'root',
        password: ENV['<PASSWORD>'] ? '' : 'password',
      }

      new(
        db_opts,
        ENV['DEBUG'],
        ENV['TEST_ENV_NUMBER'].to_i,
        Logging.logger(STDOUT),
      )
    end

    def initialize(db_opts, debug, test_env_number, logger)
      @debug = debug
      @logger = logger
      @name = SecureRandom.uuid.gsub('-', '')

      @port_provider = PortProvider.new(test_env_number)

      @logs_path = sandbox_path('logs')
      @dns_db_path = sandbox_path('director-dns.sqlite')
      @task_logs_dir = sandbox_path('boshdir/tasks')
      @blobstore_storage_dir = sandbox_path('bosh_test_blobstore')

      FileUtils.mkdir_p(@logs_path)

      setup_redis
      setup_nats

      @nginx_service = NginxService.new(sandbox_root, director_port, director_ruby_port, uaa_port, @logger)

      # all postgres connections go through this proxy (for testing automatic reconnect)
      @postgres_proxy = ConnectionProxyService.new("127.0.0.1", 5432, @port_provider.get_port(:postgres), @logger)

      director_config = sandbox_path(DirectorService::DIRECTOR_CONFIG)
      director_tmp_path = sandbox_path('boshdir')
      @director_service = DirectorService.new(director_ruby_port, redis_port, base_log_path, director_tmp_path, director_config, @logger)
      setup_heath_monitor

      @scheduler_process = Service.new(
        %W[bosh-director-scheduler -c #{director_config}],
        {output: "#{base_log_path}.scheduler.out"},
        @logger,
      )

      setup_database(db_opts)

      setup_uaa

      # Note that this is not the same object
      # as dummy cpi used inside bosh-director process
      @cpi = Bosh::Clouds::Dummy.new(
        'dir' => cloud_storage_dir
      )
      reconfigure({})
    end

    def agent_tmp_path
      cloud_storage_dir
    end

    def sandbox_path(path)
      File.join(sandbox_root, path)
    end

    def start
      @logger.info("Debug logs are saved to #{saved_logs_path}")
      setup_sandbox_root

      FileUtils.mkdir_p(cloud_storage_dir)
      FileUtils.rm_rf(logs_path)
      FileUtils.mkdir_p(logs_path)

      @postgres_proxy.start

      @redis_process.start
      @redis_socket_connector.try_to_connect

      @nginx_service.start

      @nats_process.start
      @nats_socket_connector.try_to_connect(60)

      @database.create_db
      @database_created = true

      start_uaa if @user_authentication == 'uaa'

      @director_service.start(director_config)
    end
{'fraction_non_alphanumeric': 0.10658640226628895, 'fraction_numerical': 0.0025967894239848914, 'mean_word_length': 3.4997344662772174, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '2632617', 'n_tokens_mistral': 3048, 'n_tokens_neox': 2855, 'n_words': 472}