-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdocs-preview.py
executable file
·71 lines (53 loc) · 1.98 KB
/
docs-preview.py
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
65
66
67
68
69
70
71
#!/usr/bin/env python3
import argparse
import logging
from datetime import datetime
from pathlib import Path
import boto3
logging.basicConfig(level=logging.INFO)
TEMPLATES_DIRECTORY = Path("templates")
DEFAULT_TEMPLATE_NAME = "docs-preview"
DEFAULT_BUCKET_NAME = "nhsx-covid-chest-imaging-database-preview"
CLIENT = boto3.client("cloudformation", region_name="eu-west-2")
def _stack_exists(stack_name):
response = CLIENT.describe_stacks()
stack_names = {
stack_description["StackName"] for stack_description in response["Stacks"]
}
return stack_name in stack_names
def create_docs_preview_bucket(bucket_name=DEFAULT_BUCKET_NAME):
template_body = (TEMPLATES_DIRECTORY / "docs-preview.yaml").read_text()
timestamp_suffix = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
changeset_name = f"{DEFAULT_TEMPLATE_NAME}-{timestamp_suffix}"
stack_name = f"{DEFAULT_TEMPLATE_NAME}-{bucket_name}"
logging.info(f"Stack name: {stack_name}")
# Update or create as needed
changeset_type = "UPDATE" if _stack_exists(stack_name) else "CREATE"
# Construct template parameters
parameters = [
{"ParameterKey": "BucketNameParameter", "ParameterValue": bucket_name}
]
# Submit the change set
response = CLIENT.create_change_set(
StackName=stack_name,
TemplateBody=template_body,
ChangeSetName=changeset_name,
ChangeSetType=changeset_type,
Capabilities=["CAPABILITY_NAMED_IAM"],
Parameters=parameters,
)
changeset_arn = response["Id"]
return changeset_arn
def main(bucket_name):
changeset_arn = create_docs_preview_bucket(bucket_name)
logging.info(f"Created changeset for docs preview bucket with ARN: {changeset_arn}")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-b",
"--bucket",
help="The docs preview bucket's name",
default=DEFAULT_BUCKET_NAME,
)
args = parser.parse_args()
main(args.bucket)