<commit_before>module Kontena
  class HaproxyConfigGenerator
    include Celluloid
    include Celluloid::Logger
    include Celluloid::Notifications

    attr_reader :options, :config, :vhosts

    def initialize(opts)
      @options = opts
      @config = {}
      create_vhosts
      create_default_config
      subscribe 'backends_resolved', :update_config
    end

    def create_default_config
      config['global'] = [
        "maxconn %s" % options[:maxconn],
        "pidfile /var/run/haproxy.pid",
        "user haproxy",
        "group haproxy",
        "daemon",
        "stats socket /var/run/haproxy.stats level admin"
      ]
      config['defaults'] = [
        "mode %s" % options[:mode]
      ]
      if options[:option]
        options[:option].each do |option|
          config['defaults'] << 'option %s' % option.strip
        end
      end
      if options[:timeout]
        options[:timeout].each do |timeout|
          config['defaults'] << 'timeout %s' % timeout.strip
        end
      end
    end

    ##
    # @param [String] topic
    # @param [Hash] backends
    def update_config(topic, backends)
      create_frontend
      create_backend(backends)
      publish('haproxy:config_generated', config_to_string)
    end

    def create_frontend
      frontend = []
      frontend << 'bind 0.0.0.0:%s' % options[:frontend_port]
      if options[:ssl] != ''
        frontend << 'redirect scheme https code 301 if !{ ssl_fc }'
        frontend << 'bind 0.0.0.0:443 %s' % options[:ssl]
        frontend << 'reqadd X-Forwarded-Proto:\ https'
      end

      if options[:virtual_hosts]
        vhosts.each do |service, domain|
          id = service.downcase.sub('-', '_')
          frontend << 'acl host_%s hdr(host) -i %s' % [id, domain]
          frontend << 'use_backend %s_cluster if host_%s' % [id, id]
        end
      else
        frontend << 'default_backend default_service'
      end
      config['frontend default_frontend'] = frontend
    end

    ##
    # @param [Hash] backend_services
    def create_backend(backend_services)
      if options[:virtual_hosts] != ''
        create_virtual_host_backends(backend_services)
      else
        create_default_backend(backend_services)
      end
    end

    ##
    # @param [Hash] backend_services
    def create_virtual_host_backends(backend_services)
      vhosts.each do |service, domain|
        if backend_services[service]
          backend = []
          backend << 'balance %s' % options[:balance]
          i = 1
          backend_services[service].each do |host|
            backend << 'server %s %s:%s' % ["#{service}-#{i}", host[:ip], host[:port]]
            i += 1
          end
          id = service.downcase.sub('-', '_')
          config["backend %s_cluster" % id] = backend
        end
      end
    end

    ##
    # @param [Hash] backend_services
    def create_default_backend(backend_services)
      backend = []
      backend << 'balance %s' % options[:balance]
      backend_services.each do |service, hosts|
        i = 1
        hosts.each do |host|
          backend << 'server %s %s:%s' % ["#{service}-#{i}", host[:ip], host[:port]]
          i += 1
        end
      end<commit_msg>improve backend server name<commit_after>module Kontena
  class HaproxyConfigGenerator
    include Celluloid
    include Celluloid::Logger
    include Celluloid::Notifications

    attr_reader :options, :config, :vhosts

    def initialize(opts)
      @options = opts
      @config = {}
      create_vhosts
      create_default_config
      subscribe 'backends_resolved', :update_config
    end

    def create_default_config
      config['global'] = [
        "maxconn %s" % options[:maxconn],
        "pidfile /var/run/haproxy.pid",
        "user haproxy",
        "group haproxy",
        "daemon",
        "stats socket /var/run/haproxy.stats level admin"
      ]
      config['defaults'] = [
        "mode %s" % options[:mode]
      ]
      if options[:option]
        options[:option].each do |option|
          config['defaults'] << 'option %s' % option.strip
        end
      end
      if options[:timeout]
        options[:timeout].each do |timeout|
          config['defaults'] << 'timeout %s' % timeout.strip
        end
      end
    end

    ##
    # @param [String] topic
    # @param [Hash] backends
    def update_config(topic, backends)
      create_frontend
      create_backend(backends)
      publish('haproxy:config_generated', config_to_string)
    end

    def create_frontend
      frontend = []
      frontend << 'bind 0.0.0.0:%s' % options[:frontend_port]
      if options[:ssl] != ''
        frontend << 'redirect scheme https code 301 if !{ ssl_fc }'
        frontend << 'bind 0.0.0.0:443 %s' % options[:ssl]
        frontend << 'reqadd X-Forwarded-Proto:\ https'
      end

      if options[:virtual_hosts]
        vhosts.each do |service, domain|
          id = service.downcase.sub('-', '_')
          frontend << 'acl host_%s hdr(host) -i %s' % [id, domain]
          frontend << 'use_backend %s_cluster if host_%s' % [id, id]
        end
      else
        frontend << 'default_backend default_service'
      end
      config['frontend default_frontend'] = frontend
    end

    ##
    # @param [Hash] backend_services
    def create_backend(backend_services)
      if options[:virtual_hosts] != ''
        create_virtual_host_backends(backend_services)
      else
        create_default_backend(backend_services)
      end
    end

    ##
    # @param [Hash] backend_services
    def create_virtual_host_backends(backend_services)
      vhosts.each do |service, domain|
        if backend_services[service]
          backend = []
          backend << 'balance %s' % options[:balance]
          service_name = service.gsub('.', '-')
          i = 1
          backend_services[service].each do |host|
            backend << 'server %s %s:%s' % ["#{service_name}-#{i}", host[:ip], host[:port]]
            i += 1
          end
          id = service.downcase.sub('-', '_')
          config["backend %s_cluster" % id] = backend
        end
      end
    end

    ##
    # @param [Hash] backend_services
    def create_default_backend(backend_services)
      backend = []
      backend << 'balance %s' % options[:balance]
      backend_services.each do |service, hosts|
        service_name = service.gsub('.', '-')
        i = 1
        hosts.each do |host|
          backend << 'server %s %s:%s' % ["#{service_name}-#{i}", host[:ip], host[:port]]
          i += 1
        end
      end
{'fraction_non_alphanumeric': 0.12430939226519337, 'fraction_numerical': 0.0055248618784530384, 'mean_word_length': 2.2166831194471865, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '5922810', 'n_tokens_mistral': 2016, 'n_tokens_neox': 1961, 'n_words': 458}