Esempio n. 1
0
def lsec2():
    '''List instances types used in EC2
    '''
    if len(sys.argv) < 3:
        print(
            'usage: {0} {1} \'name filter\''.format(
                sys.argv[0], lsec2.__name__
            )
        )
        sys.exit(1)

    t = {}
    total_price = zero_price
    fulllist = ap.get_all_instances(
        ec2.region, 'ec2', 'ri-v2/linux-unix-shared'
    )
    for i in ec2.resource.instances.filter(
        Filters=[{'Name': 'tag:Name', 'Values': [sys.argv[2]]}]
    ):
        az = i.placement['AvailabilityZone'].split('-')[-1]
        if not az in t:
            t[az] = {i.instance_type: 0}

        if i.instance_type in t[az]:
            t[az][i.instance_type] += 1
        else:
            t[az][i.instance_type] = 1

        prices = ap.instance_price(fulllist, i.instance_type)
        total_price = _update_price(total_price, prices)

    print(yaml.dump(t, default_flow_style=False))

    _print_total_price(total_price)
Esempio n. 2
0
def lsyaml():
    '''Lists instances types used in the descriptive ``yaml`` file
    '''
    t = {}
    fulllist = ap.get_all_instances(
        ec2.region, 'ec2', 'ri-v2/linux-unix-shared'
    )
    total_price = zero_price
    for f in sys.argv[2:]:
        y = getyaml(lsyaml.__name__, f)

        for reg in y: # loop through profiles
            for azlst in y[reg]: # loop through AZ list
                for az in azlst: # loop through AZ
                    for instance in azlst[az]:
                        saz = az.split('-')[-1]
                        if not saz in t:
                            t[saz] = {instance['type']:  0}
    
                        if instance['type'] in t[saz]:
                            t[saz][instance['type']] += 1
                        else:
                            t[saz][instance['type']] = 1

                        prices = ap.instance_price(fulllist, instance['type'])
                        total_price = _update_price(total_price, prices)

    print(yaml.dump(t, default_flow_style=False))

    _print_total_price(total_price)