diff --git a/openstack/CleanWlmDatabase/ReadMe.md b/openstack/CleanWlmDatabase/ReadMe.md index ce84ef8..942366d 100644 --- a/openstack/CleanWlmDatabase/ReadMe.md +++ b/openstack/CleanWlmDatabase/ReadMe.md @@ -57,6 +57,18 @@ 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. + + +## 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 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. 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: