<commit_before>###################################################################
# Copyright 2013-2015 All Rights Reserved
# Authors: The Paradrop Team
###################################################################

from paradrop.lib.utils.output import out, logPrefix
import docker
import json


def startChute(update):
    out.info('-- %s Attempting to start new Chute %s \n' % (logPrefix(), update.name))

    repo = update.name + ":latest"
    dockerfile = update.dockerfile
    name = update.name

    host_config = build_host_config(update)
    
    c = docker.Client(base_url="unix://var/run/docker.sock")

    for line in c.build(rm=True, tag=repo, fileobj=dockerfile):
        for key, value in json.loads(line).iteritems():
            if isinstance(value, dict):
                continue
            elif key == 'stream':
                update.pkg.request.write(str(value))
            else:
                update.pkg.request.write(str(value) + '\n')
    container = c.create_container(
        image=repo, name=name, host_config=host_config
    )

    out.info("-- %s Successfully started chute with Id: %s\n" % (logPrefix(), str(container.get('Id'))))
    c.start(container.get('Id'))

def removeChute(update):
    out.info('-- %s Attempting to remove chute %s\n' % (logPrefix(), update.name))
    c = docker.Client(base_url='unix://var/run/docker.sock')
    repo = update.name + ":latest"
    name = update.name
    try:
        c.remove_container(container=name, force=True)
        c.remove_image(image=repo)
    except Exception as e:
        update.complete(success=False, message= e.explanation)

def stopChute(update):
    out.info('-- %s Attempting to stop chute %s\n' % (logPrefix(), update.name))
    c = docker.Client(base_url='unix://var/run/docker.sock')
    try:
        c.stop(container=update.name)
    except Exception as e:
        update.complete(success=False, message= e.explanation)

def restartChute(update):
    out.info('-- %s Attempting to start chute %s\n' % (logPrefix(), update.name))
    c = docker.Client(base_url='unix://var/run/docker.sock')
    try:
        c.start(container=update.name)
    except Exception as e:
        update.complete(success=False, message= e.explanation)

def build_host_config(update):

    return docker.utils.create_host_config(
        #TO support
        port_bindings=update.host_config.get('port_bindings'),
        restart_policy=update.host_config.get('restart_policy'),
        binds=update.host_config.get('binds'),
        links=update.host_config.get('links'),
        dns=update.host_config.get('dns'),
        #not supported
        #network_mode=update.host_config.get('network_mode'),
        #extra_hosts=update.host_config.get('extra_hosts'),
        devices=[],
        lxc_conf={},<commit_msg>removed allowing restart policy in config file now defaults to always restart but quit after 5 failed attempts<commit_after>###################################################################
# Copyright 2013-2015 All Rights Reserved
# Authors: The Paradrop Team
###################################################################

from paradrop.lib.utils.output import out, logPrefix
import docker
import json


def startChute(update):
    out.info('-- %s Attempting to start new Chute %s \n' % (logPrefix(), update.name))

    repo = update.name + ":latest"
    dockerfile = update.dockerfile
    name = update.name

    host_config = build_host_config(update)
    
    c = docker.Client(base_url="unix://var/run/docker.sock")

    for line in c.build(rm=True, tag=repo, fileobj=dockerfile):
        for key, value in json.loads(line).iteritems():
            if isinstance(value, dict):
                continue
            elif key == 'stream':
                update.pkg.request.write(str(value))
            else:
                update.pkg.request.write(str(value) + '\n')
    container = c.create_container(
        image=repo, name=name, host_config=host_config
    )

    out.info("-- %s Successfully started chute with Id: %s\n" % (logPrefix(), str(container.get('Id'))))
    c.start(container.get('Id'))

def removeChute(update):
    out.info('-- %s Attempting to remove chute %s\n' % (logPrefix(), update.name))
    c = docker.Client(base_url='unix://var/run/docker.sock')
    repo = update.name + ":latest"
    name = update.name
    try:
        c.remove_container(container=name, force=True)
        c.remove_image(image=repo)
    except Exception as e:
        update.complete(success=False, message= e.explanation)

def stopChute(update):
    out.info('-- %s Attempting to stop chute %s\n' % (logPrefix(), update.name))
    c = docker.Client(base_url='unix://var/run/docker.sock')
    try:
        c.stop(container=update.name)
    except Exception as e:
        update.complete(success=False, message= e.explanation)

def restartChute(update):
    out.info('-- %s Attempting to start chute %s\n' % (logPrefix(), update.name))
    c = docker.Client(base_url='unix://var/run/docker.sock')
    try:
        c.start(container=update.name)
    except Exception as e:
        update.complete(success=False, message= e.explanation)

def build_host_config(update):

    return docker.utils.create_host_config(
        #TO support
        port_bindings=update.host_config.get('port_bindings'),
        binds=update.host_config.get('binds'),
        links=update.host_config.get('links'),
        dns=update.host_config.get('dns'),
        #not supported/managed by us
        #network_mode=update.host_config.get('network_mode'),
        #extra_hosts=update.host_config.get('extra_hosts'),
        restart_policy={'MaximumRetryCount': 5, 'Name': 'always'},
        devices=[],
        lxc_conf={},
{'fraction_non_alphanumeric': 0.17821434835993685, 'fraction_numerical': 0.003157340817400456, 'mean_word_length': 3.2615844544095665, 'pattern_counts': {'":': 4, '<': 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': '2752726', 'n_tokens_mistral': 1786, 'n_tokens_neox': 1705, 'n_words': 363}