forked from vantage-sh/ec2instances.info
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtasks.py
More file actions
422 lines (358 loc) · 13.9 KB
/
tasks.py
File metadata and controls
422 lines (358 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# To use this script you must have the following environment variables set:
# AWS_ACCESS_KEY_ID
# AWS_SECRET_ACCESS_KEY
# as explained in AWS documentation: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html
import json
import os
import traceback
import mimetypes
import boto3
import requests
from invoke import task
from invocations.console import confirm
from six.moves import SimpleHTTPServer, socketserver
from rds import scrape as rds_scrape
from cache import scrape as cache_scrape
from redshift import scrape as redshift_scrape
from opensearch import scrape as opensearch_scrape
from render import render
from render import build_sitemap
from render import about_page
from render import sponsors_page
from scrape import scrape
BUCKET_NAME = "www.ec2instances.info"
abspath = lambda filename: os.path.join(
os.path.abspath(os.path.dirname(__file__)), filename
)
HTTP_HOST = os.getenv("HTTP_HOST", "127.0.0.1")
HTTP_PORT = os.getenv("HTTP_PORT", "8080")
REMOTE_WEBSITE_DATA_PREFIX = os.getenv(
"REMOTE_WEBSITE_DATA_PREFIX", "https://cloud-instances.info"
)
LOCAL_FILE_BASE_PATH = "www"
def fetch_from_website_and_write_to_file(file_path):
remote_url = f"{REMOTE_WEBSITE_DATA_PREFIX}/{file_path}"
file_path = f"{LOCAL_FILE_BASE_PATH}/{file_path}"
os.makedirs(os.path.dirname(file_path), exist_ok=True)
try:
response = requests.get(remote_url)
response.raise_for_status()
data = response.json()
with open(file_path, "w") as f:
f.write(json.dumps(data))
print(
f"INFO: data fetched from: {remote_url} and saved to local file: {file_path}"
)
return data
except requests.exceptions.RequestException as e:
print(f"ERROR: Network error while fetching data: {e}")
return None
except json.JSONDecodeError as e:
print(f"ERROR: Invalid JSON received: {e}")
return None
except Exception as e:
print(f"ERROR: Unexpected error: {e}")
return None
@task
def build(c, refresh_data=False):
"""Scrape AWS sources for data and build the site"""
scrape_ec2(c, refresh_data)
scrape_rds(c, refresh_data)
scrape_elasticache(c, refresh_data)
scrape_redshift(c, refresh_data)
scrape_opensearch(c, refresh_data)
render_html(c)
@task
def scrape_ec2(c, refresh_data):
"""Scrape EC2 data from AWS and save to local file"""
ec2_file = "instances.json"
if not refresh_data:
result = fetch_from_website_and_write_to_file(ec2_file)
if result is None:
print("Unable to fetch data, proceed to scrap")
else:
# data is fetched from website, no need to scrap aws
return
try:
scrape(f"{LOCAL_FILE_BASE_PATH}/{ec2_file}")
except Exception as e:
print("ERROR: Unable to scrape EC2 data")
print(traceback.print_exc())
@task
def scrape_rds(c, refresh_data):
"""Scrape RDS data from AWS and save to local file"""
rds_file = "rds/instances.json"
if not refresh_data:
result = fetch_from_website_and_write_to_file(rds_file)
if result is None:
print("Unable to fetch data, proceed to scrap")
else:
# data is fetched from website, no need to scrap aws
return
try:
rds_scrape(f"{LOCAL_FILE_BASE_PATH}/{rds_file}")
except Exception as e:
print("ERROR: Unable to scrape RDS data")
print(traceback.print_exc())
@task
def scrape_elasticache(c, refresh_data):
"""Scrape Cache instance data from AWS and save to local file"""
elasticache_file = "cache/instances.json"
if not refresh_data:
result = fetch_from_website_and_write_to_file(elasticache_file)
if result is None:
print("Unable to fetch data, proceed to scrap")
else:
# data is fetched from website, no need to scrap aws
return
try:
cache_scrape(f"{LOCAL_FILE_BASE_PATH}/{elasticache_file}")
except Exception as e:
print("ERROR: Unable to scrape Cache data")
print(traceback.print_exc())
@task
def scrape_redshift(c, refresh_data):
"""Scrape Redshift instance data from AWS and save to local file"""
redshift_file = "redshift/instances.json"
if not refresh_data:
result = fetch_from_website_and_write_to_file(redshift_file)
if result is None:
print("Unable to fetch data, proceed to scrap")
else:
# data is fetched from website, no need to scrap aws
return
try:
redshift_scrape(f"{LOCAL_FILE_BASE_PATH}/{redshift_file}")
except Exception as e:
print("ERROR: Unable to scrape Redshift data")
print(traceback.print_exc())
@task
def scrape_opensearch(c, refresh_data):
"""Scrape OpenSearch instance data from AWS and save to local file"""
opensearch_file = "opensearch/instances.json"
if not refresh_data:
result = fetch_from_website_and_write_to_file(opensearch_file)
if result is None:
print("Unable to fetch data, proceed to scrap")
else:
# data is fetched from website, no need to scrap aws
return
try:
opensearch_scrape(f"{LOCAL_FILE_BASE_PATH}/{opensearch_file}")
except Exception as e:
print("ERROR: Unable to scrape OpenSearch data")
print(traceback.print_exc())
@task
def serve(c):
class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
# The URL does not include ".html". Add it to serve the file for dev
if "/aws/" in self.path:
if "?" in self.path:
self.path = (
self.path.split("?")[0] + ".html?" + self.path.split("?")[1]
)
else:
self.path += ".html"
print(self.path)
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
"""Serve site contents locally for development"""
os.chdir("www/")
httpd = socketserver.TCPServer((HTTP_HOST, int(HTTP_PORT)), MyHandler)
print(
"Serving on http://{}:{}".format(
httpd.socket.getsockname()[0], httpd.socket.getsockname()[1]
)
)
httpd.serve_forever()
@task
def render_html(c):
"""Render HTML but do not update data from Amazon"""
sitemap = []
sitemap.extend(render("www/instances.json", "in/index.html.mako", "www/index.html"))
sitemap.extend(
render("www/rds/instances.json", "in/rds.html.mako", "www/rds/index.html")
)
sitemap.extend(
render("www/cache/instances.json", "in/cache.html.mako", "www/cache/index.html")
)
sitemap.extend(
render(
"www/redshift/instances.json",
"in/redshift.html.mako",
"www/redshift/index.html",
)
)
sitemap.extend(
render(
"www/opensearch/instances.json",
"in/opensearch.html.mako",
"www/opensearch/index.html",
)
)
sitemap.append(about_page())
sitemap.append(sponsors_page())
build_sitemap(sitemap)
@task
def bucket_create(c):
"""Creates the S3 bucket used to host the site"""
s3 = boto3.client("s3")
# Create bucket with public read access
s3.create_bucket(Bucket=BUCKET_NAME, ACL="public-read")
# Configure website hosting
s3.put_bucket_website(
Bucket=BUCKET_NAME,
WebsiteConfiguration={
"IndexDocument": {"Suffix": "index.html"},
"ErrorDocument": {"Key": "error.html"},
},
)
print(f"Bucket {BUCKET_NAME!r} created.")
@task
def bucket_delete(c):
"""Deletes the S3 bucket used to host the site"""
# Get bucket name from environment variable or use default
BUCKET_NAME = os.environ.get("BUCKET_NAME", "www.ec2instances.info")
if not confirm(f"Are you sure you want to delete the bucket {BUCKET_NAME!r}?"):
print("Aborting at user request.")
exit(1)
# Determine if we're using R2 or S3 based on environment variables
if os.environ.get("R2_ACCOUNT_ID"):
# Using R2
print(f"Deleting Cloudflare R2 bucket {BUCKET_NAME}")
endpoint_url = (
f"https://{os.environ.get('R2_ACCOUNT_ID')}.r2.cloudflarestorage.com"
)
s3 = boto3.client(
"s3",
endpoint_url=endpoint_url,
aws_access_key_id=os.environ.get("R2_ACCESS_KEY_ID"),
aws_secret_access_key=os.environ.get("R2_SECRET_ACCESS_KEY"),
region_name="auto",
)
# R2 doesn't support ACL
extra_args_base = {}
else:
# Using AWS S3
print(f"Deleting AWS S3 bucket {BUCKET_NAME}")
s3 = boto3.client("s3")
bucket = s3.Bucket(BUCKET_NAME)
# Delete all objects in the bucket first
bucket.objects.all().delete()
# Then delete the bucket
bucket.delete()
print(f"Bucket {BUCKET_NAME!r} deleted.")
@task
def deploy(c, root_dir="www", max_workers=30):
"""Deploy current content to Cloudflare R2 or S3 with parallel uploads"""
import concurrent.futures
# Get bucket name from environment variable or use default
BUCKET_NAME = os.environ.get("BUCKET_NAME", "www.ec2instances.info")
# Determine if we're using R2 or S3 based on environment variables
if os.environ.get("R2_ACCOUNT_ID"):
# Using R2
print(f"Deploying to Cloudflare R2 bucket: {BUCKET_NAME}")
endpoint_url = (
f"https://{os.environ.get('R2_ACCOUNT_ID')}.r2.cloudflarestorage.com"
)
s3 = boto3.client(
"s3",
endpoint_url=endpoint_url,
aws_access_key_id=os.environ.get("R2_ACCESS_KEY_ID"),
aws_secret_access_key=os.environ.get("R2_SECRET_ACCESS_KEY"),
region_name="auto",
)
# R2 doesn't support ACL
extra_args_base = {}
else:
# Using AWS S3
print(f"Deploying to AWS S3 bucket: {BUCKET_NAME}")
s3 = boto3.client("s3")
# S3 requires ACL for public access
extra_args_base = {"ACL": "public-read"}
# Collect all files to upload
upload_tasks = []
for root, dirs, files in os.walk(root_dir):
for name in files:
if name.startswith("."):
continue
local_path = os.path.join(root, name)
remote_path = local_path[len(root_dir) + 1 :]
upload_tasks.append((local_path, remote_path, name))
total_files = len(upload_tasks)
print(f"Uploading {total_files} files to {BUCKET_NAME}...")
# Function to handle a single file upload
def upload_file(task):
local_path, remote_path, name = task
uploads = []
try:
# Set content type based on file extension
content_type = (
"text/html"
if name.endswith(".html")
else mimetypes.guess_type(local_path)[0]
)
# Prepare extra args with content type
extra_args = extra_args_base.copy()
if content_type:
extra_args["ContentType"] = content_type
# Define the keys and labels for upload
upload_targets = []
# Handle index.html files specially
if name == "index.html":
# Upload with original path (e.g., "rds/index.html" or "index.html" for the root index file)
upload_targets.append((remote_path, "Index HTML"))
# For directory index files, like "rds/index.html" also create clean directory URLs
dir_path = os.path.dirname(remote_path)
if dir_path:
# For subdirectory index.html (e.g., "rds/index.html")
# Create "rds/" and "rds" URLs
upload_targets.append((dir_path + "/", "Directory URL"))
upload_targets.append((dir_path, "Directory URL (no slash)"))
# For root index.html, don't create a "/" URL - that's not supported by R2. We handled that using a simple redirect Cloudflare Worker.
# Regular HTML files (not index.html)
elif name.endswith(".html"):
# Get clean path without extension
clean_path = remote_path[:-5]
upload_targets = [
(remote_path, "Original URL"), # With .html extension
(clean_path, "Clean URL"), # Without .html extension
(clean_path + "/", "Directory URL"), # With trailing slash
]
# Non-HTML files - upload with standard path
else:
upload_targets = [(remote_path, "Standard")]
# Upload all versions of the file
for target_key, label in upload_targets:
s3.upload_file(
Filename=local_path,
Bucket=BUCKET_NAME,
Key=target_key,
ExtraArgs=extra_args,
)
uploads.append((target_key, label))
return local_path, uploads, None
except Exception as e:
return local_path, None, str(e)
# Upload files in parallel
success_count = 0
error_count = 0
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
future_to_task = {
executor.submit(upload_file, task): task for task in upload_tasks
}
for future in concurrent.futures.as_completed(future_to_task):
local_path, uploads, error = future.result()
if error:
print(f"ERROR uploading {local_path}: {error}")
error_count += 1
else:
for path, type_label in uploads:
print(f"✓ {local_path} -> {BUCKET_NAME}/{path} ({type_label})")
success_count += 1
print(f"\nDeployment completed: {success_count} successful, {error_count} failed")
@task(default=True)
def update(c):
"""Build and deploy the site"""
build(c)
deploy(c)