This repository has been archived by the owner on Jun 7, 2019. It is now read-only.
/
misc.py
236 lines (185 loc) · 5.58 KB
/
misc.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# -*- coding: utf-8 -*-
import time
from fabric.api import *
from fabric.colors import green as _green, yellow as _yellow, red as _red
from global_conf import *
import boto
import boto.ec2.networkinterface
import boto.ec2.address
import boto.ec2.elb
from lb import recipe
from global_conf import ec2_key, ec2_secret
try:
ec2_amis = env.ec2_amis
ec2_secgroups = env.ec2_secgroups
ec2_keypair = env.ec2_keypair
ec2_instancetype = env.ec2_instancetype
except:
pass
try:
env_config = env.env_config
fabconf = env.fabconf
except:
pass
def _destroy_instance(instance):
"""
Destroy's instance
"""
conn = boto.connect_ec2(ec2_key, ec2_secret)
try:
conn.terminate_instances(instance_ids=[instance])
print(_yellow("Image {} destroyed successfully".format(instance)))
except Exception as e:
print(_red("Exception was raised: {}".format(str(e))))
def _get_lb():
"""
get load balancers
"""
conn = boto.ec2.elb.ELBConnection(ec2_key, ec2_secret)
lb = conn.get_all_load_balancers()
return lb
def _register_instance_in_lb(lb_name, *args):
"""
set one or more instances to a load balancer
"""
lb_conn = boto.ec2.elb.ELBConnection(ec2_key, ec2_secret)
return lb_conn.register_instances(lb_name, *args)
def _deregister_instance_from_lb(lb_name, *args):
"""
delete
"""
lb_conn = boto.ec2.elb.ELBConnection(ec2_key, ec2_secret)
return lb_conn.deregister_instances(lb_name, *args)
def _get_all_instances():
"""
get list of all running instances
"""
conn = boto.connect_ec2(ec2_key, ec2_secret)
reservations = conn.get_all_instances()
instances = [i for r in reservations for i in r.instances]
return instances
def _oven(recipe):
"""
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(ec2_key, ec2_secret)
assert ec2_amis is not None
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']})
conn.create_tags([instance.id], {"Env": env.environment})
conn.create_tags([instance.id], {"Recipe": fabconf['INSTANCE_RECIPE']})
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, instance.id
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=None):
"""
Does variable replacement
"""
if context is None:
context = fabconf
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 _export_envs(params):
"""
Export env to profile
"""
for c in env_config:
_run("sudo sh -c 'echo \"%(KEY)s=%(VALUE)s\" >> /etc/environment'" %
{'KEY': c, 'VALUE': env_config[c]})
def _virtualenv_command(command):
"""
Activates virtualenv and runs command
"""
with cd(fabconf['APPS_DIR']):
run(fabconf['ACTIVATE'] + ' && ' + command)
def _run_command(cmd, *args):
"""
run manage.py command
"""
def _deploy(recipe):
"""
deploy changes from github
"""
conn = boto.connect_ec2(ec2_key, ec2_secret)
reservations = conn.get_all_instances()
instances = [i for r in reservations for i in r.instances]
for instance in instances:
tags = instance.tags
if instance.state == 'running' and 'Env' in tags:
if tags['Env'] == env.environment and tags['Name'] == 'AppServer':
print(_yellow('Deploying changes to instance: %s' %
instance.id))
env.host_string = instance.public_dns_name
env.user = fabconf['SERVER_USERNAME']
env.key_filename = fabconf['SSH_PRIVATE_KEY_PATH']
_oven(recipe)