This repository has been archived by the owner on May 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
fabfile.py
164 lines (132 loc) · 4.25 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
from __future__ import with_statement
import datetime
from fabric.api import *
from config.fabconfig import *
env.hosts = REMOTE_HOSTS
env.user = USER
env.sudo_user = SUDO_USER
env.key_filename = KEY_FILENAME
def setup_clean():
with prefix('source %s/bin/activate' % VENV_PATH):
with cd(WORKING_PATH):
try:
run("rm -rf %s" % CURRENT_DIR)
except:
pass
try:
run("rm -rf %s" % RELEASES_DIR)
except:
pass
try:
run("rm -rf %s" % CHECKOUT_DIR)
except:
pass
run("mkdir -p %s %s" % (RELEASES_DIR, SHARED_DIR))
run("git clone %s %s" % (GIT_URL, CHECKOUT_DIR))
create_release()
symlink_current()
def use_branch():
with cd("%s/%s" % (WORKING_PATH, CHECKOUT_DIR)):
run("git checkout %s" % GIT_BRANCH)
def update_remote_code():
with cd("%s/%s" % (WORKING_PATH, CHECKOUT_DIR)):
run("git pull")
def create_release():
release = datetime.datetime.now().isoformat()
release_dir = "%s/%s" % (RELEASES_DIR, release)
with cd(WORKING_PATH):
run("mkdir %s" % release_dir)
run("cp -Rv %s/* %s/" % (CHECKOUT_DIR, release_dir))
run("rm -rf %s/.git %s/.gitignore" % (release_dir, release_dir))
def symlink_current():
with cd(WORKING_PATH):
dirs = run("ls -d %s/*/" % RELEASES_DIR).split()
dirs.sort()
dirs.reverse()
dir = dirs[0].strip('/')
try:
run("unlink %s" % CURRENT_DIR)
except:
pass
run("ln -nfs %s %s" % (dir, CURRENT_DIR))
with cd(dir):
for path in SHARED_FILES:
run("ln -nfs %s/%s/%s %s" % (WORKING_PATH, SHARED_DIR,
path.split('/')[-1],
path))
with cd("%s/bin" % HOME_PATH):
try:
run("unlink ./run")
run("unlink ./stop")
except:
pass
run("ln -nfs %s/%s/config/run" % (WORKING_PATH, CURRENT_DIR))
run("ln -nfs %s/%s/config/stop" % (WORKING_PATH, CURRENT_DIR))
def sync_remote_assets():
with prefix("source %s/bin/activate" % VENV_PATH):
with cd("%s/%s" % (WORKING_PATH, CURRENT_DIR)):
run("./manage.py collectstatic --noinput")
try:
run("./manage.py compress --force")
except:
pass
def install_dependencies():
with prefix("source %s/bin/activate" % VENV_PATH):
with cd("%s/%s" % (WORKING_PATH, CURRENT_DIR)):
run("pip install -r requirements.txt")
def run_migrations():
with prefix("source %s/bin/activate" % VENV_PATH):
with cd("%s/%s" % (WORKING_PATH, CURRENT_DIR)):
try:
run("./manage.py createcachetable %s" % CACHE_TABLE_NAME)
except:
pass
run("./manage.py syncdb --noinput")
run("./manage.py migrate --noinput")
def stop():
run("sudo /sbin/stop tcamp", pty=False)
# with cd(HOME_PATH):
# run("./bin/stop")
def restart():
run("sudo /sbin/restart tcamp", pty=False)
# with cd(HOME_PATH):
# run("./bin/run")
def dump_cache():
run("sudo /etc/init.d/memcached restart", pty=False)
def cleanup():
with cd(WORKING_PATH):
dirs = run("ls -d %s/*/" % RELEASES_DIR).split()
dirs.sort()
dirs.reverse()
try:
to_clean = dirs[PAST_RELEASES:]
for dir in to_clean:
run("rm -rf %s" % dir)
except IndexError:
pass
def stage():
local("git push heroku master")
def deploy():
update_remote_code()
create_release()
symlink_current()
install_dependencies()
sync_remote_assets()
run_migrations()
cleanup()
restart()
def rollback():
with cd(WORKING_PATH):
dirs = run("ls -d %s/*/" % RELEASES_DIR).split()
dirs.sort()
dirs.reverse()
dir = dirs[1].strip('/')
try:
run("unlink %s" % CURRENT_DIR)
except:
pass
run("ln -s %s %s" % (dir, CURRENT_DIR))
run("rm -rf %s" % dirs[0].strip('/'))
install_dependencies()
sync_remote_assets()
restart()