<commit_before>import socket
import docker
import config

from nginx import NginxConfigRenderer


class Manifold():

    def __init__(self, app, dock):
        self.app = app
        self.dock = dock
        self.config = config.ManifoldConfig

    def reload_nginx(self):
        try:
            self.dock.kill(self.config.NGINX_CONTAINER,
                           'SIGHUP')
        except docker.errors.APIError as err:
            # TODO logger
            # Nginx container is probably not running
            print('Could not reload nginx: {}'.format(err))

    def nginx_write_and_reload(self, renderer=NginxConfigRenderer):
        renderer = NginxConfigRenderer(self)
        renderer.write(self.minions())
        self.reload_nginx()

    def minion_by_id(self, cont_id):
        container = self.dock.inspect_container(container=cont_id)
        return Minion(self, container)

    def minions(self, trunc=True, all=False):
        containers = self.containers(trunc=trunc, all=all)
        return [Minion(self, container) for container in containers]

    def containers(self, trunc=True,  all=False):
        # TODO: filter according to labels
        return self.dock.containers(trunc=trunc, all=all)

    @property
    def hostname(self):
        return socket.gethostname()

    @property
    def port(self):
        return '5000'

    @property
    def subdomain(self):
        return self.config.SUBDOMAIN

    @property
    def nginx_server_name(self):
        return r'~^{}\..*$'.format(self.subdomain)


class Composition():
    """ A composition of minions """


class Minion():

    def __init__(self, manifold, container):
        self.app = manifold.app
        self.dock = manifold.dock
        self.manifold = manifold
        self.container = container

    @property
    def hostname(self):
        return self.container['Id']

    @property
    def subdomain(self):
        return '{}.{}'.format(self.hostname, self.manifold.subdomain)

    @property
    def nginx_server_name(self):
        return r'~^{}\.{}\..*$'.format(self.hostname, self.manifold.subdomain)

    # TODO: we could have several ports in one minion and we could have
    # several minion in one 'docker-compose'
    @property
    def port(self):
        port_info = self.dock.port(self.container['Id'], 8069)
        if not port_info:
            # FIXME: just for test as I don't have a running odoo image right
            # now
            port_info = self.dock.port(self.container['Id'], 5000)
        return port_info[0]['HostPort'] if port_info else ''

    @property
    def ip(self):
        inspect = self.dock.inspect_container(container=self.container['Id'])<commit_msg>Keep a short id for the container<commit_after>import socket
import docker
import config

from nginx import NginxConfigRenderer


class Manifold():

    def __init__(self, app, dock):
        self.app = app
        self.dock = dock
        self.config = config.ManifoldConfig

    def reload_nginx(self):
        try:
            self.dock.kill(self.config.NGINX_CONTAINER,
                           'SIGHUP')
        except docker.errors.APIError as err:
            # TODO logger
            # Nginx container is probably not running
            print('Could not reload nginx: {}'.format(err))

    def nginx_write_and_reload(self, renderer=NginxConfigRenderer):
        renderer = NginxConfigRenderer(self)
        renderer.write(self.minions())
        self.reload_nginx()

    def minion_by_id(self, cont_id):
        container = self.dock.inspect_container(container=cont_id)
        return Minion(self, container)

    def minions(self, trunc=True, all=False):
        containers = self.containers(trunc=trunc, all=all)
        return [Minion(self, container) for container in containers]

    def containers(self, trunc=True,  all=False):
        # TODO: filter according to labels
        return self.dock.containers(trunc=trunc, all=all)

    @property
    def hostname(self):
        return socket.gethostname()

    @property
    def port(self):
        return '5000'

    @property
    def subdomain(self):
        return self.config.SUBDOMAIN

    @property
    def nginx_server_name(self):
        return r'~^{}\..*$'.format(self.subdomain)


class Composition():
    """ A composition of minions """


class Minion():

    def __init__(self, manifold, container):
        self.app = manifold.app
        self.dock = manifold.dock
        self.manifold = manifold
        self.container = container

    @property
    def hostname(self):
        return self.container['Id'][:12]

    @property
    def subdomain(self):
        return '{}.{}'.format(self.hostname, self.manifold.subdomain)

    @property
    def nginx_server_name(self):
        return r'~^{}\.{}\..*$'.format(self.hostname, self.manifold.subdomain)

    # TODO: we could have several ports in one minion and we could have
    # several minion in one 'docker-compose'
    @property
    def port(self):
        port_info = self.dock.port(self.container['Id'], 8069)
        if not port_info:
            # FIXME: just for test as I don't have a running odoo image right
            # now
            port_info = self.dock.port(self.container['Id'], 5000)
        return port_info[0]['HostPort'] if port_info else ''

    @property
    def ip(self):
        inspect = self.dock.inspect_container(container=self.container['Id'])
{'fraction_non_alphanumeric': 0.10781221005752459, 'fraction_numerical': 0.005195769159398776, 'mean_word_length': 2.5530652603823336, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '1814407', 'n_tokens_mistral': 1679, 'n_tokens_neox': 1559, 'n_words': 397}