forked from liqd/adhocracy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·345 lines (298 loc) · 10.1 KB
/
build.sh
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/bin/sh
DEFAULT_BRANCH=develop
GIT_URL=https://github.com/liqd/adhocracy
SERVICE_TEMPLATE=etc/sysv-init.in
SERVICE_TEMPLATE_URL=https://raw.github.com/liqd/adhocracy/$DEFAULT_BRANCH/$SERVICE_TEMPLATE
CHECK_PORT_FREE_URL=https://raw.github.com/liqd/adhocracy/$DEFAULT_BRANCH/scripts/check_port_free.py
SUPERVISOR_PORTS="5005 5006 5010"
ADHOCRACY_PORT=5001
set -e
usage() {
cat << EOF
usage: $0 [options]
Install adhocracy on debian.
OPTIONS:
-h Show this message
-M Install MySQL client libraries
-c file Use the given buildout config file
-A Do not start now
-S Do not configure system services
-s Install/Reinstall only non-superuser parts
-u Install only superuser parts
-U Set the username adhocracy should run as
-b Branch to check out
EOF
}
buildout_cfg_file=
autostart=true
setup_services=true
not_use_sudo_commands=false
not_use_user_commands=false
adhoc_user=$USER
install_mysql_client=false
arch_install=false
branch=$DEFAULT_BRANCH
if [ -n "$SUDO_USER" ]; then
adhoc_user=$SUDO_USER
fi
PKGS_TO_INSTALL=''
PKG_INSTALL_CMD=''
while getopts DpMmASsuc:U:b:R:o name
do
case $name in
M) install_mysql_client=true;;
A) autostart=false;;
S) setup_services=false;;
s) not_use_sudo_commands=true;;
u) not_use_user_commands=true;;
U) adhoc_user=$OPTARG;;
c) buildout_cfg_file=$OPTARG;;
b) branch=$OPTARG;;
?) usage
exit 2;;
*) echo "Invalid option $name!"
usage
exit 3;;
esac
done
distro=''
if which apt-get >/dev/null ; then
distro='debian'
PYTHON_CMD='python'
PIP_CMD='pip'
PKG_INSTALL_CMD='apt-get install -yqq'
fi
if which pacman >/dev/null ; then
distro='arch'
PYTHON_CMD='python2'
PIP_CMD='pip2'
PKG_INSTALL_CMD='pacman -S --needed --noconfirm'
fi
if [ -z "$distro" ] ; then
echo "Your OS is currently not supported! Aborting"
exit 35
fi
ROOTDIR_FROM_CALLER="adhocracy_buildout/"
if [ "$(basename $PWD)" = 'adhocracy_buildout' ]; then
cd "$(dirname $PWD)"
ROOTDIR_FROM_CALLER=""
fi
# Abort if we're running from somewhere else inside adhocracy_buildout
if [ "${PWD#*/adhocracy_buildout}" != "$PWD" ]; then
echo "You should not run build.sh from the adhocracy_buildout directory. Instead, run it from the directory which contains adhocracy_buildout."
exit 34
fi
if ! $not_use_sudo_commands; then
SUDO_CMD=sudo
if [ "$(id -u)" -eq 0 ]; then
SUDO_CMD=
fi
if ! $SUDO_CMD true ; then
echo 'sudo failed. Is it installed and configured?'
exit 20
fi
fi
# Prefer curl because wget < 1.14 fails on https://raw.github.com/ because
# it doesn't support x509v3 alternative names.
if which curl > /dev/null ; then
downloader_program=curl
else
if ! $not_use_sudo_commands ; then
$SUDO_CMD $PKG_INSTALL_CMD curl
downloader_program=curl
else
# Fall back to wget
wget_version=$(wget --version | head -n 1 | sed 's#[^0-9]*\([0-9][0-9.]*\).*#\1#' || true)
if test "$wget_version" = "1.12"; then
echo "WARNING: Old version of wget detected. Downloads from raw.github.com will fail. Install curl!"
fi
downloader_program=wget
fi
fi
# Usage: download URL filename
download() {
case "$downloader_program" in
curl )
curl -fsS "$1" -o "$2"
;;
wget )
wget -nv "$1" -O "$2"
;;
*)
echo "Invalid downloader"
exit 1
;;
esac
}
if [ "$buildout_cfg_file" = "hhu" ]; then
buildout_cfg_file=buildouts/hhu.cfg
elif [ -n "$buildout_cfg_file" ]; then
buildout_cfg_file=$(readlink -f "$buildout_cfg_file")
else
buildout_cfg_file=buildout.cfg
fi
if ! $not_use_sudo_commands; then
case $distro in
debian )
PKGS_TO_INSTALL=$PKGS_TO_INSTALL' gcc make build-essential bin86 unzip libpcre3-dev git mercurial python python-setuptools libssl-dev libbz2-dev libsqlite3-dev openjdk-6-jre libpq-dev'
PKGS_TO_INSTALL=$PKGS_TO_INSTALL' openssh-client mutt'
PKGS_TO_INSTALL=$PKGS_TO_INSTALL' ruby rubygems'
if $install_mysql_client; then
PKGS_TO_INSTALL=$PKGS_TO_INSTALL' libmysqlclient-dev'
fi
;;
arch )
PKGS_TO_INSTALL=$PKGS_TO_INSTALL' gcc make base-devel bin86 unzip git mercurial python2 sqlite jre7-openjdk postgresql-libs'
PKGS_TO_INSTALL=$PKGS_TO_INSTALL' openssh mutt'
PKGS_TO_INSTALL=$PKGS_TO_INSTALL' ruby'
if $install_mysql_client; then
PKGS_TO_INSTALL=$PKGS_TO_INSTALL' libmysqlclient'
fi
;;
esac
# Install all Packages
echo $PKG_INSTALL $PKGS_TO_INSTALL
$SUDO_CMD $PKG_INSTALL_CMD $PKGS_TO_INSTALL
if $setup_services; then
if [ "$adhoc_user" = "root" ]; then
echo "You are root. Please use the -U flag to set the user adhocracy should be running as"
exit 35
fi
if [ -r "adhocracy_buildout/${SERVICE_TEMPLATE}" ]; then
stmpl=$(cat "adhocracy_buildout/${SERVICE_TEMPLATE}")
else
stmpl=$(download $SERVICE_TEMPLATE_URL -)
fi
case $distro in
debian )
SERVICE_CMD='update-rc.d'
SERVICE_CMD_PREFIX='defaults'
INIT_FILE='/etc/init.d/adhocracy_services'
;;
arch )
SERVICE_CMD='systemctl enable'
INIT_FILE='/etc/rc.d/adhocracy_services'
echo "
[Unit]
Description=Adhocracy Daemon
[Service]
Type=forking
ExecStart=/bin/sh /etc/rc.d/adhocracy_services start
ExecStop=/bin/sh /etc/rc.d/adhocracy_services stop
ExecStatus=/bin/sh /etc/rc.d/adhocracy_services status
[Install]
WantedBy=multi-user.target
" | $SUDO_CMD tee >/dev/null /etc/systemd/system/adhocracy_services.service
;;
esac
echo "$stmpl" | \
sed -e "s#\${[^}]*:[^}]*user}#$adhoc_user#" \
-e "s#\${buildout:directory}#$(readlink -f .)/adhocracy_buildout#" \
-e "s#\${domains:main}#supervisord#" | \
$SUDO_CMD tee "$INIT_FILE" >/dev/null
$SUDO_CMD chmod a+x "$INIT_FILE"
$SUDO_CMD $SERVICE_CMD adhocracy_services $SERVICE_CMD_PREFIX
fi
fi
if $not_use_user_commands; then
exit 0
fi
if [ "$(id -u)" -eq 0 ]; then
echo "You should not install adhocracy as a root user"
exit 33
fi
# Create buildout directory
if ! mkdir -p adhocracy_buildout; then
echo 'Cannot create adhocracy_buildout directory. Please change to a directory where you can create files.'
exit 21
fi
if [ '!' -w adhocracy_buildout ]; then
echo 'Cannot write to adhocracy_buildout directory. Change to another directory, remove adhocracy_buildout, or run as another user'
exit 22
fi
if [ -x adhocracy_buildout/bin/supervisorctl ]; then
adhocracy_buildout/bin/supervisorctl shutdown >/dev/null 2>/dev/null || true
fi
check_port_free=adhocracy_buildout/scripts/check_port_free.py
if [ '!' -e "$check_port_free" ]; then
check_port_free_tmp=$(mktemp)
check_port_free="$check_port_free_tmp"
if ! download "$CHECK_PORT_FREE_URL" "$check_port_free_tmp"; then
ex=$?
echo "Download failed. Are you connected to the Internet?"
exit $ex
fi
fi
$PYTHON_CMD $check_port_free -g 10 --kill-pid $ADHOCRACY_PORT $SUPERVISOR_PORTS
if [ -n "$check_port_free_tmp" ]; then
rm -f $check_port_free_tmp
fi
if [ '!' -e adhocracy_buildout/.git ]; then
git clone "$GIT_URL" adhocracy_buildout
(cd adhocracy_buildout && git checkout -q "$branch")
fi
cd adhocracy_buildout
if [ '!' -e python/buildout.python/src ]; then
git submodule init
git submodule update
fi
# Install local python if necessary
if [ '!' -x bin/python ]; then
if [ '!' -f python/bin/buildout ]; then
(cd python && python bootstrap.py)
fi
(cd python && bin/buildout)
fi
# Fix until https://github.com/collective/buildout.python/pull/31 is accepted
find python/buildout.python/ -name *pyc -delete
# Fix until https://github.com/collective/buildout.python/pull/32 is accepted
if [ "$(strings bin/python | grep '^PyUnicodeUCS._DecodeLatin1$')" '!=' "$(strings eggs/lxml-*.egg/lxml/etree.so 2>/dev/null | grep '^PyUnicodeUCS._DecodeLatin1$')" ]; then
rm -rf -- eggs/lxml-*.egg
fi
# Set up adhocracy configuration
ln -s -f "${buildout_cfg_file}" ./buildout_current.cfg
# bootstrap our buildout if it is outdated or not available
HAVE_BUILDOUT_VERSION=$(bin/buildout --version 2>&1 | cut -d ' ' -f 3)
WANT_BUILDOUT_VERSION=$(sed -n 's#zc\.buildout = ##p' versions.cfg)
if test "$HAVE_BUILDOUT_VERSION" "!=" "$WANT_BUILDOUT_VERSION"; then
bin/python bootstrap.py -c buildout_current.cfg
fi
# Install adhocracy
bin/buildout -c buildout_current.cfg
# Install adhocracy interactive script
echo '#!/bin/sh
set -e
cd "$(dirname $(dirname $(readlink -f $0)))"
# Remove caches (workaround: cache fails when switching adhocracy.client_location)
rm -rf var/data/templates
cp etc/adhocracy.ini etc/adhocracy-interactive.ini
# Comment out the following line to restrict access to local only
sed "s#host = .*#host = 0.0.0.0#" -i etc/adhocracy-interactive.ini
exec bin/paster serve --reload etc/adhocracy-interactive.ini
' > "bin/adhocracy_interactive.sh"
chmod a+x "bin/adhocracy_interactive.sh"
# Autostart adhocracy
if $autostart; then
bin/supervisord
echo "Use ${ROOTDIR_FROM_CALLER}bin/supervisorctl to control running services."
python scripts/check_port_free.py -o -g 20 ${SUPERVISOR_PORTS}
if bin/supervisorctl status | grep -vq RUNNING; then
sleep 2 # Wait for supervisord to register that everything's running
if bin/supervisorctl status | grep -vq RUNNING; then
echo 'Failed to start all services:'
bin/supervisorctl status
exit 31
fi
fi
pasterOutput=$(bin/paster setup-app etc/adhocracy.ini --name=content)
if echo "$pasterOutput" | grep -q ERROR; then
echo "$pasterOutput"
echo 'Error in paster setup'
exit 32
fi
echo
echo
echo "Type ${ROOTDIR_FROM_CALLER}bin/adhocracy_interactive.sh to run the interactive paster daemon."
echo "Then, navigate to http://127.0.0.1:${ADHOCRACY_PORT}/ to see adhocracy!"
echo "Use the email \"[email protected]\" and password \"password\" to login."
fi