diff --git a/.gitignore b/.gitignore index 3c45dd1a1..0fdf5be1d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,8 @@ settings.yaml +metadata.json +openshift-install +.openshift-install +.openshift_install.log .vscode/ .idea .cache/ diff --git a/cloudwash/cli.py b/cloudwash/cli.py index fb2f441fa..d15263dc9 100644 --- a/cloudwash/cli.py +++ b/cloudwash/cli.py @@ -81,12 +81,13 @@ def azure(ctx, vms, discs, nics, pips, _all, _all_rg): @cleanup_providers.command(help="Cleanup Amazon provider") @common_options @click.option("--pips", is_flag=True, help="Remove only Public IPs from the provider") +@click.option("--ocps", is_flag=True, help="Remove only unused OCPs from the provider") @click.pass_context -def ec2(ctx, vms, discs, nics, pips, _all): +def ec2(ctx, vms, discs, nics, pips, ocps, _all): # Validate Amazon Settings validate_provider(ctx.command.name) is_dry_run = ctx.parent.params["dry"] - ec2Cleanup(vms=vms, discs=discs, nics=nics, pips=pips, _all=_all, dry_run=is_dry_run) + ec2Cleanup(vms=vms, discs=discs, nics=nics, pips=pips, ocps=ocps, _all=_all, dry_run=is_dry_run) @cleanup_providers.command(help="Cleanup VMWare provider") diff --git a/cloudwash/providers/ec2.py b/cloudwash/providers/ec2.py index 1e82a7c90..bab8fb078 100644 --- a/cloudwash/providers/ec2.py +++ b/cloudwash/providers/ec2.py @@ -5,7 +5,7 @@ from cloudwash.utils import dry_data from cloudwash.utils import echo_dry from cloudwash.utils import total_running_time - +from cloudwash.utils import delete_ocp def cleanup(**kwargs): @@ -18,6 +18,7 @@ def cleanup(**kwargs): for region in regions: dry_data['VMS']['stop'] = [] dry_data['VMS']['skip'] = [] + dry_data["OCPS"]["delete"] = [] for items in data: dry_data[items]['delete'] = [] with compute_client("ec2", ec2_region=region) as ec2_client: @@ -51,6 +52,13 @@ def dry_pips(): [dry_data["PIPS"]["delete"].append(dpip["AllocationId"]) for dpip in rpips] return dry_data["PIPS"]["delete"] + def dry_ocps(): + all_ocps = ec2_client.list_ocps() + for ocp in all_ocps: + # TODO: Filter according to the SLA_MINUTES + dry_data["OCPS"]["delete"].append(ocp) + return dry_data["OCPS"]["delete"] + # Remove / Stop VMs def remove_vms(avms): # Remove VMs @@ -82,5 +90,12 @@ def remove_vms(avms): if not is_dry_run: ec2_client.remove_all_unused_ips() logger.info(f"Removed PIPs: \n{rpips}") + if kwargs["ocps"]: + rocps = dry_ocps() + if not is_dry_run: + for ocp in rocps: + delete_ocp(ocp) + ocp_names = [ocp["name"] for ocp in rocps] + logger.info(f"[WIP] Removed OCP clusters: \n{ocp_names}") if is_dry_run: echo_dry(dry_data) diff --git a/cloudwash/utils.py b/cloudwash/utils.py index a1be26c6c..3765c44a2 100644 --- a/cloudwash/utils.py +++ b/cloudwash/utils.py @@ -3,6 +3,7 @@ from datetime import datetime import pytz +import json from cloudwash.logger import logger @@ -11,8 +12,10 @@ "NICS": {"delete": []}, "DISCS": {"delete": []}, "PIPS": {"delete": []}, + "OCPS": {"delete": []}, "RESOURCES": {"delete": []}, } + dry_data.update(_vms_dict) @@ -29,18 +32,22 @@ def echo_dry(dry_data=None) -> None: deletable_discs = dry_data["DISCS"]["delete"] deletable_nics = dry_data["NICS"]["delete"] deletable_pips = dry_data["PIPS"]["delete"] if "PIPS" in dry_data else None + deletable_ocps = [ocp["name"] for ocp in dry_data["OCPS"]["delete"]] deletable_resources = dry_data["RESOURCES"]["delete"] if deletable_vms or stopable_vms or skipped_vms: logger.info( f"VMs:\n\tDeletable: {deletable_vms}\n\tStoppable: {stopable_vms}\n\t" "Skip: {skipped_vms}" ) + if deletable_discs: logger.info(f"DISCs:\n\tDeletable: {deletable_discs}") if deletable_nics: logger.info(f"NICs:\n\tDeletable: {deletable_nics}") if deletable_pips: logger.info(f"PIPs:\n\tDeletable: {deletable_pips}") + if deletable_ocps: + logger.info(f"OCPs:\n\tDeletable: {deletable_ocps}") if deletable_resources: logger.info(f"RESOURCEs:\n\tDeletable: {deletable_resources}") if not any( @@ -102,3 +109,25 @@ def gce_zones() -> list: _zones_combo = {**_bcds, **_abcfs, **_abcs} zones = [f"{loc}-{zone}" for loc, zones in _zones_combo.items() for zone in zones] return zones + +def delete_ocp(ocp): + name = ocp["name"] + logger.info(f"Delete OCP '{name}'") + metadata = ocp["metadata"] + with open("metadata.json", "w") as outfile: + json.dump(metadata, outfile) + # TODO: finish the uninstallation process + # NOTE: this doesn;t direcly use AWS api and thus it should not be in wrapanapi + # + #if exists('openshift-install'): + # logger.info("ocp installer exists") + #else: + # logger.info("ocp installer doesn't exist") + # wget.download('https://mirror.openshift.com/pub/openshift-v4/clients/ocp/latest-4.10/openshift-install-linux.tar.gz') + # tar = tarfile.open('openshift-install-linux.tar.gz', "r:gz") + # tar.extractall() + # tar.close() + #my_env = os.environ.copy() + #my_env["AWS_ACCESS_KEY_ID"] = settings.providers.ec2.username + #my_env["AWS_SECRET_ACCESS_KEY"] = settings.providers.ec2.password + #subprocess.call(['./openshift-install' , 'destroy', 'cluster', '--log-level=debug'], env=my_env)