blob: 3d30407348e2e6bc933ae6a8afba05b4938b0397 [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"
Luigi Santivettic2994d12020-11-03 23:01:48 +000063declare -r pythonenv_con_d="$mod_staging_d/pelican-venv-con"
Luigi Santivettied568d42020-05-18 00:53:17 +010064
65declare -r pelican_version="4.0.1"
66declare -r markdown_version="3.1.1"
67declare -r setuptools_version="45"
68
69declare -ir apache_has_dummy_certs=1
70if [ $apache_has_dummy_certs -eq 1 ]; then
71 declare -r letsencrypt_d="$mod_staging_d/dummy_cert"
72else
73 declare -r letsencrypt_d="/etc/letsencrypt"
74fi
75declare -r certificates_l="$etc_d/certificates"
76
77declare -ar PELICAN_CLEAN_TARGETS=( pelican-all )
78declare -r apache_has_media=0
79if [ $apache_has_media -eq 1 ]; then
80 declare -r clean_skiptag_sh="clean_skiptag.sh"
81 declare -r skiptag="_.${host_name}."
82 declare -r media_d="$HOME/media"
83 declare -r photos_d="$media_d/photos"
84 declare -r videos_d="$media_d/videos"
85 declare -ar PELICAN_MAKE_TARGETS=( pelican-all )
86 declare -ar PELICAN_INSTALL_TARGETS=( pelican-all )
87else
88 declare -ar PELICAN_MAKE_TARGETS=( pelican-html )
89 declare -ar PELICAN_INSTALL_TARGETS=( pelican-html pelican-theme )
90fi
91declare -ar CODEIGN_INSTALL_TARGETS=( codeign-all )
92
93declare -ar mod_more_dirs=( \
94 $www_d $html_d $etc_d $conf_d $log_d $available_d $enabled_d \
95 $application_d $views_d $controllers_d $mod_authnz_external_d \
96)
97
98declare -ar mod_more_files=( \
99 $ports_f $apache2_f $vhost_http_f $vhost_https_f $vhost_gerrit_f \
100 $dockerfile_f $http_authentication_bang_f \
101)
102
103declare -ar mod_more_trefs=( \
104 ports_t apache2_t vhost_http_t vhost_https_t vhost_gerrit_t \
105 dockerfile_t http_authentication_bang_t \
106)
107
108function tod_watch
109{
110 __watch_module_common || return $s_err
111}
112
113function tod_doins
114{
115 __doins_module_common || return $s_err
116 __apache_do_sites || return $s_err
117 __apache_do_links || return $s_err
118}
119
120function tod_fetch
121{
122 __fetch_module_common || return $s_err
123}
124
125function tod_upins
126{
127 __upins_module_common || return $s_err
128}
129
130function tod_upmod
131{
132 __fetch_module_common || return $s_err
133 __apache_do_ext_modules || return $s_err
134 __apache_do_dummy_cert || return $s_err
135 __apache_do_backend || return $s_err
136 __apache_do_frontend || return $s_err
137}
138
139function tod_clmod
140{
141 __clmod_module_common || return $s_err
142}
143
144function tod_clins
145{
146 __clins_module_common || return $s_err
147}
148
149function tod_doall
150{
151 # upmod must come after
152 tod_doins && tod_upmod || return $s_err
153}
154
155function tod_upall
156{
157 # upmod must come after
158 tod_upins && tod_upmod || return $s_err
159}
160
161function __apache_do_ext_modules
162{
Luigi Santivetti34c79f62020-11-03 22:58:35 +0000163 local src_d="${mod_staging_d}/${mod_authnz_external_mirror}"
164 local dst_d="${mod_authnz_external_d}"
165 if [ -d "$src_d" ] && [ -d "$dst_d" ]; then
166 rm -rf ${dst_d}/* || return $s_err
167 cp -ar ${src_d}/* ${dst_d}
Luigi Santivetti11526b82020-10-28 23:58:03 +0000168 else
169 lets -l -w "nothing to do for apache external modules"
170 return $s_ok
171 fi
Luigi Santivettied568d42020-05-18 00:53:17 +0100172}
173
174function __apache_do_dummy_cert_helper
175{
176 local -r digital_sign="digitalSignature\nextendedKeyUsage=serverAuth"
177 local -r dn_dns="DNS:${host_name}\nkeyUsage=$digital_sign"
178 local -r dn_lhs="[dn]\nCN=${host_name}\n[req]\ndistinguished_name"
179 local -r dn_rhs="dn\n[EXT]\nsubjectAltName"
180
181 openssl req -x509 -out ${host_name}.crt -keyout ${host_name}.key \
182 -newkey rsa:2048 -nodes -sha256 \
183 -subj "/CN=${host_name}" -extensions EXT \
184 -config <(printf "$dn_lhs = $dn_rhs=$dn_dns") 2>&1 | lets -l -x "openssl"
185
186 return ${PIPESTATUS[0]}
187}
188
189function __apache_do_dummy_cert
190{
191 if [ $apache_has_dummy_certs -eq 1 ]; then
192 if [ ! -d $letsencrypt_d ]; then
193 mkdir -p $letsencrypt_d && \
194 pushd $letsencrypt_d >/dev/null || return $s_err
195 __apache_do_dummy_cert_helper && \
196 echo "# Dummy $host_name conf" > $host_name.conf && \
197 popd >/dev/null || {
198 lets -l -e "failed to make dummy certificates"
199 popd >/dev/null; return $s_err
200 }
201 fi
202 fi
203
204 lets -l -i "using $module SSL certificates from: $letsencrypt_d"
205 return $s_ok
206}
207
208function __apache_do_links
209{
210 __apache_do_dummy_cert || return $s_err
211
212 # NOTE: container path must match localhost path
213 #
214 # i.e.
215 # localhost:/etc container:/etc OK
216 # localhost:/home/luigi container:/home/??? NOT OK
217 #
218 ln -sf "${letsencrypt_d}" "${certificates_l}"
219}
220
221function __apache_do_sites
222{
223 if [ ! -d "$enabled_d" ]; then
224 lets -l -e "invalid: $enabled_d"
225 return 1
226 fi
227
228 # This list controls what site to enable
229 local -a sites=( $vhost_http_f $vhost_https_f $vhost_gerrit_f )
230
231 pushd $enabled_d &>/dev/null
232
233 local file
234 for file in ${sites[@]}; do
235 local link_name="$(basename "$file")"
236 file="../sites-available/$link_name"
237
238 if [ -f "$file" ]; then
239 ln -sf $file $link_name
240 else
241 lets -l -e "failed link $file"
242 popd &>/dev/null
243 return 1
244 fi
245 done
246
247 popd &>/dev/null
248}
249
Luigi Santivettic2994d12020-11-03 23:01:48 +0000250function __apache_do_global_packages
251{
252 local -r venv_dir=$1; shift
253 # Append flags
254 local install="pip3 install ${@}"
255 local -a global_packages=( "virtualenv" "pip" "wheel" )
256
257 $install --user ${global_packages[@]} 2>&1 | lets -l -x "pip3"
258
259 python3 -m venv "$venv_dir" 2>&1 | lets -l -x "python"
260 [ "${PIPESTATUS[0]}" -eq 0 ] || {
261 lets -l -e "virtual environment: create failed"
262 return $s_err
263 }
264}
265
266function __apache_do_local_packages
267{
268 local -r venv_dir=$1; shift
269 local -r requirements=$1; shift
270 # Append flags
271 local install="pip3 install ${@}"
272 local -a local_packages=(
273 "wheel"
274 "setuptools==$setuptools_version"
275 "pelican==$pelican_version"
276 "markdown==$markdown_version"
277 )
278
279 source "$venv_dir/bin/activate" || return $s_err
280 (
281 $install ${local_packages[@]} 2>&1 | lets -l -x "pip3"
282 [ "${PIPESTATUS[0]}" -eq 0 ] || {
283 deactivate
284 return $s_err2
285 }
286
287 if [ -f "$requirements" ]; then
288 while read -r pkg; do
289 $install $pkg 2>&1 | lets -l -x "pip3"
290 [ "${PIPESTATUS[0]}" -eq 0 ] || {
291 deactivate
292 return $s_err3
293 }
294 done < "$requirements"
295 else
296 lets -l -w "no requirments file"
297 fi
298
299 deactivate
300 )
301 case "$?" in
302 $s_err2) lets -l -e "install local packages"; return $s_err ;;
303 $s_err3) lets -l -e "install $requirements"; return $s_err ;;
304 esac
305}
306
Luigi Santivettied568d42020-05-18 00:53:17 +0100307# @1 : absolute path to the target directory
308# @2 : (optional) requirements txt file
309function __apache_do_venv
310{
311 local -ar deps=( python3-dev python3-pip python3-venv python3-wheel )
312
Luigi Santivettic2994d12020-11-03 23:01:48 +0000313 if [ -z "$1" ]; then
Luigi Santivettied568d42020-05-18 00:53:17 +0100314 lets -l -e "virtual environment: invalid params"
315 return $s_err
316 fi
317
318 if [ ! -e "$1/bin/activate" ]; then
Luigi Santivettic2994d12020-11-03 23:01:48 +0000319 if which apt-get &>/dev/null; then
320 util_install_dependency ${deps[@]} || {
321 lets -l -e "install python dependencies"
322 return $s_err
323 }
324 fi
Luigi Santivettied568d42020-05-18 00:53:17 +0100325
326 mkdir -p "$1" || {
327 lets -l -e "virtual envirnment: mkdir failed"
328 return $s_err
329 }
330
Luigi Santivettic2994d12020-11-03 23:01:48 +0000331 __apache_do_global_packages $1 "--no-cache-dir --upgrade" && \
332 __apache_do_local_packages $1 $2 "--no-cache-dir --upgrade" || return $s_err
Luigi Santivettied568d42020-05-18 00:53:17 +0100333 else
334 lets -l -w "virtual environment: exists already"
Luigi Santivettic2994d12020-11-03 23:01:48 +0000335 __apache_do_local_packages $1 $2 || return $s_err
Luigi Santivettied568d42020-05-18 00:53:17 +0100336 fi
337
338 return $s_ok
339}
340
341function __apache_frontend_clean
342{
343 local target
344
345 for target in ${PELICAN_CLEAN_TARGETS[@]}; do
346 case "$target" in
347 pelican-html ) make clean-html || return $s_err ;;
348 pelican-theme ) make clean-theme || return $s_err ;;
349 pelican-photos ) make clean-photos || return $s_err ;;
350 pelican-videos ) make clean-videos || return $s_err ;;
351 pelican-distclean ) make distclean || return $s_err ;;
352 pelican-all ) make clean || return $s_err ;;
353 esac
354 done
355}
356
357function __apache_frontend_make
358{
359 local target
360
361 for target in ${PELICAN_MAKE_TARGETS[@]}; do
362 case "$target" in
363 pelican-html)
364 PELICAN_PHOTO_LIBRARY=$photos_d \
365 PELICAN_PHOTO_EXCLUDEALL="1" \
366 PELICAN_VIDEO_LIBRARY=$videos_d \
367 PELICAN_VIDEO_EXCLUDEALL="1" \
368 PELICAN_SITEURL=$host_name \
369 PELICAN_CONTENT=$content_d \
370 make html || return $s_err ;;
371 pelican-all)
372 PELICAN_PHOTO_LIBRARY=$photos_d \
373 PELICAN_PHOTO_EXCLUDE=${EXCLUDED_PHOTOS[@]} \
374 PELICAN_PHOTO_SKIPTAG=$skiptag \
375 PELICAN_VIDEO_LIBRARY=$videos_d \
376 PELICAN_VIDEO_EXCLUDE=${EXCLUDED_VIDEOS[@]} \
377 PELICAN_SITEURL=$host_name \
378 PELICAN_VIDEO_SKIPTAG=$skiptag \
379 PELICAN_CONTENT=$content_d \
380 PELICAN="pelican" \
381 make html || return $s_err ;;
382 esac
383 done
384}
385
386
387# FIXME: improve integration with skiptag
388function __finalise_install
389{
390 [ ! -f "$1/$clean_skiptag_sh" ] || {
Luigi Santivettie30b4c92020-10-25 21:22:19 +0000391 pushd "$1" &>/dev/null || return $s_err
392 sudo -E /bin/bash "$1/$clean_skiptag_sh"
393 popd &>/dev/null
Luigi Santivettied568d42020-05-18 00:53:17 +0100394 }
395}
396
397function __apache_frontend_install
398{
399 local target
400
401 [ -d "$views_d" ] && [ -d "$html_d" ] || {
402 lets -l -w "not installing frontend"
403 return $s_ok
404 }
405
406 for target in ${PELICAN_INSTALL_TARGETS[@]}; do
407 case "$target" in
408 pelican-html)
409 INSTALLDIR=$views_d \
Luigi Santivettie30b4c92020-10-25 21:22:19 +0000410 sudo -E make install-html || return $s_err ;;
Luigi Santivettied568d42020-05-18 00:53:17 +0100411 pelican-theme)
412 INSTALLDIR_THEME=$html_d \
Luigi Santivettie30b4c92020-10-25 21:22:19 +0000413 sudo -E make install-theme || return $s_err ;;
Luigi Santivettied568d42020-05-18 00:53:17 +0100414 pelican-photos)
415 INSTALLDIR_PHOTOS=$photos_d \
Luigi Santivettie30b4c92020-10-25 21:22:19 +0000416 sudo -E make install-photos || return $s_err ;;
Luigi Santivettied568d42020-05-18 00:53:17 +0100417 pelican-videos)
418 INSTALLDIR_VIDEOS=$videos_d \
Luigi Santivettie30b4c92020-10-25 21:22:19 +0000419 sudo -E make install-videos || return $s_err ;;
Luigi Santivettied568d42020-05-18 00:53:17 +0100420 pelican-all)
421 INSTALLDIR=$views_d \
422 INSTALLDIR_THEME=$html_d \
423 INSTALLDIR_PHOTOS=$photos_d \
424 INSTALLDIR_VIDEOS=$videos_d \
Luigi Santivettie30b4c92020-10-25 21:22:19 +0000425 sudo -E make install || return $s_err
Luigi Santivettied568d42020-05-18 00:53:17 +0100426
427 # FIXME: improve integration with skiptag
428 __finalise_install $photos_d || \
429 lets -l -e "Failed to finalise photos"
430 __finalise_install $videos_d || \
431 lets -l -e "Failed to finalise videos" ;;
432 esac
433 done
Luigi Santivettie30b4c92020-10-25 21:22:19 +0000434
435 # Default in Linux in www-data, apply to all installed contents here
436 local -ir uid=$(stat -c "%u" $html_d)
437 local -ir gid=$(stat -c "%g" $html_d)
438
439 sudo find $html_d -type f -exec /bin/bash -c \
440 "set -x; chmod 644 {} && chown $uid:$gid {}" \; || return $s_err
441 sudo find $views_d -type f -exec /bin/bash -c \
442 "set -x; chmod 644 {} && chown $uid:$gid {}" \; || return $s_err
443 sudo find $html_d -type d -exec /bin/bash -c \
444 "set -x; chmod 755 {} && chown $uid:$gid {}" \; || return $s_err
445 sudo find $views_d -type d -exec /bin/bash -c \
446 "set -x; chmod 755 {} && chown $uid:$gid {}" \; || return $s_err
447
448 if [ -d "$photos_d" ]; then
449 sudo find $photos_d -type f -exec /bin/bash -c \
450 "set -x; chmod 644 {} && chown $uid:$gid {}" \; || return $s_err
451 sudo find $photos_d -type d -exec /bin/bash -c \
452 "set -x; chmod 755 {} && chown $uid:$gid {}" \; || return $s_err
453 fi
454
455 if [ -d "$videos_d" ]; then
456 sudo find $videos_d -type f -exec /bin/bash -c \
457 "set -x; chmod 644 {} && chown $uid:$gid {}" \; || return $s_err
458 sudo find $videos_d -type d -exec /bin/bash -c \
459 "set -x; chmod 755 {} && chown $uid:$gid {}" \; || return $s_err
460 fi
Luigi Santivettied568d42020-05-18 00:53:17 +0100461}
462
463function __create_apache_frontend
464{
465 if [ $apache_has_media -eq 1 ]; then
466 [ -d "$photos_d" ] || mkdir -p "$photos_d" || return $s_err
467 [ -d "$videos_d" ] || mkdir -p "$videos_d" || return $s_err
468 fi
469
470 set -e; source $1/bin/activate; set +e || return $s_err3
471
472 __apache_frontend_clean || { deactivate; return $s_err4; }
473 __apache_frontend_make || { deactivate; return $s_err5; }
474 __apache_frontend_install || { deactivate; return $s_err6; }
475 __apache_frontend_clean || { deactivate; return $s_err4; }
476
477 deactivate; true
478}
479
480function __apache_do_frontend
481{
482 local req="requirements.txt"
483 local file="$(realpath "$(find "$frontend_d" -name $req)")"
484
485 # Is this running in a container? If so this script assumes all global
486 # system wise dependencies to be provided at build time, only care about
487 # local packages taht can vary on the runtime.
488 if [ -z "$instance_d" ]; then
489 lets -l -i "virutal environment: container"
490 local venv_d=$pythonenv_con_d
491 else
492 local venv_d=$pythonenv_d
493 fi
494
495 __apache_do_venv $venv_d $file || return $s_err
496 lets -l -d "virtual environment: complete"
497
498 pushd $frontend_d &>/dev/null
Luigi Santivettic2994d12020-11-03 23:01:48 +0000499 __create_apache_frontend $venv_d 2>&1 | lets -l -x "make"
Luigi Santivettied568d42020-05-18 00:53:17 +0100500 case "${PIPESTATUS[0]}" in
501 $s_ok ) popd &>/dev/null; return $s_ok ;;
502 $s_err3 ) lets -l -e "failed to activate python" ;;
503 $s_err4 ) lets -l -e "failed to clean frontend" ;;
504 $s_err5 ) lets -l -e "failed to make frontend" ;;
505 $s_err6 ) lets -l -e "failed to install frontend" ;;
506 * ) lets -l -e "failed unknown error" ;;
507 esac; popd &>/dev/null; return $s_err
508}
509
510function __apache_backend_install
511{
512 local target
513
514 [ -d "$backend_d/application" ] && \
515 [ -d "$backend_d/system" ] && [ -f "$backend_d/index.php" ] || {
516 lets -l -w "not installing backend"
517 return $s_ok
518 }
519
520 for target in ${CODEIGN_INSTALL_TARGETS[@]}; do
521 case "$target" in
522 codeign-all)
Luigi Santivettie30b4c92020-10-25 21:22:19 +0000523 sudo -E cp -ra $backend_d/application $www_d || return $s_err2
524 sudo -E cp -ra $backend_d/system $www_d || return $s_err3
525 sudo -E cp -a $backend_d/index.php $html_d || return $s_err4 ;;
Luigi Santivettied568d42020-05-18 00:53:17 +0100526 esac
527 done
Luigi Santivettie30b4c92020-10-25 21:22:19 +0000528
529 # Default in Linux in www-data, apply to all installed contents here
530 local -ir uid=$(stat -c "%u" $html_d)
531 local -ir gid=$(stat -c "%g" $html_d)
532
533 sudo find "$www_d/application" -type f -exec /bin/bash -c \
534 "set -x; chmod 644 {} && chown $uid:$gid {}" \; || return $s_err
535 sudo find "$www_d/application" -type d -exec /bin/bash -c \
536 "set -x; chmod 755 {} && chown $uid:$gid {}" \; || return $s_err
537 sudo find "$www_d/system" -type f -exec /bin/bash -c \
538 "set -x; chmod 644 {} && chown $uid:$gid {}" \; || return $s_err
539 sudo find "$www_d/system" -type d -exec /bin/bash -c \
540 "set -x; chmod 755 {} && chown $uid:$gid {}" \; || return $s_err
541 sudo chmod 644 "$html_d/index.php"
542 sudo chown $uid:$gid "$html_d/index.php"
Luigi Santivettied568d42020-05-18 00:53:17 +0100543}
544
545function __create_apache_backend
546{
547 __apache_backend_install
548}
549
550function __apache_do_backend
551{
552 __create_apache_backend 2>&1 | lets -l -x "copy"
553 case "$?" in
554 $s_err2 ) lets -l -e "failed to install application code" ;;
555 $s_err3 ) lets -l -e "failed to install system code" ;;
556 $s_err4 ) lets -l -e "failed to install index.html" ;;
557 esac
558}