<commit_before>#!/bin/bash
# called when confd detects changes
ME=$(basename $0)
IN=/tmp/backends.txt
OUT=/etc/haproxy/haproxy.cfg
BASE=/etc/haproxy/haproxy-base.cfg
TMP=/tmp/haproxy-tmp.cfg

. /tmp/SLING_ENV.sh

function std_listen_opts() {
  echo "  balance roundrobin"
  echo "  option httpclose"
  echo "  option forwardfor"
}

function enable_stats() {
    echo "  stats enable"
    echo "  stats uri $1"
    echo "  stats show-desc Sling adaptTo 2016 demo"
}

# generate list of Sling servers with roles matching supplied regexp
function generate_servers() {
  # $1:role selection regexp
  # $2:egrep options
  echo "  #  sling servers selected using regexp '${1}' with options '${2}'"
  S_ARRAY=$(grep -v '^ *$' < $IN | sort | awk -F# '{ print $1 ":" $2 ":" $3}' | sort -u | egrep ${1} ${2})
  for i in ${S_ARRAY[@]} 
  do
    set $(echo $i | tr ':' ' ')
    # $1:role, $2:IP, $3:port
    echo "  server sling-$1-$2:$3 $2:$3 check"
  done
}

function generate_config() {
	
	echo
	echo "# haproxy config generated by $ME using SLING_FRONTEND_ROLE_REGEXP=${SLING_FRONTEND_ROLE_REGEXP}"
	echo "# from the Sling instance information registered in etcd, read via $IN"

    echo	
	echo "# client requests hit the Sling resolvers"
    echo "frontend http_80"
	echo "  bind 0.0.0.0:80"
	echo "  default_backend sling_resolvers_loadbalancer"
	echo
	echo "backend sling_resolvers_loadbalancer"
	enable_stats /haproxy/stats
	std_listen_opts
    generate_servers ${SLING_FRONTEND_ROLE_REGEXP}

    echo	
	echo "# Sling worker instances, selected by a Sling-Worker-Role header"
    echo "frontend workers_81"
	echo "  bind 0.0.0.0:81"
	echo "  # TODO default backend role should be provided by environment"
	echo "  # TODO currently this backend is not found until default service is up"
	echo "  default_backend default_worker_loadbalancer"
	
    # ACLs and backends for per-role routing
	ROLES=$(grep -v '^ *$' < $IN | awk -F# '{ print $1}' | sort | egrep -v ${SLING_FRONTEND_ROLE_REGEXP})
	for r in $ROLES
	do
      echo
      echo "  acl hdr_role_${r} hdr_beg(Sling-Worker-Role) -i ${r}"
	  echo "  use_backend ${r}_worker_loadbalancer if hdr_role_${r}"
	done
	
	for r in $ROLES
	do
      echo
	  echo "backend ${r}_worker_loadbalancer"
      std_listen_opts "/" "Sling workers for role ${r}"
	  generate_servers ${r}
	done
	
	echo
}<commit_msg>Fix haproxy for multiple processors in the same role<commit_after>#!/bin/bash
# called when confd detects changes
ME=$(basename $0)
IN=/tmp/backends.txt
OUT=/etc/haproxy/haproxy.cfg
BASE=/etc/haproxy/haproxy-base.cfg
TMP=/tmp/haproxy-tmp.cfg

. /tmp/SLING_ENV.sh

function std_listen_opts() {
  echo "  balance roundrobin"
  echo "  option httpclose"
  echo "  option forwardfor"
}

function enable_stats() {
    echo "  stats enable"
    echo "  stats uri $1"
    echo "  stats show-desc Sling adaptTo 2016 demo"
}

# generate list of Sling servers with roles matching supplied regexp
function generate_servers() {
  # $1:role selection regexp
  # $2:egrep options
  echo "  #  sling servers selected using regexp '${1}' with options '${2}'"
  S_ARRAY=$(grep -v '^ *$' < $IN | sort | awk -F# '{ print $1 ":" $2 ":" $3}' | sort -u | egrep ${1} ${2})
  for i in ${S_ARRAY[@]} 
  do
    set $(echo $i | tr ':' ' ')
    # $1:role, $2:IP, $3:port
    echo "  server sling-$1-$2:$3 $2:$3 check"
  done
}

function generate_config() {
	
	echo
	echo "# haproxy config generated by $ME using SLING_FRONTEND_ROLE_REGEXP=${SLING_FRONTEND_ROLE_REGEXP}"
	echo "# from the Sling instance information registered in etcd, read via $IN"

    echo	
	echo "# client requests hit the Sling resolvers"
    echo "frontend http_80"
	echo "  bind 0.0.0.0:80"
	echo "  default_backend sling_resolvers_loadbalancer"
	echo
	echo "backend sling_resolvers_loadbalancer"
	enable_stats /haproxy/stats
	std_listen_opts
    generate_servers ${SLING_FRONTEND_ROLE_REGEXP}

    echo	
	echo "# Sling worker instances, selected by a Sling-Worker-Role header"
    echo "frontend workers_81"
	echo "  bind 0.0.0.0:81"
	echo "  # TODO default backend role should be provided by environment"
	echo "  # TODO currently this backend is not found until default service is up"
	echo "  default_backend default_worker_loadbalancer"
	
    # ACLs and backends for per-role routing
	ROLES=$(grep -v '^ *$' < $IN | awk -F# '{ print $1}' | sort -u | egrep -v ${SLING_FRONTEND_ROLE_REGEXP})
	for r in $ROLES
	do
      echo
      echo "  acl hdr_role_${r} hdr_beg(Sling-Worker-Role) -i ${r}"
	  echo "  use_backend ${r}_worker_loadbalancer if hdr_role_${r}"
	done
	
	for r in $ROLES
	do
      echo
	  echo "backend ${r}_worker_loadbalancer"
      std_listen_opts "/" "Sling workers for role ${r}"
	  generate_servers ${r}
	done
	
	echo
}
{'fraction_non_alphanumeric': 0.1199238578680203, 'fraction_numerical': 0.01692047377326565, 'mean_word_length': 3.5957240038872693, 'pattern_counts': {'":': 4, '<': 7, '<?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': '6622496', 'n_tokens_mistral': 1799, 'n_tokens_neox': 1728, 'n_words': 499}