forked from tommywo/Serverless-Goat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverlessrepo-deploy.py
More file actions
executable file
·131 lines (115 loc) · 4.66 KB
/
serverlessrepo-deploy.py
File metadata and controls
executable file
·131 lines (115 loc) · 4.66 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python3
import boto3
import botocore.exceptions
import subprocess
import sys
import time
import yaml
import urllib.request
import ssl
def get_application(serverlessrepo, application_name):
list_applications_result = serverlessrepo.list_applications()
for application in list_applications_result["Applications"]:
if application["Name"] == application_name:
return serverlessrepo.get_application(ApplicationId=application["ApplicationId"])
next_token = list_applications_result.get("NextToken")
while next_token:
list_applications_result = serverlessrepo.list_applications(NextToken=next_token)
for application in list_applications_result["Applications"]:
if application["Name"] == application_name:
return serverlessrepo.get_application(ApplicationId=application["ApplicationId"])
next_token = list_applications_result.get("NextToken")
return None
def main():
config_yaml_file = open("serverlessrepo.yaml")
config_yaml_str = config_yaml_file.read()
config_yaml_file.close()
config = yaml.load(config_yaml_str)
print("Installing dependencies...")
subprocess.check_call(
["npm", "install"], cwd="./src/api/convert"
)
print("Packaging template...")
packaged_template_yaml = subprocess.check_output(
["aws", "cloudformation", "package", "--profile", config["AWSProfile"], "--template-file", "template.yaml",
"--s3-bucket", config["S3Bucket"]]
).decode().split("\n", 1)[1]
session = boto3.Session(profile_name=config["AWSProfile"])
serverlessrepo = session.client("serverlessrepo", region_name=config["Region"])
application = get_application(serverlessrepo, config["Name"])
readme_md_file = open("README.md")
readme_md_str = readme_md_file.read()
readme_md_file.close()
license_txt_file = open("LICENSE")
license_txt_str = license_txt_file.read()
license_txt_file.close()
if application:
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
old_license_txt_str = urllib.request.urlopen(application["LicenseUrl"], context=ctx).read().decode()
if old_license_txt_str == license_txt_str and config["SpdxLicenseId"] == application["SpdxLicenseId"]:
print("Updating application...")
application = serverlessrepo.update_application(
ApplicationId=application["ApplicationId"],
Author=config["Author"],
Description=config["Description"],
HomePageUrl=config["HomePageUrl"],
Labels=config["Labels"],
ReadmeBody=readme_md_str
)
else:
print("License of existing application cannot be updated!")
sys.exit(1)
else:
print("Creating application...")
application = serverlessrepo.create_application(
Name=config["Name"],
Author=config["Author"],
Description=config["Description"],
HomePageUrl=config["HomePageUrl"],
Labels=config["Labels"],
ReadmeBody=readme_md_str,
SpdxLicenseId=config["SpdxLicenseId"],
LicenseBody=license_txt_str
)
print("Creating application version...")
try:
serverlessrepo.create_application_version(
ApplicationId=application["ApplicationId"],
SemanticVersion=config["SemanticVersion"],
SourceCodeUrl=config["SourceCodeUrl"],
TemplateBody=packaged_template_yaml
)
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == 'ConflictException':
print("Cannot update existing application version!")
sys.exit(1)
else:
raise e
if config["public"]:
print("Making application public...")
try:
time.sleep(5)
serverlessrepo.put_application_policy(
ApplicationId=application["ApplicationId"],
Statements=[
{
"Actions": [
"Deploy",
],
"Principals": [
"*",
],
"StatementId": "PublicAccess"
},
]
)
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == 'BadRequestException':
print("Cannot make application public: %s" % e.response['Error']['Message'])
pass
else:
raise e
if __name__ == "__main__":
main()