def start_install_workflow(test_name, timeout):
    logger.info(GREEN + 'Installing...' + RESET)
    try:
        executions_list(test_name)
        executions_start('install', test_name, timeout)
    except EcosystemTimeout:
        # Give 5 seconds grace.
        executions_list(test_name)
        wait_for_execution(test_name, 'install', 10)
    else:
        wait_for_execution(test_name, 'install', timeout)
def resume_install_workflow(test_name, timeout):
    exec_id = find_install_execution_to_resume(test_name)
    logger.debug('execution to resume: {id}'.format(id=exec_id))
    try:
        logger.info('resuming...')
        executions_resume(exec_id, timeout)
    except EcosystemTimeout:
        # Give 5 seconds grace.
        executions_list(test_name)
        wait_for_execution(test_name, 'install', 10)
    else:
        wait_for_execution(test_name, 'install', timeout)
def _basic_blueprint_test(blueprint_file_name,
                          test_name,
                          inputs=None,
                          timeout=None,
                          endpoint_name=None,
                          endpoint_value=None):
    """
    Simple blueprint install/uninstall test.
    :param blueprint_file_name:
    :param test_name:
    :param inputs:
    :param timeout:
    :return:
    """

    timeout = timeout or TIMEOUT
    if inputs != '':
        inputs = inputs or os.path.join(
            os.path.dirname(blueprint_file_name), 'inputs/test-inputs.yaml')
    logger.info('Blueprints list: {0}'.format(
        cloudify_exec('cfy blueprints list')))
    blueprints_upload(blueprint_file_name, test_name)
    logger.info('Deployments list: {0}'.format(
        cloudify_exec('cfy deployments list')))
    deployments_create(test_name, inputs)
    sleep(5)
    logger.info(GREEN + 'Installing...' + RESET)
    try:
        executions_list(test_name)
        executions_start('install', test_name, timeout)
    except EcosystemTimeout:
        # Give 5 seconds grace.
        executions_list(test_name)
        wait_for_execution(test_name, 'install', 10)
    else:
        wait_for_execution(test_name, 'install', timeout)
    if endpoint_name and endpoint_value:
        verify_endpoint(
            get_deployment_output_by_name(
                test_name,
                endpoint_name
            ), endpoint_value)
    logger.info(BLUE + 'Uninstalling...' + RESET)
    executions_start('uninstall', test_name, timeout)
    wait_for_execution(test_name, 'uninstall', timeout)
    try:
        deployment_delete(test_name)
        blueprints_delete(test_name)
    except Exception as e:
        logger.info(RED +
                    'Failed to delete blueprint, {0}'.format(str(e)) +
                    RESET)
def find_executions_to_cancel(deployment_id):
    """
    Find all the executions to cancel.
    :param deployment_id:
    :return:
    """
    executions = executions_list(deployment_id)
    try:
        # Get all install and update executions
        filtered_executions = \
            [e['id'] for e in executions if
             e['workflow_id'] in ['install', 'update'] and e['status'].lower()
             in ['pending', 'started']]
        # For debugging
        logger.info(
            "these are potential executions to cancel: {executions}".format(
                executions=filtered_executions))
    except (IndexError, KeyError):
        logger.info(
            'Workflows to cancel for deployment {dep_id} was not '
            'found.'.format(
                dep_id=deployment_id))
        filtered_executions = []

    return filtered_executions
def find_install_execution_to_resume(deployment_id):
    """
    Find the last install execution to resume.
    :param deployment_id:
    :return:
    """
    executions = executions_list(deployment_id)
    try:
        # Get the last install execution
        ex = [e for e in executions
              if 'install' == e['workflow_id']][-1]
        # For debugging
        logger.info("these are potential executions to resume")
        logger.info([e for e in executions if 'install' == e['workflow_id']])
    except (IndexError, KeyError):
        raise EcosystemTestException(
            'Workflow install to resume for deployment {dep_id} was not '
            'found.'.format(
                dep_id=deployment_id))

    if ex['status'].lower() not in ['failed', 'cancelled']:
        raise EcosystemTestException(
            'Found install execution with id: {id} but with status {status},'
            'can`t resume this execution'.format(
                id=ex['id'], status=ex['status']))
    return ex['id']
def handle_deployment_update(blueprint_file_name,
                             update_bp_name,
                             test_name,
                             inputs,
                             timeout):
    logger.info('updating deployment...')
    try:
        logger.info('Blueprints list: {0}'.format(
            cloudify_exec('cfy blueprints list')))
        blueprints_upload(blueprint_file_name, update_bp_name)
        deployment_update(test_name,
                          update_bp_name,
                          inputs,
                          timeout)
    except EcosystemTimeout:
        # Give 5 seconds grace.
        executions_list(test_name)
        wait_for_execution(test_name, 'update', 10)
    else:
        wait_for_execution(test_name, 'update', timeout)