forked from vantage-sh/ec2instances.info
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
77 lines (66 loc) · 2.35 KB
/
fabfile.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
# 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 webbrowser
from boto import connect_s3
from boto.s3.key import Key
from fabric.api import abort, task
from fabric.contrib.console import confirm
from render import render
from scrape import scrape
BUCKET_NAME = 'www.ec2instances.info'
abspath = lambda filename: os.path.join(os.path.abspath(os.path.dirname(__file__)),
filename)
@task
def build():
"""Scrape AWS sources for data and build the site"""
data_file = 'www/instances.json'
try:
scrape(data_file)
except Exception, e:
print "ERROR: Unable to scrape site data: %s" % e
render(data_file, 'in/index.html.mako', 'www/index.html')
@task
def preview():
url = 'file://localhost/%s' % (abspath('www/index.html'))
webbrowser.open(url, new=2)
@task
def bucket_create():
"""Creates the S3 bucket used to host the site"""
conn = connect_s3()
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():
"""Deletes the S3 bucket used to host the site"""
if not confirm("Are you sure you want to delete the bucket %r?" % BUCKET_NAME):
abort('Aborting at user request.')
conn = connect_s3()
conn.delete_bucket(BUCKET_NAME)
print 'Bucket %r deleted.' % BUCKET_NAME
@task
def deploy(root_dir='www'):
"""Deploy current content"""
conn = connect_s3()
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
headers = {
"Cache-Control": "max-age=86400, must-revalidate"}
k.set_contents_from_filename(local_path, headers=headers,
policy='public-read')
@task(default=True)
def update():
"""Build and deploy the site"""
build()
deploy()