<commit_before>import os
import json
from django.conf import settings
from celery.task import task
from nailgun.models import Environment, Node, Role

@task
def create_chef_config(environment_id):
    env_id = environment_id
    nodes = Node.objects.filter(environment__id=env_id)
    roles = Role.objects.all()
    if not (nodes and roles):
        resp = rc.NOT_FOUND
        resp.write("Roles or Nodes list is empty")
        return resp

    nodes_per_role = {}
    # For each role in the system
    for r in roles:
        # Find nodes that have this role. Filter nodes by env_id
        nodes_per_role[r.name] = \
                [x.name for x in r.nodes.filter(environment__id=env_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'] = \
                ["role[" + x.name + "]" for x in n.roles.all()]
        solo_json['all_roles'] = nodes_per_role

        filepath = os.path.join(settings.CHEF_CONF_FOLDER,
                n.name + '.json')
        f = open(filepath, 'w')
        f.write(json.dumps(solo_json))
        f.close()<commit_msg>Chef configuration creation task minor fixes<commit_after>import os
import json
from django.conf import settings
from celery.task import task
from nailgun.models import Environment, Node, Role

@task(ignore_result=True)
def create_chef_config(environment_id):
    env_id = environment_id
    nodes = Node.objects.filter(environment__id=env_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 env_id
        nodes_per_role[r.name] = \
                [x.name for x in r.nodes.filter(environment__id=env_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'] = \
                ["role[" + x.name + "]" for x in n.roles.all()]
        solo_json['all_roles'] = nodes_per_role

        filepath = os.path.join(settings.CHEF_CONF_FOLDER,
                n.name + '.json')
        f = open(filepath, 'w')
        f.write(json.dumps(solo_json))
        f.close()
{'fraction_non_alphanumeric': 0.09592529711375213, 'fraction_numerical': 0.0, 'mean_word_length': 2.539039039039039, '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': '4977366', 'n_tokens_mistral': 744, 'n_tokens_neox': 734, 'n_words': 211}