<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.
############

import os
import json
from StringIO import StringIO

from cloudify_rest_client.exceptions import MissingRequiredDeploymentInputError
from cloudify_rest_client.exceptions import UnknownDeploymentInputError

from .. import table
from .. import utils
from ..local import load_env
from ..cli import cfy, helptexts
from ..logger import get_events_logger
from .. import execution_events_fetcher
from ..constants import DEFAULT_BLUEPRINT_PATH
from ..exceptions import CloudifyCliError, SuppressedCloudifyCliError


@cfy.group(name='deployments')
@cfy.options.verbose()
def deployments():
    """Handle deployments on the Manager
    """
    pass


@cfy.pass_logger
def _print_deployment_inputs(client, blueprint_id, logger):
    blueprint = client.blueprints.get(blueprint_id)

    logger.info('Deployment inputs:')
    inputs_output = StringIO()
    for input_name, input_def in blueprint.plan['inputs'].iteritems():
        inputs_output.write('\t{0}:{1}'.format(input_name, os.linesep))
        for k, v in input_def.iteritems():
            inputs_output.write('\t\t{0}: {1}{2}'.format(k, v, os.linesep))
    inputs_output.write(os.linesep)
    logger.info(inputs_output.getvalue())


@cfy.command(name='list', short_help='List deployments [manager only]')
@cfy.options.blueprint_id()
@cfy.options.sort_by()
@cfy.options.descending
@cfy.options.verbose()
@cfy.assert_manager_active
@cfy.pass_client()
@cfy.pass_logger
def manager_list(blueprint_id, sort_by, descending, logger, client):
    """List deployments

    If `--blueprint-id` is provided, list deployments for that blueprint.
    Otherwise, list deployments for all blueprints.
    """
    if blueprint_id:
        logger.info('Listing deployments for blueprint {0}...'.format(
            blueprint_id))
    else:
        logger.info('Listing all deployments...')

    deployments = client.deployments.list(
        sort=sort_by, is_descending=descending)
    if blueprint_id:
        deployments = filter(lambda deployment:
                             deployment['blueprint_id'] == blueprint_id,
                             deployments)

    columns = ['id', 'blueprint_id', 'created_at', 'updated_at']
    pt = table.generate(columns, data=deployments)
    table.log('Deployments:', pt)


@cfy.command(name='update', short_help='Update a deployment [manager only]')
@cfy.argument('deployment-id')
@cfy.options.blueprint_path(required=True)
@cfy.options.inputs
@cfy.options.blueprint_filename()
@cfy.options.workflow_id('update')
@cfy.options.skip_install
@cfy.options.skip_uninstall
@cfy.options.force(help=helptexts.FORCE_UPDATE)
@cfy.options.include_logs
@cfy.options.json_output
@cfy.options.verbose()
@cfy.assert_manager_active
@cfy.pass_client()
@cfy.pass_logger
def manager_update(deployment_id,
                   blueprint_path,
                   inputs,
                   blueprint_filename,
                   skip_install,
                   skip_uninstall,
                   workflow_id,
                   force,
                   include_logs,
                   json_output,
                   logger,
                   client):
    """Update a specified deployment according to the specified blueprint

    `DEPLOYMENT_ID` is the deployment's id to update.
    """
    if not utils.is_archive(blueprint_path) and \
            blueprint_filename not in (DEFAULT_BLUEPRINT_PATH, blueprint_path):
        raise CloudifyCliError(
            '--blueprint-filename param should be passed '
            'only when updating from an archive'
        )

    blueprint_or_archive_path = blueprint_path
    logger.info('Updating deployment {0} using blueprint {1}'.format(
        deployment_id, blueprint_or_archive_path))

    deployment_update = client.deployment_updates.update(
        deployment_id,
        blueprint_or_archive_path,
        application_file_name=blueprint_filename,
        inputs=inputs,
        workflow_id=workflow_id,
        skip_install=skip_install,
        skip_uninstall=skip_uninstall,
        force=force)
    events_logger = get_events_logger(json_output)

    execution = execution_events_fetcher.wait_for_execution(
        client,
        client.executions.get(deployment_update.execution_id),
        events_handler=events_logger,
        include_logs=include_logs,
        timeout=None)  # don't timeout ever

    if execution.error:
        logger.info("Execution of workflow '{0}' for deployment "
                    "'{1}' failed. [error={2}]"
                    .format(execution.workflow_id,
                            execution.deployment_id,
                            execution.error))
        logger.info('Failed updating deployment {dep_id}. Deployment update '
                    'id: {depup_id}. Execution id: {exec_id}'
                    .format(depup_id=deployment_update.id,
                            dep_id=deployment_id,
                            exec_id=execution.id))
        raise SuppressedCloudifyCliError()
    else:
        logger.info("Finished executing workflow '{0}' on deployment "
                    "'{1}'".format(execution.workflow_id,
                                   execution.deployment_id))
        logger.info('Successfully updated deployment {dep_id}. '
                    'Deployment update id: {depup_id}. Execution id: {exec_id}'
                    .format(depup_id=deployment_update.id,
                            dep_id=deployment_id,
                            exec_id=execution.id))


@cfy.command(name='create',
             short_help='Create a deployment [manager only]')
@cfy.argument('deployment-id', required=False)
@cfy.options.blueprint_id(required=True)
@cfy.options.deployment_id()
@cfy.options.inputs
@cfy.options.verbose()
@cfy.assert_manager_active
@cfy.pass_client()
@cfy.pass_logger
def manager_create(blueprint_id, deployment_id, inputs, logger, client):
    """Create a deployment on the manager

    `DEPLOYMENT_ID` is the id of the deployment you'd like to create.
    """
    logger.info('Creating new deployment from blueprint {0}...'.format(
        blueprint_id))
    deployment_id = deployment_id or blueprint_id

    try:
        deployment = client.deployments.create(
            blueprint_id, deployment_id, inputs=inputs)
    except MissingRequiredDeploymentInputError as e:
        logger.info('Unable to create deployment. Not all '
                    'required inputs have been specified...')
        _print_deployment_inputs(client, blueprint_id)
        raise CloudifyCliError(str(e))
    except UnknownDeploymentInputError as e:
        logger.info(
            'Unable to create deployment, an unknown input was specified...')
        _print_deployment_inputs(client, blueprint_id)
        raise CloudifyCliError(str(e))

    logger.info("Deployment created. The deployment's id is {0}".format(
        deployment.id))


@cfy.command(name='delete',
             short_help='Delete a deployment [manager only]')
@cfy.argument('deployment-id')
@cfy.options.force(help=helptexts.IGNORE_LIVE_NODES)
@cfy.options.verbose()
@cfy.assert_manager_active
@cfy.pass_client()
@cfy.pass_logger
def manager_delete(deployment_id, force, logger, client):
    """Delete a deployment from the manager

    `DEPLOYMENT_ID` is the id of the deployment to delete.
    """
    logger.info('Deleting deployment {0}...'.format(deployment_id))
    client.deployments.delete(deployment_id, force)
    logger.info("Deployment deleted")


@cfy.command(name='outputs',
             short_help='Show deployment outputs [manager only]')
@cfy.argument('deployment-id')
@cfy.options.verbose()
@cfy.assert_manager_active
@cfy.pass_client()
@cfy.pass_logger
def manager_outputs(deployment_id, logger, client):
    """Retrieve outputs for a specific deployment

    `DEPLOYMENT_ID` is the id of the deployment to print outputs for.
    """
    logger.info('Retrieving outputs for deployment {0}...'.format(
        deployment_id))
    dep = client.deployments.get(deployment_id, _include=['outputs'])
    outputs_def = dep.outputs
    response = client.deployments.outputs.get(deployment_id)
    outputs_ = StringIO()
    for output_name, output in response.outputs.iteritems():
        outputs_.write(' - "{0}":{1}'.format(output_name, os.linesep))
        description = outputs_def[output_name].get('description', '')
        outputs_.write('     Description: {0}{1}'.format(description,
                                                         os.linesep))
        outputs_.write('     Value: {0}{1}'.format(output, os.linesep))
    logger.info(outputs_.getvalue())


@cfy.command(name='inputs',
             short_help='Show deployment inputs [manager only]')
@cfy.argument('deployment-id')
@cfy.options.verbose()
@cfy.assert_manager_active
@cfy.pass_client()
@cfy.pass_logger
def manager_inputs(deployment_id, logger, client):
    """Retrieve inputs for a specific deployment

    `DEPLOYMENT_ID` is the id of the deployment to print inputs for.
    """
    logger.info('Retrieving inputs for deployment {0}...'.format(
        deployment_id))
    dep = client.deployments.get(deployment_id, _include=['inputs'])
    inputs_ = StringIO()
    for input_name, input in dep.inputs.iteritems():
        inputs_.write(' - "{0}":{1}'.format(input_name, os.linesep))
        inputs_.write('     Value: {0}{1}'.format(input, os.linesep))
    logger.info(inputs_.getvalue())


@cfy.command(name='inputs', short_help='Show deployment inputs [locally]')
@cfy.options.verbose()
@cfy.options.blueprint_id(required=True, multiple_blueprints=True)
@cfy.pass_logger
def local_inputs(blueprint_id, logger):
    """Display inputs for the execution
    """
    env = load_env(blueprint_id)
    logger.info(json.dumps(env.plan['inputs'] or {}, sort_keys=True, indent=2))


@cfy.command(name='outputs', short_help='Show deployment outputs [locally]')
@cfy.options.verbose()
@cfy.options.blueprint_id(required=True, multiple_blueprints=True)
@cfy.pass_logger
def local_outputs(blueprint_id, logger):
    """Display outputs for the execution
    """
    env = load_env(blueprint_id)
    logger.info(json.dumps(env.outputs() or {}, sort_keys=True, indent=2))
<commit_msg>CFY-6003 Remove deployment argument (#449)<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.
############

import os
import json
from StringIO import StringIO

from cloudify_rest_client.exceptions import MissingRequiredDeploymentInputError
from cloudify_rest_client.exceptions import UnknownDeploymentInputError

from .. import table
from .. import utils
from ..local import load_env
from ..cli import cfy, helptexts
from ..logger import get_events_logger
from .. import execution_events_fetcher
from ..constants import DEFAULT_BLUEPRINT_PATH
from ..exceptions import CloudifyCliError, SuppressedCloudifyCliError


@cfy.group(name='deployments')
@cfy.options.verbose()
def deployments():
    """Handle deployments on the Manager
    """
    pass


@cfy.pass_logger
def _print_deployment_inputs(client, blueprint_id, logger):
    blueprint = client.blueprints.get(blueprint_id)

    logger.info('Deployment inputs:')
    inputs_output = StringIO()
    for input_name, input_def in blueprint.plan['inputs'].iteritems():
        inputs_output.write('\t{0}:{1}'.format(input_name, os.linesep))
        for k, v in input_def.iteritems():
            inputs_output.write('\t\t{0}: {1}{2}'.format(k, v, os.linesep))
    inputs_output.write(os.linesep)
    logger.info(inputs_output.getvalue())


@cfy.command(name='list', short_help='List deployments [manager only]')
@cfy.options.blueprint_id()
@cfy.options.sort_by()
@cfy.options.descending
@cfy.options.verbose()
@cfy.assert_manager_active
@cfy.pass_client()
@cfy.pass_logger
def manager_list(blueprint_id, sort_by, descending, logger, client):
    """List deployments

    If `--blueprint-id` is provided, list deployments for that blueprint.
    Otherwise, list deployments for all blueprints.
    """
    if blueprint_id:
        logger.info('Listing deployments for blueprint {0}...'.format(
            blueprint_id))
    else:
        logger.info('Listing all deployments...')

    deployments = client.deployments.list(
        sort=sort_by, is_descending=descending)
    if blueprint_id:
        deployments = filter(lambda deployment:
                             deployment['blueprint_id'] == blueprint_id,
                             deployments)

    columns = ['id', 'blueprint_id', 'created_at', 'updated_at']
    pt = table.generate(columns, data=deployments)
    table.log('Deployments:', pt)


@cfy.command(name='update', short_help='Update a deployment [manager only]')
@cfy.argument('deployment-id')
@cfy.options.blueprint_path(required=True)
@cfy.options.inputs
@cfy.options.blueprint_filename()
@cfy.options.workflow_id('update')
@cfy.options.skip_install
@cfy.options.skip_uninstall
@cfy.options.force(help=helptexts.FORCE_UPDATE)
@cfy.options.include_logs
@cfy.options.json_output
@cfy.options.verbose()
@cfy.assert_manager_active
@cfy.pass_client()
@cfy.pass_logger
def manager_update(deployment_id,
                   blueprint_path,
                   inputs,
                   blueprint_filename,
                   skip_install,
                   skip_uninstall,
                   workflow_id,
                   force,
                   include_logs,
                   json_output,
                   logger,
                   client):
    """Update a specified deployment according to the specified blueprint

    `DEPLOYMENT_ID` is the deployment's id to update.
    """
    if not utils.is_archive(blueprint_path) and \
            blueprint_filename not in (DEFAULT_BLUEPRINT_PATH, blueprint_path):
        raise CloudifyCliError(
            '--blueprint-filename param should be passed '
            'only when updating from an archive'
        )

    blueprint_or_archive_path = blueprint_path
    logger.info('Updating deployment {0} using blueprint {1}'.format(
        deployment_id, blueprint_or_archive_path))

    deployment_update = client.deployment_updates.update(
        deployment_id,
        blueprint_or_archive_path,
        application_file_name=blueprint_filename,
        inputs=inputs,
        workflow_id=workflow_id,
        skip_install=skip_install,
        skip_uninstall=skip_uninstall,
        force=force)
    events_logger = get_events_logger(json_output)

    execution = execution_events_fetcher.wait_for_execution(
        client,
        client.executions.get(deployment_update.execution_id),
        events_handler=events_logger,
        include_logs=include_logs,
        timeout=None)  # don't timeout ever

    if execution.error:
        logger.info("Execution of workflow '{0}' for deployment "
                    "'{1}' failed. [error={2}]"
                    .format(execution.workflow_id,
                            execution.deployment_id,
                            execution.error))
        logger.info('Failed updating deployment {dep_id}. Deployment update '
                    'id: {depup_id}. Execution id: {exec_id}'
                    .format(depup_id=deployment_update.id,
                            dep_id=deployment_id,
                            exec_id=execution.id))
        raise SuppressedCloudifyCliError()
    else:
        logger.info("Finished executing workflow '{0}' on deployment "
                    "'{1}'".format(execution.workflow_id,
                                   execution.deployment_id))
        logger.info('Successfully updated deployment {dep_id}. '
                    'Deployment update id: {depup_id}. Execution id: {exec_id}'
                    .format(depup_id=deployment_update.id,
                            dep_id=deployment_id,
                            exec_id=execution.id))


@cfy.command(name='create',
             short_help='Create a deployment [manager only]')
@cfy.options.blueprint_id(required=True)
@cfy.options.deployment_id()
@cfy.options.inputs
@cfy.options.verbose()
@cfy.assert_manager_active
@cfy.pass_client()
@cfy.pass_logger
def manager_create(blueprint_id, deployment_id, inputs, logger, client):
    """Create a deployment on the manager."""
    logger.info('Creating new deployment from blueprint {0}...'.format(
        blueprint_id))
    deployment_id = deployment_id or blueprint_id

    try:
        deployment = client.deployments.create(
            blueprint_id, deployment_id, inputs=inputs)
    except MissingRequiredDeploymentInputError as e:
        logger.info('Unable to create deployment. Not all '
                    'required inputs have been specified...')
        _print_deployment_inputs(client, blueprint_id)
        raise CloudifyCliError(str(e))
    except UnknownDeploymentInputError as e:
        logger.info(
            'Unable to create deployment, an unknown input was specified...')
        _print_deployment_inputs(client, blueprint_id)
        raise CloudifyCliError(str(e))

    logger.info("Deployment created. The deployment's id is {0}".format(
        deployment.id))


@cfy.command(name='delete',
             short_help='Delete a deployment [manager only]')
@cfy.argument('deployment-id')
@cfy.options.force(help=helptexts.IGNORE_LIVE_NODES)
@cfy.options.verbose()
@cfy.assert_manager_active
@cfy.pass_client()
@cfy.pass_logger
def manager_delete(deployment_id, force, logger, client):
    """Delete a deployment from the manager

    `DEPLOYMENT_ID` is the id of the deployment to delete.
    """
    logger.info('Deleting deployment {0}...'.format(deployment_id))
    client.deployments.delete(deployment_id, force)
    logger.info("Deployment deleted")


@cfy.command(name='outputs',
             short_help='Show deployment outputs [manager only]')
@cfy.argument('deployment-id')
@cfy.options.verbose()
@cfy.assert_manager_active
@cfy.pass_client()
@cfy.pass_logger
def manager_outputs(deployment_id, logger, client):
    """Retrieve outputs for a specific deployment

    `DEPLOYMENT_ID` is the id of the deployment to print outputs for.
    """
    logger.info('Retrieving outputs for deployment {0}...'.format(
        deployment_id))
    dep = client.deployments.get(deployment_id, _include=['outputs'])
    outputs_def = dep.outputs
    response = client.deployments.outputs.get(deployment_id)
    outputs_ = StringIO()
    for output_name, output in response.outputs.iteritems():
        outputs_.write(' - "{0}":{1}'.format(output_name, os.linesep))
        description = outputs_def[output_name].get('description', '')
        outputs_.write('     Description: {0}{1}'.format(description,
                                                         os.linesep))
        outputs_.write('     Value: {0}{1}'.format(output, os.linesep))
    logger.info(outputs_.getvalue())


@cfy.command(name='inputs',
             short_help='Show deployment inputs [manager only]')
@cfy.argument('deployment-id')
@cfy.options.verbose()
@cfy.assert_manager_active
@cfy.pass_client()
@cfy.pass_logger
def manager_inputs(deployment_id, logger, client):
    """Retrieve inputs for a specific deployment

    `DEPLOYMENT_ID` is the id of the deployment to print inputs for.
    """
    logger.info('Retrieving inputs for deployment {0}...'.format(
        deployment_id))
    dep = client.deployments.get(deployment_id, _include=['inputs'])
    inputs_ = StringIO()
    for input_name, input in dep.inputs.iteritems():
        inputs_.write(' - "{0}":{1}'.format(input_name, os.linesep))
        inputs_.write('     Value: {0}{1}'.format(input, os.linesep))
    logger.info(inputs_.getvalue())


@cfy.command(name='inputs', short_help='Show deployment inputs [locally]')
@cfy.options.verbose()
@cfy.options.blueprint_id(required=True, multiple_blueprints=True)
@cfy.pass_logger
def local_inputs(blueprint_id, logger):
    """Display inputs for the execution
    """
    env = load_env(blueprint_id)
    logger.info(json.dumps(env.plan['inputs'] or {}, sort_keys=True, indent=2))


@cfy.command(name='outputs', short_help='Show deployment outputs [locally]')
@cfy.options.verbose()
@cfy.options.blueprint_id(required=True, multiple_blueprints=True)
@cfy.pass_logger
def local_outputs(blueprint_id, logger):
    """Display outputs for the execution
    """
    env = load_env(blueprint_id)
    logger.info(json.dumps(env.outputs() or {}, sort_keys=True, indent=2))

{'fraction_non_alphanumeric': 0.11087836894288507, 'fraction_numerical': 0.0038635199925522506, 'mean_word_length': 3.192, 'pattern_counts': {'":': 4, '<': 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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6327449', 'n_tokens_mistral': 6425, 'n_tokens_neox': 6273, 'n_words': 1403}