forked from migurski/Extractotron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-instance.py
69 lines (50 loc) · 2.12 KB
/
run-instance.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
""" Run an extractor instance.
"""
from optparse import OptionParser
from boto.ec2 import EC2Connection
from httplib import HTTPConnection
from urlparse import urljoin
from urllib import urlencode
parser = OptionParser(usage="%prog [options] <aws key> <aws secret> <s3 bucket>")
defaults = dict(ami_id='ami-68ad5201', type='m2.xlarge', run=True)
parser.set_defaults(**defaults)
parser.add_option('--ami-id', dest='ami_id',
help='AMI ID, default %(ami_id)s' % defaults)
parser.add_option('--type', dest='type',
help='Instance type, default %(type)s' % defaults)
parser.add_option('--no-run', dest='run', action='store_false',
help="Don't actually run the instance, just output user-data.")
def post_script(filename):
"""
"""
form = dict(language='Bash', title=filename)
form.update(dict(content=open(filename).read()))
body = urlencode(form)
head = {'Content-Length': len(body), 'Content-Type': 'application/x-www-form-urlencoded'}
conn = HTTPConnection('dpaste.com')
conn.request('POST', '/api/v1/', headers=head)
conn.send(body)
resp = conn.getresponse()
if resp.status >= 400:
print resp.getheaders()
raise Exception('Received status %d from dpaste.com' % resp.status)
href = resp.getheader('location')
href = urljoin(href, 'plain/')
return href
if __name__ == '__main__':
try:
options, (aws_key, aws_secret, s3_bucket) = parser.parse_args()
except ValueError:
parser.print_usage()
exit(1)
user_data = open('extract.sh').read()
user_data = user_data.replace('$KEY', aws_key)
user_data = user_data.replace('$SECRET', aws_secret)
user_data = user_data.replace('$BUCKET', s3_bucket)
user_data = user_data.replace('$OSMOSIS_HREF', post_script('osmosis.sh'))
user_data = user_data.replace('$COASTSHAPES_HREF', post_script('coastshapes.sh'))
if options.run:
conn = EC2Connection(aws_key, aws_secret)
print conn.run_instances(options.ami_id, instance_type=options.type, user_data=user_data)
else:
print user_data