INFRA-2230 | Abhishek | Add scripts for migration of alb to shared albs in production

This commit is contained in:
Abhishek Katiyar
2024-04-04 15:20:45 +05:30
parent 5b5b79444a
commit 7330a89219

View File

@@ -0,0 +1,70 @@
import requests
import copy
import time
#gi credentials
url = "http://localhost:3000"
cookie = ""
x_xsrf_token = ""
s = requests.Session()
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()
count=0
updated_manifests_ids = []
def addNewLb(manifest, existing_lb):
new_lb = copy.deepcopy(existing_lb)
print("New LB to be added:\n", new_lb)
del new_lb['id']
del new_lb['version']
new_lb['groupName'] = manifest['team']['name'].lower() + "-sharedalb"
parts = new_lb['endpoint'].split(".")
parts[0] = parts[0] + '-shared'
new_lb['endpoint'] = ".".join(parts)
new_lb['type'] = "sharedAlbAcrossNamespace"
new_lb['isDeployed'] = False
# Append the new load balancer to the list
manifest['deployment']['loadBalancers'].append(new_lb)
print("New load balancer added to manifest:\n", new_lb)
for i in manifest_list:
r = s.get(url + "/api/manifest/" + str(i['id']))
manifest = r.json()
perform_post = False
# environment should be prod
if manifest['environment'] == 'prod' and 'deployment' in manifest and 'loadBalancers' in manifest['deployment']:
# should not have any security groups attached to deployment
if 'securityGroup' in manifest['deployment'] and len(manifest['deployment']['securityGroup']) != 0:
continue
# Check conditions to decide whether to add new_lb
for lb in manifest['deployment']['loadBalancers']:
if not 'id' in lb:
continue # Skip already cloned load balancers
if ((lb['idleTimeout'] != 60) or (lb['stickiness'] != False) or (lb['type'] != 'alb') or
('internal' not in lb['accessPolicies']) or (len(lb['accessPolicies']) != 1) or
('extraSecurityGroups' in lb and len(lb['extraSecurityGroups']) != 0)):
continue
addNewLb(manifest, lb)
perform_post = True
count += 1
if perform_post:
response = s.post(url + "/api/manifest", json=manifest)
print(response.json())
if response.status_code == 200:
print('Submitted manifest\n')
updated_manifests_ids.append(manifest['id'])
time.sleep(2)
break
print(updated_manifests_ids)
print("Total count:", count)