-
Notifications
You must be signed in to change notification settings - Fork 13
/
utility_redcap.py
120 lines (102 loc) · 4.59 KB
/
utility_redcap.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
from fabric.api import *
from tempfile import mkstemp
import os
import utility
try:
import configparser
except:
from six.moves import configparser
__all__ = []
def get_current_redcap_version():
"""
gets the current redcap version from database
"""
with settings(user=env.deploy_user):
with hide('output'):
current_version = run('mysql -s -N -e "SELECT value from redcap_config WHERE field_name=\'redcap_version\'"')
return current_version
def make_upload_target():
"""
Make the directory from which new software will be deployed,
e.g., /var/www.backup/redcap-20160117T1543/
"""
env.upload_target_backup_dir = '/'.join([env.upload_project_full_path, env.remote_project_name])
with settings(user=env.deploy_user):
run("mkdir -p %(upload_target_backup_dir)s" % env)
def upload_package_and_extract(name, upgrade=False):
"""
Upload the redcap package and extract it into the directory from which new
software will be deployed, e.g., /var/www.backup/redcap-20160117T1543/
"""
# NOTE: run as $ fab <env> package make_upload_target upe ...necessary env
# variables are set by package and make_upload_target funcitons
with settings(user=env.deploy_user):
# Make a temp folder to upload the tar to
temp1 = run('mktemp -d')
put(name, temp1)
# Test where temp/'receiving' is
temp2 = run('mktemp -d')
# Extract in temp ... -C specifies what directory to extract to
# Extract to temp2 so the tar is not included in the contents
run('tar -xzf %s/%s -C %s' % (temp1, name, temp2))
# Transfer contents from temp2/redcap to ultimate destination
with settings(warn_only=True):
if run('test -d %s/webtools2/pdf/font/unifont' % env.upload_target_backup_dir).succeeded:
run('chmod ug+w %s/webtools2/pdf/font/unifont/*' % env.upload_target_backup_dir)
# Write the new code on top of the existing code
if upgrade == False:
run('rsync -rc %s/redcap/* %s' % (temp2, env.upload_target_backup_dir))
else:
# exclude some files during upgrades
exclusions = "--exclude=database.php --exclude=hook_functions.php"
run('rsync -rc %s %s/redcap/* %s' % (exclusions, temp2, env.upload_target_backup_dir))
# make sure the temp file directory in redcap web space will be writeable
run('chmod -R g+w %s/temp' % env.upload_target_backup_dir)
run('chmod -R g+w %s/modules' % env.upload_target_backup_dir)
# Remove the temp directories
run('rm -rf %s %s' % (temp1, temp2))
def move_software_to_live():
"""
Replace the symbolic link to the old code with symbolic link to new code.
"""
with settings(user=env.deploy_user):
with settings(warn_only=True):
if run("test -d %(live_project_full_path)s" % env).succeeded:
# we need to back this directory up on the fly, destroy it and then symlink it back into existence
with settings(warn_only=False):
new_backup_dir = env.upload_target_backup_dir + "-previous"
run("mkdir -p %s" % new_backup_dir)
run("cp -rf -P %s/* %s" % (env.live_project_full_path, new_backup_dir))
run("rm -rf %s" % env.live_project_full_path)
# now switch the new code to live
run('ln -s %s %s' % (env.upload_target_backup_dir,env.live_project_full_path))
# update directory permissions
run('chmod 775 %s/modules' %env.upload_target_backup_dir)
def set_redcap_base_url():
"""
Set the REDCap base url
"""
set_redcap_config('redcap_base_url', env.url_of_deployed_app)
def set_redcap_config(field_name="", value=""):
"""
Update a single values in the redcap config table
"""
with settings(user=env.deploy_user):
run('echo "update redcap_config set value=\'%s\' where field_name = \'%s\';" | mysql' % (value, field_name))
def test(warn_only=False):
"""
Run all tests against a running REDCap instance
"""
utility.write_remote_my_cnf()
version = get_current_redcap_version()
utility.delete_remote_my_cnf()
local("python tests/test.py %s/ redcap_v%s/" % (env.url_of_deployed_app,version))
with settings(warn_only=True):
if local("python tests/test.py %s/ redcap_v%s/" % (env.url_of_deployed_app,version)).failed:
if warn_only:
warn("One or more tests failed.")
return(False)
else:
abort("One or more tests failed.")
else:
return(True)