forked from zbyte64/Fabulous
/
fabulous.py
211 lines (169 loc) · 5.03 KB
/
fabulous.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
from fabric.api import *
from fabric.colors import green as _green, yellow as _yellow
from fabric.exceptions import NetworkError
from fabulous_conf import fabconf
from cookbook import recipe
import boto
import time
import os
env.user = fabconf['SERVER_USERNAME']
env.key_filename = fabconf.get('SSH_PRIVATE_KEY_PATH', os.path.join(fabconf['SSH_PATH'], fabconf['EC2_KEY_NAME']))
def ulous(giturl=None, environ_file=None):
"""
*** This is what you run the first time ***
"""
if giturl:
fabconf['GITHUB_REPO'] = giturl
if environ_file:
fabconf['ENVIRON_FILE'] = environ_file
fabconf.setdefault('ENVIRON_PAYLOAD', '')
if fabconf.get('ENVIRON_FILE'):
fabconf['ENVIRON_PAYLOAD'] += '\n'+open(fabconf['ENVIRON_FILE'], 'r').read()
fab()
def fab():
"""
This does the real work for the ulous() task. Is here to provide backwards compatibility
"""
start_time = time.time()
print(_green("Started..."))
env.host_string = _create_server()
print(_green("Waiting 30 seconds for server to boot..."))
time.sleep(30)
print(_green("Polling server..."))
retries = 6
while retries > 0:
retries -= 1
try:
_run('ls')
except NetworkError:
if retries:
time.sleep(5)
else:
raise
else:
break
_oven()
end_time = time.time()
print(_green("Runtime: %f minutes" % ((end_time - start_time) / 60)))
print(_green(env.host_string))
def _oven():
"""
Cooks the recipe. Fabulous!
"""
for ingredient in recipe:
try:
print(_yellow(ingredient['message']))
except KeyError:
pass
globals()["_" + ingredient['action']](ingredient['params'])
def _create_server():
"""
Creates EC2 Instance
"""
print(_yellow("Creating instance"))
conn = boto.connect_ec2(fabconf['ec2_key'], fabconf['ec2_secret'])
image = conn.get_all_images(fabconf['ec2_amis'])
reservation = image[0].run(1, 1, fabconf['ec2_keypair'], fabconf['ec2_secgroups'],
instance_type=fabconf['ec2_instancetype'])
instance = reservation.instances[0]
conn.create_tags([instance.id], {"Name":fabconf['INSTANCE_NAME_TAG']})
while instance.state == u'pending':
print(_yellow("Instance state: %s" % instance.state))
time.sleep(10)
instance.update()
print(_green("Instance state: %s" % instance.state))
print(_green("Public dns: %s" % instance.public_dns_name))
return instance.public_dns_name
def _virtualenv(params):
"""
Allows running commands on the server
with an active virtualenv
"""
with cd(fabconf['APPS_DIR']):
_virtualenv_command(params)
def _apt(params, attempts=2):
"""
Runs apt-get install commands
"""
for pkg in params:
_sudo("apt-get install -qq %s" % pkg, attempts)
def _pip(params, attempts=2):
"""
Runs pip install commands
"""
for pkg in params:
_sudo("pip install %s" % pkg, attempts)
def _gem(params, attempts=2):
"""
Runs gem install commands
"""
for pkg in params:
_sudo("gem install %s" % pkg, attempts)
def _run(params, attempts=1):
"""
Runs command with active user
"""
command = _render(params)
while attempts > 0:
attempts -= 1
try:
run(command)
except:
if attempts < 1:
raise
def _sudo(params, attempts=1):
"""
Run command as root
"""
command = _render(params)
while attempts > 0:
attempts -= 1
try:
sudo(command)
except:
if attempts < 1:
raise
def _put(params):
"""
Moves a file from local computer to server
"""
put(_render(params['file']), _render(params['destination']))
def _put_template(params):
"""
Same as _put() but it loads a file and does variable replacement
"""
f = open(_render(params['template']), 'r')
template = f.read()
run(_write_to(_render(template), _render(params['destination'])))
def _render(template, context=fabconf):
"""
Does variable replacement
"""
return template % context
def _write_to(string, path):
"""
Writes a string to a file on the server
"""
return "echo '" + string + "' > " + path
def _append_to(string, path):
"""
Appends to a file on the server
"""
return "echo '" + string + "' >> " + path
def _virtualenv_command(params, attempts=1):
"""
Activates virtualenv and runs command
"""
command = _render(params)
with cd(fabconf['APPS_DIR']):
while attempts > 0:
attempts -= 1
try:
sudo(fabconf['ACTIVATE'] + ' && ' + command, user=fabconf['SERVER_USERNAME'])
except:
if attempts < 1:
raise
def _write_env(params):
payload = fabconf['ENVIRON_PAYLOAD'] + '\n' + _render(params)
path = _render('/home/%(SERVER_USERNAME)s/%(PROJECT_NAME)s.env')
run(_write_to(payload, path))