-
Notifications
You must be signed in to change notification settings - Fork 2
/
fabfile.py
258 lines (199 loc) · 7.83 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# -*- coding: utf-8 -*-
"""
This is a collection of useful utility functions when working with docker on different environments.
In order to use these functions, install fabric on your local machine with::
pip install fabric
Please note: Fabric is a remote code execution tool, NOT a remote configuration tool. While you can copy files
from here to there, it is not a good replacement for salt or ansible in this regard.
There is a function called `production` where you need to fill in the details about your production machine(s).
You can then run::
fab production status
to get the status of your stack
To list all available commands, run::
fab -l
"""
from fabric.operations import local as lrun, run, sudo, put
from fabric.api import *
from fabric.colors import green, red, yellow, blue
import os
import datetime
import tempfile
import textwrap
import time
ENV = env
def _copy_secrets():
"""
Copies secrets from local to remote.
:return:
"""
secret = ".env.{}".format(ENV.name)
remote_path = os.path.join(ENV.project_dir, ".env")
print(blue("Copying {secret} to {remote_path} on {host}".format(
secret=secret, remote_path=remote_path, host=ENV.host
)))
put(secret, remote_path)
with cd(ENV.project_dir):
run("echo 'DEPLOYMENT_DATETIME=%s' >> %s" % (ENV.DEPLOYMENT_DATETIME, remote_path))
def rollback(commit="HEAD~1"):
"""
Rollback to a previous commit and build the stack
:param commit: Commit you want to roll back to. Default is the previous commit
"""
with ENV.cd(ENV.project_dir):
ENV.run("git checkout {}".format(commit))
deploy()
def env(name="prod"):
"""
Set environment based on your local .env.<name> file
"""
filename = ".env.{}".format(name)
if not os.path.isfile(filename):
print(red("Missing {} file".format(filename)))
raise SystemExit()
with open(".env.{}".format(name)) as env_file:
ENV.name = name
for line in env_file:
line = line.strip()
if not line or line.startswith("#"):
continue
key, value = line.split("=")
if key.startswith("FAB_") and value:
ENV.__setattr__(key.replace("FAB_", "").lower(), value)
if ENV.name == "local":
ENV.run = lambda *args, **kwargs: lrun(capture=True, *args, **kwargs)
ENV.cd = lcd
else:
ENV.run = run # if you don't log in as root, replace with 'ENV.run = sudo'
ENV.cd = cd
def _check_env():
if not hasattr(ENV, "name"):
print(red(textwrap.dedent("""
You need to specify environment, by:
fab env:<env_name> deploy
or:
fab deploy:<env_name>
""")))
raise SystemExit()
def deploy(branch="master"):
"""
Pulls the latest changes from master, rebuilt and restarts the stack
"""
_check_env()
ENV.DEPLOYMENT_DATETIME = datetime.datetime.utcnow().isoformat()
deployment_branch = ENV.DEPLOYMENT_DATETIME.replace(":", "").replace(".", "")
lrun("git push origin {}".format(branch))
_copy_secrets()
with ENV.cd(ENV.project_dir):
docker_compose("run postgres backup before-deploy-at-{}".format(ENV.DEPLOYMENT_DATETIME))
ENV.run("git fetch --all")
ENV.run("git checkout -f origin/{} -b {}".format(branch, deployment_branch))
_build_and_restart("django-a")
time.sleep(60)
# just to make sure they are on
docker_compose("start postgres")
docker_compose("start redis")
time.sleep(10)
_build_and_restart("django-b")
_build_and_restart("django-chroniker")
docker_compose("run django-a python manage.py searchv2_build")
def _build_and_restart(service):
docker_compose("build " + service)
docker_compose("create " + service)
docker_compose("stop " + service)
docker_compose("start " + service)
def docker_compose(command):
"""
Run a docker-compose command
:param command: Command you want to run
"""
with ENV.cd(ENV.project_dir):
return ENV.run("docker-compose -f {file} {command}".format(file=ENV.compose_file, command=command))
def download_db(filename="tmp.sqlc"):
"""
Download and apply database from remote environment to your local environment
"""
filename, ext = filename.split('.')
temp_dirpath = "/tmp/"
remote_filename = "{}-{}".format(
ENV.name,
datetime.datetime.utcnow().isoformat(),
)
with ENV.cd(ENV.project_dir):
docker_compose("run postgres backup {}".format(remote_filename))
remote_sql_dump_filepath = os.path.join(temp_dirpath, '{}.{}'.format(remote_filename, ext))
container_id = docker_compose("ps -q postgres").split()[0]
ENV.run("docker cp {}:/backups/{} {}".format(
container_id,
'{}.{}'.format(remote_filename, ext),
remote_sql_dump_filepath
))
get(remote_sql_dump_filepath, '{}/{}.{}'.format(temp_dirpath, filename, ext))
# docker_compose("run postgres rm /backups/{}.{}".format(remote_filename, ext))
# ENV.run("rm {}".format(remote_sql_dump_filepath))
print("Remote done!")
def restore_db(filename="tmp.sqlc"):
temp_dirpath = "/tmp/"
remote_filename = filename or "{}-{}".format(
ENV.name,
datetime.datetime.utcnow().isoformat()
)
with ENV.cd(ENV.project_dir):
local_sql_dump_filepath = os.path.join(temp_dirpath, filename)
container_id = docker_compose("ps -q postgres").split()[0]
if ENV.name == "local":
file_to_copy = local_sql_dump_filepath
remote_sql_dump_filepath = None
else:
remote_sql_dump_filepath = os.path.join(temp_dirpath, remote_filename)
put(local_sql_dump_filepath, remote_sql_dump_filepath)
file_to_copy = remote_sql_dump_filepath
ENV.run("docker cp {} {}:/backups/{}".format(
file_to_copy,
container_id,
remote_filename,
))
print(docker_compose("run postgres restore {}".format(remote_filename)))
docker_compose("run postgres rm /backups/{}".format(remote_filename))
if ENV.name != "local":
ENV.run("rm {}".format(remote_sql_dump_filepath))
docker_compose("up -d")
print(green("Your {} environment has now new database!".format(ENV.name)))
def download_media(media_dir="/tmp", override="n"):
"""
Download and replace all files from media directory from remote environment to your local environment
"""
temp_dirpath = tempfile.mkdtemp()
media_dir = os.path.abspath(media_dir)
target_media_dir = os.path.join(media_dir, "media")
if override != "y" and prompt(
'{} directory will be override. Continue? [y/N]'.format(target_media_dir),
default='n',
validate=r'^y|n$'
) == 'n':
exit()
lrun('rm -rfv {}'.format(target_media_dir))
ENV.run("mkdir -p {}".format(temp_dirpath))
container_id = docker_compose("ps -q django-a").split()[0]
ENV.run(
"docker cp {}:/data/media/ {}".format(
container_id,
temp_dirpath
)
)
get("{}/media/".format(temp_dirpath), media_dir)
ENV.run("rm -rf {}".format(temp_dirpath))
def upload_media(media_dir="/tmp"):
media_dir = os.path.abspath(os.path.join(media_dir, "media"))
temp_dirpath = tempfile.mkdtemp()
ENV.run("mkdir -p {}".format(temp_dirpath))
put(media_dir, temp_dirpath)
django_service_name = "django" if ENV.name == 'local' else "django-a"
container_id = docker_compose("ps -q {}".format(django_service_name)).split()[0]
ENV.run(
"docker cp {}/media {}:/data/".format(
temp_dirpath,
container_id,
)
)
# TODO: add fix for https://github.com/noisy/steemprojects.com/issues/59
# files should have proper ownership