<commit_before>#!/usr/bin/python -u

"""Generate HAProxy configuration (using Marathon as a data source)"""

import json
import logging
import os
import re
import subprocess
import sys
import time

import jinja2
import kazoo.client


def main():

    logging.basicConfig()

    zk = kazoo.client.KazooClient(hosts=os.getenv('ZK', 'localhost:2181'))
    zk.start()

    #
    zk.ensure_path("/haproxy")
    try:
        zk.create("/haproxy/config", "")
    except kazoo.exceptions.NodeExistsError:
        pass

    #
    marathon = os.getenv('MARATHON', '127.0.0.1:8080')

    while True:
        apps = []

        p = subprocess.Popen(["curl", "-fsS", "http://%s/v2/apps" % marathon], stdout=subprocess.PIPE)
        assert p.wait() == 0

        appIds = [app['id'] for app in json.load(p.stdout)['apps']]

        for appId in appIds:
            p = subprocess.Popen(["curl", "-fsS", "http://%s/v2/apps%s" % (marathon, appId)], stdout=subprocess.PIPE)
            assert p.wait() == 0

            app = json.load(p.stdout)['app']

            if not 'portMappings' in app['container']['docker']:
                continue

            apps.append(app)

        #
        def regex_replace(string, pattern, repl):
            return re.sub(pattern, repl, string)

        environment = jinja2.Environment()
        environment.filters['regex_replace'] = regex_replace

        with open('files/haproxy.cfg.j2') as f:
            x = f.read()
        template = environment.from_string(x)

        haproxy_cfg = template.render(apps=apps)
        haproxy_cfg = re.sub(r'\n{2,}', '\n\n', haproxy_cfg).rstrip() + '\n'

        data, stat = zk.get("/haproxy/config")

        if haproxy_cfg != data:
            print("set", file=sys.stderr)
            zk.set("/haproxy/config", haproxy_cfg.encode('utf-8'))
        time.sleep(10)

<commit_msg>Update 0.1.0/app.py<commit_after>#!/usr/bin/python -u

"""Generate HAProxy configuration (using Marathon as a data source)"""

import json
import logging
import os
import re
import subprocess
import sys
import time

import jinja2
import kazoo.client


def main():

    logging.basicConfig()

    zk = kazoo.client.KazooClient(hosts=os.getenv('ZK', 'localhost:2181'))
    zk.start()

    #
    zk.ensure_path("/haproxy")
    try:
        zk.create("/haproxy/config", b"")
    except kazoo.exceptions.NodeExistsError:
        pass

    #
    marathon = os.getenv('MARATHON', '127.0.0.1:8080')

    while True:
        apps = []

        p = subprocess.Popen(["curl", "-fsS", "http://%s/v2/apps" % marathon], stdout=subprocess.PIPE)
        assert p.wait() == 0

        appIds = [app['id'] for app in json.loads(p.stdout.read().decode('utf-8'))['apps']]

        for appId in appIds:
            p = subprocess.Popen(["curl", "-fsS", "http://%s/v2/apps%s" % (marathon, appId)], stdout=subprocess.PIPE)
            assert p.wait() == 0

            app = json.loads(p.stdout.read().decode('utf-8'))['app']

            if not 'portMappings' in app['container']['docker']:
                continue

            apps.append(app)

        #
        def regex_replace(string, pattern, repl):
            return re.sub(pattern, repl, string)

        environment = jinja2.Environment()
        environment.filters['regex_replace'] = regex_replace

        with open('files/haproxy.cfg.j2') as f:
            x = f.read()
        template = environment.from_string(x)

        haproxy_cfg = template.render(apps=apps)
        haproxy_cfg = re.sub(r'\n{2,}', '\n\n', haproxy_cfg).rstrip() + '\n'

        data, stat = zk.get("/haproxy/config")

        if haproxy_cfg != data.decode('utf-8'):
            print("set", file=sys.stderr)
            zk.set("/haproxy/config", haproxy_cfg.encode('utf-8'))
        time.sleep(10)


{'fraction_non_alphanumeric': 0.15900026588673224, 'fraction_numerical': 0.014889657006115395, 'mean_word_length': 2.395306859205776, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2036771', 'n_tokens_mistral': 1357, 'n_tokens_neox': 1254, 'n_words': 250}