<commit_before># -*- coding: utf-8 -*-

import os

from ansible.playbook import PlayBook
from ansible import utils
from ansible import callbacks
import ansible.constants as C
from fabric import api as fabric_api

from solar.core.handlers import base
from solar import errors
from solar.core.provider import SVNProvider


ROLES_PATH = '/etc/ansible/roles'


class AnsiblePlaybook(base.BaseHandler):

    def download_roles(self, urls):
        if not os.path.exists(ROLES_PATH):
            os.makedirs(ROLES_PATH)
        for url in urls:
            provider = SVNProvider(url)
            provider.run()
            fabric_api.local('cp -r {} {}'.format(
                provider.directory, ROLES_PATH))

    def action(self, resource, action):
        # This would require to put this file to remote and execute it (mostly)
        log.debug("Ansible playbook is not ported to pluggable transports")
        action_file = os.path.join(
            resource.metadata['actions_path'],
            resource.metadata['actions'][action])
        stats = callbacks.AggregateStats()
        playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
        runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)

        variables = resource.args_dict()
        if 'roles' in variables:
            self.download_roles(variables['roles'])

        remote_user = variables.get('ssh_user') or C.DEFAULT_REMOTE_USER
        private_key_file = variables.get('ssh_key') or C.DEFAULT_PRIVATE_KEY_FILE
        if variables.get('ip'):
            host = variables['ip']
            transport = C.DEFAULT_TRANSPORT
        else:
            host = 'localhost'
            transport = 'local'
        C.HOST_KEY_CHECKING = False
        play = PlayBook(
            playbook=action_file,
            remote_user=remote_user,
            host_list = [host],
            private_key_file=private_key_file,
            extra_vars=variables,
            callbacks=playbook_cb,
            runner_callbacks=runner_cb,
            stats=stats,
            transport=transport)

        play.run()
        summary = stats.summarize(host)

        if summary.get('unreachable') or summary.get('failures'):
            raise errors.SolarError(
                'Ansible playbook %s failed with next summary %s',
                action_file, summary)<commit_msg>ansible_playbook.py: fix missing log import<commit_after># -*- coding: utf-8 -*-

import os

from ansible.playbook import PlayBook
from ansible import utils
from ansible import callbacks
import ansible.constants as C
from fabric import api as fabric_api

from solar.core.log import log
from solar.core.handlers import base
from solar import errors
from solar.core.provider import SVNProvider

ROLES_PATH = '/etc/ansible/roles'


class AnsiblePlaybook(base.BaseHandler):

    def download_roles(self, urls):
        if not os.path.exists(ROLES_PATH):
            os.makedirs(ROLES_PATH)
        for url in urls:
            provider = SVNProvider(url)
            provider.run()
            fabric_api.local('cp -r {} {}'.format(
                provider.directory, ROLES_PATH))

    def action(self, resource, action):
        # This would require to put this file to remote and execute it (mostly)
        log.debug("Ansible playbook is not ported to pluggable transports")
        action_file = os.path.join(
            resource.metadata['actions_path'],
            resource.metadata['actions'][action])
        stats = callbacks.AggregateStats()
        playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
        runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)

        variables = resource.args_dict()
        if 'roles' in variables:
            self.download_roles(variables['roles'])

        remote_user = variables.get('ssh_user') or C.DEFAULT_REMOTE_USER
        private_key_file = variables.get('ssh_key') or C.DEFAULT_PRIVATE_KEY_FILE
        if variables.get('ip'):
            host = variables['ip']
            transport = C.DEFAULT_TRANSPORT
        else:
            host = 'localhost'
            transport = 'local'
        C.HOST_KEY_CHECKING = False
        play = PlayBook(
            playbook=action_file,
            remote_user=remote_user,
            host_list=[host],
            private_key_file=private_key_file,
            extra_vars=variables,
            callbacks=playbook_cb,
            runner_callbacks=runner_cb,
            stats=stats,
            transport=transport)

        play.run()
        summary = stats.summarize(host)

        if summary.get('unreachable') or summary.get('failures'):
            raise errors.SolarError(
                'Ansible playbook %s failed with next summary %s',
                action_file, summary)
{'fraction_non_alphanumeric': 0.08798999165971642, 'fraction_numerical': 0.0004170141784820684, 'mean_word_length': 2.419101924447612, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7650692', 'n_tokens_mistral': 1367, 'n_tokens_neox': 1342, 'n_words': 320}