blob: 7a96d05ec8b2282c3652314cd95dfb54cd543816 [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{
Luigi Santivetti34c79f62020-11-03 22:58:35 +0000162 local src_d="${mod_staging_d}/${mod_authnz_external_mirror}"
163 local dst_d="${mod_authnz_external_d}"
164 if [ -d "$src_d" ] && [ -d "$dst_d" ]; then
165 rm -rf ${dst_d}/* || return $s_err
166 cp -ar ${src_d}/* ${dst_d}
Luigi Santivetti11526b82020-10-28 23:58:03 +0000167 else
168 lets -l -w "nothing to do for apache external modules"
169 return $s_ok
170 fi
Luigi Santivettied568d42020-05-18 00:53:17 +0100171}
172
173function __apache_do_dummy_cert_helper
174{
175 local -r digital_sign="digitalSignature\nextendedKeyUsage=serverAuth"
176 local -r dn_dns="DNS:${host_name}\nkeyUsage=$digital_sign"
177 local -r dn_lhs="[dn]\nCN=${host_name}\n[req]\ndistinguished_name"
178 local -r dn_rhs="dn\n[EXT]\nsubjectAltName"
179
180 openssl req -x509 -out ${host_name}.crt -keyout ${host_name}.key \
181 -newkey rsa:2048 -nodes -sha256 \
182 -subj "/CN=${host_name}" -extensions EXT \
183 -config <(printf "$dn_lhs = $dn_rhs=$dn_dns") 2>&1 | lets -l -x "openssl"
184
185 return ${PIPESTATUS[0]}
186}
187
188function __apache_do_dummy_cert
189{
190 if [ $apache_has_dummy_certs -eq 1 ]; then
191 if [ ! -d $letsencrypt_d ]; then
192 mkdir -p $letsencrypt_d && \
193 pushd $letsencrypt_d >/dev/null || return $s_err
194 __apache_do_dummy_cert_helper && \
195 echo "# Dummy $host_name conf" > $host_name.conf && \
196 popd >/dev/null || {
197 lets -l -e "failed to make dummy certificates"
198 popd >/dev/null; return $s_err
199 }
200 fi
201 fi
202
203 lets -l -i "using $module SSL certificates from: $letsencrypt_d"
204 return $s_ok
205}
206
207function __apache_do_links
208{
209 __apache_do_dummy_cert || return $s_err
210
211 # NOTE: container path must match localhost path
212 #
213 # i.e.
214 # localhost:/etc container:/etc OK
215 # localhost:/home/luigi container:/home/??? NOT OK
216 #
217 ln -sf "${letsencrypt_d}" "${certificates_l}"
218}
219
220function __apache_do_sites
221{
222 if [ ! -d "$enabled_d" ]; then
223 lets -l -e "invalid: $enabled_d"
224 return 1
225 fi
226
227 # This list controls what site to enable
228 local -a sites=( $vhost_http_f $vhost_https_f $vhost_gerrit_f )
229
230 pushd $enabled_d &>/dev/null
231
232 local file
233 for file in ${sites[@]}; do
234 local link_name="$(basename "$file")"
235 file="../sites-available/$link_name"
236
237 if [ -f "$file" ]; then
238 ln -sf $file $link_name
239 else
240 lets -l -e "failed link $file"
241 popd &>/dev/null
242 return 1
243 fi
244 done
245
246 popd &>/dev/null
247}
248
249# @1 : absolute path to the target directory
250# @2 : (optional) requirements txt file
251function __apache_do_venv
252{
253 local -ar deps=( python3-dev python3-pip python3-venv python3-wheel )
254
255 if [ -z "$1" ] || [ ! -f "$2" ]; then
256 lets -l -e "virtual environment: invalid params"
257 return $s_err
258 fi
259
260 if [ ! -e "$1/bin/activate" ]; then
261 local install="python3 -m pip install --no-cache-dir --upgrade"
262 local -a global_packages=( "virtualenv" "pip" )
263 local -a local_packages=(
264 "wheel"
265 "setuptools==$setuptools_version"
266 "pelican==$pelican_version"
267 "markdown==$markdown_version"
268 )
269
270 util_install_dependency ${deps[@]} || {
271 lets -l -e "install python dependencies"
272 return $s_err
273 }
274
275 mkdir -p "$1" || {
276 lets -l -e "virtual envirnment: mkdir failed"
277 return $s_err
278 }
279
280 $install --user ${global_packages[@]} 2>&1 | lets -l -x "pip3"
281
282 python3 -m venv "$1" 2>&1 | lets -l -x "python"
283 [ "${PIPESTATUS[0]}" -eq 0 ] || {
284 lets -l -e "virtual environment: create failed"
285 return $s_err
286 }
287
288 source "$1/bin/activate" || return $s_err
289 (
290 $install ${local_packages[@]} 2>&1 | lets -l -x "pip3"
291 [ "${PIPESTATUS[0]}" -eq 0 ] || {
292 deactivate
293 return $s_err2
294 }
295
296 while read -r pkg; do
297 $install $pkg 2>&1 | lets -l -x "pip3"
298 [ "${PIPESTATUS[0]}" -eq 0 ] || {
299 deactivate
300 return $s_err3
301 }
302 done < "$2"
303
304 deactivate
305 )
306 case "$?" in
307 $s_err2) lets -l -e "install local packages"; return $s_err ;;
308 $s_err3) lets -l -e "install $(basename $2)"; return $s_err ;;
309 esac
310 else
311 lets -l -w "virtual environment: exists already"
312 fi
313
314 return $s_ok
315}
316
317function __apache_frontend_clean
318{
319 local target
320
321 for target in ${PELICAN_CLEAN_TARGETS[@]}; do
322 case "$target" in
323 pelican-html ) make clean-html || return $s_err ;;
324 pelican-theme ) make clean-theme || return $s_err ;;
325 pelican-photos ) make clean-photos || return $s_err ;;
326 pelican-videos ) make clean-videos || return $s_err ;;
327 pelican-distclean ) make distclean || return $s_err ;;
328 pelican-all ) make clean || return $s_err ;;
329 esac
330 done
331}
332
333function __apache_frontend_make
334{
335 local target
336
337 for target in ${PELICAN_MAKE_TARGETS[@]}; do
338 case "$target" in
339 pelican-html)
340 PELICAN_PHOTO_LIBRARY=$photos_d \
341 PELICAN_PHOTO_EXCLUDEALL="1" \
342 PELICAN_VIDEO_LIBRARY=$videos_d \
343 PELICAN_VIDEO_EXCLUDEALL="1" \
344 PELICAN_SITEURL=$host_name \
345 PELICAN_CONTENT=$content_d \
346 make html || return $s_err ;;
347 pelican-all)
348 PELICAN_PHOTO_LIBRARY=$photos_d \
349 PELICAN_PHOTO_EXCLUDE=${EXCLUDED_PHOTOS[@]} \
350 PELICAN_PHOTO_SKIPTAG=$skiptag \
351 PELICAN_VIDEO_LIBRARY=$videos_d \
352 PELICAN_VIDEO_EXCLUDE=${EXCLUDED_VIDEOS[@]} \
353 PELICAN_SITEURL=$host_name \
354 PELICAN_VIDEO_SKIPTAG=$skiptag \
355 PELICAN_CONTENT=$content_d \
356 PELICAN="pelican" \
357 make html || return $s_err ;;
358 esac
359 done
360}
361
362
363# FIXME: improve integration with skiptag
364function __finalise_install
365{
366 [ ! -f "$1/$clean_skiptag_sh" ] || {
Luigi Santivettie30b4c92020-10-25 21:22:19 +0000367 pushd "$1" &>/dev/null || return $s_err
368 sudo -E /bin/bash "$1/$clean_skiptag_sh"
369 popd &>/dev/null
Luigi Santivettied568d42020-05-18 00:53:17 +0100370 }
371}
372
373function __apache_frontend_install
374{
375 local target
376
377 [ -d "$views_d" ] && [ -d "$html_d" ] || {
378 lets -l -w "not installing frontend"
379 return $s_ok
380 }
381
382 for target in ${PELICAN_INSTALL_TARGETS[@]}; do
383 case "$target" in
384 pelican-html)
385 INSTALLDIR=$views_d \
Luigi Santivettie30b4c92020-10-25 21:22:19 +0000386 sudo -E make install-html || return $s_err ;;
Luigi Santivettied568d42020-05-18 00:53:17 +0100387 pelican-theme)
388 INSTALLDIR_THEME=$html_d \
Luigi Santivettie30b4c92020-10-25 21:22:19 +0000389 sudo -E make install-theme || return $s_err ;;
Luigi Santivettied568d42020-05-18 00:53:17 +0100390 pelican-photos)
391 INSTALLDIR_PHOTOS=$photos_d \
Luigi Santivettie30b4c92020-10-25 21:22:19 +0000392 sudo -E make install-photos || return $s_err ;;
Luigi Santivettied568d42020-05-18 00:53:17 +0100393 pelican-videos)
394 INSTALLDIR_VIDEOS=$videos_d \
Luigi Santivettie30b4c92020-10-25 21:22:19 +0000395 sudo -E make install-videos || return $s_err ;;
Luigi Santivettied568d42020-05-18 00:53:17 +0100396 pelican-all)
397 INSTALLDIR=$views_d \
398 INSTALLDIR_THEME=$html_d \
399 INSTALLDIR_PHOTOS=$photos_d \
400 INSTALLDIR_VIDEOS=$videos_d \
Luigi Santivettie30b4c92020-10-25 21:22:19 +0000401 sudo -E make install || return $s_err
Luigi Santivettied568d42020-05-18 00:53:17 +0100402
403 # FIXME: improve integration with skiptag
404 __finalise_install $photos_d || \
405 lets -l -e "Failed to finalise photos"
406 __finalise_install $videos_d || \
407 lets -l -e "Failed to finalise videos" ;;
408 esac
409 done
Luigi Santivettie30b4c92020-10-25 21:22:19 +0000410
411 # Default in Linux in www-data, apply to all installed contents here
412 local -ir uid=$(stat -c "%u" $html_d)
413 local -ir gid=$(stat -c "%g" $html_d)
414
415 sudo find $html_d -type f -exec /bin/bash -c \
416 "set -x; chmod 644 {} && chown $uid:$gid {}" \; || return $s_err
417 sudo find $views_d -type f -exec /bin/bash -c \
418 "set -x; chmod 644 {} && chown $uid:$gid {}" \; || return $s_err
419 sudo find $html_d -type d -exec /bin/bash -c \
420 "set -x; chmod 755 {} && chown $uid:$gid {}" \; || return $s_err
421 sudo find $views_d -type d -exec /bin/bash -c \
422 "set -x; chmod 755 {} && chown $uid:$gid {}" \; || return $s_err
423
424 if [ -d "$photos_d" ]; then
425 sudo find $photos_d -type f -exec /bin/bash -c \
426 "set -x; chmod 644 {} && chown $uid:$gid {}" \; || return $s_err
427 sudo find $photos_d -type d -exec /bin/bash -c \
428 "set -x; chmod 755 {} && chown $uid:$gid {}" \; || return $s_err
429 fi
430
431 if [ -d "$videos_d" ]; then
432 sudo find $videos_d -type f -exec /bin/bash -c \
433 "set -x; chmod 644 {} && chown $uid:$gid {}" \; || return $s_err
434 sudo find $videos_d -type d -exec /bin/bash -c \
435 "set -x; chmod 755 {} && chown $uid:$gid {}" \; || return $s_err
436 fi
Luigi Santivettied568d42020-05-18 00:53:17 +0100437}
438
439function __create_apache_frontend
440{
441 if [ $apache_has_media -eq 1 ]; then
442 [ -d "$photos_d" ] || mkdir -p "$photos_d" || return $s_err
443 [ -d "$videos_d" ] || mkdir -p "$videos_d" || return $s_err
444 fi
445
446 set -e; source $1/bin/activate; set +e || return $s_err3
447
448 __apache_frontend_clean || { deactivate; return $s_err4; }
449 __apache_frontend_make || { deactivate; return $s_err5; }
450 __apache_frontend_install || { deactivate; return $s_err6; }
451 __apache_frontend_clean || { deactivate; return $s_err4; }
452
453 deactivate; true
454}
455
456function __apache_do_frontend
457{
458 local req="requirements.txt"
459 local file="$(realpath "$(find "$frontend_d" -name $req)")"
460
461 # Is this running in a container? If so this script assumes all global
462 # system wise dependencies to be provided at build time, only care about
463 # local packages taht can vary on the runtime.
464 if [ -z "$instance_d" ]; then
465 lets -l -i "virutal environment: container"
466 local venv_d=$pythonenv_con_d
467 else
468 local venv_d=$pythonenv_d
469 fi
470
471 __apache_do_venv $venv_d $file || return $s_err
472 lets -l -d "virtual environment: complete"
473
474 pushd $frontend_d &>/dev/null
475 __create_apache_frontend $pythonenv_d 2>&1 | lets -l -x "make"
476 case "${PIPESTATUS[0]}" in
477 $s_ok ) popd &>/dev/null; return $s_ok ;;
478 $s_err3 ) lets -l -e "failed to activate python" ;;
479 $s_err4 ) lets -l -e "failed to clean frontend" ;;
480 $s_err5 ) lets -l -e "failed to make frontend" ;;
481 $s_err6 ) lets -l -e "failed to install frontend" ;;
482 * ) lets -l -e "failed unknown error" ;;
483 esac; popd &>/dev/null; return $s_err
484}
485
486function __apache_backend_install
487{
488 local target
489
490 [ -d "$backend_d/application" ] && \
491 [ -d "$backend_d/system" ] && [ -f "$backend_d/index.php" ] || {
492 lets -l -w "not installing backend"
493 return $s_ok
494 }
495
496 for target in ${CODEIGN_INSTALL_TARGETS[@]}; do
497 case "$target" in
498 codeign-all)
Luigi Santivettie30b4c92020-10-25 21:22:19 +0000499 sudo -E cp -ra $backend_d/application $www_d || return $s_err2
500 sudo -E cp -ra $backend_d/system $www_d || return $s_err3
501 sudo -E cp -a $backend_d/index.php $html_d || return $s_err4 ;;
Luigi Santivettied568d42020-05-18 00:53:17 +0100502 esac
503 done
Luigi Santivettie30b4c92020-10-25 21:22:19 +0000504
505 # Default in Linux in www-data, apply to all installed contents here
506 local -ir uid=$(stat -c "%u" $html_d)
507 local -ir gid=$(stat -c "%g" $html_d)
508
509 sudo find "$www_d/application" -type f -exec /bin/bash -c \
510 "set -x; chmod 644 {} && chown $uid:$gid {}" \; || return $s_err
511 sudo find "$www_d/application" -type d -exec /bin/bash -c \
512 "set -x; chmod 755 {} && chown $uid:$gid {}" \; || return $s_err
513 sudo find "$www_d/system" -type f -exec /bin/bash -c \
514 "set -x; chmod 644 {} && chown $uid:$gid {}" \; || return $s_err
515 sudo find "$www_d/system" -type d -exec /bin/bash -c \
516 "set -x; chmod 755 {} && chown $uid:$gid {}" \; || return $s_err
517 sudo chmod 644 "$html_d/index.php"
518 sudo chown $uid:$gid "$html_d/index.php"
Luigi Santivettied568d42020-05-18 00:53:17 +0100519}
520
521function __create_apache_backend
522{
523 __apache_backend_install
524}
525
526function __apache_do_backend
527{
528 __create_apache_backend 2>&1 | lets -l -x "copy"
529 case "$?" in
530 $s_err2 ) lets -l -e "failed to install application code" ;;
531 $s_err3 ) lets -l -e "failed to install system code" ;;
532 $s_err4 ) lets -l -e "failed to install index.html" ;;
533 esac
534}