From a8a089983535117549b9cb730a5f5ca45b3042da Mon Sep 17 00:00:00 2001 From: root Date: Wed, 29 Dec 2021 13:33:51 +0000 Subject: [PATCH 1/2] Delete snapshot physically from database --- openstack/CleanWlmDatabase/ReadMe.md | 12 ++++ .../CleanWlmDatabase/config/snapshot_ids.txt | 4 ++ openstack/CleanWlmDatabase/snapshot_delete.py | 55 +++++++++++++++++++ .../CleanWlmDatabase/workloads_delete.py | 2 +- 4 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 openstack/CleanWlmDatabase/config/snapshot_ids.txt create mode 100644 openstack/CleanWlmDatabase/snapshot_delete.py diff --git a/openstack/CleanWlmDatabase/ReadMe.md b/openstack/CleanWlmDatabase/ReadMe.md index ce84ef8..b2e9a2a 100644 --- a/openstack/CleanWlmDatabase/ReadMe.md +++ b/openstack/CleanWlmDatabase/ReadMe.md @@ -60,3 +60,15 @@ Utility consists of following artifacts: 3. All the required workloads will be physically deleted from the database along with respective snapshots and related metadata, VMs mapping, etc. 4. Corresponding log files will be created in logs directory. + + +## Steps to delete snapshots from database + +1. Provide ids of snapshot which are to be deleted (*physically*) from the database in config/snapshot_ids.txt file. + +2. Execute snapshot_delete.py script. + + > *python snapshot_delete.py* +3. All the required snapshots will be physically deleted from the database. + +4. Corresponding log files will be created in logs directory. diff --git a/openstack/CleanWlmDatabase/config/snapshot_ids.txt b/openstack/CleanWlmDatabase/config/snapshot_ids.txt new file mode 100644 index 0000000..922db6b --- /dev/null +++ b/openstack/CleanWlmDatabase/config/snapshot_ids.txt @@ -0,0 +1,4 @@ +b1ce2f2b-ed68-40ec-b737-2719bfe51f36 +fb79b4f8-2ba0-442c-8100-fb47c6d401b2 +2514cb28-88f0-4a48-9a09-34aeb44ff543 +c5ab19af-1d1a-4a4d-a717-99c5aed70b6b diff --git a/openstack/CleanWlmDatabase/snapshot_delete.py b/openstack/CleanWlmDatabase/snapshot_delete.py new file mode 100644 index 0000000..0b4e90b --- /dev/null +++ b/openstack/CleanWlmDatabase/snapshot_delete.py @@ -0,0 +1,55 @@ +import os +import datetime +from lib.utility import (setup_logger, + file_manager, + connect_db + ) + + +def delete_snapshots(curr, conn, snapshots_ids): + for snapshot_id in snapshot_ids: + try: + snapshot_exists = "SELECT EXISTS(SELECT * FROM snapshots WHERE id = %s AND deleted = 1 AND status = \"deleted\")" + curr.execute(snapshot_exists, (snapshot_id,)) + temp = curr.fetchall()[0][0] + if temp == 1: + Log_wlm_delete.info("snapshot_id: %s Exists" % snapshot_id) + query = "DELETE FROM snapshots WHERE id = %s" + curr.execute(query, (snapshot_id,)) + Log_wlm_delete.info("snapshot_id: %s removed \n" % snapshot_id) + else: + Log_wlm.info("snapshot_id: %s doesn't Exist \n" % snapshot_id) + conn.commit() + + except Exception as err: + Log_wlm.exception(err) + raise err + + +if __name__ == '__main__': + + conn = None + cursor = None + + dir_name = 'log/snapshotmgr-' + datetime.datetime.now().strftime('%b-%d-%Y-%H:%M:%S') + log_dir = os.path.join(os.getcwd(), dir_name) + os.makedirs(log_dir) + snapshot_gen = log_dir + '/snapshotmgr-snapshot.log' + snapshot_del = log_dir + '/snapshotmgr-snapshot-delete.log' + Log_wlm = setup_logger('snapshotmgr', snapshot_gen) + Log_wlm_delete = setup_logger('snapshotmgr_del', snapshot_del) + try: + conn, cursor = connect_db(Log_wlm) + if cursor: + snapshot_ids = file_manager('config/snapshot_ids.txt', Log_wlm, file_type='text', mode='r') + delete_snapshots(cursor, conn, snapshot_ids) + + except Exception as err: + Log_wlm.exception(err) + + finally: + if conn and conn.is_connected(): + cursor.close() + conn.close() + Log_wlm.info("connection closed \n") + diff --git a/openstack/CleanWlmDatabase/workloads_delete.py b/openstack/CleanWlmDatabase/workloads_delete.py index bf9bfcc..7d1bc34 100644 --- a/openstack/CleanWlmDatabase/workloads_delete.py +++ b/openstack/CleanWlmDatabase/workloads_delete.py @@ -9,7 +9,7 @@ def delete_workload(curr, conn, workload_ids): for workload_id in workload_ids: try: - workload_exists = "SELECT EXISTS(SELECT * FROM workloads WHERE id = %s)" + workload_exists = "SELECT EXISTS(SELECT * FROM workloads WHERE id = %s AND deleted = 1 AND status = \"deleted\")" curr.execute(workload_exists, (workload_id,)) temp = curr.fetchall()[0][0] if temp == 1: From fa270e6d969e96812bb2fcb8c84fab74bf149cb6 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 30 Dec 2021 12:09:01 +0000 Subject: [PATCH 2/2] TVAULT-4840 Changes made in Readme file --- openstack/CleanWlmDatabase/ReadMe.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openstack/CleanWlmDatabase/ReadMe.md b/openstack/CleanWlmDatabase/ReadMe.md index b2e9a2a..942366d 100644 --- a/openstack/CleanWlmDatabase/ReadMe.md +++ b/openstack/CleanWlmDatabase/ReadMe.md @@ -57,7 +57,7 @@ Utility consists of following artifacts: 2. Execute workloads_delete.py script. > *python workloads_delete.py* -3. All the required workloads will be physically deleted from the database along with respective snapshots and related metadata, VMs mapping, etc. +3. All the required workloads which is soft deleted will be physically deleted from the database along with respective snapshots and related metadata, VMs mapping, etc. 4. Corresponding log files will be created in logs directory. @@ -69,6 +69,6 @@ Utility consists of following artifacts: 2. Execute snapshot_delete.py script. > *python snapshot_delete.py* -3. All the required snapshots will be physically deleted from the database. +3. All the required snapshots which is soft deleted will be physically deleted from the database along with respective snapshots VMs and related metadata etc. 4. Corresponding log files will be created in logs directory.