<commit_before>import os
import time
import requests
import subprocess
from collections import defaultdict

def id_to_backend(appId):
	return appId.replace('/', '_')

def id_to_url(appId):
	return ".".join(reversed(appId[1:].split('/')))

def create_config(marathon_url, login, password):
	apps = requests.get('http://%s/v2/apps' % marathon_url, auth=(login, password)).json()
	apps_to_load_balance = []
	for app in apps['apps']:
		if 'portMappings' in app['container']['docker'] and app['container']['docker']['portMappings'] != None:
			for portMapping in app['container']['docker']['portMappings']:
				if portMapping['containerPort'] == 80:
					apps_to_load_balance.append(app['id'])

	tasks = requests.get('http://%s/v2/tasks' % marathon_url, headers={'Accept': 'application/json'}, auth=(login, password)).json()
	urls_per_app = defaultdict(list)
	for task in tasks["tasks"]:
		if task["appId"] in apps_to_load_balance:
			url = "%s:%s" % (task["host"], task["ports"][0])
			urls_per_app[task["appId"]].append(url)

	config = """
global
	log 127.0.0.1   local0
	log 127.0.0.1   local1 notice
	maxconn 4096
	user haproxy
	group haproxy

defaults
	log     global
	mode    http
	option  httplog
	option  dontlognull
	option forwardfor
	option http-server-close
	timeout connect		10000
	timeout client		60000
	timeout server		60000

frontend http-in
	bind *:80

	acl demo hdr(host) -i demo.cloudasr.com
	redirect location http://www.cloudasr.com/demo code 301 if demo
	acl no_www hdr(host) -i cloudasr.com
	redirect prefix http://www.cloudasr.com code 301 if no_www
"""

	for appId in urls_per_app.keys():
		config += "	acl host%s	hdr(host) -i %s\n" % (id_to_backend(appId), id_to_url(appId))

	for appId in urls_per_app.keys():
		config += "	use_backend %s if host%s\n" % (id_to_backend(appId), id_to_backend(appId))

	for (appId, urls) in urls_per_app.iteritems():
		config += """
backend %s
	balance leastconn
	option httpclose
	option forwardfor
	cookie JSESSIONID prefix
	cookie SERVERID insert indirect nocache
""" % (id_to_backend(appId))

	for (i, url) in enumerate(urls):
		config += "	server node%d %s cookie A check\n" % (i, url)

	return config

if __name__ == '__main__':
	marathon_url = os.environ.get('MARATHON_URL')
	login = os.environ.get('MARATHON_LOGIN', None)
	password = <PASSWORD>.get('MARATHON_PASSWORD', None)

	last_config = None
	while True:
		config = create_config(marathon_url, login, password)

		if last_config != config:
			with open('/etc/haproxy/haproxy.cfg', 'w') as f:
				f.write(config)

			print "%s Configuration changed" % time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
			subprocess.call(("service", "haproxy", "reload"))
			last_config = config

		time.sleep(10)

<commit_msg>Bug fixed.<commit_after>import os
import time
import requests
import subprocess
from collections import defaultdict

def id_to_backend(appId):
	return appId.replace('/', '_')

def id_to_url(appId):
	return ".".join(reversed(appId[1:].split('/')))

def create_config(marathon_url, login, password):
	apps = requests.get('http://%s/v2/apps' % marathon_url, auth=(login, password)).json()
	apps_to_load_balance = []
	for app in apps['apps']:
		if 'portMappings' in app['container']['docker'] and app['container']['docker']['portMappings'] != None:
			for portMapping in app['container']['docker']['portMappings']:
				if portMapping['containerPort'] == 80:
					apps_to_load_balance.append(app['id'])

	tasks = requests.get('http://%s/v2/tasks' % marathon_url, headers={'Accept': 'application/json'}, auth=(login, password)).json()
	urls_per_app = defaultdict(list)
	for task in tasks["tasks"]:
		if task["appId"] in apps_to_load_balance:
			url = "%s:%s" % (task["host"], task["ports"][0])
			urls_per_app[task["appId"]].append(url)

	config = """
global
	log 127.0.0.1   local0
	log 127.0.0.1   local1 notice
	maxconn 4096
	user haproxy
	group haproxy

defaults
	log     global
	mode    http
	option  httplog
	option  dontlognull
	option forwardfor
	option http-server-close
	timeout connect		10000
	timeout client		60000
	timeout server		60000

frontend http-in
	bind *:80

	acl demo hdr(host) -i demo.cloudasr.com
	redirect location http://www.cloudasr.com/demo code 301 if demo
	acl no_www hdr(host) -i cloudasr.com
	redirect prefix http://www.cloudasr.com code 301 if no_www
"""

	for appId in urls_per_app.keys():
		config += "	acl host%s	hdr(host) -i %s\n" % (id_to_backend(appId), id_to_url(appId))

	for appId in urls_per_app.keys():
		config += "	use_backend %s if host%s\n" % (id_to_backend(appId), id_to_backend(appId))

	for (appId, urls) in urls_per_app.iteritems():
		config += """
backend %s
	balance leastconn
	option httpclose
	option forwardfor
	cookie JSESSIONID prefix
	cookie SERVERID insert indirect nocache
""" % (id_to_backend(appId))

		for (i, url) in enumerate(urls):
			config += "	server node%d %s cookie A check\n" % (i, url)

	return config

if __name__ == '__main__':
	marathon_url = os.environ.get('MARATHON_URL')
	login = os.environ.get('MARATHON_LOGIN', None)
	password = os.environ.get('<PASSWORD>PASSWORD', None)

	last_config = None
	while True:
		config = create_config(marathon_url, login, password)

		if last_config != config:
			with open('/etc/haproxy/haproxy.cfg', 'w') as f:
				f.write(config)

			print "%s Configuration changed" % time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
			subprocess.call(("service", "haproxy", "reload"))
			last_config = config

		time.sleep(10)


{'fraction_non_alphanumeric': 0.14882697947214077, 'fraction_numerical': 0.017961876832844576, 'mean_word_length': 4.732142857142857, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '2828050', 'n_tokens_mistral': 2141, 'n_tokens_neox': 1972, 'n_words': 479}