-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
143 lines (126 loc) · 4.41 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
from fabric.api import *
from fabric.contrib.files import exists, append, comment
from fabric.colors import red
import os
env.sitename = os.path.basename(os.getcwd())
env.mongo_host = 'fire.rccc.ou.edu'
env.psql_host = 'fire.rccc.ou.edu'
env.apache_config = '/etc/httpd/conf.d/%(sitename)s.conf' % env
env.python = '/usr/bin/python2.6'
def testing():
"""
Work on staging environment
"""
env.settings = 'testing'
env.path = '/var/www/apps/%(sitename)s' % env
env.virtpy = '%(path)s/virtpy' % env
env.log_path = '%(path)s/log' % env
env.hosts = ['test.cybercommons.org']
def fire():
"""
Setup on fire.rccc.ou.edu
"""
env.settings = 'production'
env.path = '/scratch/www/wsgi_sites/%(sitename)s' % env
env.virtpy = '%(path)s/virtpy' % env
env.log_path = '%(path)s/log' % env
env.hosts = ['fire.rccc.ou.edu']
def production():
"""
Work on production environment
"""
env.settings = 'production'
env.path = '/var/www/apps/%(sitename)s' % env
env.virtpy = '%(path)s/virtpy' % env
env.log_path = '%(path)s/log' % env
env.hosts = ['production.cybercommons.org']
def setup():
"""
Setup directories and copy everything but virtual environment to server,
then install virtual environment based on requirements.txt
"""
setup_directories()
copy_working_dir()
setup_virtualenv()
install_requirements()
apache_config()
bounce_apache()
def deploy():
"""
Deploy changes which don't impact virtual environment
"""
copy_working_dir()
bounce_apache()
def setup_directories():
"""
Setup directories on the remote system
"""
if not exists('%(path)s' % env):
sudo('mkdir -p %(path)s' % env)
sudo('mkdir -p %(log_path)s' % env)
sudo('mkdir -p %(virtpy)s' % env)
sudo('chgrp -R cybercom %(path)s' % env)
sudo('chmod -R g+rw %(path)s' % env)
def virtualenv(command):
"""
Wrapper to activate and run virtual environment
"""
with cd(env.virtpy):
run('source %(virtpy)s/bin/activate' % env + '&&' + command)
def setup_virtualenv():
"""
Install the virtual environment
"""
run('virtualenv -p %(python)s --no-site-packages %(virtpy)s' % env)
def bounce_apache():
""" Restart the apache web server """
sudo('/etc/init.d/httpd restart')
def apache_config(secure=False):
"""
Set the apache config file to point to wsgi. Assumes app will be accessible at /sitename/ and
.wsgi named sitename.wsgi
"""
# check if apache config lines exist in old wsgi_sites.conf and comment if found
comment('/etc/httpd/conf.d/wsgi_sites.conf', r'^WSGIScriptAlias /%(sitename)s .*$' % env, use_sudo=True)
confline = 'WSGIScriptAlias /%(sitename)s %(path)s/siteconfig.wsgi' %env
append('%(apache_config)s' % env, confline, use_sudo=True)
if secure:
secure_app = """
<Location /%(sitename)s>
AuthType Basic
require valid-user
TKTAuthLoginURL http://test.cybercommons.org/accounts/login/
TKTAuthTimeoutURL http://test.cybercommons.org/accounts/login/?timeout=1
TKTAuthPostTimeoutURL http://test.cybercommons.org/accounts/login/?posttimeout=1
TKTAuthUnauthURL http://test.cybercommons.org/accounts/login/?unauth=1
TKTAuthIgnoreIP on
TKTAuthBackArgName next
</Location>
""" % (env)
append('%(apache_config)s' % env, secure_app, use_sudo=True)
def copy_working_dir():
"""
Shuttle application code from local to remote
"""
local('tar --exclude virtpy -czf /tmp/deploy_%(sitename)s.tgz .' % env)
put('/tmp/deploy_%(sitename)s.tgz' % env, '%(path)s/deploy_%(sitename)s.tgz' % env)
sudo('cd %(path)s; tar -xf deploy_%(sitename)s.tgz; rm deploy_%(sitename)s.tgz' % env)
local('rm /tmp/deploy_%(sitename)s.tgz' % env)
def install_requirements():
"""
Install the contents of requirements.txt to the virtual environment
"""
check = exists('%(path)s/requirements.txt' % env)
if check:
virtualenv('pip install -E %(virtpy)s -r %(path)s/requirements.txt' % env)
else:
print red("Can't find requirements.txt!")
def upgrade_requirements():
"""
Install the contents of requirements.txt to the virtual environment
"""
check = exists('%(path)s/requirements.txt' % env)
if check:
virtualenv('pip install --upgrade -E %(virtpy)s -r %(path)s/requirements.txt' % env)
else:
print red("Can't find requirements.txt!")