-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathec2_custom_script_server.py
executable file
·57 lines (50 loc) · 1.47 KB
/
ec2_custom_script_server.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
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import gzip
import cStringIO
import os
script = """#!/bin/sh
echo "Installing boto..."
cd /root
git clone git://github.com/boto/boto.git
cd boto
python setup.py install
echo "...done"
"""
cloud_config = """
packages:
- python-setuptools
runcmd:
- [ easy_install, boto ]
"""
def create_txt_part(path, subtype, filename=None):
fp = open(path)
s = fp.read()
fp.close()
txt = MIMEText(s, _subtype=subtype)
if filename:
txt.add_header('Content-Disposition',
'attachment', filename=filename)
return txt
def build_userdata(metric_dir):
mp = MIMEMultipart()
# Add our part handler
path = os.path.join(metric_dir, 'metric_part_handler.py')
txt = create_txt_part(path, 'part-handler', 'metric_part_handler.py')
mp.attach(txt)
# Add the boto config file
path = os.path.join(metric_dir, 'boto.cfg')
txt = create_txt_part(path, 'x-config', 'boto.cfg')
mp.attach(txt)
# Add the cloud-config
txt = MIMEText(cloud_config, _subtype='cloud-config')
mp.attach(txt)
# Add disk metric
path = os.path.join(metric_dir, 'metric_disk_usage')
txt = create_txt_part(path, 'x-metric', 'metric_disk_usage')
mp.attach(txt)
gfileobj = cStringIO.StringIO()
gfile = gzip.GzipFile(fileobj=gfileobj, mode='wb')
gfile.write(mp.as_string())
gfile.close()
return gfileobj.getvalue()