Esempio n. 1
0
def uninstall(deployment_id, workflow_id, parameters,
              allow_custom_parameters, timeout, include_logs, json):

    # Although the `uninstall` command does not use the `force` argument,
    # we are using the `executions start` handler as a part of it.
    # As a result, we need to provide it with a `force` argument, which is
    # defined below.
    force = False

    # if no workflow was supplied, execute the `uninstall` workflow
    if not workflow_id:
        workflow_id = DEFAULT_UNINSTALL_WORKFLOW

    executions.start(workflow_id=workflow_id,
                     deployment_id=deployment_id,
                     timeout=timeout,
                     force=force,
                     allow_custom_parameters=allow_custom_parameters,
                     include_logs=include_logs,
                     parameters=parameters,
                     json=json)

    # before deleting the deployment, save its blueprint_id, so we will be able
    # to delete the blueprint after deleting the deployment
    client = utils.get_rest_client()
    deployment = client.deployments.get(deployment_id,
                                        _include=['blueprint_id'])
    blueprint_id = deployment.blueprint_id

    deployments.delete(deployment_id, ignore_live_nodes=False)

    blueprints.delete(blueprint_id)
Esempio n. 2
0
def install(blueprint_path, blueprint_id, validate_blueprint, archive_location,
            blueprint_filename, deployment_id, inputs, workflow_id, parameters,
            allow_custom_parameters, timeout, include_logs,
            auto_generate_ids, json):

    # First, make sure the `blueprint_path` wasn't supplied with
    # `archive_location` or with `blueprint_filename`
    _check_for_mutually_exclusive_arguments(blueprint_path,
                                            archive_location,
                                            blueprint_filename)

    # The presence of the `archive_location` argument is used to distinguish
    # between `install` in 'blueprints upload' mode,
    # and `install` in 'blueprints publish archive' mode.
    if archive_location:
        blueprints.check_if_archive_type_is_supported(archive_location)

        if not blueprint_filename:
            blueprint_filename = DEFAULT_BLUEPRINT_FILE_NAME

        # If blueprint_id wasn't supplied, assign it to the name of the archive
        if not blueprint_id:
            (archive_location, archive_location_type) = \
                blueprints.determine_archive_type(archive_location)
            # if the archive is a local path, assign blueprint_id the name of
            # the archive file without the extension
            if archive_location_type == 'path':
                filename, ext = os.path.splitext(
                    os.path.basename(archive_location))
                blueprint_id = filename
            # if the archive is a url, assign blueprint_id name of the file
            # that the url leads to, without the extension.
            # e.g. http://example.com/path/archive.zip?para=val#sect -> archive
            elif archive_location_type == 'url':
                path = urlparse.urlparse(archive_location).path
                archive_file = path.split('/')[-1]
                archive_name = archive_file.split('.')[0]
                blueprint_id = archive_name
            else:
                raise CloudifyCliError("The archive's source is not a local "
                                       'file path nor a web url')

        # auto-generate blueprint id if necessary
        if _auto_generate_ids(auto_generate_ids):
            blueprint_id = _generate_suffixed_id(blueprint_id)

        blueprints.publish_archive(archive_location,
                                   blueprint_filename,
                                   blueprint_id)
    else:
        blueprint_path_supplied = bool(blueprint_path)
        if not blueprint_path:
            blueprint_path = os.path.join(utils.get_cwd(),
                                          DEFAULT_BLUEPRINT_PATH)

        # If blueprint_id wasn't supplied, assign it to the name of
        # folder containing the application's blueprint file.
        if not blueprint_id:
            blueprint_id = os.path.basename(
                os.path.dirname(
                    os.path.abspath(blueprint_path)))

        # Try opening `blueprint_path`, since `blueprints.upload` expects the
        # `blueprint_path` argument to be a file.
        # (The reason for this is beyond me. That's just the way it is)

        if _auto_generate_ids(auto_generate_ids):
            blueprint_id = _generate_suffixed_id(blueprint_id)

        try:
            with open(blueprint_path) as blueprint_file:
                blueprints.upload(blueprint_file,
                                  blueprint_id,
                                  validate_blueprint)
        except IOError as e:

            # No such file or directory
            if not blueprint_path_supplied and e.errno == errno.ENOENT:
                raise CloudifyCliError(
                    'Your blueprint was not found in the path: {0}.\n\n'
                    'Consider providing an explicit path to your blueprint '
                    'using the `-p`/`--blueprint-path` flag, like so:\n'
                    '`cfy install -p /path/to/blueprint_file.yaml`\n'
                    .format(blueprint_path)
                )
            else:
                raise CloudifyCliError(
                    'A problem was encountered while trying to open '
                    '{0}.\n({1})'.format(blueprint_path, e))

    # If deployment_id wasn't supplied, use the same name as the blueprint id.
    if not deployment_id:
        deployment_id = blueprint_id

    # generate deployment-id suffix if necessary
    if _auto_generate_ids(auto_generate_ids):
        deployment_id = _generate_suffixed_id(deployment_id)

    # If no inputs were supplied, and there is a file named inputs.yaml in
    # the cwd, use it as the inputs file
    if not inputs:
        if os.path.isfile(
            os.path.join(utils.get_cwd(),
                         DEFAULT_INPUTS_PATH_FOR_INSTALL_COMMAND)):

            inputs = DEFAULT_INPUTS_PATH_FOR_INSTALL_COMMAND

    deployments.create(blueprint_id,
                       deployment_id,
                       inputs)

    # although the `install` command does not need the `force` argument,
    # we *are* using the `executions start` handler as a part of it.
    # as a result, we need to provide it with a `force` argument, which is
    # defined below.
    force = False

    # if no workflow was supplied, execute the `install` workflow
    if not workflow_id:
        workflow_id = DEFAULT_INSTALL_WORKFLOW

    executions.start(workflow_id=workflow_id,
                     deployment_id=deployment_id,
                     timeout=timeout,
                     force=force,
                     allow_custom_parameters=allow_custom_parameters,
                     include_logs=include_logs,
                     parameters=parameters,
                     json=json)