Skip to content

Commit 5ba7c63

Browse files
charliejllewellynCharlie LlewellynBrian969
authored
Cleanup fix - add ability to change AcceleratorName (#684)
* edited to support cleanup without directory services enabled * added ability to change AcceleratorName in prefix * corrected pattern matching for codecommit * corrected repo name * corrected to reference AccaleratorPrefix * tweak readme.md Co-authored-by: Charlie Llewellyn <cjl@amazon.co.uk> Co-authored-by: Brian969 <56414362+Brian969@users.noreply.github.com>
1 parent b95e9ef commit 5ba7c63

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

reference-artifacts/Custom-Scripts/SEA-uninstall/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ The logic of the script is the following:
7272

7373
3. Before running this script you must manually delete AWS SSO.
7474

75-
4. Execute the script `python3 aws-sea-cleanup.py`
75+
4. Execute the script `python3 aws-sea-cleanup.py`.
76+
77+
**Note: ** if you used a different AcceleratorPrefix you can use `python3 aws-sea-cleanup.py --AcceleratorPrefix YOUR_ACCELERATOR_PREFIX`.
78+
7679
5. Manual steps (in the Organization Management account):
7780
- In Secrets Manager, set the Secret `accelerator/config/last-successful-commit` to an empty string;
7881
- In DynamoDB, delete the 3 `PBMMAccel-*` tables;

reference-artifacts/Custom-Scripts/SEA-uninstall/aws-sea-cleanup.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
import sys
1010
import argparse
1111
import base64
12+
import re
1213
from tabulate import tabulate
1314
from os import path
1415

1516

1617
parser = argparse.ArgumentParser(
1718
description="A development script that cleans up resources deployed by the accelerator. Use Administrator AWS credentials in the master account when running this script."
1819
)
20+
parser.add_argument('--AcceleratorPrefix', default='PBMMAccel', help='The value set in AcceleratorPrefix')
1921

2022
organizations = boto3.client("organizations")
2123
sts = boto3.client("sts")
@@ -243,7 +245,7 @@ def delete_scps(credentials, region):
243245

244246
for scp in scps["Policies"]:
245247
scp_name = scp["Name"]
246-
if scp_name.startswith("PBMMAccel"):
248+
if scp_name.startswith(AcceleratorPrefix):
247249
print("Detaching SCP '{}'".format(scp["Name"]))
248250
targets = organizations.list_targets_for_policy(PolicyId=scp["Id"])
249251

@@ -279,7 +281,7 @@ def master_cleanup(credentials, region):
279281

280282
for stackset in stacksets["Summaries"]:
281283
name = stackset["StackSetName"]
282-
if name.startswith("PBMMAccel"):
284+
if name.startswith(AcceleratorPrefix):
283285
instances = cloudformation.list_stack_instances(StackSetName=name)
284286
instances_accounts = list(map(lambda x: x["Account"], instances["Summaries"]))
285287
instances_regions = list(set(map(lambda x: x["Region"], instances["Summaries"])))
@@ -309,7 +311,7 @@ def master_cleanup(credentials, region):
309311
print("Done. Stack {} deleted".format(name))
310312

311313

312-
cloud_trail_name = "PBMMAccel-Org-Trail"
314+
cloud_trail_name = AcceleratorPrefix + "-Org-Trail"
313315
cloudtrail = boto3.client("cloudtrail",
314316
region_name=region,
315317
aws_access_key_id=credentials["Credentials"]["AccessKeyId"],
@@ -704,7 +706,7 @@ def thread_cwl_cleanup(region, admin_role_arn, accountId):
704706
while True:
705707

706708
for log_group in log_groups["logGroups"]:
707-
if "PBMMAccel-" in log_group["logGroupName"]:
709+
if AcceleratorPrefix in log_group["logGroupName"]:
708710
print("Deleting log group '{}' in {} for {}".format(log_group["logGroupName"], region, accountId))
709711
cwl.delete_log_group(logGroupName=log_group["logGroupName"])
710712
print("Deleting log group '{}' in {} for {}".format(log_group["logGroupName"], region, accountId))
@@ -817,6 +819,9 @@ def cleanup_directory_sharing_load_config():
817819
mad_account = config["mandatory-account-configs"][mad_account_name]["account-name"]
818820
if "mad" not in config["mandatory-account-configs"][mad_account_name]["deployments"]:
819821
return "mad not configured"
822+
elif config["mandatory-account-configs"][mad_account_name]["deployments"]["mad"] == False:
823+
return "mad not configured"
824+
820825
mad_dns_domain = config["mandatory-account-configs"][mad_account_name]["deployments"]["mad"]["dns-domain"]
821826

822827

@@ -872,6 +877,8 @@ def cleanup_route53_resolver_load_config():
872877
central_account_name = config["global-options"]["central-operations-services"]["account"]
873878
if "mad" not in config["mandatory-account-configs"][central_account_name]["deployments"]:
874879
return "mad not configured"
880+
elif config["mandatory-account-configs"][central_account_name]["deployments"]["mad"] == False:
881+
return "mad not configured"
875882

876883
central_resolver_rule_account = config["mandatory-account-configs"][central_account_name]["deployments"]["mad"]["central-resolver-rule-account"]
877884

@@ -916,7 +923,11 @@ def backup_config():
916923
print("Backing up config.json from CodeCommit...")
917924
try:
918925
for repo in repos["repositories"]:
919-
if repo["repositoryName"].startswith("PBMM"):
926+
if AcceleratorPrefix != 'PBMMAccel':
927+
CodeCommitPrefix = AcceleratorPrefix
928+
else:
929+
CodeCommitPrefix = 'PBMM'
930+
if repo["repositoryName"].startswith(CodeCommitPrefix):
920931
file = cc.get_file(
921932
repositoryName=repo["repositoryName"],
922933
filePath='/config.json'
@@ -936,8 +947,11 @@ def backup_config():
936947

937948
def configure_args():
938949
parser.parse_args()
950+
args = parser.parse_args()
951+
AcceleratorPrefix = re.sub('-$', '', args.AcceleratorPrefix)
952+
return AcceleratorPrefix
939953

940954
if __name__ == "__main__":
941-
configure_args()
955+
AcceleratorPrefix = configure_args()
942956
backup_config()
943957
cleanup()

0 commit comments

Comments
 (0)