INFRA-2270 | Ashvin | Migrate using psql

This file was earlier written to migrate using API but because lack of metadata, submit does not go through. Now the logic queries direct to database instead.
This commit is contained in:
Ashvin Sharma
2023-10-03 18:51:54 +05:30
parent 9a4aa3b105
commit 7471fe0ce4

View File

@@ -1,48 +1,110 @@
import time
import requests
#!/usr/bin/python3
url = "" # deployment portal url
s = requests.Session()
cookie = "" # cookie
x_xsrf_token = "" # token
s.headers.update({'cookie': cookie, 'x-xsrf-token': x_xsrf_token})
list_manifest_path = "/api/manifest/list"
r = s.get(url + list_manifest_path)
manifest_list = r.json()
import json
for i in manifest_list:
perform_post = False
r = s.get(url + "/api/manifest/" + str(i['id']))
manifest = r.json()
if 'deployment' in manifest:
deployment = manifest['deployment']
if 'alerts' in deployment:
alerts = deployment['alerts']
for alert_type, alert_list in alerts.items():
for alert in alert_list:
if isinstance(alert, dict) and 'threshold' in alert and isinstance(
alert['threshold'], str) and alert['threshold'].isdigit():
perform_post = True
alert['threshold'] = int(alert['threshold'])
import psycopg2
if 'elasticSearch' in deployment:
elasticSearch = deployment['elasticSearch']
if 'instance' in elasticSearch:
if 'cpu' in elasticSearch['instance'] and isinstance(
elasticSearch['instance']['cpu'], str) and elasticSearch['instance'][
'cpu'].isdigit():
perform_post = True
instance['cpu'] = int(instance['cpu'])
db_params = {
'dbname': '',
'user': '',
'password': '',
'host': '',
'port': ''
}
if manifest['metadata']['disasterRecovery'] == "Yes":
perform_post = True
manifest['metadata']['disasterRecovery'] = "True"
if manifest['metadata']['disasterRecovery'] == "No":
perform_post = True
manifest['metadata']['disasterRecovery'] = "False"
def transform_alert_threshold_and_elasticsearch_cpu():
print("Transforming alert threshold and elasticsearch cpu")
try:
conn = psycopg2.connect(**db_params)
cur = conn.cursor()
if perform_post:
response = s.post(url + "/api/manifest", json=manifest)
print("{} response of post is {}".format(i['id'], response.text))
time.sleep(2)
fetch_query = "SELECT id, data FROM temp_deployment"
cur.execute(fetch_query)
records = cur.fetchall()
for record in records:
_id = record[0]
json_data = record[1]
save = False
print("Checking for id: {}".format(_id))
if 'alerts' in json_data:
alerts = json_data['alerts']
for alert_type, alert_list in alerts.items():
for alert in alert_list:
if isinstance(alert, dict) and 'threshold' in alert and isinstance(
alert['threshold'], str) and alert['threshold'].isdigit():
print(
"Found threshold as string for id: {} at {}".format(_id,
alert_type))
save = True
alert['threshold'] = int(alert['threshold'])
if 'elasticSearch' in json_data:
es = json_data['elasticSearch']
if 'instance' in es:
instance = es['instance']
if 'cpu' in instance and isinstance(
instance['cpu'], str) and instance['cpu'].isdigit():
print("Found cpu as string for id: {}".format(_id))
save = True
instance['cpu'] = int(instance['cpu'])
if save:
updated_jsonb = json.dumps(json_data)
update_query = "UPDATE temp_deployment SET data = %s WHERE id = %s"
cur.execute(update_query, (updated_jsonb, _id))
conn.commit()
except Exception as e:
print("Error:", str(e))
finally:
if conn is not None:
conn.close()
def transform_disaster_recovery():
print("Transforming disasterRecovery")
try:
conn = psycopg2.connect(**db_params)
cur = conn.cursor()
fetch_query = "SELECT id, metadata FROM temp_manifest where metadata is not null"
cur.execute(fetch_query)
records = cur.fetchall()
for record in records:
_id = record[0]
json_data = record[1]
save = False
print("Checking for id: {}".format(_id))
if 'disasterRecovery' in json_data:
if json_data['disasterRecovery'] == "Yes":
print("Found disasterRecovery as Yes for id: {}".format(_id))
save = True
json_data['disasterRecovery'] = "True"
if json_data['disasterRecovery'] == "No":
print("Found disasterRecovery as No for id: {}".format(_id))
save = True
json_data['disasterRecovery'] = "False"
if save:
updated_jsonb = json.dumps(json_data)
update_query = "UPDATE temp_manifest SET metadata = %s WHERE id = %s"
cur.execute(update_query, (updated_jsonb, _id))
conn.commit()
except Exception as e:
print("Error:", str(e))
finally:
if conn is not None:
conn.close()
transform_alert_threshold_and_elasticsearch_cpu()
transform_disaster_recovery()