-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfabfile.py
executable file
·183 lines (146 loc) · 7.39 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"""
BVD v1.0
Copyright (c) 2012 Voltage Security
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import subprocess, os, types, shlex, re
from multiprocessing import Process
from fabric import api
"""Globals"""
app_name = 'bvd'
wrapper = '. /usr/local/bin/virtualenvwrapper.sh;'
path_to_bvd = '/opt/bvd'
def install_requirements(*args,**kwargs):
#TODO: If need be, we can add a local pypi mirror for any additional packages
#install = 'pip install -r requirements.txt --extra-index-url %s' % (pypi_mirror)
install = 'pip install --upgrade --user -r requirements.txt'
if kwargs.get('remote'):
api.run('%(install)s' % dict(install=install))
else:
#check if user
#TODO: provide arguments: whether a user wants to use a virtualenv
#mkvirtualenv = '%s %s' % (wrapper,'mkvirtualenv %s' % app_name)
#workon = '%s %s' % (wrapper,'workon %s' % app_name)
install = '%s -e %s' % (install, os.getcwd())
#cmd = '%s ; %s ; %s' % (mkvirtualenv, workon, install)
cmd = install
out, err = subprocess.Popen(cmd,shell=True).communicate()
def start_django_dev_server(*args,**kwargs):
cmd = 'cd ./src/bvd && python manage.py runserver 0.0.0.0:8000'
subprocess.call(cmd, shell=True)
print 'CI Monitor is running under http://localhost:8000'
def sync_db(*args,**kwargs):
cmd = 'cd ./src/bvd && python manage.py syncdb --noinput && python manage.py createcachetable bvd_cache_table'
subprocess.call(cmd,shell=True)
def migrate_db(*args, **kwargs):
cmd = 'cd ./src/bvd && python manage.py migrate'
subprocess.call(cmd, shell=True)
def git_pull(*args, **kwargs):
"""
This function:
Updates the local repository by runnning `git pull`
"""
subprocess.call('git pull', shell=True)
def update_packages(*args, **kwargs):
subprocess.call('pip install --upgrade -r requirements.txt', shell=True)
def collectstatic(*args, **kwargs):
subprocess.call('cd ./src/bvd && python manage.py collectstatic --noinput', shell=True)
def reload_wsgi(*args, **kwargs):
subprocess.call('touch src/config/bvd.wsgi', shell=True)
def refresh_kiosk(*args, **kwargs):
subprocess.call('./src/config/kiosk_refresh.sh', shell=True)
def local(*args,**kwargs):
"""
Main function to be run for local development. Function installs requirements, then starts the django dev server.
Before running this function, check to make sure the CI_INSTALLATIONS tuple in settings.py is properly set to your CI servers
"""
install_requirements()
sync_db()
start_django_dev_server()
def ci_build(*args,**kwargs):
"""
Function to be run by a CI build system. Function installs requirements, and application to $USER's home directory.
"""
install_requirements()
#TODO: Add Apache config and restart
def configure_apache(*args, **kwargs):
"""
This function:
1 - Tells the user to add these two lines to /etc/apache2/httpd.conf:
LoadModule wsgi_module libexec/apache2/mod_wsgi.so
Include {{ path_to_bvd}}/src/config/bvd.conf
where {{ path_to_bvd }} is the location of the installation of BVD
2 - In src/config/bvd.conf.template, replaces {{ path_to_bvd }} with the location of the installation of BVD
3 - Moves to src/config/bvd.conf
4 - Tells the user to restart apache with `apachectl -k restart`
"""
print "Add these two lines to the appropriate places in /etc/apache2/httpd.conf:"
print "LoadModule wsgi_module libexec/apache2/mod_wsgi.so"
print "Include {{ path_to_bvd }}/src/config/bvd.conf"
print "where {{ path_to_bvd }} is the location of the installation of BVD"
# Check for the existence of bvd.conf before the template is 'rendered'
bvd_config_exists = os.path.isfile("%s/src/config/bvd.conf" % path_to_bvd)
# Render the template to the correct location
if not bvd_config_exists:
with open("%s/src/config/bvd.conf.template" % path_to_bvd, 'r') as template:
with open("%s/src/config/bvd.conf" % path_to_bvd, 'w') as destination:
for line in template:
destination.write(re.sub(r"{{ *path_to_bvd *}}", path_to_bvd, line))
# Restart Apache
print "Then restart apache with `apachectl -k restart`"
def configure_automatic_start_on_login_osx(*args, **kwargs):
"""
This Function:
1 - Checks for/creates directory ~/Library/LaunchAgents
2 - copies src/config/com.user.loginscript.plist.template to ~/Library/LaunchAgents/
3 - replaces {{ path_to_bvd }} with the location of the installation of BVD
4 - moves com.user.loginscript.plist.template to com.user.loginscript.plist
5 - calls `launchctl load ~/Library/LaunchAgents/com.user.loginscript.plist`
6 - outputs a message to STDOUT asking the user to test that the script works by calling
`launchctl start com.user.loginscript`
"""
os.path.exists(os.path.expanduser("~/Library/LaunchAgents"))
with open("%s/src/config/com.user.loginscript.plist.template" % path_to_bvd, 'r') as template:
with open(os.path.expanduser("~/Library/LaunchAgents/com.user.loginscript.plist"), 'w') as destination:
for line in template:
destination.write(re.sub(r"{{ *path_to_bvd *}}", path_to_bvd, line))
subprocess.call(["launchctl", "load", os.path.expanduser("~/Library/LaunchAgents/com.user.loginscript.plist")])
print "test that the automatic launch script works by calling `launchctl start com.user.loginscript`"
def update_bvd(*args, **kwargs):
"""
This function:
1 - Updates the local git repository
2 - Updates package requirements
3 - Syncs the database
4 - Migrates the database with South
5 - Collects static files
6 - Reloads wsgi by `touch`ing the wsgi config file
7 - Refreshes Chrome tab with a force reload of all page assets
"""
git_pull()
update_packages()
sync_db()
migrate_db()
collectstatic()
reload_wsgi()
refresh_kiosk()