<commit_before>########
# Copyright (c) 2014 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.
############

from cloudify_rest_client.exceptions import CloudifyClientError

from .. import env
from .use import use
from .. import exceptions
from ..env import profile
from ..cli import cfy, helptexts
from ..bootstrap import bootstrap as bs


@cfy.command(name='teardown', short_help='Teardown a manager [manager only]')
@cfy.options.force(help=helptexts.FORCE_TEARDOWN)
@cfy.options.ignore_deployments
@cfy.options.task_retries()
@cfy.options.task_retry_interval()
@cfy.options.task_thread_pool_size()
@cfy.options.verbose()
@cfy.assert_manager_active()
def teardown(force,
             ignore_deployments,
             task_retries,
             task_retry_interval,
             task_thread_pool_size):
    """Teardown the manager
    """
    _assert_force(force)

    try:
        manager_ip = profile.manager_ip
    except exceptions.CloudifyCliError:
        # manager ip does not exist in the local context
        # this can mean one of two things:
        # 1. bootstrap was unsuccessful
        # 2. we are in the wrong directory
        try:
            bs.load_env()
            # this means we are probably in the right directory
            # which means the teardown was unsuccessful, try to teardown
            # anyway
        except BaseException:
            # this means we are in the wrong directory, have the user
            # execute the 'use' command to retrieve manager deployment,
            # because other wise we cannot bootstrap from here. If the
            # manager is down, the user must return to the original
            # directory in order to teardown
            raise exceptions.CloudifyCliError(
                "You are attempting to teardown from an "
                "invalid directory. Please execute `cfy use` before "
                "running this command. If the manager is "
                "unavailable, you must execute this command from the "
                "directory you initially bootstrapped from, or from the last "
                "directory a `cfy use` command was executed on this manager.")
        else:
            _do_teardown(
                task_retries,
                task_retry_interval,
                task_thread_pool_size)
    else:
        # make sure we don't teardown the manager if there are running
        # deployments, unless the user explicitly specified it.
        _validate_deployments(ignore_deployments, manager_ip)

        # update local provider context since the server ip might have
        # changed in case it has gone through a recovery process.
        _update_local_provider_context(manager_ip)

        # execute teardown
        _do_teardown(
            task_retries,
            task_retry_interval,
            task_thread_pool_size)


@cfy.pass_logger
def _update_local_provider_context(manager_ip, logger):
    try:
        use(manager_ip, profile.rest_port)
    except BaseException as e:
        logger.warning('Failed to retrieve provider context: {0}. This '
                       'may cause a leaking manager '
                       'in case it has gone through a '
                       'recovery process'.format(str(e)))

<commit_msg>CFY-6005. Fix use callback invocation (#458)<commit_after>########
# Copyright (c) 2014 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.
############

from cloudify_rest_client.exceptions import CloudifyClientError

from .. import env
from .use import use
from .. import exceptions
from ..cli import cfy, helptexts
from ..bootstrap import bootstrap as bs


@cfy.command(name='teardown', short_help='Teardown a manager [manager only]')
@cfy.options.force(help=helptexts.FORCE_TEARDOWN)
@cfy.options.ignore_deployments
@cfy.options.task_retries()
@cfy.options.task_retry_interval()
@cfy.options.task_thread_pool_size()
@cfy.options.verbose()
@cfy.pass_context
@cfy.assert_manager_active()
def teardown(ctx,
             force,
             ignore_deployments,
             task_retries,
             task_retry_interval,
             task_thread_pool_size):
    """Teardown the manager
    """
    _assert_force(force)

    try:
        manager_ip = env.profile.manager_ip
    except exceptions.CloudifyCliError:
        # manager ip does not exist in the local context
        # this can mean one of two things:
        # 1. bootstrap was unsuccessful
        # 2. we are in the wrong directory
        try:
            bs.load_env()
            # this means we are probably in the right directory
            # which means the teardown was unsuccessful, try to teardown
            # anyway
        except BaseException:
            # this means we are in the wrong directory, have the user
            # execute the 'use' command to retrieve manager deployment,
            # because other wise we cannot bootstrap from here. If the
            # manager is down, the user must return to the original
            # directory in order to teardown
            raise exceptions.CloudifyCliError(
                "You are attempting to teardown from an "
                "invalid directory. Please execute `cfy use` before "
                "running this command. If the manager is "
                "unavailable, you must execute this command from the "
                "directory you initially bootstrapped from, or from the last "
                "directory a `cfy use` command was executed on this manager.")
        else:
            _do_teardown(
                task_retries,
                task_retry_interval,
                task_thread_pool_size)
    else:
        # make sure we don't teardown the manager if there are running
        # deployments, unless the user explicitly specified it.
        _validate_deployments(ignore_deployments, manager_ip)

        # update local provider context since the server ip might have
        # changed in case it has gone through a recovery process.
        _update_local_provider_context(ctx, manager_ip)

        # execute teardown
        _do_teardown(
            task_retries,
            task_retry_interval,
            task_thread_pool_size)


@cfy.pass_logger
def _update_local_provider_context(ctx, manager_ip, logger):
    try:
        ctx.invoke(
            use,
            profile_name=manager_ip,
            rest_port=env.profile.rest_port,
        )
    except BaseException as e:
        logger.warning('Failed to retrieve provider context: {0}. This '
                       'may cause a leaking manager '
                       'in case it has gone through a '
                       'recovery process'.format(str(e)))


{'fraction_non_alphanumeric': 0.06652887223872885, 'fraction_numerical': 0.0037462860095594883, 'mean_word_length': 2.4105726872246698, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1764172', 'n_tokens_mistral': 2050, 'n_tokens_neox': 1971, 'n_words': 757}