예제 #1
0
def lambda_snapshot(event, context):
    """Snapshot a single instance when called by AWS Lambda."""

    # baseline logging for lambda
    utils.configure_logging(context, LOG)

    if not (event and event.get('Records')):
        LOG.warn('lambda_snapshot must be invoked from an SNS topic: %s',
                 str(event))
        return

    records = event.get('Records')
    for record in records:
        sns = record.get('Sns')
        if not sns:
            LOG.warn('lambda_snapshot missing an SNS section: %s', str(event))
            continue

        message = sns.get('Message')
        if not message:
            LOG.warn('lambda_snapshot missing a message section: %s',
                     str(event))
            continue

        message_json = json.loads(message)

        if 'region' not in message_json:
            LOG.warn('lambda_snapshot missing specific keys: %s', str(event))
            continue

        # call the snapshot perform method
        snapshot.perform_snapshot(context, message_json['region'])

        LOG.info('Function lambda_snapshot completed')
예제 #2
0
def lambda_fanout_replication(event, context):
    """Fanout SNS messages to replicate snapshots when called by AWS Lambda."""

    # baseline logging for lambda
    utils.configure_logging(context, LOG)

    # for every region, send to this function
    replication.perform_fanout_all_regions(context)

    LOG.info('Function lambda_fanout_replication completed')
예제 #3
0
def lambda_fanout_snapshot(event, context):
    """Fanout SNS messages to trigger snapshots when called by AWS Lambda."""

    # baseline logging for lambda
    utils.configure_logging(context, LOG)

    # for every region and every instance, send to this function
    snapshot.perform_fanout_all_regions(context)

    LOG.info('Function lambda_fanout_snapshot completed')
예제 #4
0
def main(arv=None):
    """ebs-snapper command line interface."""
    # Check for Python 2.7 (required for Lambda)
    if not (sys.version_info[0] == 2 and sys.version_info[1] == 7):
        raise RuntimeError('ebs-snapper requires Python 2.7')

    # allow 90m for the cli, instead of lambda's 5
    CTX.set_remaining_time_in_millis(60000 * 90)

    parser = argparse.ArgumentParser(
        version=('version %s' % ebs_snapper.__version__),
        description='Configure, cleanup, or take scheduled EBS volume snapshots'
    )

    verbose = parser.add_mutually_exclusive_group()
    verbose.add_argument('-V',
                         dest='loglevel',
                         action='store_const',
                         const=logging.INFO,
                         help="Set log-level to INFO.")
    verbose.add_argument('-VV',
                         dest='loglevel',
                         action='store_const',
                         const=logging.DEBUG,
                         help="Set log-level to DEBUG.")
    parser.set_defaults(loglevel=logging.WARNING)

    # region setting
    parser.add_argument(
        '-t',
        '--tool_region',
        dest='conf_toolregion',
        nargs='?',
        default='us-east-1',
        help="dynamodb & SNS region used by ebs-snapper (us-east-1 is default)"
    )

    # Sub-commands & help
    subparsers = parser.add_subparsers(help='sub-command help')

    # snapshot subcommand (fanout)
    snapshot_help = '''
        execute snapshots for one or more EBS volumes (if due)
    '''
    parser_snapshot = subparsers.add_parser('snapshot', help=snapshot_help)
    parser_snapshot.set_defaults(func=shell_fanout_snapshot)

    # clean subcommand (fanout)
    clean_help = '''
        clean up one or more EBS snapshots (if due)
    '''
    parser_clean = subparsers.add_parser('clean', help=clean_help)
    parser_clean.set_defaults(func=shell_fanout_clean)

    # snapshot replication subcommand (fanout)
    snapshot_replication_help = '''
        replicate snapshots to secondary region or clean up existing replicated snapshots
    '''
    parser_snapshot_replication = subparsers.add_parser(
        'replication', help=snapshot_replication_help)
    parser_snapshot_replication.set_defaults(
        func=shell_fanout_snapshot_replication)

    # deploy subcommand
    deploy_help = '''
        deploy this tool (or update to a new version) on the account
    '''
    parser_deploy = subparsers.add_parser('deploy', help=deploy_help)
    parser_deploy.add_argument('-a',
                               '--aws_account_id',
                               nargs='?',
                               default=None)
    parser_deploy.add_argument('-n',
                               '--no_build',
                               dest='no_build',
                               action='store_const',
                               const=True,
                               default=False)
    parser_deploy.add_argument('-m',
                               '--no_upload',
                               dest='no_upload',
                               action='store_const',
                               const=True,
                               default=False)
    parser_deploy.add_argument('-o',
                               '--no_stack',
                               dest='no_stack',
                               action='store_const',
                               const=True,
                               default=False)
    parser_deploy.set_defaults(func=shell_deploy)

    # configure subcommand (get, set, delete)
    config_help = '''
        manipulate cleanup and snapshot configuration settings
    '''
    parser_configure = subparsers.add_parser('configure', help=config_help)
    parser_configure.set_defaults(func=shell_configure)

    # what action for configure?
    action_group = parser_configure.add_mutually_exclusive_group(required=True)
    action_group.add_argument(
        '-c',
        '--check',
        dest='conf_action',
        action='store_const',
        const='check',
        help="Sanity check for poorly configured backups")
    action_group.add_argument('-l',
                              '--list',
                              dest='conf_action',
                              action='store_const',
                              const='list',
                              help="List configuration items")
    action_group.add_argument('-g',
                              '--get',
                              dest='conf_action',
                              action='store_const',
                              const='get',
                              help="Get configuration item")
    action_group.add_argument('-s',
                              '--set',
                              dest='conf_action',
                              action='store_const',
                              const='set',
                              help="Set configuration item")
    action_group.add_argument('-d',
                              '--delete',
                              dest='conf_action',
                              action='store_const',
                              const='del',
                              help="Delete configuration item")
    action_group.set_defaults(conf_action=None)

    # configure parameters
    parser_configure.add_argument('-a',
                                  '--aws_account_id',
                                  nargs='?',
                                  default=None)
    parser_configure.add_argument('-e', '--extra', nargs='?', default=None)
    parser_configure.add_argument('object_id', nargs='?', default=None)
    parser_configure.add_argument('configuration_json',
                                  nargs='?',
                                  default=None)

    # do all the things!
    try:
        args = parser.parse_args()

        # baseline logging for shell, ensure boto stays quiet
        utils.configure_logging(CTX, LOG, args.loglevel)

        args.func(args)
    except Exception:  # pylint: disable=broad-except
        print('Unexpected error. Please report this traceback.',
              file=sys.stderr)

        traceback.print_exc()
        sys.stderr.flush()
        sys.exit(1)