-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathfabfile.py
153 lines (123 loc) · 4.52 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
# Uzo sur via loka komputilo:
#
# python3 -m pip install fabric
# fab -H ps --prompt-for-login-password staging deploy
# fab -H ps --prompt-for-login-password prod deploy
#
# Funkcias se vi havas la jenan sekcion en via .ssh/config:
# Host ps
# HostName pasportaservo.org
import sys
from fabric import task
env = {}
@task
def prod(conn):
if env:
sys.exit(f"Site [{env['site']}] and branch [{env['branch']}] are already configured!")
env['site'] = "prod"
env['branch'] = "prod"
@task
def staging(conn):
if env:
sys.exit(f"Site [{env['site']}] and branch [{env['branch']}] are already configured!")
env['site'] = "staging"
env['branch'] = "master"
def _init_venv_string():
return f"source ~/.bash_profile; workon {env['site']}"
@task(auto_shortflags=False)
def pull(conn, remote="origin", branch="master", runlocal=True):
if runlocal:
conn.local(f"git pull --rebase {remote} {branch}")
else:
conn.run("git checkout -- locale/*/django*.mo")
conn.run("git status")
conn.run(f"git pull {remote} {branch}", pty=True)
@task(auto_shortflags=False)
def checkout(conn, remote="origin", branch="master", runlocal=True):
command = f"git checkout {remote}/{branch}"
if runlocal:
conn.local(command)
else:
conn.run(command)
@task(auto_shortflags=False)
def requirements(conn, runlocal=False, inside_env=False):
command = (
f"python3 -m pip install -Ur requirements{'/dev' if runlocal else ''}.txt"
" | grep -v -e 'Requirement already satisfied' -e 'Requirement already up-to-date'"
)
if runlocal:
result = conn.local(command, warn=True)
else:
if not inside_env:
if not env:
sys.exit("Site not defined, use staging/prod.")
with conn.prefix(_init_venv_string()):
result = conn.run(command, warn=True, pty=True)
else:
result = conn.run(command, warn=True, pty=True)
if result.exited not in (0, 1):
sys.exit(result.exited)
if not runlocal and not inside_env:
site_ctl(conn, command="restart")
site_ctl(conn, command="status", needs_su=False)
@task(auto_shortflags=False)
def makestrings(conn, locale="eo", runlocal=True):
if runlocal:
command = (
"python3 ./manage.py makemessages"
f" --locale {locale} --ignore \"htmlcov/*\" --add-location=file"
)
conn.local(command)
# Remote generation is not supported.
@task(auto_shortflags=False)
def updatestrings(conn, runlocal=True, inside_env=False):
command = "python3 ./manage.py compilemessages"
if not runlocal:
if not inside_env:
if not env:
sys.exit("Site not defined, use staging/prod.")
with conn.prefix(_init_venv_string()):
conn.run(command)
site_ctl(conn, command="restart")
site_ctl(conn, command="status", needs_su=False)
else:
conn.run(command)
else:
conn.local(command)
@task
def updatestatic(conn):
conn.run("./manage.py compilejsi18n -l eo")
conn.run("./manage.py compilescss")
extra_args = "--ignore=*.scss" if env['site'] == "prod" else ""
conn.run(
f"./manage.py collectstatic --verbosity 1 --noinput {extra_args}"
" | grep -v 'Found another file with the destination path' "
)
conn.run("./manage.py compress --verbosity 0 --force")
@task
def migrate(conn):
conn.run("./manage.py migrate --noinput")
@task(auto_shortflags=False)
def site_ctl(conn, command, service_name="pasportaservo", needs_su=True):
if not env:
sys.exit("Site not defined, use staging/prod.")
conn.run(
("sudo " if needs_su else "")
+ f"systemctl {command} {service_name}.{env['site']}.service",
pty=True)
@task(auto_shortflags=False)
def deploy(conn, mode="full", remote="origin"):
if not env:
sys.exit("Site not defined, use staging/prod.")
with conn.prefix(_init_venv_string()):
checkout(conn, remote, env['branch'], runlocal=False)
pull(conn, remote, env['branch'], runlocal=False)
if mode == "full":
requirements(conn, runlocal=False, inside_env=True)
updatestrings(conn, runlocal=False, inside_env=True)
updatestatic(conn)
migrate(conn)
if mode != "html":
site_ctl(conn, command="restart")
site_ctl(conn, command="status", service_name="memcached-ps", needs_su=False)
site_ctl(conn, command="status", needs_su=False)