-
Notifications
You must be signed in to change notification settings - Fork 10
/
invenio-initialise-virtualenv
executable file
·177 lines (151 loc) · 6.14 KB
/
invenio-initialise-virtualenv
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
#!/bin/bash
#
# A helper devscript to initialise virtualenv for Invenio development.
# Assumes certain sudo rights. Typical usage:
#
# $ # Invenio sources are cloned in ~/private/src/invenio
# $ mkvirtualenv invenio-maint-1.0
# $ invenio-initialise-virtualenv --yes-i-know
# $ deactivate && workon invenio-maint-1.0
# $ invenio-recreate-demo-site --yes-i-know
#
# For more information, see
# <https://github.com/tiborsimko/invenio-devscripts>.
#
# Tibor Simko <[email protected]>
#
# Copyright (C) 2012, 2013, 2014, 2015 CERN.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
# config section:
CFG_INVENIO_SRCDIR=${CFG_INVENIO_SRCDIR:=~/private/src/invenio}
CFG_INVENIO_PREFIX=${CFG_INVENIO_PREFIX:=/opt/invenio}
CFG_INVENIO_HOSTNAME=${CFG_INVENIO_HOSTNAME:=pcuds06}
CFG_INVENIO_DOMAINNAME=${CFG_INVENIO_DOMAINNAME:=cern.ch}
CFG_INVENIO_PORT_HTTP=${CFG_INVENIO_PORT_HTTP:=80}
CFG_INVENIO_PORT_HTTPS=${CFG_INVENIO_PORT_HTTPS:=443}
CFG_INVENIO_USER=${CFG_INVENIO_USER:=$(whoami)}
CFG_INVENIO_ADMIN=${CFG_INVENIO_ADMIN:[email protected]}
CFG_INVENIO_DATABASE_NAME=${CFG_INVENIO_DATABASE_NAME:=invenio}
CFG_INVENIO_DATABASE_USER=${CFG_INVENIO_DATABASE_USER:=invenio}
CFG_INVENIO_DATABASE_PASS=${CFG_INVENIO_DATABASE_PASS:=my123p\$ss}
CFG_INVENIO_APACHECTL=${CFG_INVENIO_APACHECTL:=/etc/init.d/apache2}
CFG_INVENIO_MYSQLCTL=${CFG_INVENIO_MYSQLCTL:=/etc/init.d/mysql}
CFG_INVENIO_VIRTUALENVS=${CFG_INVENIO_VIRTUALENVS:=$HOME/.virtualenvs}
# sanity check: virtualenv name
if [ "x$VIRTUAL_ENV" = "x" ]; then
echo "[ERROR] Not running from within some virtual environment."
echo "[ERROR] Please run 'mkvirtualenv invenio-maint-1.0' or similar first."
exit
fi
VEHOME=$CFG_INVENIO_VIRTUALENVS
VENAME=$(basename $VIRTUAL_ENV)
# derive DB name out of VE name:
DBNAME=$(echo $VENAME | sed "s/\-//g" | sed "s/\.//g")
# derive Invenio branch name out of VE name:
BRANCHNAME=$(echo $VENAME | sed "s/^invenio-//g")
# helper variables derived out of VE location:
VESRCDIR=$VEHOME/$VENAME/src/invenio
VEDSTDIR=$VEHOME/$VENAME/opt/invenio
# sanity check: CLI confirmation
if [[ "$@" != *"--yes-i-know"* ]]; then
echo "[ERROR] You did not use --yes-i-know. Not going to initialise virtualenv $VENAME."
exit
fi
# quit on errors and potentially unbound symbols:
set -o errexit
set -o nounset
# sanity check: hostname
if [ "`hostname -s`" != "$CFG_INVENIO_HOSTNAME" ]; then
echo "[ERROR] This hostname is not whitelisted for initialising virtualenv."
exit
fi
# verify whether this virtual environment wasn't already initialised:
if [ -d "$VEHOME/$VENAME/opt" ]; then
echo "[ERROR] It seems that directory $VEHOME/$VENAME/opt already exists."
echo "[ERROR] Trying to initialise a venv that was already initialised?"
echo "[ERROR] Exiting."
exit
fi
# initialise virtualenv:
echo "[INFO] Creating postactivate hook..."
cat > $VEHOME/$VENAME/bin/postactivate <<EOF1
#!/bin/bash
# set up configuration variables:
export CFG_INVENIO_SRCDIR=$VESRCDIR
export CFG_INVENIO_PREFIX=$VEDSTDIR
export CFG_INVENIO_USER='$CFG_INVENIO_USER'
export CFG_INVENIO_DATABASE_NAME='$DBNAME'
export CFG_INVENIO_DATABASE_USER='$DBNAME'
export CFG_INVENIO_DATABASE_PASS='$CFG_INVENIO_DATABASE_PASS'
# amend Apache vhost symlinks:
sudo systemctl stop apache2
sudo rm -f /etc/apache2/sites-enabled/invenio.conf
sudo ln -s $VEDSTDIR/etc/apache/invenio-apache-vhost.conf \
/etc/apache2/sites-enabled/invenio.conf
sudo rm -f /etc/apache2/sites-enabled/invenio-ssl.conf
sudo ln -s $VEDSTDIR/etc/apache/invenio-apache-vhost-ssl.conf \
/etc/apache2/sites-enabled/invenio-ssl.conf
sudo systemctl start apache2
# set PATH:
export PATH=$VEDSTDIR/bin:\$PATH
# switch directory:
cdvirtualenv src/invenio
# end of file
EOF1
chmod u+x $VEHOME/$VENAME/bin/postactivate
echo "[INFO] Creating postdeactivate hook..."
cat > $VEHOME/$VENAME/bin/postdeactivate <<EOF2
#!/bin/bash
# unset variables:
unset CFG_INVENIO_SRCDIR
unset CFG_INVENIO_PREFIX
unset CFG_INVENIO_USER
unset CFG_INVENIO_DATABASE_NAME
unset CFG_INVENIO_DATABASE_USER
unset CFG_INVENIO_DATABASE_PASS
# remove Apache vhost symlinks:
sudo systemctl stop apache2
sudo rm -f /etc/apache2/sites-enabled/invenio.conf
sudo rm -f /etc/apache2/sites-enabled/invenio-ssl.conf
# switch directory:
cd $HOME
# end of file
EOF2
chmod u+x $VEHOME/$VENAME/bin/postdeactivate
echo "[INFO] Setting up database..."
echo "CREATE DATABASE $DBNAME DEFAULT CHARACTER SET utf8; GRANT ALL PRIVILEGES ON $DBNAME.* TO $DBNAME@localhost IDENTIFIED BY '$CFG_INVENIO_DATABASE_PASS';" | mysql -u root -B
echo "[INFO] Cloning Invenio sources..."
git-new-workdir $CFG_INVENIO_SRCDIR $VESRCDIR $BRANCHNAME
echo "[INFO] Creating Invenio target directory..."
mkdir -p $VEDSTDIR
sudo chown -R $CFG_INVENIO_USER $VEDSTDIR
echo "[INFO] Setting up Invenio site-packages symlink..."
SITEPACKAGES=$($VEHOME/$VENAME/bin/python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")
ln -s $VEDSTDIR/lib/python/invenio $SITEPACKAGES/invenio
echo "[INFO] Installing Python pre-requisites..."
if [[ "$@" != *"--skip-pip"* ]]; then
if [ -e $VESRCDIR/requirements.txt ]; then
pip install -r $VESRCDIR/requirements.txt
fi
set +o errexit # on Debian Jessie, we need to specify HDF5_DIR:
HDF5_DIR=$(dirname $(locate libhdf5.so | head -1)) pip install cython h5py
set -o errexit
if [ -e $VESRCDIR/requirements-extras.txt ]; then
pip install -r $VESRCDIR/requirements-extras.txt --allow-all-external --allow-unverified gnuplot-py
fi
fi
echo "[INFO] Done. You may want to run now:"
echo "[INFO] $ deactivate && workon $VENAME"
echo "[INFO] $ invenio-recreate-demo-site --yes-i-know"