Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: Oded Viner <[email protected]>
  • Loading branch information
OdedViner committed Dec 8, 2024
1 parent 7ea1386 commit f6df0be
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
45 changes: 26 additions & 19 deletions ocs_ci/cleanup/ibm/cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def ibm_cleanup():

def delete_buckets(hours):
""" """
status = []
config.ENV_DATA["cluster_path"] = "/"
config.ENV_DATA["cluster_name"] = "cluster"
ibm_cloud_ipi_obj = IBMCloudIPI()
Expand All @@ -50,28 +51,34 @@ def delete_buckets(hours):
try:
ibm_cloud_ipi_obj.delete_bucket(bucket_delete)
except Exception as e:
logger.info(f"Failed to delete {bucket_delete}\nerror: {e}")
log = f"Failed to delete {bucket_delete}\nerror: {e}"
logger.info(log)
status.append(log)
if len(status) > 0:
raise Exception(status)


def buckets_to_delete(buckets, hours):
""" """
"""
Buckets to Delete
Args:
"""
buckets_delete = []
current_time = datetime.utcnow()
for bucket in buckets:
try:
bucket_name = bucket["Name"]
creation_date = datetime.strptime(
bucket["CreationDate"], "%Y-%m-%dT%H:%M:%S.%fZ"
)
# Check if the bucket matches any prefix rule
hours_bucket = hours
for prefix, max_age_hours in defaults.BUCKET_PREFIXES_SPECIAL_RULES.items():
if re.match(prefix, bucket_name):
hours_bucket = max_age_hours
if hours_bucket == "never":
continue
if current_time - creation_date > timedelta(hours=int(hours_bucket)):
buckets_delete.append(bucket_name)
except Exception as e:
logger.error(e)
return buckets_delete
bucket_name = bucket["Name"]
creation_date = datetime.strptime(
bucket["CreationDate"], "%Y-%m-%dT%H:%M:%S.%fZ"
)
# Check if the bucket matches any prefix rule
hours_bucket = hours
for prefix, max_age_hours in defaults.BUCKET_PREFIXES_SPECIAL_RULES.items():
if re.match(prefix, bucket_name):
hours_bucket = max_age_hours
if hours_bucket == "never":
continue
if current_time - creation_date > timedelta(hours=int(hours_bucket)):
buckets_delete.append(bucket_name)
return buckets_delete[:10]
11 changes: 11 additions & 0 deletions ocs_ci/deployment/ibmcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,10 @@ def prepare_custom_vpc_and_network(self):
)

def get_bucket_list(self):
"""
Get Buckets in 'ODF-QE-IBM-COS'
"""
cmd = "ibmcloud resource service-instance 'ODF-QE-IBM-COS' --output json"
proc = exec_cmd(cmd)
instance_objs = json.loads(proc.stdout)
Expand All @@ -563,6 +567,13 @@ def get_bucket_list(self):
return json.loads(proc.stdout).get("Buckets")

def delete_bucket(self, bucket_name):
"""
Delete Bucket
Args:
bucket_name (str): bucket name
"""
cmd = f"ibmcloud cos bucket-location-get --bucket {bucket_name}"
proc = exec_cmd(cmd)
match = re.search(r"Region: (\w+)", proc.stdout.decode("utf-8"))
Expand Down

0 comments on commit f6df0be

Please sign in to comment.