def cleanup_ibmc_ceph_nodes(ibm_cred, pattern): """ Clean up the DNS records, instance and volumes that matches the given pattern. Args: ibm_cred global configuration file(ibm) pattern pattern to match instance name """ glbs = ibm_cred.get("globals") ibmc = glbs.get("ibm-credentials") ibmc_client = get_ibm_service(access_key=ibmc["access-key"], service_url=ibmc["service-url"]) resp = ibmc_client.list_instances(vpc_name=ibmc["vpc_name"]) resources = resp.get_result() instances = [i for i in resources["instances"] if pattern in i["name"]] # Throttling removal otherwise Cloudflare will blacklist us counter = 0 with parallel() as p: for instance in instances: sleep(counter * 3) vsi = CephVMNodeIBM( access_key=ibmc["access-key"], service_url=ibmc["service-url"], node=instance, ) p.spawn(vsi.delete, ibmc["zone_name"]) counter += 1 log.info(f"Done cleaning up nodes with pattern {pattern}")
def cleanup_ibmc_ceph_nodes(ibm_cred, pattern): """ Clean up the DNS records, instance and volumes that matches the given pattern. Args: ibm_cred global configuration file(ibm) pattern pattern to match instance name """ glbs = ibm_cred.get("globals") ibmc = glbs.get("ibm-credentials") ibmc_client = get_ibm_service(access_key=ibmc["access-key"], service_url=ibmc["service-url"]) resp = ibmc_client.list_instances(vpc_name=ibmc["vpc_name"]) if resp.get_status_code() != 200: log.warn("Failed to retrieve instances") return 1 instances = [ i for i in resp.get_result()["instances"] if pattern in i["name"] ] while "next" in resp.get_result().keys(): start = resp.get_result()["next"]["href"].split("start=")[-1] resp = ibmc_client.list_instances(start=start, vpc_name=ibmc["vpc_name"]) if resp.get_status_code() != 200: log.warn("Failed to fetch instance details, breaking out.") break instance_list = [ i for i in resp.get_result()["instances"] if pattern in i["name"] ] instances += instance_list # Throttling removal otherwise Cloudflare will blacklist us counter = 0 with parallel() as p: for instance in instances: sleep(counter * 3) vsi = CephVMNodeIBM( access_key=ibmc["access-key"], service_url=ibmc["service-url"], node=instance, ) p.spawn(vsi.delete, ibmc["zone_name"]) counter += 1 log.info(f"Done cleaning up nodes with pattern {pattern}")
def run(args: Dict): """ Using the provided credential file, this method removes the orphan volume entries from ibm cloud. Arguments: args: Dict - containing the key/value pairs passed by the user Returns: 0 on success or 1 for failures """ cred_file = args["--creds"] with open(cred_file, "r") as cred_stream: yh = yaml.safe_load(cred_stream) ibm_cred = yh["globals"]["ibm-credentials"] ibmc_client = get_ibm_service(access_key=ibm_cred["access-key"], service_url=ibm_cred["service-url"]) resp = ibmc_client.list_volumes() if resp.get_status_code() != 200: print("\nFailed to retrieve volumes\n") return 1 response = resp.get_result() orphan_volumes = [ volume for volume in response["volumes"] if volume["status"] == "available" and not volume["volume_attachments"] ] if not orphan_volumes: print("\nThere are no orphan volumes in the IBM environment\n") return 0 sleep_time = 1 for volume in orphan_volumes: print(f"\nDeleting orphan volume {volume['name']}\n") resp = ibmc_client.delete_volume(id=volume["id"]) if resp.get_status_code() != 200: print("\nFailed to delete volume\n") return 1 sleep(sleep_time) sleep_time += 1 print( "\nSuccessfully removed the orphan volumes from IBM environment\n") return 0
def run(args: Dict): """ Using the provided credential file, this method removes the orphan DNS entries from ibm cloud. Arguments: args: Dict - containing the key/value pairs passed by the user Returns: 0 on success or 1 for failures """ cred_file = args["--creds"] with open(cred_file, "r") as cred_stream: yh = yaml.safe_load(cred_stream) ibm_cred = yh["globals"]["ibm-credentials"] ibmc_client = get_ibm_service(access_key=ibm_cred["access-key"], service_url=ibm_cred["service-url"]) dns_client = get_dns_service(access_key=ibm_cred["access-key"]) dns_zone = dns_client.list_dnszones(instance_id) if dns_zone.get_status_code() != 200: print( f"Failed to get dns zone for given instance id: {instance_id}") return 1 dns_zone_id = get_dns_zone_id(ibm_cred["zone_name"], dns_zone.get_result()) resource = dns_client.list_resource_records(instance_id=instance_id, dnszone_id=dns_zone_id) if resource.get_status_code() != 200: print( f"Failed to get dns records from zone: {ibm_cred['zone_name']}" ) return 1 records = resource.get_result() resp = ibmc_client.list_instances(vpc_name=ibm_cred["vpc_name"]) if resp.get_status_code() != 200: print("Failed to retrieve instances") return 1 instances = resp.get_result()["instances"] if "next" in resp.get_result().keys(): start = resp.get_result()["next"]["href"].split("start=")[-1] iteration = math.ceil(resp.get_result()["total_count"] / resp.get_result()["limit"]) for i in range(1, iteration): list_inst = ibmc_client.list_instances( start=start, vpc_name=ibm_cred["vpc_name"]) if list_inst.get_status_code() != 200: print("Failed to retrieve instances") return 1 list_instances = list_inst.get_result()["instances"] instances += list_instances if "next" in list_inst.get_result().keys(): start = list_inst.get_result()["next"]["href"].split( "start=")[-1] if len(instances) != resp.get_result()["total_count"]: print( f"Failed to list all the instances, total:{resp.get_result()['total_count']} listed:{len(instances)}" ) return 1 ip_address = [ i["primary_network_interface"]["primary_ipv4_address"] for i in instances ] for record in records["resource_records"]: if record["type"] == "A" and record["rdata"][ "ip"] not in ip_address: if not record["name"].startswith("ceph-qe"): if record.get("linked_ptr_record"): print( f"Deleting PTR record {record['linked_ptr_record']['name']}" ) dns_client.delete_resource_record( instance_id=instance_id, dnszone_id=dns_zone_id, record_id=record["linked_ptr_record"]["id"], ) print(f"Deleting Address record {record['name']}") dns_client.delete_resource_record( instance_id=instance_id, dnszone_id=dns_zone_id, record_id=record["id"], ) print( "\nSuccessfully removed the orphan DNS record from IBM environment\n" ) return 0