forked from gcollazo/Fabulous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabulous.py
164 lines (126 loc) · 3.69 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
from fabric.api import env, run, sudo, put, cd
from fabric.colors import green as _green, yellow as _yellow
from fabulous_conf import *
from cookbook import recipe
import boto
import time
env.user = fabconf['SERVER_USERNAME']
env.key_filename = fabconf['SSH_PRIVATE_KEY_PATH']
def ulous():
"""
*** This is what you run the first time ***
"""
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()
#env.host_string = "ec2-50-17-172-13.compute-1.amazonaws.com"
print(_green("Waiting 30 seconds for server to boot..."))
time.sleep(30)
_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!
"""
started = False;
for ingredient in recipe:
if not started:
if ingredient['action'] == "start":
started = True
continue
if ingredient['action'] == "stop":
break
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(ec2_key, ec2_secret)
image = conn.get_all_images(ec2_amis)
reservation = image[0].run(1, 1, ec2_keypair, ec2_secgroups,
instance_type=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(_render(params))
def _apt(params):
"""
Runs apt-get install commands
"""
for pkg in params:
_sudo("apt-get install -qq %s" % pkg)
def _pip(params):
"""
Runs pip install commands
"""
for pkg in params:
_sudo("pip install %s" % pkg)
def _run(params):
"""
Runs command with active user
"""
command = _render(params)
run(command)
def _sudo(params):
"""
Run command as root
"""
command = _render(params)
sudo(command)
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(command):
"""
Activates virtualenv and runs command
"""
with cd(fabconf['APPS_DIR']):
sudo(fabconf['ACTIVATE'] + ' && ' + command, user=fabconf['SERVER_USERNAME'])