-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathpublish
executable file
·112 lines (100 loc) · 4.01 KB
/
publish
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python
import argparse
import logging
import subprocess
import sys
import time
import boto3
logging.basicConfig(
stream=sys.stdout,
level=logging.INFO,
format='[%(asctime)s] %(levelname)s - %(message)s'
)
parser = argparse.ArgumentParser(
description="Build, submit, test and publish a CloudFormation resource on the public registry"
)
parser.add_argument("--resource-folder", "-f", required=True, help="The resource folder")
parser.add_argument("--region", "-r", required=True, help="The AWS region where to publish the resource")
parser.add_argument("--type-name", "-t", required=True, help="The resource type name")
parser.add_argument("--version", "-v", required=False, help="The version of the resource to publish")
parser.add_argument("--skip-submit", help="Skip the submit step", action="store_true")
parser.add_argument("--skip-tests", help="Skip the tests step", action="store_true")
parser.add_argument("--skip-publish", help="Skip the publish step", action="store_true")
args = parser.parse_args()
TYPE_CONFIGURATION = """
{
"DatadogCredentials": {
"ApiKey": "{{resolve:secretsmanager:DD_KEYS:SecretString:DD_TEST_CLIENT_API_KEY}}",
"ApplicationKey": "{{resolve:secretsmanager:DD_KEYS:SecretString:DD_TEST_CLIENT_APP_KEY}}"
}
}
"""
# Build and register resource
if not args.skip_submit:
try:
logging.info("Generating docs and resource-role.yaml")
subprocess.check_call(
["cfn", "generate"], cwd=args.resource_folder
)
logging.info("Submitting resource")
subprocess.check_call(
["cfn", "submit", "--region", args.region, "--set-default"], cwd=args.resource_folder
)
except Exception:
logging.exception("Error submitting resource")
exit(1)
client = boto3.client("cloudformation", region_name=args.region)
# Set type configuration for resource
try:
logging.info("Configuring resource")
res = client.set_type_configuration(Type="RESOURCE", TypeName=args.type_name, Configuration=TYPE_CONFIGURATION)
except Exception:
logging.exception("Error configuring resource")
exit(1)
# Execute contract tests on resource
if not args.skip_tests:
type_arn = ""
try:
logging.info("Starting contract tests")
res = client.test_type(Type="RESOURCE", TypeName=args.type_name, LogDeliveryBucket="test-type-logs")
type_arn = res["TypeVersionArn"]
except Exception:
logging.exception("Error starting contract tests")
exit(1)
# Wait for tests to finish
while True:
test_status = ""
test_status_desc = ""
try:
res = client.describe_type(Arn=type_arn)
test_status = res["TypeTestsStatus"]
test_status_desc = res["TypeTestsStatusDescription"]
except Exception:
logging.exception("Error checking contract tests status")
exit(1)
if test_status == "PASSED" or test_status == "FAILED":
break
logging.info(f"Waiting for contract test to finish. Current status: {test_status_desc}")
time.sleep(10)
if test_status == "FAILED":
logging.error("Contract tests failed, aborting.")
exit(1)
# Publish resource
if not args.skip_publish:
try:
logging.info("Register as publisher")
try:
res = client.register_publisher(
AcceptTermsAndConditions=True,
ConnectionArn="arn:aws:codestar-connections:us-east-1:088054601418:connection/bfde435c-304c-4630-ac45-04cb7ceeb159"
)
except client.exceptions.CFNRegistryException as e:
logging.debug("error registering publisher, likely already registered: %s", e)
logging.info("Publishing resource")
if args.version:
client.publish_type(Type="RESOURCE", TypeName=args.type_name, PublicVersionNumber=args.version)
else:
client.publish_type(Type="RESOURCE", TypeName=args.type_name)
except Exception:
logging.exception("Error publishing resource")
exit(1)