/
fabfile.py
217 lines (146 loc) · 5.49 KB
/
fabfile.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
import os
import re
from contextlib import contextmanager
from shutil import rmtree
from fabric.api import env, cd, put, run as _run, sudo as _sudo # noqa
from fabric.network import connect, normalize, HostConnectionCache, join_host_strings # noqa
from fabric.state import connections # noqa
from wrapt import decorator
from deployment_tools.utils import load_config
ATTEMPTS = 10
@decorator
def _ignore_timeout_wrapper(wrapped, _, args, kwargs):
try:
return wrapped(*args, **kwargs)
except TimeoutError:
return wrapped(*args, **kwargs)
run = _ignore_timeout_wrapper(_run)
sudo = _ignore_timeout_wrapper(_sudo)
def _delete_excess_files(
init_directory,
dir_templates=(r'^__pycache__$', r'^[.]cache$', r'^[.]idea$', r'^htmlcov$'),
file_templates=(r'^[\S]*.pyc$', r'^.coverage$', r'^[\S]*.sqlite3$', r'^[\S]*.cmd', r'^TODO$')
):
compiled_dir_templates = [re.compile(template) for template in dir_templates]
compiled_file_templates = [re.compile(template) for template in file_templates]
for path, dirs, files in os.walk(init_directory, topdown=True):
for directory in [d for d in dirs if any(template.match(d) for template in compiled_dir_templates)]:
rmtree(os.path.join(path, directory), ignore_errors=True)
for file in [f for f in files if any(template.match(f) for template in compiled_file_templates)]:
try:
os.remove(os.path.join(path, file))
except: # noqa
pass
def get_project_dir():
return env['{}_project_dir'.format(env.host_string)]
@load_config('server_config.ini')
def tune_env(config, *_, **__): # noqa
for params in config.values():
host_string = join_host_strings(params['USER'], params['HOST'], params.get('PORT'))
env.hosts.append(host_string)
env.connection_attempts = ATTEMPTS
if env.key_filename is None:
env.key_filename = [params['KEY_PATH']]
else:
env.key_filename.append(params['KEY_PATH'])
env['{}_project_dir'.format(host_string)] = params['PROJECT_DIR']
def prepare_projects():
projects_path = os.path.join('deployment_tools', 'src')
for project_dir in os.listdir(projects_path):
project_abs_path = os.path.join(projects_path, project_dir)
if not os.path.isdir(project_abs_path):
continue
_delete_excess_files('deployment_tools')
def upload_files():
sudo('mkdir -p {}'.format(get_project_dir()))
sudo('chmod 777 -R {}'.format(get_project_dir()))
with cd(get_project_dir()):
put('deployment_tools/*', './')
def remove_files():
project_dir = get_project_dir()
if re.match('^/opt/[a-zA-Z/]*$', project_dir):
sudo('rm -rf {}'.format(os.path.join(get_project_dir(), '*')))
else:
raise Exception
def chmod_opt():
sudo('chmod 777 -R /opt')
def install_system_dependencies():
sudo('''yum groupinstall -y development && \\
yum install -y openssl-devel bzip2-devel wget gcc
''')
def install_python():
with cd('/opt'):
run('wget http://www.python.org/ftp/python/3.5.2/Python-3.5.2.tar.xz && tar -xvJf Python-3.5.2.tar.xz')
python_path = '/opt/python'
with cd('Python-3.5.2'):
run('./configure --prefix={} && make && make altinstall'.format(python_path))
run('rm -f Python-3.5.2.tar.xz')
def install_python_dependencies():
with cd(get_project_dir()):
run('/opt/python/bin/pip3.5 install -r requirements.txt')
def install_docker():
with cd(get_project_dir()):
run('/opt/python/bin/invoke install_docker')
def create_docker_network():
with cd(get_project_dir()):
run('/opt/python/bin/invoke create_docker_network')
def prepare_to_start():
with cd(get_project_dir()):
run('/opt/python/bin/invoke prepare_files')
def build_services():
with cd(get_project_dir()):
run('/opt/python/bin/invoke build_services')
def up_services():
with cd(get_project_dir()):
run('/opt/python/bin/invoke up_services')
def chmod_sockets():
with cd(get_project_dir()):
run('/opt/python/bin/invoke chmod_sockets')
def ps_services():
with cd(get_project_dir()):
run('/opt/python/bin/invoke ps_services')
def down_services():
with cd(get_project_dir()):
run('/opt/python/bin/invoke down_services')
def remove_images():
with cd(get_project_dir()):
run('/opt/python/bin/invoke remove_images')
@contextmanager
def disconnect():
yield
host = env.host_string
if host and host in connections:
normalized_host = normalize(host)
connections[host].get_transport().close()
connect(normalized_host[0], normalized_host[1], normalized_host[2], HostConnectionCache())
def deploy():
with disconnect():
chmod_opt()
prepare_projects()
upload_files()
install_system_dependencies()
install_python()
install_python_dependencies()
install_docker()
with disconnect():
create_docker_network()
prepare_to_start()
with disconnect():
build_services()
with disconnect():
up_services()
chmod_sockets()
def force_update():
with disconnect():
prepare_projects()
down_services()
remove_images()
remove_files()
upload_files()
install_python_dependencies()
prepare_to_start()
with disconnect():
build_services()
with disconnect():
up_services()
chmod_sockets()