-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfabfile.py
More file actions
181 lines (136 loc) · 4.74 KB
/
Copy pathfabfile.py
File metadata and controls
181 lines (136 loc) · 4.74 KB
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
"""
Starter fabfile for deploying a Django project.
Designed for Webfaction, but should work on any similar hosting system.
Change all the things marked CHANGEME. Other things can be left at their
defaults if you are happy with the default layout.
"""
__author__ = "Bicheng Zhang"
__copyright__ = "Copyright 2013, XiaoMaiFeng"
import posixpath
from fabric.api import run, local, abort, env, put, settings, cd, task
from fabric.decorators import runs_once
from fabric.contrib.files import exists
from fabric.context_managers import cd, lcd, settings, hide
# Host and login username:
env.hosts = ['jchen93@108.168.205.67'] ## CHANGEME!
# Directory where everything to do with this app will be stored on the server.
DJANGO_APP_ROOT = '/home/jchen93/webapps/xmf_test/' ## CHANGEME!
# Directory where static sources should be collected. This must equal the value
# of STATIC_ROOT in the settings.py that is used on the server.
STATIC_ROOT = '/static/' ## CHANGEME!
# Subdirectory of DJANGO_APP_ROOT in which project sources will be stored
SRC_SUBDIR = 'src'
DJANGO_APP_SRC = DJANGO_APP_ROOT + SRC_SUBDIR
# Subdirectory of DJANGO_APP_ROOT in which virtualenv will be stored
VENV_SUBDIR = 'venv'
# Python version
PYTHON_BIN = "python2.7"
PYTHON_PREFIX = "" # e.g. /usr/local Use "" for automatic
PYTHON_FULL_PATH = "%s/bin/%s" % (PYTHON_PREFIX, PYTHON_BIN) if PYTHON_PREFIX else PYTHON_BIN
# Commands to stop and start the webserver that is serving the Django app.
# CHANGEME! These defaults work for Webfaction
DJANGO_SERVER_STOP = posixpath.join(DJANGO_APP_ROOT, 'apache2', 'bin', 'stop')
DJANGO_SERVER_START = posixpath.join(DJANGO_APP_ROOT, 'apache2', 'bin', 'start')
DJANGO_SERVER_RESTART = None
src_dir = posixpath.join(DJANGO_APP_ROOT, SRC_SUBDIR)
venv_dir = posixpath.join(DJANGO_APP_ROOT, VENV_SUBDIR)
def virtualenv(venv_dir):
"""
Context manager that establishes a virtualenv to use.
"""
return settings(venv=venv_dir)
def run_venv(command, **kwargs):
"""
Runs a command in a virtualenv (which has been specified using
the virtualenv context manager
"""
run("source %s/bin/activate" % env.venv + " && " + command, **kwargs)
def install_dependencies():
ensure_virtualenv()
with virtualenv(venv_dir):
with cd(src_dir):
run_venv("pip install -r requirements.txt")
def ensure_virtualenv():
if exists(venv_dir):
return
with cd(DJANGO_APP_ROOT):
run("virtualenv --no-site-packages --python=%s %s" %
(PYTHON_BIN, VENV_SUBDIR))
run("echo %s > %s/lib/%s/site-packages/projectsource.pth" %
(src_dir, VENV_SUBDIR, PYTHON_BIN))
def ensure_src_dir():
if not exists(src_dir):
run("mkdir -p %s" % src_dir)
with cd(src_dir):
if not exists(posixpath.join(src_dir, '.hg')):
run("hg init")
def push_sources():
"""
Push source code to server
"""
#ensure_src_dir()
local("hg push -f ssh://%(user)s@%(host)s/%(path)s" %
dict(host=env.host,
user=env.user,
path=src_dir,
))
with cd(src_dir):
run("hg update")
@task
def webserver_stop():
"""
Stop the webserver that is running the Django instance
"""
run(DJANGO_SERVER_STOP)
@task
def webserver_start():
"""
Startsp the webserver that is running the Django instance
"""
run(DJANGO_SERVER_START)
@task
def webserver_restart():
"""
Restarts the webserver that is running the Django instance
"""
if DJANGO_SERVER_RESTART:
run(DJANGO_SERVER_RESTART)
else:
with settings(warn_only=True):
webserver_stop()
webserver_start()
def build_static():
assert STATIC_ROOT.strip() != '' and STATIC_ROOT.strip() != '/'
# Before Django 1.4 we don't have the --clear option to collectstatic
run("rm -rf %s/*" % STATIC_ROOT)
with virtualenv(venv_dir):
with cd(src_dir):
run_venv("./manage.py collectstatic -v 0 --noinput")
run("chmod -R ugo+r %s" % STATIC_ROOT)
@task
def first_deployment_mode():
"""
Use before first deployment to switch on fake south migrations.
"""
env.initial_deploy = True
def update_database():
with virtualenv(venv_dir):
with cd(src_dir):
if getattr(env, 'initial_deploy', False):
run_venv("./manage.py syncdb --all")
run_venv("./manage.py migrate --fake --noinput")
else:
run_venv("./manage.py syncdb --noinput")
run_venv("./manage.py migrate --noinput")
@task
def deploy():
"""
Deploy project.
"""
# with settings(warn_only=True):
# webserver_stop()
push_sources()
#install_dependencies()
#update_database()
#build_static()
#webserver_start()