-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanup_sandboxes.py
More file actions
64 lines (50 loc) · 1.98 KB
/
cleanup_sandboxes.py
File metadata and controls
64 lines (50 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import boto3, os, requests, sys
from botocore.exceptions import ClientError
def trigger_delete_workflow(token: str, sandbox: str):
owner = "NHSDigital"
repo = "national-document-repository-infrastructure"
workflow = "tear-down-sandbox.yml"
url = f"https://api.github.com/repos/{owner}/{repo}/actions/workflows/{workflow}/dispatches"
headers = {
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {token}",
"X-GitHub-Api-Version": "2022-11-28",
}
inputs = {
"git_ref": "main",
"sandbox_name": sandbox,
"environment": "development",
}
resp = requests.post(
url, headers=headers, json={"ref": "main", "inputs": inputs}
)
resp.raise_for_status()
def get_workspaces() -> list[str]:
client = boto3.client("appconfig")
try:
applications = client.list_applications().get("Items")
if not applications:
print("Failed to extract AppConfig applications")
sys.exit(0)
workspaces = []
for application in applications:
name = application.get("Name")
if not name:
print("Failed to extract TF workspace from AppConfig application")
sys.exit(1)
if name.startswith("RepositoryConfiguration-"):
workspaces.append(name.replace("RepositoryConfiguration-", ""))
return workspaces
except ClientError as e:
print(f"Failed to extract TF workspace from AppConfig applications: {str(e)}")
sys.exit(1)
if __name__ == "__main__":
gh_pat = os.getenv("GIT_WORKFLOW_PAT")
if not gh_pat:
sys.exit("GIT_WORKFLOW_PAT not set")
#Add persisting environments here
excluded = ["ndr-dev"]
workspaces = get_workspaces()
for workspace in workspaces:
if workspace not in excluded:
trigger_delete_workflow(token=gh_pat, sandbox=workspace)