blob: 6c932af6edb8e568e69f5c9b886d0c3ad059110f [file] [log] [blame]
Luigi Santivettied568d42020-05-18 00:53:17 +01001#!/bin/bash
2#
3# module.sh - apache
4#
5# Copyright 2019 Luigi Santivetti <luigi.santivetti@gmail.com>
6
7# Permission is hereby granted, free of charge, to any person obtaining a
8# copy of this software and associated documentation files (the "Software"),
9# to deal in the Software without restriction, including without limitation
10# the rights to use, copy, modify, merge, publish, distribute, sublicense,
11# and/or sell copies of the Software, and to permit persons to whom the
12# Software is furnished to do so, subject to the following conditions:
13
14# The above copyright notice and this permission notice (including the next
15# paragraph) shall be included in all copies or substantial portions of the
16# Software.
17
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21# ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25declare -r module="apache"
26source $common_sh
27
28module_enable $module
29declare -ar depmod=( mysql gerrit )
30
31declare -r etc_d="$mod_rootfs_d/etc"
32declare -r www_d="$mod_rootfs_d/var/www"
33declare -r log_d="$mod_rootfs_d/var/log/apache2"
34
35declare -r application_d="$www_d/application"
36declare -r html_d="$www_d/html"
37
38declare -r conf_d="$etc_d/apache2"
39declare -r available_d="$conf_d/sites-available"
40declare -r enabled_d="$conf_d/sites-enabled"
41declare -r views_d="$application_d/views"
42declare -r controllers_d="$application_d/controllers"
43
44# Docker context directory
45declare -r mod_authnz_external_mirror="mod-auth-external.git"
46declare -r mod_authnz_external_branch="mod_authnz_external-3.3.3"
47declare -r mod_authnz_external_d="${mod_docker_d}/mod_authnz_external"
48
49# Output files
50declare -r ports_f="$conf_d/ports.conf"
51declare -r apache2_f="$conf_d/apache2.conf"
52declare -r dockerfile_f="$mod_docker_d/Dockerfile"
53declare -r vhost_http_f="$available_d/vhost-http.conf"
54declare -r vhost_https_f="$available_d/vhost-https.conf"
55declare -r vhost_gerrit_f="$available_d/vhost-gerrit.conf"
56declare -r http_authentication_bang_f="${www_d}/http_authentication"
57
58# Staged resources
59declare -r backend_d="$mod_staging_d/code-igniter-v3-giggi.git"
60declare -r frontend_d="$mod_staging_d/pelican-subtle-giggi.git"
61declare -r content_d="$mod_staging_d/mdcon.git"
62declare -r pythonenv_d="$mod_staging_d/pelican-venv"
63
64declare -r pelican_version="4.0.1"
65declare -r markdown_version="3.1.1"
66declare -r setuptools_version="45"
67
68declare -ir apache_has_dummy_certs=1
69if [ $apache_has_dummy_certs -eq 1 ]; then
70 declare -r letsencrypt_d="$mod_staging_d/dummy_cert"
71else
72 declare -r letsencrypt_d="/etc/letsencrypt"
73fi
74declare -r certificates_l="$etc_d/certificates"
75
76declare -ar PELICAN_CLEAN_TARGETS=( pelican-all )
77declare -r apache_has_media=0
78if [ $apache_has_media -eq 1 ]; then
79 declare -r clean_skiptag_sh="clean_skiptag.sh"
80 declare -r skiptag="_.${host_name}."
81 declare -r media_d="$HOME/media"
82 declare -r photos_d="$media_d/photos"
83 declare -r videos_d="$media_d/videos"
84 declare -ar PELICAN_MAKE_TARGETS=( pelican-all )
85 declare -ar PELICAN_INSTALL_TARGETS=( pelican-all )
86else
87 declare -ar PELICAN_MAKE_TARGETS=( pelican-html )
88 declare -ar PELICAN_INSTALL_TARGETS=( pelican-html pelican-theme )
89fi
90declare -ar CODEIGN_INSTALL_TARGETS=( codeign-all )
91
92declare -ar mod_more_dirs=( \
93 $www_d $html_d $etc_d $conf_d $log_d $available_d $enabled_d \
94 $application_d $views_d $controllers_d $mod_authnz_external_d \
95)
96
97declare -ar mod_more_files=( \
98 $ports_f $apache2_f $vhost_http_f $vhost_https_f $vhost_gerrit_f \
99 $dockerfile_f $http_authentication_bang_f \
100)
101
102declare -ar mod_more_trefs=( \
103 ports_t apache2_t vhost_http_t vhost_https_t vhost_gerrit_t \
104 dockerfile_t http_authentication_bang_t \
105)
106
107function tod_watch
108{
109 __watch_module_common || return $s_err
110}
111
112function tod_doins
113{
114 __doins_module_common || return $s_err
115 __apache_do_sites || return $s_err
116 __apache_do_links || return $s_err
117}
118
119function tod_fetch
120{
121 __fetch_module_common || return $s_err
122}
123
124function tod_upins
125{
126 __upins_module_common || return $s_err
127}
128
129function tod_upmod
130{
131 __fetch_module_common || return $s_err
132 __apache_do_ext_modules || return $s_err
133 __apache_do_dummy_cert || return $s_err
134 __apache_do_backend || return $s_err
135 __apache_do_frontend || return $s_err
136}
137
138function tod_clmod
139{
140 __clmod_module_common || return $s_err
141}
142
143function tod_clins
144{
145 __clins_module_common || return $s_err
146}
147
148function tod_doall
149{
150 # upmod must come after
151 tod_doins && tod_upmod || return $s_err
152}
153
154function tod_upall
155{
156 # upmod must come after
157 tod_upins && tod_upmod || return $s_err
158}
159
160function __apache_do_ext_modules
161{
162 local origin_d="${mod_staging_d}/${mod_authnz_external_mirror}"
Luigi Santivetti11526b82020-10-28 23:58:03 +0000163 if [ -d "$origin_d" ]; then
164 rm -rf ${mod_authnz_external_d}/* || return $s_err
165 cp -ar ${origin_d}/* ${mod_authnz_external_d}
166 else
167 lets -l -w "nothing to do for apache external modules"
168 return $s_ok
169 fi
Luigi Santivettied568d42020-05-18 00:53:17 +0100170}
171
172function __apache_do_dummy_cert_helper
173{
174 local -r digital_sign="digitalSignature\nextendedKeyUsage=serverAuth"
175 local -r dn_dns="DNS:${host_name}\nkeyUsage=$digital_sign"
176 local -r dn_lhs="[dn]\nCN=${host_name}\n[req]\ndistinguished_name"
177 local -r dn_rhs="dn\n[EXT]\nsubjectAltName"
178
179 openssl req -x509 -out ${host_name}.crt -keyout ${host_name}.key \
180 -newkey rsa:2048 -nodes -sha256 \
181 -subj "/CN=${host_name}" -extensions EXT \
182 -config <(printf "$dn_lhs = $dn_rhs=$dn_dns") 2>&1 | lets -l -x "openssl"
183
184 return ${PIPESTATUS[0]}
185}
186
187function __apache_do_dummy_cert
188{
189 if [ $apache_has_dummy_certs -eq 1 ]; then
190 if [ ! -d $letsencrypt_d ]; then
191 mkdir -p $letsencrypt_d && \
192 pushd $letsencrypt_d >/dev/null || return $s_err
193 __apache_do_dummy_cert_helper && \
194 echo "# Dummy $host_name conf" > $host_name.conf && \
195 popd >/dev/null || {
196 lets -l -e "failed to make dummy certificates"
197 popd >/dev/null; return $s_err
198 }
199 fi
200 fi
201
202 lets -l -i "using $module SSL certificates from: $letsencrypt_d"
203 return $s_ok
204}
205
206function __apache_do_links
207{
208 __apache_do_dummy_cert || return $s_err
209
210 # NOTE: container path must match localhost path
211 #
212 # i.e.
213 # localhost:/etc container:/etc OK
214 # localhost:/home/luigi container:/home/??? NOT OK
215 #
216 ln -sf "${letsencrypt_d}" "${certificates_l}"
217}
218
219function __apache_do_sites
220{
221 if [ ! -d "$enabled_d" ]; then
222 lets -l -e "invalid: $enabled_d"
223 return 1
224 fi
225
226 # This list controls what site to enable
227 local -a sites=( $vhost_http_f $vhost_https_f $vhost_gerrit_f )
228
229 pushd $enabled_d &>/dev/null
230
231 local file
232 for file in ${sites[@]}; do
233 local link_name="$(basename "$file")"
234 file="../sites-available/$link_name"
235
236 if [ -f "$file" ]; then
237 ln -sf $file $link_name
238 else
239 lets -l -e "failed link $file"
240 popd &>/dev/null
241 return 1
242 fi
243 done
244
245 popd &>/dev/null
246}
247
248# @1 : absolute path to the target directory
249# @2 : (optional) requirements txt file
250function __apache_do_venv
251{
252 local -ar deps=( python3-dev python3-pip python3-venv python3-wheel )
253
254 if [ -z "$1" ] || [ ! -f "$2" ]; then
255 lets -l -e "virtual environment: invalid params"
256 return $s_err
257 fi
258
259 if [ ! -e "$1/bin/activate" ]; then
260 local install="python3 -m pip install --no-cache-dir --upgrade"
261 local -a global_packages=( "virtualenv" "pip" )
262 local -a local_packages=(
263 "wheel"
264 "setuptools==$setuptools_version"
265 "pelican==$pelican_version"
266 "markdown==$markdown_version"
267 )
268
269 util_install_dependency ${deps[@]} || {
270 lets -l -e "install python dependencies"
271 return $s_err
272 }
273
274 mkdir -p "$1" || {
275 lets -l -e "virtual envirnment: mkdir failed"
276 return $s_err
277 }
278
279 $install --user ${global_packages[@]} 2>&1 | lets -l -x "pip3"
280
281 python3 -m venv "$1" 2>&1 | lets -l -x "python"
282 [ "${PIPESTATUS[0]}" -eq 0 ] || {
283 lets -l -e "virtual environment: create failed"
284 return $s_err
285 }
286
287 source "$1/bin/activate" || return $s_err
288 (
289 $install ${local_packages[@]} 2>&1 | lets -l -x "pip3"
290 [ "${PIPESTATUS[0]}" -eq 0 ] || {
291 deactivate
292 return $s_err2
293 }
294
295 while read -r pkg; do
296 $install $pkg 2>&1 | lets -l -x "pip3"
297 [ "${PIPESTATUS[0]}" -eq 0 ] || {
298 deactivate
299 return $s_err3
300 }
301 done < "$2"
302
303 deactivate
304 )
305 case "$?" in
306 $s_err2) lets -l -e "install local packages"; return $s_err ;;
307 $s_err3) lets -l -e "install $(basename $2)"; return $s_err ;;
308 esac
309 else
310 lets -l -w "virtual environment: exists already"
311 fi
312
313 return $s_ok
314}
315
316function __apache_frontend_clean
317{
318 local target
319
320 for target in ${PELICAN_CLEAN_TARGETS[@]}; do
321 case "$target" in
322 pelican-html ) make clean-html || return $s_err ;;
323 pelican-theme ) make clean-theme || return $s_err ;;
324 pelican-photos ) make clean-photos || return $s_err ;;
325 pelican-videos ) make clean-videos || return $s_err ;;
326 pelican-distclean ) make distclean || return $s_err ;;
327 pelican-all ) make clean || return $s_err ;;
328 esac
329 done
330}
331
332function __apache_frontend_make
333{
334 local target
335
336 for target in ${PELICAN_MAKE_TARGETS[@]}; do
337 case "$target" in
338 pelican-html)
339 PELICAN_PHOTO_LIBRARY=$photos_d \
340 PELICAN_PHOTO_EXCLUDEALL="1" \
341 PELICAN_VIDEO_LIBRARY=$videos_d \
342 PELICAN_VIDEO_EXCLUDEALL="1" \
343 PELICAN_SITEURL=$host_name \
344 PELICAN_CONTENT=$content_d \
345 make html || return $s_err ;;
346 pelican-all)
347 PELICAN_PHOTO_LIBRARY=$photos_d \
348 PELICAN_PHOTO_EXCLUDE=${EXCLUDED_PHOTOS[@]} \
349 PELICAN_PHOTO_SKIPTAG=$skiptag \
350 PELICAN_VIDEO_LIBRARY=$videos_d \
351 PELICAN_VIDEO_EXCLUDE=${EXCLUDED_VIDEOS[@]} \
352 PELICAN_SITEURL=$host_name \
353 PELICAN_VIDEO_SKIPTAG=$skiptag \
354 PELICAN_CONTENT=$content_d \
355 PELICAN="pelican" \
356 make html || return $s_err ;;
357 esac
358 done
359}
360
361
362# FIXME: improve integration with skiptag
363function __finalise_install
364{
365 [ ! -f "$1/$clean_skiptag_sh" ] || {
Luigi Santivettie30b4c92020-10-25 21:22:19 +0000366 pushd "$1" &>/dev/null || return $s_err
367 sudo -E /bin/bash "$1/$clean_skiptag_sh"
368 popd &>/dev/null
Luigi Santivettied568d42020-05-18 00:53:17 +0100369 }
370}
371
372function __apache_frontend_install
373{
374 local target
375
376 [ -d "$views_d" ] && [ -d "$html_d" ] || {
377 lets -l -w "not installing frontend"
378 return $s_ok
379 }
380
381 for target in ${PELICAN_INSTALL_TARGETS[@]}; do
382 case "$target" in
383 pelican-html)
384 INSTALLDIR=$views_d \
Luigi Santivettie30b4c92020-10-25 21:22:19 +0000385 sudo -E make install-html || return $s_err ;;
Luigi Santivettied568d42020-05-18 00:53:17 +0100386 pelican-theme)
387 INSTALLDIR_THEME=$html_d \
Luigi Santivettie30b4c92020-10-25 21:22:19 +0000388 sudo -E make install-theme || return $s_err ;;
Luigi Santivettied568d42020-05-18 00:53:17 +0100389 pelican-photos)
390 INSTALLDIR_PHOTOS=$photos_d \
Luigi Santivettie30b4c92020-10-25 21:22:19 +0000391 sudo -E make install-photos || return $s_err ;;
Luigi Santivettied568d42020-05-18 00:53:17 +0100392 pelican-videos)
393 INSTALLDIR_VIDEOS=$videos_d \
Luigi Santivettie30b4c92020-10-25 21:22:19 +0000394 sudo -E make install-videos || return $s_err ;;
Luigi Santivettied568d42020-05-18 00:53:17 +0100395 pelican-all)
396 INSTALLDIR=$views_d \
397 INSTALLDIR_THEME=$html_d \
398 INSTALLDIR_PHOTOS=$photos_d \
399 INSTALLDIR_VIDEOS=$videos_d \
Luigi Santivettie30b4c92020-10-25 21:22:19 +0000400 sudo -E make install || return $s_err
Luigi Santivettied568d42020-05-18 00:53:17 +0100401
402 # FIXME: improve integration with skiptag
403 __finalise_install $photos_d || \
404 lets -l -e "Failed to finalise photos"
405 __finalise_install $videos_d || \
406 lets -l -e "Failed to finalise videos" ;;
407 esac
408 done
Luigi Santivettie30b4c92020-10-25 21:22:19 +0000409
410 # Default in Linux in www-data, apply to all installed contents here
411 local -ir uid=$(stat -c "%u" $html_d)
412 local -ir gid=$(stat -c "%g" $html_d)
413
414 sudo find $html_d -type f -exec /bin/bash -c \
415 "set -x; chmod 644 {} && chown $uid:$gid {}" \; || return $s_err
416 sudo find $views_d -type f -exec /bin/bash -c \
417 "set -x; chmod 644 {} && chown $uid:$gid {}" \; || return $s_err
418 sudo find $html_d -type d -exec /bin/bash -c \
419 "set -x; chmod 755 {} && chown $uid:$gid {}" \; || return $s_err
420 sudo find $views_d -type d -exec /bin/bash -c \
421 "set -x; chmod 755 {} && chown $uid:$gid {}" \; || return $s_err
422
423 if [ -d "$photos_d" ]; then
424 sudo find $photos_d -type f -exec /bin/bash -c \
425 "set -x; chmod 644 {} && chown $uid:$gid {}" \; || return $s_err
426 sudo find $photos_d -type d -exec /bin/bash -c \
427 "set -x; chmod 755 {} && chown $uid:$gid {}" \; || return $s_err
428 fi
429
430 if [ -d "$videos_d" ]; then
431 sudo find $videos_d -type f -exec /bin/bash -c \
432 "set -x; chmod 644 {} && chown $uid:$gid {}" \; || return $s_err
433 sudo find $videos_d -type d -exec /bin/bash -c \
434 "set -x; chmod 755 {} && chown $uid:$gid {}" \; || return $s_err
435 fi
Luigi Santivettied568d42020-05-18 00:53:17 +0100436}
437
438function __create_apache_frontend
439{
440 if [ $apache_has_media -eq 1 ]; then
441 [ -d "$photos_d" ] || mkdir -p "$photos_d" || return $s_err
442 [ -d "$videos_d" ] || mkdir -p "$videos_d" || return $s_err
443 fi
444
445 set -e; source $1/bin/activate; set +e || return $s_err3
446
447 __apache_frontend_clean || { deactivate; return $s_err4; }
448 __apache_frontend_make || { deactivate; return $s_err5; }
449 __apache_frontend_install || { deactivate; return $s_err6; }
450 __apache_frontend_clean || { deactivate; return $s_err4; }
451
452 deactivate; true
453}
454
455function __apache_do_frontend
456{
457 local req="requirements.txt"
458 local file="$(realpath "$(find "$frontend_d" -name $req)")"
459
460 # Is this running in a container? If so this script assumes all global
461 # system wise dependencies to be provided at build time, only care about
462 # local packages taht can vary on the runtime.
463 if [ -z "$instance_d" ]; then
464 lets -l -i "virutal environment: container"
465 local venv_d=$pythonenv_con_d
466 else
467 local venv_d=$pythonenv_d
468 fi
469
470 __apache_do_venv $venv_d $file || return $s_err
471 lets -l -d "virtual environment: complete"
472
473 pushd $frontend_d &>/dev/null
474 __create_apache_frontend $pythonenv_d 2>&1 | lets -l -x "make"
475 case "${PIPESTATUS[0]}" in
476 $s_ok ) popd &>/dev/null; return $s_ok ;;
477 $s_err3 ) lets -l -e "failed to activate python" ;;
478 $s_err4 ) lets -l -e "failed to clean frontend" ;;
479 $s_err5 ) lets -l -e "failed to make frontend" ;;
480 $s_err6 ) lets -l -e "failed to install frontend" ;;
481 * ) lets -l -e "failed unknown error" ;;
482 esac; popd &>/dev/null; return $s_err
483}
484
485function __apache_backend_install
486{
487 local target
488
489 [ -d "$backend_d/application" ] && \
490 [ -d "$backend_d/system" ] && [ -f "$backend_d/index.php" ] || {
491 lets -l -w "not installing backend"
492 return $s_ok
493 }
494
495 for target in ${CODEIGN_INSTALL_TARGETS[@]}; do
496 case "$target" in
497 codeign-all)
Luigi Santivettie30b4c92020-10-25 21:22:19 +0000498 sudo -E cp -ra $backend_d/application $www_d || return $s_err2
499 sudo -E cp -ra $backend_d/system $www_d || return $s_err3
500 sudo -E cp -a $backend_d/index.php $html_d || return $s_err4 ;;
Luigi Santivettied568d42020-05-18 00:53:17 +0100501 esac
502 done
Luigi Santivettie30b4c92020-10-25 21:22:19 +0000503
504 # Default in Linux in www-data, apply to all installed contents here
505 local -ir uid=$(stat -c "%u" $html_d)
506 local -ir gid=$(stat -c "%g" $html_d)
507
508 sudo find "$www_d/application" -type f -exec /bin/bash -c \
509 "set -x; chmod 644 {} && chown $uid:$gid {}" \; || return $s_err
510 sudo find "$www_d/application" -type d -exec /bin/bash -c \
511 "set -x; chmod 755 {} && chown $uid:$gid {}" \; || return $s_err
512 sudo find "$www_d/system" -type f -exec /bin/bash -c \
513 "set -x; chmod 644 {} && chown $uid:$gid {}" \; || return $s_err
514 sudo find "$www_d/system" -type d -exec /bin/bash -c \
515 "set -x; chmod 755 {} && chown $uid:$gid {}" \; || return $s_err
516 sudo chmod 644 "$html_d/index.php"
517 sudo chown $uid:$gid "$html_d/index.php"
Luigi Santivettied568d42020-05-18 00:53:17 +0100518}
519
520function __create_apache_backend
521{
522 __apache_backend_install
523}
524
525function __apache_do_backend
526{
527 __create_apache_backend 2>&1 | lets -l -x "copy"
528 case "$?" in
529 $s_err2 ) lets -l -e "failed to install application code" ;;
530 $s_err3 ) lets -l -e "failed to install system code" ;;
531 $s_err4 ) lets -l -e "failed to install index.html" ;;
532 esac
533}