-
Notifications
You must be signed in to change notification settings - Fork 8
/
update_version.py
executable file
·146 lines (138 loc) · 6.56 KB
/
update_version.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
import json
import shutil
import os
import boto3
s3 = boto3.client('s3')
nsolidVersion = input("N|Solid Version:\n")
consoleFile = input("N|Solid Console File (full path):\n")
runtimeFile = input("N|Solid Runtime File (full path):\n")
consoleContent = json.loads(open(consoleFile).read())
runtimeContent = json.loads(open(runtimeFile).read())
print("\nRunning...\n")
# AMI-LIST.md update
if "**" + nsolidVersion + "**" in open('AMI-LIST.md').read():
with open('AMI-LIST.md') as oldfile, open('.AMI-LIST.updated.md', 'w') as newfile:
for line in oldfile:
if "**" + nsolidVersion + "**" not in line:
newfile.write(line)
else:
shutil.copyfile('./AMI-LIST.md', './.AMI-LIST.updated.md')
for console in consoleContent['imageIds']:
for runtime in runtimeContent['imageIds']:
if console['region'] == runtime['region']:
# Make AMIs public
ec2 = boto3.client('ec2', region_name=console['region'])
ec2.modify_image_attribute(
Attribute='launchPermission',
ImageId=console['ami'],
OperationType='add',
UserGroups=[
'all'
]
)
ec2.modify_image_attribute(
Attribute='launchPermission',
ImageId=runtime['ami'],
OperationType='add',
UserGroups=[
'all'
]
)
# Update File
with open("./.AMI-LIST.updated.md", "r") as inFile:
lines = inFile.readlines()
with open("./.AMI-LIST.updated.md", "w") as outFile:
correctRegion = False
for line in lines:
if line.startswith("## " + console['region']):
correctRegion = True
if line.startswith("|----------------|") and correctRegion:
line = (line + "| **" + nsolidVersion + "** "
"| `" + console['ami'] + "` "
"| `" + runtime['ami'] + "` |\n")
correctRegion = False
outFile.write(line)
amiFile = open("./.AMI-LIST.updated.md").read()
with open('AMI-LIST.md', 'w') as originalFile:
originalFile.write(amiFile)
os.remove("./.AMI-LIST.updated.md")
# AMI Maps update
s3 = boto3.client('s3')
consoleContents = s3.list_objects_v2(Bucket="ns-cloud-artifacts", Prefix="nsolid/nsolid-console")
runtimeContents = s3.list_objects_v2(Bucket="ns-cloud-artifacts", Prefix="nsolid/nsolid-runtime")
consoleKeys = []
runtimeKeys = []
for c in consoleContents['Contents']:
consoleKeys.append({"date": c['LastModified'].strftime('%s'),"key": c['Key']})
for r in runtimeContents['Contents']:
runtimeKeys.append({"date": r['LastModified'].strftime('%s'),"key": r['Key']})
consoleKeys = sorted(consoleKeys, key=lambda x: x['date'], reverse=True)
runtimeKeys = sorted(runtimeKeys, key=lambda x: x['date'], reverse=True)
consolePreviousKey = consoleKeys[0]['key']
runtimePreviousKey = runtimeKeys[0]['key']
if consolePreviousKey == 'nsolid/nsolid-console-' + nsolidVersion.replace('.', '') + '.json':
s3.delete_object(
Bucket='ns-cloud-artifacts',
Key=consolePreviousKey
)
consoleContents = s3.list_objects_v2(Bucket="ns-cloud-artifacts", Prefix="nsolid/nsolid-console")
consoleKeys = []
for c in consoleContents['Contents']:
consoleKeys.append({"date": c['LastModified'].strftime('%s'),"key": c['Key']})
consoleKeys = sorted(consoleKeys, key=lambda x: x['date'], reverse=True)
consolePreviousKey = consoleKeys[0]['key']
if runtimePreviousKey == 'nsolid/nsolid-runtime-' + nsolidVersion.replace('.', '') + '.json':
s3.delete_object(
Bucket='ns-cloud-artifacts',
Key=runtimePreviousKey
)
runtimeContents = s3.list_objects_v2(Bucket="ns-cloud-artifacts", Prefix="nsolid/nsolid-runtime")
runtimeKeys = []
for r in runtimeContents['Contents']:
runtimeKeys.append({"date": r['LastModified'].strftime('%s'),"key": r['Key']})
runtimeKeys = sorted(runtimeKeys, key=lambda x: x['date'], reverse=True)
runtimePreviousKey = runtimeKeys[0]['key']
consoleObj = s3.get_object(Bucket="ns-cloud-artifacts", Key=consolePreviousKey)
runtimeObj = s3.get_object(Bucket="ns-cloud-artifacts", Key=runtimePreviousKey)
consoleBody = consoleObj['Body'].read()
runtimeBody = runtimeObj['Body'].read()
previousConsole = json.loads(consoleBody.decode('utf-8'))
previousRuntime = json.loads(runtimeBody.decode('utf-8'))
with open(consoleFile, 'rb') as consoleData:
upload = s3.upload_fileobj(consoleData, 'ns-cloud-artifacts', 'nsolid/nsolid-console-' + nsolidVersion.replace('.', '') + '.json')
with open(runtimeFile, 'rb') as runtimeData:
upload = s3.upload_fileobj(runtimeData, 'ns-cloud-artifacts', 'nsolid/nsolid-runtime-' + nsolidVersion.replace('.', '') + '.json')
# Console Update
for newConsole in consoleContent['imageIds']:
for oldConsole in previousConsole['imageIds']:
if newConsole['region'] == oldConsole['region']:
templates = os.listdir('./templates')
for template in templates:
if ".json" in template:
with open("./templates/" + template, "r") as inFile:
lines = inFile.readlines()
with open("./templates/" + template, "w") as outFile:
for line in lines:
if oldConsole['ami'] in line:
updatedLine = line.replace(oldConsole['ami'], newConsole['ami'])
outFile.write(updatedLine)
else:
outFile.write(line)
# Runtime Update
for newRuntime in runtimeContent['imageIds']:
for oldRuntime in previousRuntime['imageIds']:
if newRuntime['region'] == oldRuntime['region']:
templates = os.listdir('./templates')
for template in templates:
if ".json" in template:
with open("./templates/" + template, "r") as inFile:
lines = inFile.readlines()
with open("./templates/" + template, "w") as outFile:
for line in lines:
if oldRuntime['ami'] in line:
updatedLine = line.replace(oldRuntime['ami'], newRuntime['ami'])
outFile.write(updatedLine)
else:
outFile.write(line)
print('Update Complete.')