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