-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaws-processing.py
More file actions
288 lines (263 loc) · 13.9 KB
/
aws-processing.py
File metadata and controls
288 lines (263 loc) · 13.9 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import sys
import boto3
import re
import semver
import os
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')
"""
Product Codes from Palo's Doc site:
https://docs.paloaltonetworks.com/vm-series/11-0/vm-series-deployment/set-up-the-vm-series-firewall-on-aws/deploy-the-vm-series-firewall-on-aws/obtain-the-ami/get-amazon-machine-image-ids
"""
byol = '6njl1pau431dv1qxipg63mvah'
bundle1 = 'e9yfvyj3uag5uo5j2hjikv74n'
bundle2 = 'hd44w1chf26uv4p52cdynb2o'
panorama = 'eclz7j04vu9lf8ont8ta3n17o'
marketplace_owner_id = '679593333241'
# Create a boto3 session
session = boto3.Session()
# Get a list of all regions
client = boto3.client('ec2')
regions = [region['RegionName'] for region in client.describe_regions()['Regions']]
try:
# Initialise an empty list to store version numbers based on licensing model
list_of_byol = []
list_of_bundle1 = []
list_of_bundle2 = []
list_of_panorama = []
# Initialise an empty dictionaries to store AMI IDs based on licensing model
amis_by_version_byol = {}
amis_by_version_bundle1 = {}
amis_by_version_bundle2 = {}
amis_by_version_panorama = {}
for region in regions:
# Create an EC2 client for the region
ec2 = session.client('ec2', region_name=region)
ami_images = ec2.describe_images(Filters=[{'Name': 'name', 'Values': ['PA-VM-AWS*']}])
# Iterate through each AMI image
for ami in ami_images['Images']:
# Check if 'ProductCodes' exists and is not empty
if 'ProductCodes' in ami and ami['ProductCodes']:
# Check if AMI is BYOL and owned by the marketplace owner
if ami['ProductCodes'][0]['ProductCodeId'] == byol and ami['OwnerId'] == marketplace_owner_id:
# Extract the version number from the AMI name
# Check if the version is a hotfix (i.e. ends with "-hXX")
match = re.search(r'.*PA-VM-AWS-(\d+.\d+.\d+-h\d{1,2})-', ami['Name'])
if match:
# Extract the version number from the match
ver = match.group(1)
list_of_byol.append(ver)
if ver not in amis_by_version_byol:
amis_by_version_byol[ver] = {}
amis_by_version_byol[ver][region] = ami['ImageId']
else:
# Split the AMI name by hyphens and extract the 4th element (i.e. the version number)
ver = ami['Name'].split("-")[3]
# Add the version number to the list of BYOL versions
list_of_byol.append(ver)
# If the version number is not already a key in the dictionary, add it
if ver not in amis_by_version_byol:
amis_by_version_byol[ver] = {}
# Add the AMI ID to the dictionary, with the region as the key
amis_by_version_byol[ver][region] = ami['ImageId']
else:
print(f"Skipping BYOL AMI {ami.get('ImageId', 'Unknown')} ({ami.get('Name', 'Unknown')}) in {region} due to missing ProductCode")
# Iterate through each AMI image
for ami in ami_images['Images']:
# Check if 'ProductCodes' exists and is not empty
if 'ProductCodes' in ami and ami['ProductCodes']:
# Check if AMI is BUNDLE1 and owned by the marketplace owner
if ami['ProductCodes'][0]['ProductCodeId'] == bundle1 and ami['OwnerId'] == marketplace_owner_id:
# Extract the version number from the AMI name
# Check if the version is a hotfix (i.e. ends with "-hXX")
match = re.search(r'.*PA-VM-AWS-(\d+.\d+.\d+-h\d{1,2}|)-', ami['Name'])
if match:
# Extract the version number from the match
ver = match.group(1)
list_of_bundle1.append(ver)
if ver not in amis_by_version_bundle1:
amis_by_version_bundle1[ver] = {}
amis_by_version_bundle1[ver][region] = ami['ImageId']
else:
# Split the AMI name by hyphens and extract the 4th element (i.e. the version number)
ver = ami['Name'].split("-")[3]
# Add the version number to the list of BUNDLE1 versions
list_of_bundle1.append(ver)
# If the version number is not already a key in the dictionary, add it
if ver not in amis_by_version_bundle1:
amis_by_version_bundle1[ver] = {}
# Add the AMI ID to the dictionary, with the region as the key
amis_by_version_bundle1[ver][region] = ami['ImageId']
else:
print(f"Skipping Bundle1 AMI {ami.get('ImageId', 'Unknown')} ({ami.get('Name', 'Unknown')}) in {region} due to missing ProductCode")
# Iterate through each AMI image
for ami in ami_images['Images']:
# Check if 'ProductCodes' exists and is not empty
if 'ProductCodes' in ami and ami['ProductCodes']:
# Check if AMI is BUNDLE2 and owned by the marketplace owner
if ami['ProductCodes'][0]['ProductCodeId'] == bundle2 and ami['OwnerId'] == marketplace_owner_id:
# Extract the version number from the AMI name
# Check if the version is a hotfix (i.e. ends with "-hXX")
match = re.search(r'.*PA-VM-AWS-(\d+.\d+.\d+-h\d{1,2})-', ami['Name'])
if match:
# Extract the version number from the match
ver = match.group(1)
list_of_bundle2.append(ver)
if ver not in amis_by_version_bundle2:
amis_by_version_bundle2[ver] = {}
amis_by_version_bundle2[ver][region] = ami['ImageId']
else:
# Split the AMI name by hyphens and extract the 4th element (i.e. the version number)
ver = ami['Name'].split("-")[3]
# Add the version number to the list of BUNDLE2 versions
list_of_bundle2.append(ver)
# If the version number is not already a key in the dictionary, add it
if ver not in amis_by_version_bundle2:
amis_by_version_bundle2[ver] = {}
# Add the AMI ID to the dictionary, with the region as the key
amis_by_version_bundle2[ver][region] = ami['ImageId']
else:
print(f"Skipping Bundle2 AMI {ami.get('ImageId', 'Unknown')} ({ami.get('Name', 'Unknown')}) in {region} due to missing ProductCode")
ami_images = ec2.describe_images(Filters=[{'Name': 'name', 'Values': ['Panorama-AWS*']}])
# Iterate through each AMI image
for ami in ami_images['Images']:
# Check if 'ProductCodes' exists and is not empty
if 'ProductCodes' in ami and ami['ProductCodes']:
# Check if AMI is BYOL and owned by the marketplace owner
if ami['ProductCodes'][0]['ProductCodeId'] == panorama and ami['OwnerId'] == marketplace_owner_id:
# Extract the version number from the AMI name
# Check if the version is a hotfix (i.e. ends with "-hXX")
match = re.search(r'.*Panorama-AWS-(\d+.\d+.\d+-h\d{1,2})-[a-z|0-9|-]{36}', ami['Name'])
if match:
# Extract the version number from the match
ver = match.group(1)
list_of_panorama.append(ver)
if ver not in amis_by_version_panorama:
amis_by_version_panorama[ver] = {}
amis_by_version_panorama[ver][region] = ami['ImageId']
else:
# Split the AMI name by hyphens and extract the 3th element (i.e. the version number)
ver = ami['Name'].split("-")[2]
# Add the version number to the list of BYOL versions
list_of_panorama.append(ver)
# If the version number is not already a key in the dictionary, add it
if ver not in amis_by_version_panorama:
amis_by_version_panorama[ver] = {}
# Add the AMI ID to the dictionary, with the region as the key
amis_by_version_panorama[ver][region] = ami['ImageId']
else:
print(f"Skipping Panorama AMI {ami.get('ImageId', 'Unknown')} ({ami.get('Name', 'Unknown')}) in {region} due to missing ProductCode")
list_of_byol = list(dict.fromkeys(list_of_byol))
list_of_byol.sort(key=semver.Version.parse)
# logging.info("list_of_byol:",list_of_byol)
list_of_bundle1 = list(dict.fromkeys(list_of_bundle1))
list_of_bundle1.sort(key=semver.Version.parse)
# logging.info("list_of_bundle1:",list_of_bundle1)
list_of_bundle2 = list(dict.fromkeys(list_of_bundle2))
list_of_bundle2.sort(key=semver.Version.parse)
# logging.info("list_of_bundle2:",list_of_bundle2)
list_of_panorama = list(dict.fromkeys(list_of_panorama))
list_of_panorama.sort(key=semver.Version.parse)
# logging.info("list_of_panorama:",list_of_panorama)
# Purge all the files under the aws/byol/ folder
byol_folder = 'aws/byol/'
if not os.path.exists(byol_folder):
os.makedirs(byol_folder)
else:
for file in os.listdir(byol_folder):
file_path = os.path.join(byol_folder, file)
if os.path.isfile(file_path):
# logging.info(file_path)
os.remove(file_path)
# Purge all the files under the aws/bundle1/ folder
bundle1_folder = 'aws/bundle1/'
if not os.path.exists(bundle1_folder):
os.makedirs(bundle1_folder)
else:
for file in os.listdir(bundle1_folder):
file_path = os.path.join(bundle1_folder, file)
if os.path.isfile(file_path):
# logging.info(file_path)
os.remove(file_path)
# Purge all the files under the aws/bundle2/ folder
bundle2_folder = 'aws/bundle2/'
if not os.path.exists(bundle2_folder):
os.makedirs(bundle2_folder)
else:
for file in os.listdir(bundle2_folder):
file_path = os.path.join(bundle2_folder, file)
if os.path.isfile(file_path):
# logging.info(file_path)
os.remove(file_path)
# Purge all the files under the aws/panorama/ folder
panorama_folder = 'aws/panorama/'
if not os.path.exists(panorama_folder):
os.makedirs(panorama_folder)
else:
for file in os.listdir(panorama_folder):
file_path = os.path.join(panorama_folder, file)
if os.path.isfile(file_path):
# logging.info(file_path)
os.remove(file_path)
# Create the aws.md file
result = ""
result += "\n# AWS\n"
result += "\n### BYOL\n"
for version in list_of_byol:
result += "- [" + version + "](aws/byol/" + version + ".md) \n"
result += "\n### PAYG Bundle 1\n"
for version in list_of_bundle1:
result += "- [" + version + "](aws/bundle1/" + version + ".md) \n"
result += "\n### PAYG Bundle 2\n"
for version in list_of_bundle2:
result += "- [" + version + "](aws/bundle2/" + version + ".md) \n"
result += "\n### Panorama\n"
for version in list_of_panorama:
result += "- [" + version + "](aws/panorama/" + version + ".md) \n"
result += "\n"
with open('aws.md','w') as file:
file.write(result)
file.close()
logging.info("aws.md file has been modified.")
# Add the AMI IDs for each version to the markdown string
for ver, amis in amis_by_version_byol.items():
ver_str = ''
ver_str += '\n # '+ ver + '\n'
for region, ami in amis.items():
ver_str += '- ' + region + ': ' + ami + '\n'
ver_str += '\n[Go back to aws.md](../../aws.md) \n'
with open('aws/byol/' + ver + '.md', 'w') as f:
f.write(ver_str)
logging.info(f"aws/byol/{ver}.md file has been modified.")
# Add the AMI IDs for each version to the markdown string
for ver, amis in amis_by_version_bundle1.items():
ver_str = ''
ver_str += '\n # '+ ver + '\n'
for region, ami in amis.items():
ver_str += '- ' + region + ': ' + ami + '\n'
ver_str += '\n[Go back to aws.md](../../aws.md) \n'
with open('aws/bundle1/' + ver + '.md', 'w') as f:
f.write(ver_str)
logging.info(f"aws/bundle1/{ver}.md file has been modified.")
# Add the AMI IDs for each version to the markdown string
for ver, amis in amis_by_version_bundle2.items():
ver_str = ''
ver_str += '\n # '+ ver + '\n'
for region, ami in amis.items():
ver_str += '- ' + region + ': ' + ami + '\n'
ver_str += '\n[Go back to aws.md](../../aws.md) \n'
with open('aws/bundle2/' + ver + '.md', 'w') as f:
f.write(ver_str)
logging.info(f"aws/bundle2/{ver}.md file has been modified.")
# Add the AMI IDs for each version to the markdown string
for ver, amis in amis_by_version_panorama.items():
ver_str = ''
ver_str += '\n # '+ ver + '\n'
for region, ami in amis.items():
ver_str += '- ' + region + ': ' + ami + '\n'
ver_str += '\n[Go back to aws.md](../../aws.md) \n'
with open('aws/panorama/' + ver + '.md', 'w') as f:
f.write(ver_str)
logging.info(f"aws/panorama/{ver}.md file has been modified.")
except Exception as e:
logging.info(f"Exception {e!r}", file=sys.stderr)