-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfabfile.py
214 lines (158 loc) · 5.13 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
import os
import time
from fabric.api import env
from fabric.context_managers import cd, prefix, quiet
from fabric.operations import get
from fabric.operations import local as fabric_local
from fabric.operations import run as fabric_run
from fabric.operations import sudo
"""
Fabric deployment scripts for github.com:trojsten/web
Notes:
- Use $HOME/.ssh/config file to configure username for connecting to inteligent.trojsten.sk
"""
env.project_name = "trojstenweb"
env.use_ssh_config = True
env.roledefs = {
"prod": {
"hosts": ["[email protected]"],
"project_path": "/home/trojstenweb/web",
"virtualenv_name": "trojstenweb-3",
"db_name": "trojstenweb",
"use_sudo": False,
"server_configuration": "/home/trojstenweb/uwsgi/*.yaml",
"local": False,
"install_poetry": True,
"dev_packages": False,
},
"beta": {
"hosts": ["inteligent.trojsten.sk:22100"],
"project_path": "/usr/local/www/trojstenweb/web",
"virtualenv_name": "trojstenweb",
"db_name": "trojstenweb",
"use_sudo": True,
"sudo_user": "trojstenweb",
"shell": "/usr/local/bin/bash -l -c",
"server_configuration": "/usr/local/www/trojstenweb/*.yaml",
"local": False,
"install_poetry": True,
"dev_packages": True,
},
"local": {
"user": os.environ.get("USER"),
"hosts": ["localhost"],
"project_path": os.path.dirname(os.path.realpath(__file__)),
"virtualenv_name": "trojstenweb",
"db_name": "trojsten",
"use_sudo": False,
"local": True,
"install_poetry": False,
"dev_packages": True,
},
}
def run(*args, **kwargs):
if env.use_sudo:
return sudo(user=env.sudo_user, *args, **kwargs)
else:
return fabric_run(*args, **kwargs)
def load_role(role_name):
for k, v in env.roledefs[role_name].items():
env[k] = v
def local():
load_role("local")
def beta():
load_role("beta")
def prod():
load_role("prod")
def checkout(target):
with cd(env.project_path):
run("git checkout -- .")
run("git checkout {}".format(target))
def pull():
with cd(env.project_path):
if env.build_requirements:
run("git reset HEAD requirements*")
run("git checkout -- requirements*")
run("git pull")
def fetch():
with cd(env.project_path):
run("git fetch")
def collectstatic():
manage("collectstatic", "--noinput")
def migrate():
manage("migrate", "--noinput")
def load_fixtures():
manage("loaddata", "fixtures/*")
def install_requirements():
with cd(env.project_path):
with prefix("workon %s" % env.virtualenv_name):
if env.install_poetry:
run("pip install poetry")
run("poetry install{}".format("" if env.dev_packages else " --no-dev"))
def manage(*args):
with cd(env.project_path):
with prefix("workon %s" % env.virtualenv_name):
run("python manage.py " + " ".join(args))
def restart_wsgi():
with cd(env.project_path):
run("touch {}".format(env.server_configuration))
def compile_translations():
with prefix("workon %s" % env.virtualenv_name):
with cd(env.project_path):
run("cd trojsten && python ../manage.py compilemessages")
def write_version_txt():
with cd(env.project_path):
run('echo -n `git describe --tag --always`" " > version.txt')
run('git show --pretty=format:"%cd" --date=short --quiet >> version.txt')
run("echo >> version.txt")
def enable_maintenance_mode():
run("touch ~/maintenance")
def disable_maintenance_mode():
run("rm -f ~/maintenance")
def dump_sql():
run("mkdir -p db-dumps")
filename = str(int(time.time()))
with cd("db-dumps"):
run("pg_dump -Fc -O -c %s > %s.sql" % (env.db_name, filename))
run("rm -f latest.sql")
run("ln -s %s.sql latest.sql" % filename)
def get_latest_dump():
run("mkdir -p db-dumps")
with cd("db-dumps"):
get("latest.sql")
def freeze_results(*args):
with cd(env.project_path):
with prefix("workon %s" % env.virtualenv_name):
run("python manage.py freeze_results " + " ".join(args))
def branch(name):
with cd(env.project_path):
run("git fetch")
run("git checkout %s" % name)
def after_pull():
install_requirements()
compile_translations()
migrate()
def update(target=None):
if not env.local:
enable_maintenance_mode()
if target:
fetch()
checkout(target)
else:
pull()
after_pull()
if not env.local:
collectstatic()
restart_wsgi()
disable_maintenance_mode()
write_version_txt()
def update_if_necessary():
with quiet():
latest_version = fabric_local('git tag --sort=v:refname -l "v*" | tail -n -1', capture=True)
with cd(env.project_path):
current_version = run("git describe --tags --always")
if latest_version and current_version != latest_version:
update(latest_version)
def version():
with cd(env.project_path):
run("cat version.txt")