Ejemplo n.º 1
0
def main():

    rc = 0

    args = arg_parse()
    region = args.region
    instance_state = args.instance_state

    inst_info_all = list()

    client = CfnControl(region=region)
    for inst, info in client.get_instance_info(instance_state=instance_state).items():
        inst_info = list()
        inst_info.append(inst)
        for k, v in info.items():
            if isinstance(v, datetime.datetime):
                v = str(v)[:-6]
            inst_info.append(v)
        inst_info_all.append(inst_info)

    print
    print_header()
    print(155 * '-')
    print('\n'.join('{:<20} {:<20} {:<20.20}  {:<30}  {:<15}  {:<7}  {:<15}  {:<20}'.format(*i) for i in Sort(inst_info_all)))
    print

    return rc
Ejemplo n.º 2
0
def main():

    rc = 0

    args = arg_parse()
    region = args.region

    client = CfnControl(region=region)

    vpc_keys_to_print = [
        'Tag_Name',
        'IsDefault',
        'CidrBlock',
    ]

    all_vpcs = client.get_vpcs()

    for vpc_id, vpc_info in all_vpcs.items():
        lines = '=' * len(vpc_id)
        print('{0}\n{1}\n{2}'.format(lines, vpc_id, lines))
        print('   Subnets: {0}'.format(get_subnets(client, vpc_id)))
        for vpc_k in vpc_keys_to_print:
            try:
                print('   {0} = {1}'.format(vpc_k, vpc_info[vpc_k]))
            except KeyError:
                pass
        print('   Security Groups: {0}'.format(get_sec_groups(client, vpc_id)))
        print("")

    return rc
Ejemplo n.º 3
0
def main():

    rc = 0

    args = arg_parse()

    region = args.region
    asg = args.asg_name

    cfn_client = CfnControl(region=region)

    instances = cfn_client.get_inst_from_asg(asg)

    for i in instances:
        print(' {}'.format(i))

    asg_status = cfn_client.ck_asg_inst_status(asg)

    return rc
Ejemplo n.º 4
0
def main():

    rc = 0

    args = arg_parse()
    region = args.region
    stack_name = args.stack_name

    client = CfnControl(region=region)
    client.get_stack_info(stack_name=stack_name)

    all_events = list()

    events = True

    while events:
        stk_status = get_stack_events(client.client_cfn, stack_name)

        for s in reversed(stk_status):
            event_id = s['EventId']
            if event_id not in all_events:
                all_events.append(event_id)
                try:
                    print('{0} {1} {2}'.format(s['LogicalResourceId'],
                                               s['ResourceStatus'],
                                               s['ResourceStatusReason']))
                except KeyError:
                    print('{0} {1}'.format(s['LogicalResourceId'],
                                           s['ResourceStatus']))
                except Exception as e:
                    raise ValueError(e)

                if s['LogicalResourceId'] == stack_name and s[
                        'ResourceStatus'] == 'ROLLBACK_COMPLETE':
                    events = False
        time.sleep(1)

    return rc
Ejemplo n.º 5
0
def main():

    args = arg_parse()

    region = args.region
    asg = args.asg
    action = args.action

    i = CfnControl(region=region, asg=asg)

    if action == 'enter-stby':
        i.asg_enter_standby()
    elif action == 'stop':
        i.asg_enter_standby()
        i.stop_instances()
    elif action == 'start':
        i.start_instances()
        i.asg_exit_standby()
    elif action == 'exit-stby':
        i.asg_exit_standby()
    elif action == 'status':
        i.ck_asg_status()
        i.ck_inst_status()
Ejemplo n.º 6
0
def main():

    rc = 0
    args = arg_parse()
    rollback = 'ROLLBACK'

    bucket = args.bucket
    create_stack = args.create_stack
    del_stack = args.del_stack
    param_file = args.param_file
    ls_stacks = args.ls_stacks
    ls_all_stack_info = args.ls_all_stack_info
    region = args.region
    stack_name = args.stack_name
    template = args.template
    no_prompt = args.no_prompt
    verbose_param_file = args.verbose_param_file

    errmsg_cr = "Creating a stack requires create flag (-c), stack name (-s), and for new stacks " \
                "the template (-t) flag or for configured stacks, the -f flag for parameters file, " \
                "which includes the template location"

    aws_profile = 'NULL'
    if args.aws_profile:
        aws_profile = args.aws_profile
        print('Using profile {0}'.format(aws_profile))

    if args.no_rollback:
        rollback = 'DO_NOTHING'

    client = CfnControl(region=region, aws_profile=aws_profile)

    if ls_all_stack_info or ls_stacks:
        if ls_all_stack_info and ls_stacks:
            errmsg = "Specify either -l or -a, not both"
            raise ValueError(errmsg)

        if ls_all_stack_info:
            print("Gathering all info on CFN stacks...")
            stacks = client.ls_stacks(show_deleted=False)
            for stack, i in sorted(stacks.items()):
                if len(stack) > 37:
                    stack = stack[:37] + ">"
                print('{0:<42.40} {1:<21.19} {2:<30.28} {3:<.30}'.format(
                    stack, str(i[0]), i[1], i[2]))
        elif ls_stacks:
            print("Listing stacks...")
            stacks = client.ls_stacks(show_deleted=False)
            for stack, i in sorted(stacks.items()):
                print(' {}'.format(stack))
    elif create_stack:
        if stack_name and param_file and not template:
            response = ""
            try:
                response = client.cr_stack(stack_name,
                                           param_file,
                                           verbose=verbose_param_file,
                                           set_rollback=rollback)
            except Exception as cr_stack_err:
                print("Got response: {0}".format(response))
                raise ValueError(cr_stack_err)

        elif template and stack_name:

            if not client.url_check(template):
                if not os.path.isfile(template):
                    errmsg = 'File "{}" does not exists'.format(template)
                    raise ValueError(errmsg)
            try:
                if param_file:
                    param_file = param_file
                    print("Parameters file specified at CLI: {}".format(
                        param_file))

                response = client.cr_stack(stack_name,
                                           param_file,
                                           verbose=verbose_param_file,
                                           set_rollback=rollback,
                                           template=template)
                return response

            except Exception as cr_stack_err:
                if "Member must have length less than or equal to 51200" in e[
                        0]:
                    if bucket:
                        print("Uploading {0} to bucket {1} and creating stack".
                              format(template, bucket))
                        response = ""
                        try:
                            template_url = client.upload_to_bucket(
                                template, bucket, template)
                            response = client.cr_stack(
                                stack_name,
                                param_file,
                                verbose=verbose_param_file,
                                set_rollback=rollback,
                                template=template_url)
                        except Exception as upload_to_bucket_err:
                            print("Got response: {0}".format(response))
                            raise ValueError(upload_to_bucket_err)
                    else:
                        errmsg = "The template has too many bytes (>51,200), use the -b flag with a bucket name, or " \
                             "upload the template to an s3 bucket and specify the bucket URL with the -t flag "
                        raise ValueError(errmsg)
                else:
                    raise ValueError(cr_stack_err)
        elif template and not stack_name:
            raise ValueError(errmsg_cr)
        elif not template and stack_name:
            raise ValueError(errmsg_cr)
        elif not template and not stack_name:
            raise ValueError(errmsg_cr)
    elif del_stack:
        if not stack_name:
            errmsg = "Must specify a stack to delete (-s)"
            raise ValueError(errmsg)
        client.del_stack(stack_name, no_prompt=no_prompt)
    elif param_file or stack_name:
        raise ValueError(errmsg_cr)
    else:
        print("No actions requested - shouldn't have got this far.")
        return 0

    return rc