<commit_before>import os
import logging

import json
import paramiko
from django.conf import settings
from celery.task import task
from celery.task import subtask
from celery.task import group

from nailgun.models import Cluster, Node, Role
from nailgun.helpers import SshConnect


logger = logging.getLogger(__name__)


@task
def deploy_cluster(cluster_id):
    nodes = Node.objects.filter(cluster__id=cluster_id)
    roles = Role.objects.all()
    if not (nodes and roles):
        raise Exception("Roles or Nodes list is empty")

    nodes_per_role = {}
    # For each role in the system
    for r in roles:
        # Find nodes that have this role. Filter nodes by cluster_id
        nodes_per_role[r.name] = \
                [x.name for x in r.node_set.filter(cluster__id=cluster_id)]

    solo_json = {}
    # Extend solo_json for each node by specifying role
    #    assignment for this particular node
    for n in nodes:
        solo_json['run_list'] = []
        for role in n.roles.all():
            for rcp in role.recipes.all():
                solo_json['run_list'] += ["recipe[%s]" % rcp.recipe]

        solo_json['all_roles'] = nodes_per_role

        filepath = os.path.join(settings.CHEF_CONF_FOLDER,
                n.id + '.json')
        f = open(filepath, 'w')
        f.write(json.dumps(solo_json))
        f.close()

    job = group(subtask(run_chef_solo, args=(n.ip, )) for n in nodes)
    result = job.apply_async()
    return result


@task
def run_chef_solo(host):
    try:
        ssh = SshConnect(host, 'root', settings.PATH_TO_SSH_KEY)
        # Returns True if succeeded
        exit_code = ssh.run("/opt/nailgun/bin/deploy")
    except (paramiko.AuthenticationException,
            paramiko.PasswordRequiredException,
            paramiko.SSHException):
        logger.exception("Can't connect to host %s.", host)
        return {host: False}
    except Exception:
        logger.exception("Error in deployment of %s.", host)
        return {host: False}
    #finally:
        #ssh.close()
    return {host: exit_code}<commit_msg>Added stub call to Cobbler from tasks, minor consmetics in tasks<commit_after>import os
import logging

import json
import paramiko
from django.conf import settings
from celery.task import task
from celery.task import subtask
from celery.task import group

from nailgun.models import Cluster, Node, Role
from nailgun.helpers import SshConnect


logger = logging.getLogger(__name__)


@task
def deploy_cluster(cluster_id):
    nodes = Node.objects.filter(cluster__id=cluster_id)
    roles = Role.objects.all()
    if not (nodes and roles):
        raise Exception("Roles or Nodes list is empty")

    nodes_per_role = {}
    # For each role in the system
    for r in roles:
        # Find nodes that have this role. Filter nodes by cluster_id
        nodes_per_role[r.name] = \
                [x.name for x in r.node_set.filter(cluster__id=cluster_id)]

    solo_json = {}
    # Extend solo_json for each node by specifying role
    #    assignment for this particular node
    for n in nodes:
        solo_json['run_list'] = []
        for role in n.roles.all():
            for rcp in role.recipes.all():
                solo_json['run_list'] += ["recipe[%s]" % rcp.recipe]

        solo_json['all_roles'] = nodes_per_role

        filepath = os.path.join(settings.CHEF_CONF_FOLDER,
                n.id + '.json')
        f = open(filepath, 'w')
        f.write(json.dumps(solo_json))
        f.close()

    job = group(subtask(bootstrap_node, args=(n.id, )) for n in nodes)
    result = job.apply_async()
    return result


@task
def bootstrap_node(node_id):
    _provision_node(node_id)

    node = Node.objects.get(id__exact=node_id)
    try:
        ssh = SshConnect(node.ip, 'root', settings.PATH_TO_SSH_KEY)
        # Returns True if succeeded
        exit_status = ssh.run("/opt/nailgun/bin/deploy")
    except (paramiko.AuthenticationException,
            paramiko.PasswordRequiredException,
            paramiko.SSHException):
        logger.exception("Can't connect to %s, ip=%s", node.id, node.ip)
        return {node.id: False}
    except Exception:
        logger.exception("Error in deployment of %s, ip=%s", node.id, node.ip)
        return {node.id: False}
    #finally:
        #ssh.close()
    return {node.id: exit_status}


# Call to Cobbler to make node ready.
def _provision_node(node_id):
    pass
{'fraction_non_alphanumeric': 0.10056882821387941, 'fraction_numerical': 0.0, 'mean_word_length': 2.7159763313609466, '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': '4117121', 'n_tokens_mistral': 1395, 'n_tokens_neox': 1356, 'n_words': 379}