<commit_before>#########
# Copyright (c) 2015 GigaSpaces Technologies Ltd. All rights reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
#  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  * See the License for the specific language governing permissions and
#  * limitations under the License.

import tempfile
import time
import threading
import sys
import os
import copy
from posixpath import join as urljoin
from contextlib import contextmanager

from cloudify import amqp_client, ctx
from cloudify import broker_config as global_broker_config
from cloudify.decorators import operation
from cloudify.manager import get_rest_client
from cloudify.models_states import AgentState, PluginInstallationState
from cloudify.constants import BROKER_PORT_SSL
from cloudify.error_handling import deserialize_known_exception
from cloudify.exceptions import NonRecoverableError, RecoverableError
from cloudify.agent_utils import create_agent_record, update_agent_record
from cloudify.utils import (get_rest_token,
                            ManagerVersion,
                            get_local_rest_certificate,
                            get_manager_name)
from cloudify._compat import reraise

from cloudify_agent.celery_app import get_celery_app
from cloudify_agent.api.plugins import installer as plugin_installer
from cloudify_agent.api.factory import DaemonFactory
from cloudify_agent.api import exceptions
from cloudify_agent.api import utils
from cloudify_agent.installer.script import (
    init_script_download_link,
    stop_agent_script_download_link,
    cleanup_scripts
)
from cloudify_agent.installer.config.agent_config import CloudifyAgentConfig
from cloudify_agent.installer.config.agent_config import \
    update_agent_runtime_properties

CELERY_TASK_RESULT_EXPIRES = 600


def _set_plugin_state(plugin, state, error=None):
    if not plugin.get('id'):
        return
    client = get_rest_client()
    try:
        agent = utils.internal.get_daemon_name()
        manager = None
    except KeyError:
        agent = None
        manager = get_manager_name()

    client.plugins.set_state(
        plugin['id'], state=state, agent_name=agent, manager_name=manager,
        error=str(error))


@operation
def install_plugins(plugins, **_):
    for plugin in plugins:
        name = plugin.get('package_name') or plugin['name']
        ctx.logger.info('Installing plugin: %s', name)
        _set_plugin_state(plugin, state=PluginInstallationState.INSTALLING)
        try:
            plugin_installer.install(
                plugin=plugin,
                deployment_id=ctx.deployment.id,
                blueprint_id=ctx.blueprint.id)
        except exceptions.PluginInstallationError as e:
            _set_plugin_state(
                plugin, state=PluginInstallationState.ERROR, error=e)
            # preserve traceback
            tpe, value, tb = sys.exc_info()
            reraise(NonRecoverableError, NonRecoverableError(str(e)), tb)
        else:
            _set_plugin_state(plugin, state=PluginInstallationState.INSTALLED)


@operation
def uninstall_plugins(plugins, **_):
    for plugin in plugins:
        ctx.logger.info('Uninstalling plugin: %s', plugin['package_name'])
        _set_plugin_state(plugin, state=PluginInstallationState.UNINSTALLING)
        plugin_installer.uninstall(plugin)
        _set_plugin_state(plugin, state=PluginInstallationState.UNINSTALLED)


@operation<commit_msg>uninstall_plugins: don't break on plugin missing (#670)<commit_after>#########
# Copyright (c) 2015 GigaSpaces Technologies Ltd. All rights reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
#  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  * See the License for the specific language governing permissions and
#  * limitations under the License.

import tempfile
import time
import threading
import sys
import os
import copy
from posixpath import join as urljoin
from contextlib import contextmanager

from cloudify import amqp_client, ctx
from cloudify import broker_config as global_broker_config
from cloudify.decorators import operation
from cloudify.manager import get_rest_client
from cloudify.models_states import AgentState, PluginInstallationState
from cloudify.constants import BROKER_PORT_SSL
from cloudify.error_handling import deserialize_known_exception
from cloudify.exceptions import NonRecoverableError, RecoverableError
from cloudify.agent_utils import create_agent_record, update_agent_record
from cloudify.utils import (get_rest_token,
                            ManagerVersion,
                            get_local_rest_certificate,
                            get_manager_name)
from cloudify._compat import reraise
from cloudify_rest_client.exceptions import CloudifyClientError

from cloudify_agent.celery_app import get_celery_app
from cloudify_agent.api.plugins import installer as plugin_installer
from cloudify_agent.api.factory import DaemonFactory
from cloudify_agent.api import exceptions
from cloudify_agent.api import utils
from cloudify_agent.installer.script import (
    init_script_download_link,
    stop_agent_script_download_link,
    cleanup_scripts
)
from cloudify_agent.installer.config.agent_config import CloudifyAgentConfig
from cloudify_agent.installer.config.agent_config import \
    update_agent_runtime_properties

CELERY_TASK_RESULT_EXPIRES = 600


def _set_plugin_state(plugin, state, error=None, allow_missing=False):
    if not plugin.get('id'):
        return
    client = get_rest_client()
    try:
        agent = utils.internal.get_daemon_name()
        manager = None
    except KeyError:
        agent = None
        manager = get_manager_name()

    try:
        client.plugins.set_state(
            plugin['id'], state=state, agent_name=agent, manager_name=manager,
            error=str(error))
    except CloudifyClientError as e:
        if e.status_code != 404 or not allow_missing:
            raise


@operation
def install_plugins(plugins, **_):
    for plugin in plugins:
        name = plugin.get('package_name') or plugin['name']
        ctx.logger.info('Installing plugin: %s', name)
        _set_plugin_state(plugin, state=PluginInstallationState.INSTALLING)
        try:
            plugin_installer.install(
                plugin=plugin,
                deployment_id=ctx.deployment.id,
                blueprint_id=ctx.blueprint.id)
        except exceptions.PluginInstallationError as e:
            _set_plugin_state(
                plugin, state=PluginInstallationState.ERROR, error=e)
            # preserve traceback
            tpe, value, tb = sys.exc_info()
            reraise(NonRecoverableError, NonRecoverableError(str(e)), tb)
        else:
            _set_plugin_state(plugin, state=PluginInstallationState.INSTALLED)


@operation
def uninstall_plugins(plugins, **_):
    for plugin in plugins:
        ctx.logger.info('Uninstalling plugin: %s', plugin['package_name'])
        _set_plugin_state(plugin, state=PluginInstallationState.UNINSTALLING,
                          allow_missing=True)
        plugin_installer.uninstall(plugin)
        _set_plugin_state(plugin, state=PluginInstallationState.UNINSTALLED,
                          allow_missing=True)


@operation
{'fraction_non_alphanumeric': 0.06524212921987609, 'fraction_numerical': 0.0035402705778227335, 'mean_word_length': 3.6015125072716696, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '214724', 'n_tokens_mistral': 2182, 'n_tokens_neox': 2125, 'n_words': 598}