-
Notifications
You must be signed in to change notification settings - Fork 5
/
fabfile.py
215 lines (163 loc) · 6.38 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
import os
import os.path
import sys
from fabric import task
from invoke.exceptions import Exit
IMAGE_REPOSITORY = "toolkit"
IMAGE_BUILD_DIR = "tmp_toolkit_deploy_{}"
DEFAULT_HOST = "cubecinema.com"
def _assert_target_set(c):
if not hasattr(c.config, "site_root"):
raise Exit("Target not specified - e.g. 'fab cube_staging deploy'")
def _assert_docker_available(c):
# print(f"ADA c type: {type(c)}\n\n")
result = c.run("docker version", hide=True, warn=True, pty=False)
if result.failed:
raise Exit("Docker not available or user does not have permission")
def _confirm(question, default=True):
prompt = f"{question} [{'Y/n' if default else 'y/N'}]"
while True:
response = input(prompt).lower().strip()
if not response:
return default
elif response in ["y", "yes"]:
return True
elif response in ["n", "no"]:
return False
else:
print("Enter'(y)es' or '(n)o'", file=sys.stderr)
@task(hosts=[DEFAULT_HOST])
def cube_staging(c):
"""Configure to deploy to staging on cubecinema.com"""
c.config.target = "staging"
c.config.site_root = "/home/staging/site"
c.config.media = "/home/staging/site/media/"
c.config.docker_image_tag = "staging"
c.config.docker_compose_file = "docker-compose-staging.yml"
c.config.docker_compose_project = "toolkit_staging"
c.config.media_user = "staging"
@task(hosts=[DEFAULT_HOST])
def cube_production(c):
"""Configure to deploy live on cubecinema.com"""
c.config.target = "production"
c.config.site_root = "/home/toolkit/site"
c.config.media = "/home/toolkit/site/media/"
c.config.docker_image_tag = "production"
c.config.docker_compose_file = "docker-compose-production.yml"
c.config.docker_compose_project = "toolkit_production"
c.config.media_user = "toolkit"
def _image_build_dir(build_root, target):
return os.path.join(build_root, IMAGE_BUILD_DIR.format(target))
@task(hosts=[DEFAULT_HOST])
def build_remote_image(c):
"""Upload git HEAD snapshot to target and build the image"""
_assert_target_set(c)
_assert_docker_available(c)
build_root = c.run("echo $HOME", hide=True).stdout.strip()
# Create tar of (local) git HEAD using the hash as the filename
git_rev = c.local(
"git rev-parse --short=10 HEAD", hide=True
).stdout.strip()
archive = f"tk-snapshot-{git_rev}.tgz"
local_root = os.path.dirname(__file__)
print("Creating site tgz")
# we need to be in the site root to create the tgz correctly:
c.local(f"cd {local_root} && git archive --format=tgz HEAD > {archive}")
# Upload to remote:
print("Uploading to remote")
c.put(archive, build_root)
image_build_dir = _image_build_dir(build_root, target=c.config.target)
# Delete old build directory
print(f"Deleting and recreating {format(image_build_dir)}")
c.run(f"rm -rf {image_build_dir}")
c.run(f"mkdir {image_build_dir}")
# Extract:
print(f"Extracting {archive}")
# Untar with -m to avoid trying to utime /media directories that
# may be owned by the webserver (which then fails)
c.run(f"cd {image_build_dir} && tar -m -xzf ../{archive}")
print("Building image")
c.run(
f"cd {image_build_dir} && docker build --pull --tag {IMAGE_REPOSITORY}:{c.config.docker_image_tag} ."
)
@task(hosts=[DEFAULT_HOST])
def docker_compose_up(c):
build_root = c.run("echo $HOME", hide=True).stdout.strip()
image_build_dir = _image_build_dir(build_root, target=c.config.target)
compose_file = os.path.join(image_build_dir, c.config.docker_compose_file)
print("Bringing up docker-compose")
c.run(
f"cd {image_build_dir} && "
f"docker-compose "
f"--project-name {c.config.docker_compose_project} "
f"--file {compose_file} "
f"up "
f"--detach "
f"--no-build"
)
@task(hosts=[DEFAULT_HOST])
def set_media_permissions(c):
"""Set media directories to g+w"""
media_dirs = [
"media/diary",
"media/printedprogramme",
"media/volunteers",
"media/images",
"media/original_images",
"media/documents",
]
c.close()
old_user = c.user
c.user = c.config.media_user
print("Setting media permissions")
for media_dir in media_dirs:
path = os.path.join(c.config.site_root, media_dir)
c.run(f"chmod g+w {path}")
c.close()
c.user = old_user
@task(hosts=[DEFAULT_HOST])
def get_media(c):
"""Rsync media content from a production server to your development environment"""
_assert_target_set(c)
c.local(
f"rsync -av --delete --exclude=thumbnails {c.config.user}@{DEFAULT_HOST}:{c.config.media}/ media/",
echo=True,
)
@task(hosts=[DEFAULT_HOST])
def sync_media_from_production_to_staging(c):
"""Rsync media content from the production server to the staging server. Invoke as fab cube_staging sync_media_from_production_to_staging"""
# TODO define explicitly
_assert_target_set(c)
c.run(
f"cd {c.config.site_root} && "
f"rsync -av --delete --exclude=thumbnails /home/toolkit/site/media/ {c.config.media}",
echo=True,
)
@task(hosts=[DEFAULT_HOST])
def fetch_database_dump(c, dump_filename="database_dump.sql"):
dump_file_gz = dump_filename + ".gz"
if os.path.exists(dump_filename) or os.path.exists(dump_file_gz):
raise Exit(f"Local file {dump_filename} already exists")
print("Generating remote database dump")
# Ask manage.py in the toolkit container for the mysqldump command to run
# (which will include the DB name, user and password)
dump_command = c.run(
f"docker exec {c.config.docker_compose_project}_toolkit_1 "
"/venv/bin/python3 manage.py mysqldump_database --print-command STDOUT",
hide=True,
).stdout.strip()
c.run(f"{dump_command} | gzip > {dump_file_gz}", hide=True)
print("Downloading database dump")
c.get(dump_file_gz, local=dump_file_gz)
c.run(f"rm {dump_file_gz}")
c.local(f"gunzip {dump_file_gz}")
@task(hosts=[DEFAULT_HOST])
def deploy(c):
"""Upload code, build image, bring docker-compose up"""
_assert_target_set(c)
if c.config.target == "production":
if not _confirm("Uploading to live site: sure?", default=False):
raise Exit("User aborted")
build_remote_image(c)
set_media_permissions(c)
docker_compose_up(c)