Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion openstack/CleanWlmDatabase/ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
4 changes: 4 additions & 0 deletions openstack/CleanWlmDatabase/config/snapshot_ids.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
b1ce2f2b-ed68-40ec-b737-2719bfe51f36
fb79b4f8-2ba0-442c-8100-fb47c6d401b2
2514cb28-88f0-4a48-9a09-34aeb44ff543
c5ab19af-1d1a-4a4d-a717-99c5aed70b6b
55 changes: 55 additions & 0 deletions openstack/CleanWlmDatabase/snapshot_delete.py
Original file line number Diff line number Diff line change
@@ -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\")"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't we fetch all the given snaps at once, something like SELECT * FROM snapshots WHERE id in (<list_of_snap_ids>) 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')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it could have been better to use argparser and take this config file from user as an argument instead of hard coding it.

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")

2 changes: 1 addition & 1 deletion openstack/CleanWlmDatabase/workloads_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -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\")"
Copy link
Copy Markdown
Contributor

@dhiraj-trilio dhiraj-trilio Dec 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we fetch all the workloads at once ? and process further ?

curr.execute(workload_exists, (workload_id,))
temp = curr.fetchall()[0][0]
if temp == 1:
Expand Down