Skip to content

Commit

Permalink
Add support for deleting OCP clusters
Browse files Browse the repository at this point in the history
Signed-off-by: Andrej Podhradsky <apodhrad@redhat.com>
  • Loading branch information
apodhrad committed Jul 28, 2022
1 parent 8a85f38 commit 9ddca14
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
settings.yaml
metadata.json
openshift-install
.openshift-install
.openshift_install.log
.vscode/
.idea
.cache/
Expand Down
5 changes: 3 additions & 2 deletions cloudwash/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
17 changes: 16 additions & 1 deletion cloudwash/providers/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
29 changes: 29 additions & 0 deletions cloudwash/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import datetime

import pytz
import json

from cloudwash.logger import logger

Expand All @@ -11,8 +12,10 @@
"NICS": {"delete": []},
"DISCS": {"delete": []},
"PIPS": {"delete": []},
"OCPS": {"delete": []},
"RESOURCES": {"delete": []},
}

dry_data.update(_vms_dict)


Expand All @@ -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(
Expand Down Expand Up @@ -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)

0 comments on commit 9ddca14

Please sign in to comment.