def main(merge):
    config = Config()
    config.obtain_secret('access_key_id')
    config.obtain_secret('access_key_secret')

    client = config.create_api_client('eu-central-1')  # eu-central-1 frankfurt
    RegionIdSelect(config).show(config)
    ZonesSelect(config).show(config)
    client = config.create_api_client()

    table = []
    request = DescribeSpotPriceHistoryRequest.DescribeSpotPriceHistoryRequest()
    request.set_ZoneId(config.get(['CreateInstanceParams', 'ZoneId']))
    request.set_NetworkType('vpc')
    instance_type = click.prompt('Enter instance type for querying price',
                                 type=str,
                                 default='ecs.gn5-c4g1.xlarge')
    request.set_InstanceType(instance_type)
    start_time = datetime.now() - timedelta(days=29)
    request.set_StartTime(start_time.strftime('%Y-%m-%dT00:00:00Z'))
    result = do_action(client, request)
    for idx, item in enumerate(result['SpotPrices']['SpotPriceType']):
        if merge and idx > 0:
            prev_item = result['SpotPrices']['SpotPriceType'][idx - 1]
            if item['SpotPrice'] != prev_item['SpotPrice']:
                table.append((item['Timestamp'], item['SpotPrice']))
        else:
            table.append((item['Timestamp'], item['SpotPrice']))
    if not table:
        print('Can not find history price in this region')
    print(tabulate(table))
def main(merge):
    config = Config()
    config.obtain_secret('access_key_id')
    config.obtain_secret('access_key_secret')

    client  = config.create_api_client('cn-hongkong')
    RegionIdSelect().show(config, client=client)
    ZonesSelect().show(config)
    client  = config.create_api_client()

    table = []
    request = DescribeSpotPriceHistoryRequest.DescribeSpotPriceHistoryRequest()
    request.set_ZoneId(config.get(['CreateInstanceParams', 'ZoneId']))
    request.set_NetworkType('vpc')
    instance_type = click.prompt('请输入你要查询的实例规格', type=str, default='ecs.gn5-c4g1.xlarge')
    request.set_InstanceType(instance_type)
    start_time = datetime.now() - timedelta(days=29)
    request.set_StartTime(start_time.strftime('%Y-%m-%dT00:00:00Z'))
    result = do_action(client, request)
    for idx, item in enumerate(result['SpotPrices']['SpotPriceType']):
        if merge and idx > 0:
            prev_item = result['SpotPrices']['SpotPriceType'][idx-1]
            if item['SpotPrice'] != prev_item['SpotPrice']:
                table.append((item['Timestamp'], item['SpotPrice']))
        else:
            table.append((item['Timestamp'], item['SpotPrice']))
    if not table:
        print('找不到该区域实例的历史价格记录')
    print(tabulate(table))
Exemplo n.º 3
0
def main():
    config = Config()
    config.obtain_secret('access_key_id')
    config.obtain_secret('access_key_secret')

    click.echo("由于需要对地域,可用区, 实例类型的每种组合都查询一次价格, 查询时间可能较长, 请耐心等待")
    table = []
    client  = config.create_api_client('cn-hongkong')
    regions = get_regions(client)
    for region in regions:
        client  = config.create_api_client(region['RegionId'])
        zones = get_zones(client)
        for zone in zones:
            ins_types = zones[0]['AvailableInstanceTypes']['InstanceTypes']
            ins_types = [t for t in ins_types if t.startswith('ecs.gn')]
            if 'ecs.gn5-c4g1.xlarge' not in ins_types:
                # 由于阿里云方面的bug, DescribeZonesRequest 返回的 AvailableInstanceTypes 可能不全
                # 这里手动插入些最常用的实例类型
                ins_types.append('ecs.gn5-c4g1.xlarge')
            for instance_type in ins_types:
                request = DescribeSpotPriceHistoryRequest.DescribeSpotPriceHistoryRequest()
                request.set_ZoneId(zone['ZoneId'])
                request.set_NetworkType('vpc')

                request.set_InstanceType(instance_type)
                result = do_action(client, request)
                try:
                    item = result['SpotPrices']['SpotPriceType'][-1]
                    table.append([zone['LocalName'], instance_type, item['Timestamp'], item['SpotPrice']])
                except IndexError:
                    pass
    table.sort(key=lambda x: (x[0], x[-1]))
    print(tabulate(table))
def main(max_price, type):
    config = Config()
    config.obtain_secret('access_key_id')
    config.obtain_secret('access_key_secret')

    click.echo("由于需要对地域,可用区, 实例类型的每种组合都查询一次价格, 查询时间可能较长, 请耐心等待")

    client = config.create_api_client('cn-hongkong')
    regions = get_regions(client)
    futures = []
    with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
        for region in regions:
            client = config.create_api_client(region['RegionId'])
            zones = get_zones(client)
            for zone in zones:
                ins_types = zones[0]['AvailableInstanceTypes']['InstanceTypes']
                ins_types = [
                    t for t in ins_types if t.startswith('ecs.{}'.format(type))
                ]
                if 'ecs.gn5-c4g1.xlarge' not in ins_types:
                    # 由于阿里云方面的bug, DescribeZonesRequest 返回的 AvailableInstanceTypes 可能不全
                    # 这里手动插入些最常用的实例类型
                    ins_types.append('ecs.gn5-c4g1.xlarge')
                for instance_type in ins_types:
                    # query_price(config, region, zone, instance_type)
                    future = executor.submit(
                        query_price,
                        config,
                        region,
                        zone,
                        instance_type,
                    )

                    futures.append(future)
    table = []
    for future in concurrent.futures.as_completed(futures):
        try:
            data = future.result()
            table.extend(data)
        except Exception as exc:
            print('Generated an exception: %s' % (exc))

    table = [row for row in table if row[-1] < max_price]
    table.sort(key=lambda x: (x[0], x[-1]))
    print(tabulate(table))
Exemplo n.º 5
0
def main(max_price, type):
    config = Config()
    config.obtain_secret('access_key_id')
    config.obtain_secret('access_key_secret')

    click.echo("This query could take a while... Please wait...")

    client = config.create_api_client('eu-central-1')
    regions = get_regions(client)
    futures = []
    with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
        for region in regions:
            client = config.create_api_client(region['RegionId'])
            zones = get_zones(client)
            for zone in zones:
                ins_types = zones[0]['AvailableInstanceTypes']['InstanceTypes']
                ins_types = [
                    t for t in ins_types if t.startswith('ecs.{}'.format(type))
                ]
                if 'ecs.gn5-c4g1.xlarge' not in ins_types:
                    # there is a open issue for DescribeZonesRequest. because the return of AvailableInstanceTypes may loss some candidates
                    # add common gpu instance type
                    ins_types.append('ecs.gn5-c4g1.xlarge')
                for instance_type in ins_types:
                    # query_price(config, region, zone, instance_type)
                    future = executor.submit(
                        query_price,
                        config,
                        region,
                        zone,
                        instance_type,
                    )

                    futures.append(future)
    table = []
    for future in concurrent.futures.as_completed(futures):
        try:
            data = future.result()
            table.extend(data)
        except Exception as exc:
            print('Generated an exception: %s' % (exc))

    table = [row for row in table if row[-1] < max_price]
    table.sort(key=lambda x: (x[0], x[-1]))
    print(tabulate(table))