# -*- coding: UTF-8 -*- import time import sys import os.path import json from core import config_endpoint_services_args, validate_env, read_configuration, \ list_subnets, describe_target_health, list_listeners, list_nlb_list, \ list_target_groups, register_listener, list_endpoint_service, create_endpoint_service_conf, \ create_nlb, create_target_group, register_targets, is_nlb_active, read_vpc_configuration, \ list_vpcs from core.tag import Tag from config_inter_region_vpc import VPC_NAME def get_or_create_endpoint_service(region, profile, env, vpc_id, ip, subnet_ids, broker_id): es_name = f"ES-tieventbus-{env}-{broker_id}" endpoint_services = list_endpoint_service(region, profile, [es_name]) if len(endpoint_services) != 0: return endpoint_services[0] tags = [ Tag("environment", env), Tag("component", "cloud-platform-eventbus"), Tag("servicetype", "dedicated"), Tag("usedby", "dbaas-control-plane-base"), ] def get_or_create_tg(): tg_name = f"TG-tieventbus-{env}-{broker_id}" target_groups = list_target_groups(region, profile, vpc_id, tg_name) if len(target_groups) == 0: tg = create_target_group(region, profile, tg_name, vpc_id, tags) else: tg = target_groups[0] arn = tg['TargetGroupArn'] desc = describe_target_health(region, profile, arn) if len(desc) == 0: register_targets(region, profile, arn, ip) return arn def get_or_create_nlb(): nlb_name = f"NLB-tieventbus-{env}-{broker_id}" nlb_list = list_nlb_list(region, profile, vpc_id, nlb_name) if len(nlb_list) != 0: nlb = nlb_list[0] else: nlb = create_nlb(region, profile, nlb_name, subnet_ids, tags) arn = nlb["LoadBalancerArn"] listeners = list_listeners(region, profile, arn) if len(listeners) == 0: tg_arn = get_or_create_tg() register_listener(region, profile, arn, tg_arn, tags) return arn nlb_arn = get_or_create_nlb() while not is_nlb_active(region, profile, nlb_arn): time.sleep(5) create_endpoint_service_conf(region, profile, nlb_arn, tags+[Tag("Name", es_name)]) endpoint_services = list_endpoint_service(region, profile, [es_name]) return endpoint_services[0] def main(): args = config_endpoint_services_args() env = args['env'] validate_env(env) profile = args['profile'] region = args['region'] config = read_configuration(env) vpc_config = read_vpc_configuration()["region_vpcs"][region] vpc_cidr = vpc_config['cidr'] vpcs = list_vpcs(region, profile, vpc_cidr, VPC_NAME) if len(vpcs) == 0: print("There are no available vpcs.") exit(1) vpc_id = vpcs[0]["VpcId"] subnets = list_subnets(region, profile, vpc_id) if len(subnets) == 0: print("failed to retrieve subnets, please check") sys.exit(1) brokers = config['brokers'] for index, b in enumerate(brokers): ip = b['ip'] host = b['host'] broker_id = host.split(".")[0] subnet_ids = [sn['SubnetId'] for sn in subnets] es = get_or_create_endpoint_service(region, profile, env, vpc_id, ip, subnet_ids, broker_id) result = { "broker": host, "service_name": es["ServiceName"], "az_ids": [sn['AvailabilityZoneId'] for sn in subnets], } with open(os.path.join(os.path.dirname(__file__), f"{env}_{region}.jsonl"), "a") as fd: json.dump(result, fd) fd.write("\n") if __name__ == '__main__': main()