#!/usr/bin/env python
# Monocyte - Search and Destroy unwanted AWS Resources relentlessly.
# Copyright 2015 Immobilien Scout GmbH
#
# 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.

"""
usage:
    monocyte [options]

options:
    --dry-run=bool valid values "True" or "False" [default: True]
    --use-handlers=HANDLER1,HANDLER2,...    aws handlers to use (order might be important)
        [default: cloudformation.Stack, ec2.Instance, ec2.Volume, rds2.Instance, rds2.Snapshot, dynamodb.Table, s3.Bucket]
    --allowed-regions-prefixes=PREFIX1,PREFIX2,...  allowed regions start with these prefixes [default: eu]
    --ignored-regions=REGION1,REGION2,...  regions to be ignored [default: cn-north-1, us-gov-west-1]
    --cwl-groupname=GROUPNAME  log events to CloudWatchLogs Group
"""


import sys

from docopt import docopt

from monocyte import Monocyte


def run():
    arguments = docopt(__doc__)
    dry_run = (arguments["--dry-run"] != "False")
    handlers = [handler.strip() for handler in arguments["--use-handlers"].split(",")]
    allowed_regions_prefixes = [prefix.strip() for prefix in arguments["--allowed-regions-prefixes"].split(",")]
    ignored_regions = [region.strip() for region in arguments["--ignored-regions"].split(",")]
    cloudwatchlogs_groupname = arguments["--cwl-groupname"]

    monocyte = Monocyte(
        allowed_regions_prefixes=allowed_regions_prefixes,
        ignored_regions=ignored_regions,
        cloudwatchlogs_groupname=cloudwatchlogs_groupname)
    sys.exit(monocyte.search_and_destroy_unwanted_resources(handlers, dry_run))


if __name__ == "__main__":
    run()
