-
Notifications
You must be signed in to change notification settings - Fork 1
/
fabfile.py
114 lines (87 loc) · 2.61 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
from fabric.api import *
from fabtools import require
from fabtools.python import virtualenv
import time
# Config
env.user = 'fermigier'
env.group = 'fermigier'
env.hosts = ['dedibox']
env.app_name = local('python setup.py --name', capture=True).strip()
env.app_root = '/home/{}/websites/{}'.format(env.user, env.app_name)
env.app_env = '/home/{}/websites/envs/{}'.format(env.user, env.app_name)
UWSGI_CONFIG_TPL = """
[uwsgi]
uid = %(user)s
gid = %(group)s
chdir = %(app_root)s
virtualenv = %(app_env)s
socket = uwsgi.sock
chmod-socket = 666
pythonpath = .
wsgi-file = wsgi.py
callable = app
plugins = python
processes = 4
threads = 2
"""
@task
def setup():
require.python.virtualenv(env.app_env)
require.files.directory(env.app_root)
@task
def install_deps():
with cd(env.app_root):
put("deps.txt", "deps.txt")
with virtualenv(env.app_env):
require.python.install_requirements("deps.txt")
@task
def refresh_uwsgi():
require.file("/etc/uwsgi/apps-available/%s.ini" % env.app_name,
contents=UWSGI_CONFIG_TPL % {
'user': env.user,
'group': env.group,
'app_root': env.app_root,
'app_env': env.app_env,
}, use_sudo=True)
sudo("ln -sf /etc/uwsgi/apps-available/%s.ini "
"/etc/uwsgi/apps-enabled/%s.ini" % (env.app_name, env.app_name))
require.service.restarted('uwsgi')
@task
def pack():
# create a new source distribution as tarball
local('rm -rf dist')
local('python setup.py sdist --formats=gztar', capture=False)
@task
def backup():
with cd(env.app_root):
now = time.strftime("%Y%m%d-%H%M%S")
run("cp feedback.csv ~/backup/lemooc-feedback-{}.csv".format(now))
run("cp data/lemooc.db ~/backup/lemooc-db-{}.db".format(now))
@task
def deploy():
# figure out the release name and version
dist = local('python setup.py --fullname', capture=True).strip()
# upload the source tarball to the temporary folder on the server
put('dist/{}.tar.gz'.format(dist), '/tmp/{}.tar.gz'.format(dist))
# create a place where we can unzip the tarball, then enter
# that directory and unzip it
with cd("/tmp"):
run('rm -rf {}/*'.format(dist))
run('tar xzf /tmp/{}.tar.gz'.format(dist))
with cd(dist):
run("tox")
run("cp -r /tmp/{}/* {}".format(dist, env.app_root))
install_deps()
with cd(env.app_root):
with virtualenv(env.app_env):
run("py.test")
with cd(env.app_root):
with virtualenv(env.app_env):
run("./manage.py create_db")
refresh_uwsgi()
run('rm -f /tmp/{}.tar.gz'.format(dist))
@task(default=True)
def default():
pack()
backup()
deploy()