-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfabfile.py
102 lines (82 loc) · 2.88 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
import os
from fabric.api import local
from fabric.context_managers import lcd
import time
BASE_DIR = os.path.dirname(__file__)
def __is_windows():
return os.name == "nt"
def __get_python():
if __is_windows():
return os.path.join(BASE_DIR,"venv/Scripts/python.exe")
else:
return os.path.join(BASE_DIR,"venv/bin/python")
def __get_pip():
if __is_windows():
return os.path.join(BASE_DIR,"venv/Scripts/pip.exe")
else:
return os.path.join(BASE_DIR,"venv/bin/pip")
def __get_manage():
return os.path.join(BASE_DIR,"api/manage.py")
def __python(command):
local("{} {}".format(__get_python(),command))
def __pip(command):
local("{} {}".format(__get_pip(),command))
def __manage(command):
__python("{} {}".format(__get_manage(),command))
####################################### Commands ###################################################
def requirements():
"""install requirements via pip"""
env = os.environ.get("woxenv", "prod")
r = os.path.join(BASE_DIR,"api","requirements", "{}.txt".format(env))
__pip("install -r {}".format(r))
def migrate():
"""make migrations and migrate db"""
__manage("makemigrations")
__manage("migrate")
def test():
"""run all tests"""
oldenv = os.environ.get("woxenv", "prod")
os.environ["woxenv"] = "test"
__manage("test {}".format(os.path.join(BASE_DIR,"api")))
os.environ["woxenv"] = oldenv
def installDependency():
local("sudo apt-get install nginx supervisor python-virtualenv uwsgi uwsgi-plugin-python postgresql libpq-dev")
def deploy():
"""deploy wox (including update) in prod"""
local("git pull")
requirements()
migrate()
test()
__manage("collectstatic --noinput")
run()
buildclient()
builddoc()
def buildclient():
"""build client"""
with lcd(os.path.join(BASE_DIR,"client")):
local("bower install --allow-root")
local("grunt build --force")
def run():
"""run app"""
env = os.environ.get("woxenv", "prod")
if env == "prod":
local("supervisorctl restart wox")
else:
__manage("runserver 0.0.0.0:7000")
def builddoc():
"""build gitbook doc"""
with lcd("/app/wox/doc"):
local("gitbook build")
def backup():
"""Backup DB and Media files to Dropbox (require dropbox-uploader installed)"""
backup_folder = os.path.join(BASE_DIR,"backup")
backup_name = time.strftime("%Y%m%d") + ".zip"
local("rm -r {}".format(backup_folder))
if not os.path.exists(backup_folder):
os.mkdir(backup_folder)
with lcd("/app"):
local("zip -r {} {}".format(os.path.join(backup_folder,"media.zip"),"wox_media"))
with lcd(backup_folder):
local("PGPASSWORD=scott pg_dump -U wox wox > {}".format("woxdb.dump"))
local("zip {} woxdb.dump media.zip".format(backup_name))
local("/root/github/Dropbox-Uploader/dropbox_uploader.sh upload {} {}".format(backup_name,backup_name))