-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
executable file
·162 lines (138 loc) · 5.92 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
from fabric.api import env, local, run, sudo, require
APT_INSTALL_PACKAGES = [
"libpq-dev",
"python-dev",
"libffi-dev",
"libssl-dev",
"python-pip",
"postgresql",
"postgresql-contrib",
"redis-server",
"supervisor"
]
base = '/server'
virtualenvs = '.virtualenvs'
projects = 'projects'
repo = 'mood-map'
# Environments
def vagrant():
# change from the default user to 'vagrant'
env.user = 'vagrant'
# connect to the port-forwarded ssh
env.hosts = ['127.0.0.1:2222']
# use vagrant ssh key
result = local('vagrant ssh-config | grep IdentityFile', capture=True)
env.key_filename = result.split()[1]
env.base_dir = base
env.virtualenvs_dir = virtualenvs
env.virtualenv = '01'
env.projects_dir = projects
env.repo_dir = repo
env.settings = 'local'
# Bootstrap commands
def bootstrap():
require('hosts', provided_by=[vagrant])
bs_install_packages()
bs_make_folder_structure()
bs_setup_virtualenv()
bs_link_project()
bs_install_requirements()
bs_install_heroku_toolbelt()
bs_setup_supervisor()
def bs_install_packages():
sudo("apt-get update")
sudo("apt-get upgrade -y")
sudo("apt-get -y install " + " ".join(APT_INSTALL_PACKAGES))
def bs_make_folder_structure():
sudo("if [ ! -d %(base_dir)s ]; then mkdir -p %(base_dir)s; chmod 777 "
"%(base_dir)s; fi" % env)
sudo("if [ ! -d %(base_dir)s/%(virtualenvs_dir)s ]; then mkdir -p %("
"base_dir)s/%("
"virtualenvs_dir)s; chmod "
"777 %(base_dir)s/%(virtualenvs_dir)s; fi" % env)
sudo("if [ ! -d %(base_dir)s/%(projects_dir)s ]; then mkdir -p %("
"base_dir)s/%("
"projects_dir)s; chmod "
"777 %(base_dir)s/%(projects_dir)s; fi" % env)
#setup logging folder
sudo("if [ ! -d %(base_dir)s/%(projects_dir)s/logs ]; then mkdir -p %("
"base_dir)s/%("
"projects_dir)s/logs/%(repo_dir)s; chmod "
"777 %(base_dir)s/%(projects_dir)s/logs/%(repo_dir)s; fi" % env)
def bs_setup_virtualenv():
"Fetches the virtualenv package"
run("if [ ! -e virtualenv-13.1.0.tar.gz ]; then wget "
" https://pypi.python.org/packages/source/v/virtualenv/virtualenv-13"
".1.0.tar.gz; fi")
run("if [ ! -d virtualenv-13.1.0 ]; then tar xzf "
"virtualenv-13.1.0.tar.gz; fi")
run("rm -f virtualenv")
run("ln -s virtualenv-13.1.0 virtualenv")
run("if [ ! -d %(base_dir)s/%(virtualenvs_dir)s/%(virtualenv)s ]; then "
"python "
"~/virtualenv/virtualenv.py --no-site-packages %(base_dir)s/%("
"virtualenvs_dir)s/%(virtualenv)s; fi" % env)
sudo("chmod 777 %(base_dir)s/%(virtualenvs_dir)s/%(virtualenv)s" % env)
def bs_link_project(): # vagrant only
run("ln -sf /vagrant %(base_dir)s/%(projects_dir)s/%(repo_dir)s" % env)
def bs_install_requirements():
sudo("source %(base_dir)s/%(virtualenvs_dir)s/%(virtualenv)s/bin/activate;"
"pip install -r %(base_dir)s/%(projects_dir)s/%("
"repo_dir)s/requirements/%(settings)s.txt;" % env)
def bs_install_heroku_toolbelt():
run("wget -O- https://toolbelt.heroku.com/install-ubuntu.sh | sh")
def bs_setup_supervisor():
run('if [ ! -e %(base_dir)s/%(projects_dir)s/logs/%('
'repo_dir)s/celery-worker.log ]; then touch %(base_dir)s/%('
'projects_dir)s/logs/%(repo_dir)s/celery-worker.log; fi' % env)
sudo('cp %(base_dir)s/%(projects_dir)s/%('
'repo_dir)s/config/supervisor/mood-map-celery.conf '
'/etc/supervisor/conf.d/mood-map-celery.conf' % env)
run('if [ ! -e %(base_dir)s/%(projects_dir)s/logs/%('
'repo_dir)s/celery-worker.log ]; then touch %(base_dir)s/%('
'projects_dir)s/logs/%(repo_dir)s/twitter_stream.log; fi' % env)
sudo('cp %(base_dir)s/%(projects_dir)s/%('
'repo_dir)s/config/supervisor/twitter_stream.conf '
'/etc/supervisor/conf.d/twitter-stream.conf' % env)
sudo('supervisorctl reread')
sudo('supervisorctl update')
# Django commands
def runserver():
run("source %(base_dir)s/%(virtualenvs_dir)s/%("
"virtualenv)s/bin/activate; python %("
"base_dir)s/%(projects_dir)s/%(repo_dir)s/manage.py "
"runserver "
"0.0.0.0:8000 --settings=config.settings.%(settings)s" % env)
def migrate():
run("source %(base_dir)s/%(virtualenvs_dir)s/%("
"virtualenv)s/bin/activate; python %("
"base_dir)s/%(projects_dir)s/%(repo_dir)s/manage.py "
"migrate --settings=config.settings.%(settings)s" % env)
def makemigrations():
run("source %(base_dir)s/%(virtualenvs_dir)s/%("
"virtualenv)s/bin/activate; python %("
"base_dir)s/%(projects_dir)s/%(repo_dir)s/manage.py "
"makemigrations --settings=config.settings.%(settings)s" % env)
def syncdb():
run("source %(base_dir)s/%(virtualenvs_dir)s/%("
"virtualenv)s/bin/activate; python %("
"base_dir)s/%(projects_dir)s/%(repo_dir)s/manage.py "
"syncdb --settings=config.settings.%(settings)s" % env)
def collectstatic():
run("source %(base_dir)s/%(virtualenvs_dir)s/%("
"virtualenv)s/bin/activate; python %("
"base_dir)s/%(projects_dir)s/%(repo_dir)s/manage.py "
"collectstatic --settings=config.settings.%(settings)s" % env)
def test():
run("source %(base_dir)s/%(virtualenvs_dir)s/%("
"virtualenv)s/bin/activate; coverage run %("
"base_dir)s/%(projects_dir)s/%(repo_dir)s/manage.py "
"test "
"--settings=config.settings.%(settings)s" % env)
def test_report():
run("source %(base_dir)s/%(virtualenvs_dir)s/%(virtualenv)s/bin/activate;"
"cd %(base_dir)s/%(projects_dir)s//%(repo_dir)s/;"
"coverage html --omit='admin.py'" % env)
# Supervisor commands
def supervisor_status():
sudo('supervisorctl status')