-
Notifications
You must be signed in to change notification settings - Fork 584
/
tasks.py
215 lines (180 loc) · 6.31 KB
/
tasks.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
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
# To use this script you must have the following environment variables set:
# AWS_ACCESS_KEY_ID
# AWS_SECRET_ACCESS_KEY
# as explained in: http://boto.s3.amazonaws.com/s3_tut.html
import os
import traceback
from boto import connect_s3
from boto.s3.connection import OrdinaryCallingFormat
from boto.s3.key import Key
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 scrape import scrape
from io import BytesIO
import gzip
import shutil
BUCKET_NAME = "www.ec2instances.info"
# Work around https://github.com/boto/boto/issues/2836 by explicitly setting
# the calling_format.
BUCKET_CALLING_FORMAT = OrdinaryCallingFormat()
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")
@task
def build(c):
"""Scrape AWS sources for data and build the site"""
scrape_ec2(c)
scrape_rds(c)
scrape_cache(c)
scrape_redshift(c)
scrape_opensearch(c)
render_html(c)
@task
def scrape_ec2(c):
"""Scrape EC2 data from AWS and save to local file"""
ec2_file = "www/instances.json"
try:
scrape(ec2_file)
except Exception as e:
print("ERROR: Unable to scrape EC2 data")
print(traceback.print_exc())
@task
def scrape_rds(c):
"""Scrape RDS data from AWS and save to local file"""
rds_file = "www/rds/instances.json"
try:
rds_scrape(rds_file)
except Exception as e:
print("ERROR: Unable to scrape RDS data")
print(traceback.print_exc())
def scrape_cache(c):
"""Scrape Cache instance data from AWS and save to local file"""
cache_file = "www/cache/instances.json"
try:
cache_scrape(cache_file)
except Exception as e:
print("ERROR: Unable to scrape Cache data")
print(traceback.print_exc())
def scrape_redshift(c):
"""Scrape Redshift instance data from AWS and save to local file"""
redshift_file = "www/redshift/instances.json"
try:
redshift_scrape(redshift_file)
except Exception as e:
print("ERROR: Unable to scrape Redshift data")
print(traceback.print_exc())
def scrape_opensearch(c):
"""Scrape OpenSearch instance data from AWS and save to local file"""
opensearch_file = "www/opensearch/instances.json"
try:
opensearch_scrape(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())
build_sitemap(sitemap)
@task
def bucket_create(c):
"""Creates the S3 bucket used to host the site"""
conn = connect_s3(calling_format=BUCKET_CALLING_FORMAT)
bucket = conn.create_bucket(BUCKET_NAME, policy="public-read")
bucket.configure_website("index.html", "error.html")
print("Bucket %r created." % BUCKET_NAME)
@task
def bucket_delete(c):
"""Deletes the S3 bucket used to host the site"""
if not confirm("Are you sure you want to delete the bucket %r?" % BUCKET_NAME):
print("Aborting at user request.")
exit(1)
conn = connect_s3(calling_format=BUCKET_CALLING_FORMAT)
conn.delete_bucket(BUCKET_NAME)
print("Bucket %r deleted." % BUCKET_NAME)
@task
def deploy(c, root_dir="www"):
"""Deploy current content"""
conn = connect_s3(calling_format=BUCKET_CALLING_FORMAT)
bucket = conn.get_bucket(BUCKET_NAME)
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 :]
print("%s -> %s/%s" % (local_path, BUCKET_NAME, remote_path))
k = Key(bucket)
k.key = remote_path
if name.endswith(".html"):
upload_file = BytesIO()
with gzip.GzipFile(fileobj=upload_file, mode="wb") as gz, open(
local_path, "rb"
) as fp:
shutil.copyfileobj(fp, gz)
upload_file.seek(0)
k.set_metadata("Content-Type", "text/html")
k.set_metadata("Content-Encoding", "gzip")
else:
upload_file = open(local_path, "rb")
k.set_contents_from_file(upload_file, policy="public-read")
@task(default=True)
def update(c):
"""Build and deploy the site"""
build(c)
deploy(c)