예제 #1
0
def stream_build_configuration_app_version_creation(app_name,
                                                    app_version_label):
    # Get the CloudWatch logs link
    successfully_generated = wait_for_app_version_attribute(
        app_name, [app_version_label], 'BuildArn', timeout=1)
    app_version_response = elasticbeanstalk.get_application_versions(
        app_name, version_labels=[app_version_label])['ApplicationVersions']

    build_response = codebuild.batch_get_builds([app_version_response[0]['BuildArn']]) \
        if successfully_generated else None

    if build_response is not None and 'logs' in build_response['builds'][0]:
        log_link_text = strings['codebuild.buildlogs'].replace(
            '{logs_link}', build_response['builds'][0]['logs']['deepLink'])
        io.echo(log_link_text)
    else:
        io.log_warning("Could not retrieve CloudWatch link for CodeBuild logs")

    # Wait for the success events
    try:
        from ebcli.operations.commonops import wait_for_success_events
        wait_for_success_events(None,
                                timeout_in_minutes=5,
                                can_abort=False,
                                version_label=app_version_label)
    except ServiceError as ex:
        LOG.debug(
            "Caught service error while creating application version '{0}' "
            "deleting the created applicaiton version as it is useless now.")
        elasticbeanstalk.delete_application_version(app_name,
                                                    app_version_label)
        raise ex
예제 #2
0
def delete_app_version_label(app_name, version_label):

    if version_label:
        # check if version_label exists under app_name
        app_versions = elasticbeanstalk.get_application_versions(app_name)['ApplicationVersions']
        #  if the given version label does not exist at all!
        if not any(version_label == app_version['VersionLabel'] for app_version in app_versions):
            raise ValidationError(strings['appversion.delete.notfound'].format(app_name, version_label))

        envs = elasticbeanstalk.get_app_environments(app_name)

        versions_in_use = [(e.version_label, e.name) for e in envs]

        # find all the environments that are using the app version
        used_envs = [version[1] for version in versions_in_use if version[0] == version_label]

        if used_envs:
            raise ValidationError(strings['appversion.delete.deployed'].format(version_label, ','.join(used_envs)))

        try:
            io.validate_action(prompts['appversion.delete.validate'].format(version_label), "y")
            elasticbeanstalk.delete_application_version(app_name, version_label)
            io.echo('Application Version deleted successfully.')
            delete_successful = True
        except ValidationError:
            io.echo('Application Version will not be deleted.')
            delete_successful = False

        return delete_successful
    else:
        raise NotFoundError(strings['appversion.delete.none'])
예제 #3
0
def delete_app_version_label(app_name, version_label):

    if version_label:
        # check if version_label exists under app_name
        app_versions = elasticbeanstalk.get_application_versions(app_name)['ApplicationVersions']
        #  if the given version label does not exist at all!
        if not any(version_label == app_version['VersionLabel'] for app_version in app_versions):
            raise ValidationError(strings['appversion.delete.notfound'].format(app_name, version_label))

        envs = elasticbeanstalk.get_app_environments(app_name)

        versions_in_use = [(e.version_label, e.name) for e in envs]

        # find all the environments that are using the app version
        used_envs = [version[1] for version in versions_in_use if version[0] == version_label]

        if used_envs:
            raise ValidationError(strings['appversion.delete.deployed'].format(version_label, ','.join(used_envs)))

        try:
            io.validate_action(prompts['appversion.delete.validate'].format(version_label), "y")
            elasticbeanstalk.delete_application_version(app_name, version_label)
            io.echo('Application Version deleted successfully.')
            delete_successful = True
        except ValidationError:
            io.echo('Application Version will not be deleted.')
            delete_successful = False

        return delete_successful
    else:
        raise NotFoundError(strings['appversion.delete.none'])
예제 #4
0
    def do_command(self):
        app_name = self.get_app_name()
        num_to_leave = self.app.pargs.num_to_leave
        older_than = self.app.pargs.older_than
        force = self.app.pargs.force

        envs = elasticbeanstalk.get_app_environments(app_name)
        versions_in_use = [e.version_label for e in envs]

        app_versions = elasticbeanstalk.get_application_versions(
            app_name)['ApplicationVersions']
        app_versions.sort(key=itemgetter('DateUpdated'), reverse=True)

        # Filter out versions currently being used
        app_versions = [
            v for v in app_versions if v['VersionLabel'] not in versions_in_use
        ]

        total_num_unused_versions = len(app_versions)

        if total_num_unused_versions < num_to_leave:
            io.echo(
                'Not enough unused application version to leave behind {0}; No application versions to delete.'
                .format(num_to_leave))
            return

        # Filter out versions newer than filter date
        app_versions = [
            v for v in app_versions if utils.get_delta_from_now_and_datetime(
                v['DateUpdated']).days > older_than
        ]

        # dont include most recent
        app_versions = app_versions[num_to_leave:]

        if app_versions:
            if not force:
                response = io.get_boolean_response(
                    '{} application versions will be deleted. '
                    'Continue?'.format(len(app_versions)))
                if not response:
                    return
        else:
            io.echo('No application versions to delete.')
            return

        for version in app_versions:
            label = version['VersionLabel']
            try:
                elasticbeanstalk.delete_application_version(app_name, label)
            except ServiceError as e:
                io.log_warning('Error deleting version {0}. Error: {1}'.format(
                    label, e.message))
예제 #5
0
def stream_build_configuration_app_version_creation(app_name, app_version_label, build_spec):
    # Get the CloudWatch logs link
    successfully_generated = wait_for_app_version_attribute(
        app_name,
        [app_version_label],
        timeout=1
    )
    app_version_response = elasticbeanstalk.get_application_versions(
        app_name,
        version_labels=[app_version_label]
    )['ApplicationVersions']

    build_response = codebuild.batch_get_builds([app_version_response[0]['BuildArn']]) \
        if successfully_generated else None

    codebuild_timeout = build_spec.timeout or 60
    if build_response is not None and 'logs' in build_response['builds'][0]:
        log_link_text = strings['codebuild.buildlogs'].replace(
            '{logs_link}',
            build_response['builds'][0]['logs']['deepLink']
        )
        io.echo(log_link_text)
        io.echo(
            "NOTE: The CodeBuild timeout is set to {0} minutes, so this "
            "operation may take upto '{0}' minutes to complete.".format(codebuild_timeout)
        )
    else:
        io.log_warning("Could not retrieve CloudWatch link for CodeBuild logs")

    try:
        # Need to lazy-import `ebcli.lib.commonops` because `pytest` is unable to load it
        # at module load-time using Python 2.7 and Python 3.4
        from ebcli.operations import commonops
        timeout_error_message = ' '.join([
            'The CodeBuild build timed out after {} minute(s).'.format(codebuild_timeout),
            "To increase the time limit, use the 'Timeout' option in the 'buildspec.yml' file."
        ])
        commonops.wait_for_success_events(
            app_name=app_name,
            can_abort=False,
            request_id=None,
            timeout_error_message=timeout_error_message,
            timeout_in_minutes=codebuild_timeout,
            version_label=app_version_label
        )

    except ServiceError as exception:
        LOG.debug("Caught service error while creating application version '{0}' "
                  "deleting the created application version as it is useless now.".format(app_version_label))
        elasticbeanstalk.delete_application_version(app_name, app_version_label)
        raise exception
예제 #6
0
def stream_build_configuration_app_version_creation(app_name, app_version_label, build_spec):
    # Get the CloudWatch logs link
    successfully_generated = wait_for_app_version_attribute(app_name, [app_version_label], 'BuildArn', timeout=1)
    app_version_response = elasticbeanstalk.get_application_versions(app_name, version_labels=[app_version_label])['ApplicationVersions']

    build_response = codebuild.batch_get_builds([app_version_response[0]['BuildArn']]) \
        if successfully_generated else None

    codebuild_timeout = build_spec.timeout or 60
    if build_response is not None and 'logs' in build_response['builds'][0]:
        log_link_text = strings['codebuild.buildlogs'].replace('{logs_link}',
                                                               build_response['builds'][0]['logs']['deepLink'])
        io.echo(log_link_text)
        io.echo("NOTE: The CodeBuild timeout is set to {0} minutes, so this operation may take upto '{0}' minutes to complete.".format(codebuild_timeout))
    else:
        io.log_warning("Could not retrieve CloudWatch link for CodeBuild logs")

    # Wait for the success events
    try:
        # Need to lazy-import `ebcli.lib.commonops` because `pytest` is unable to load it
        # at module load-time using Python 2.7 and Python 3.4
        from ebcli.operations import commonops
        timeout_error_message = ' '.join([
            'The CodeBuild build timed out after {} minute(s).'.format(codebuild_timeout),
            "To increase the time limit, use the 'Timeout' option in the 'buildspec.yml' file."
        ])
        commonops.wait_for_success_events(
            app_name=app_name,
            can_abort=False,
            request_id=None,
            timeout_error_message=timeout_error_message,
            timeout_in_minutes=codebuild_timeout,
            version_label=app_version_label
        )

    except ServiceError as exception:
        LOG.debug("Caught service error while creating application version '{0}' "
                  "deleting the created application version as it is useless now.".format(app_version_label))
        elasticbeanstalk.delete_application_version(app_name, app_version_label)
        raise exception
예제 #7
0
def delete_app_version_label(app_name, version_label):

    if version_label:
        app_versions = elasticbeanstalk.get_application_versions(
            app_name)['ApplicationVersions']
        if not any(version_label == app_version['VersionLabel']
                   for app_version in app_versions):
            raise ValidationError(strings['appversion.delete.notfound'].format(
                app_name, version_label))

        envs = elasticbeanstalk.get_app_environments(app_name)

        versions_in_use = [(e.version_label, e.name) for e in envs]

        used_envs = [
            version[1] for version in versions_in_use
            if version[0] == version_label
        ]

        if used_envs:
            raise ValidationError(strings['appversion.delete.deployed'].format(
                version_label, ','.join(used_envs)))

        should_delete = io.get_boolean_response(
            text=prompts['appversion.delete.validate'].format(version_label),
            default=True)
        if not should_delete:
            io.echo('Application Version will not be deleted.')
            delete_successful = False
        else:
            elasticbeanstalk.delete_application_version(
                app_name, version_label)
            io.echo('Application Version deleted successfully.')
            delete_successful = True

        return delete_successful
    else:
        raise NotFoundError(strings['appversion.delete.none'])