on acefa65 help sign in
searx/utils/lib.sh
1845 lines 49 kB history raw
1#!/usr/bin/env bash
2# -*- coding: utf-8; mode: sh indent-tabs-mode: nil -*-
3# SPDX-License-Identifier: AGPL-3.0-or-later
4# shellcheck disable=SC2059,SC1117
6# ubuntu, debian, arch, fedora, centos ...
7DIST_ID=$(source /etc/os-release; echo "$ID");
8# shellcheck disable=SC2034
9DIST_VERS=$(source /etc/os-release; echo "$VERSION_ID");
11ADMIN_NAME="${ADMIN_NAME:-$(git config user.name)}"
12ADMIN_NAME="${ADMIN_NAME:-$USER}"
14ADMIN_EMAIL="${ADMIN_EMAIL:-$(git config user.email)}"
15ADMIN_EMAIL="${ADMIN_EMAIL:-$USER@$(hostname)}"
17if [[ -z "${REPO_ROOT}" ]]; then
18 REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")
19 while [ -h "${REPO_ROOT}" ] ; do
20 REPO_ROOT=$(readlink "${REPO_ROOT}")
21 done
22 REPO_ROOT=$(cd "${REPO_ROOT}/.." && pwd -P )
23fi
25if [[ -z ${TEMPLATES} ]]; then
26 TEMPLATES="${REPO_ROOT}/utils/templates"
27fi
29if [[ -z "$CACHE" ]]; then
30 CACHE="${REPO_ROOT}/cache"
31fi
33if [[ -z ${DIFF_CMD} ]]; then
34 DIFF_CMD="diff -u"
35 if command -v colordiff >/dev/null; then
36 DIFF_CMD="colordiff -u"
37 fi
38fi
40DOT_CONFIG="${DOT_CONFIG:-${REPO_ROOT}/.config.sh}"
42source_dot_config() {
43 if [[ ! -e "${DOT_CONFIG}" ]]; then
44 err_msg "configuration does not exists at: ${DOT_CONFIG}"
45 return 42
46 fi
47 # shellcheck disable=SC1090
48 source "${DOT_CONFIG}"
51sudo_or_exit() {
52 # usage: sudo_or_exit
54 if [ ! "$(id -u)" -eq 0 ]; then
55 err_msg "this command requires root (sudo) privilege!" >&2
56 exit 42
57 fi
60required_commands() {
62 # usage: required_commands [cmd1 ...]
64 local exit_val=0
65 while [ -n "$1" ]; do
67 if ! command -v "$1" &>/dev/null; then
68 err_msg "missing command $1"
69 exit_val=42
70 fi
71 shift
72 done
73 return $exit_val
76# colors
77# ------
79# shellcheck disable=SC2034
80set_terminal_colors() {
81 _colors=8
82 _creset='\e[0m' # reset all attributes
84 _Black='\e[0;30m'
85 _White='\e[1;37m'
86 _Red='\e[0;31m'
87 _Green='\e[0;32m'
88 _Yellow='\e[0;33m'
89 _Blue='\e[0;94m'
90 _Violet='\e[0;35m'
91 _Cyan='\e[0;36m'
93 _BBlack='\e[1;30m'
94 _BWhite='\e[1;37m'
95 _BRed='\e[1;31m'
96 _BGreen='\e[1;32m'
97 _BYellow='\e[1;33m'
98 _BBlue='\e[1;94m'
99 _BPurple='\e[1;35m'
100 _BCyan='\e[1;36m'
103if [ ! -p /dev/stdout ] && [ ! "$TERM" = 'dumb' ] && [ ! "$TERM" = 'unknown' ]; then
104 set_terminal_colors
105fi
107# reST
108# ----
110if command -v fmt >/dev/null; then
111 export FMT="fmt -u"
112else
113 export FMT="cat"
114fi
116rst_title() {
117 # usage: rst_title <header-text> [part|chapter|section]
119 case ${2-chapter} in
120 part) printf "\n${_BGreen}${1//?/=}${_creset}\n${_BCyan}${1}${_creset}\n${_BGreen}${1//?/=}${_creset}\n";;
121 chapter) printf "\n${_BCyan}${1}${_creset}\n${_BGreen}${1//?/=}${_creset}\n";;
122 section) printf "\n${_BCyan}${1}${_creset}\n${_BGreen}${1//?/-}${_creset}\n";;
123 *)
124 err_msg "invalid argument '${2}' in line $(caller)"
125 return 42
126 ;;
127 esac
130rst_para() {
131 # usage: RST_INDENT=1 rst_para "lorem ipsum ..."
132 local prefix=''
133 if [[ -n $RST_INDENT ]] && [[ $RST_INDENT -gt 0 ]]; then
134 prefix="$(for i in $(seq 1 "$RST_INDENT"); do printf " "; done)"
135 echo -en "\n$*\n" | $FMT | prefix_stdout "$prefix"
136 else
137 echo -en "\n$*\n" | $FMT
138 fi
141die() {
142 echo -e "${_BRed}ERROR:${_creset} ${BASH_SOURCE[1]}: line ${BASH_LINENO[0]}: ${2-died ${1-1}}" >&2;
143 exit "${1-1}"
146die_caller() {
147 echo -e "${_BRed}ERROR:${_creset} ${BASH_SOURCE[2]}: line ${BASH_LINENO[1]}: ${FUNCNAME[1]}(): ${2-died ${1-1}}" >&2;
148 exit "${1-1}"
151err_msg() { echo -e "${_BRed}ERROR:${_creset} $*" >&2; }
152warn_msg() { echo -e "${_BBlue}WARN:${_creset} $*" >&2; }
153info_msg() { echo -e "${_BYellow}INFO:${_creset} $*" >&2; }
155build_msg() {
156 local tag="$1 "
157 shift
158 echo -e "${_Blue}${tag:0:10}${_creset}$*"
161dump_return() {
163 # Use this as last command in your function to prompt an ERROR message if
164 # the exit code is not zero.
166 local err=$1
167 [ "$err" -ne "0" ] && err_msg "${FUNCNAME[1]} exit with error ($err)"
168 return "$err"
171clean_stdin() {
172 if [[ $(uname -s) != 'Darwin' ]]; then
173 while read -r -n1 -t 0.1; do : ; done
174 fi
177wait_key(){
178 # usage: wait_key [<timeout in sec>]
180 clean_stdin
181 local _t=$1
182 local msg="${MSG}"
183 [[ -z "$msg" ]] && msg="${_Green}** press any [${_BCyan}KEY${_Green}] to continue **${_creset}"
185 [[ -n $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
186 [[ -n $_t ]] && _t="-t $_t"
187 printf "$msg"
188 # shellcheck disable=SC2086
189 read -r -s -n1 $_t
190 echo
191 clean_stdin
194ask_yn() {
195 # usage: ask_yn <prompt-text> [Ny|Yn] [<timeout in sec>]
197 local EXIT_YES=0 # exit status 0 --> successful
198 local EXIT_NO=1 # exit status 1 --> error code
200 local _t=$3
201 [[ -n $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
202 [[ -n $_t ]] && _t="-t $_t"
203 case "${FORCE_SELECTION:-${2}}" in
204 Y) return ${EXIT_YES} ;;
205 N) return ${EXIT_NO} ;;
206 Yn)
207 local exit_val=${EXIT_YES}
208 local choice="[${_BGreen}YES${_creset}/no]"
209 local default="Yes"
210 ;;
211 *)
212 local exit_val=${EXIT_NO}
213 local choice="[${_BGreen}NO${_creset}/yes]"
214 local default="No"
215 ;;
216 esac
217 echo
218 while true; do
219 clean_stdin
220 printf "$1 ${choice} "
221 # shellcheck disable=SC2086
222 read -r -n1 $_t
223 if [[ -z $REPLY ]]; then
224 printf "$default\n"; break
225 elif [[ $REPLY =~ ^[Yy]$ ]]; then
226 exit_val=${EXIT_YES}
227 printf "\n"
228 break
229 elif [[ $REPLY =~ ^[Nn]$ ]]; then
230 exit_val=${EXIT_NO}
231 printf "\n"
232 break
233 fi
234 _t=""
235 err_msg "invalid choice"
236 done
237 clean_stdin
238 return $exit_val
241tee_stderr () {
243 # usage::
244 # tee_stderr 1 <<EOF | python -i
245 # print("hello")
246 # EOF
247 # ...
248 # >>> print("hello")
249 # hello
251 local _t="0";
252 if [[ -n $1 ]] ; then _t="$1"; fi
254 (while read -r line; do
255 # shellcheck disable=SC2086
256 sleep $_t
257 echo -e "$line" >&2
258 echo "$line"
259 done)
262prefix_stdout () {
263 # usage: <cmd> | prefix_stdout [prefix]
265 local prefix="${_BYellow}-->|${_creset}"
267 if [[ -n $1 ]] ; then prefix="$1"; fi
269 # shellcheck disable=SC2162
270 (while IFS= read line; do
271 echo -e "${prefix}$line"
272 done)
275append_line() {
277 # usage: append_line <line> <file>
278 #
279 # Append line if not exists, create file if not exists. E.g::
280 #
281 # append_line 'source ~/.foo' ~/bashrc
283 local LINE=$1
284 local FILE=$2
285 grep -qFs -- "$LINE" "$FILE" || echo "$LINE" >> "$FILE"
288cache_download() {
290 # usage: cache_download <url> <local-filename>
292 local exit_value=0
294 if [[ -n ${SUDO_USER} ]]; then
295 sudo -u "${SUDO_USER}" mkdir -p "${CACHE}"
296 else
297 mkdir -p "${CACHE}"
298 fi
300 if [[ -f "${CACHE}/$2" ]] ; then
301 info_msg "already cached: $1"
302 info_msg " --> ${CACHE}/$2"
303 fi
305 if [[ ! -f "${CACHE}/$2" ]]; then
306 info_msg "caching: $1"
307 info_msg " --> ${CACHE}/$2"
308 if [[ -n ${SUDO_USER} ]]; then
309 sudo -u "${SUDO_USER}" wget --progress=bar -O "${CACHE}/$2" "$1" ; exit_value=$?
310 else
311 wget --progress=bar -O "${CACHE}/$2" "$1" ; exit_value=$?
312 fi
313 if [[ ! $exit_value = 0 ]]; then
314 err_msg "failed to download: $1"
315 fi
316 fi
319backup_file() {
321 # usage: backup_file /path/to/file.foo
323 local stamp
324 stamp=$(date +"_%Y%m%d_%H%M%S")
325 info_msg "create backup: ${1}${stamp}"
326 cp -a "${1}" "${1}${stamp}"
329choose_one() {
331 # usage:
332 #
333 # DEFAULT_SELECT= 2 \
334 # choose_one <name> "your selection?" "Coffee" "Coffee with milk"
336 local default=${DEFAULT_SELECT-1}
337 local REPLY
338 local env_name=$1 && shift
339 local choice=$1;
340 local max="${#@}"
341 local _t
342 [[ -n $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
343 [[ -n $_t ]] && _t="-t $_t"
345 list=("$@")
346 echo -e "${_BGreen}Menu::${_creset}"
347 for ((i=1; i<= $((max -1)); i++)); do
348 if [[ "$i" == "$default" ]]; then
349 echo -e " ${_BGreen}$i.${_creset}) ${list[$i]} [default]"
350 else
351 echo -e " $i.) ${list[$i]}"
352 fi
353 done
354 while true; do
355 clean_stdin
356 printf "$1 [${_BGreen}$default${_creset}] "
358 if (( 10 > max )); then
359 # shellcheck disable=SC2086
360 read -r -n1 $_t
361 else
362 # shellcheck disable=SC2086,SC2229
363 read -r $_t
364 fi
365 # selection fits
366 [[ $REPLY =~ ^-?[0-9]+$ ]] && (( REPLY > 0 )) && (( REPLY < max )) && break
368 # take default
369 [[ -z $REPLY ]] && REPLY=$default && break
371 _t=""
372 err_msg "invalid choice"
373 done
374 eval "$env_name"='${list[${REPLY}]}'
375 echo
376 clean_stdin
379install_template() {
381 # usage:
382 #
383 # install_template [--no-eval] [--variant=<name>] \
384 # {file} [{owner} [{group} [{chmod}]]]
385 #
386 # E.g. the origin of variant 'raw' of /etc/updatedb.conf is::
387 #
388 # ${TEMPLATES}/etc/updatedb.conf:raw
389 #
390 # To install variant 'raw' of /etc/updatedb.conf without evaluated
391 # replacements you can use::
392 #
393 # install_template --variant=raw --no-eval \
394 # /etc/updatedb.conf root root 644
396 local _reply=""
397 local do_eval=1
398 local variant=""
399 local pos_args=("$0")
401 for i in "$@"; do
402 case $i in
403 --no-eval) do_eval=0; shift ;;
404 --variant=*) variant=":${i#*=}"; shift ;;
405 *) pos_args+=("$i") ;;
406 esac
407 done
409 local dst="${pos_args[1]}"
410 local template_origin="${TEMPLATES}${dst}${variant}"
411 local template_file="${TEMPLATES}${dst}"
413 local owner="${pos_args[2]-$(id -un)}"
414 local group="${pos_args[3]-$(id -gn)}"
415 local chmod="${pos_args[4]-644}"
417 info_msg "install (eval=$do_eval): ${dst}"
418 [[ -n $variant ]] && info_msg "variant --> ${variant}"
420 if [[ ! -f "${template_origin}" ]] ; then
421 err_msg "${template_origin} does not exists"
422 err_msg "... can't install $dst"
423 wait_key 30
424 return 42
425 fi
427 if [[ "$do_eval" == "1" ]]; then
428 template_file="${CACHE}${dst}${variant}"
429 info_msg "BUILD template ${template_file}"
430 if [[ -n ${SUDO_USER} ]]; then
431 sudo -u "${SUDO_USER}" mkdir -p "$(dirname "${template_file}")"
432 else
433 mkdir -p "$(dirname "${template_file}")"
434 fi
435 # shellcheck disable=SC2086
436 eval "echo \"$(cat ${template_origin})\"" > "${template_file}"
437 if [[ -n ${SUDO_USER} ]]; then
438 chown "${SUDO_USER}:${SUDO_USER}" "${template_file}"
439 fi
440 else
441 template_file=$template_origin
442 fi
444 mkdir -p "$(dirname "${dst}")"
446 if [[ ! -f "${dst}" ]]; then
447 info_msg "install: ${template_file}"
448 sudo -H install -v -o "${owner}" -g "${group}" -m "${chmod}" \
449 "${template_file}" "${dst}" | prefix_stdout
450 return $?
451 fi
453 if [[ -f "${dst}" ]] && cmp --silent "${template_file}" "${dst}" ; then
454 info_msg "file ${dst} allready installed"
455 return 0
456 fi
458 info_msg "diffrent file ${dst} allready exists on this host"
460 while true; do
461 choose_one _reply "choose next step with file $dst" \
462 "replace file" \
463 "leave file unchanged" \
464 "interactiv shell" \
465 "diff files"
467 case $_reply in
468 "replace file")
469 info_msg "install: ${template_file}"
470 sudo -H install -v -o "${owner}" -g "${group}" -m "${chmod}" \
471 "${template_file}" "${dst}" | prefix_stdout
472 break
473 ;;
474 "leave file unchanged")
475 break
476 ;;
477 "interactiv shell")
478 echo -e "// edit ${_Red}${dst}${_creset} to your needs"
479 echo -e "// exit with [${_BCyan}CTRL-D${_creset}]"
480 sudo -H -u "${owner}" -i
481 $DIFF_CMD "${dst}" "${template_file}"
482 echo
483 echo -e "// ${_BBlack}did you edit file ...${_creset}"
484 echo -en "// ${_Red}${dst}${_creset}"
485 if ask_yn "//${_BBlack}... to your needs?${_creset}"; then
486 break
487 fi
488 ;;
489 "diff files")
490 $DIFF_CMD "${dst}" "${template_file}" | prefix_stdout
491 esac
492 done
496service_is_available() {
498 # usage: service_is_available <URL>
500 [[ -z $1 ]] && die_caller 42 "missing argument <URL>"
501 local URL="$1"
502 http_code=$(curl -H 'Cache-Control: no-cache' \
503 --silent -o /dev/null --head --write-out '%{http_code}' --insecure \
504 "${URL}")
505 exit_val=$?
506 if [[ $exit_val = 0 ]]; then
507 info_msg "got $http_code from ${URL}"
508 fi
509 case "$http_code" in
510 404|410|423) exit_val=$http_code;;
511 esac
512 return "$exit_val"
515# python
516# ------
518PY="${PY:=3}"
519PYTHON="${PYTHON:=python$PY}"
520PY_ENV="${PY_ENV:=local/py${PY}}"
521PY_ENV_BIN="${PY_ENV}/bin"
522PY_ENV_REQ="${PY_ENV_REQ:=${REPO_ROOT}/requirements*.txt}"
524# List of python packages (folders) or modules (files) installed by command:
525# pyenv.install
526PYOBJECTS="${PYOBJECTS:=.}"
528# folder where the python distribution takes place
529PYDIST="${PYDIST:=dist}"
531# folder where the intermediate build files take place
532PYBUILD="${PYBUILD:=build/py${PY}}"
534# https://www.python.org/dev/peps/pep-0508/#extras
535#PY_SETUP_EXTRAS='[develop,test]'
536PY_SETUP_EXTRAS="${PY_SETUP_EXTRAS:=[develop,test]}"
538PIP_BOILERPLATE=( pip wheel setuptools )
540# shellcheck disable=SC2120
541pyenv() {
543 # usage: pyenv [vtenv_opts ...]
544 #
545 # vtenv_opts: see 'pip install --help'
546 #
547 # Builds virtualenv with 'requirements*.txt' (PY_ENV_REQ) installed. The
548 # virtualenv will be reused by validating sha256sum of the requirement
549 # files.
551 required_commands \
552 sha256sum "${PYTHON}" \
553 || exit
555 local pip_req=()
557 if ! pyenv.OK > /dev/null; then
558 rm -f "${PY_ENV}/${PY_ENV_REQ}.sha256"
559 pyenv.drop > /dev/null
560 build_msg PYENV "[virtualenv] installing ${PY_ENV_REQ} into ${PY_ENV}"
562 "${PYTHON}" -m venv "$@" "${PY_ENV}" || exit
563 "${PY_ENV_BIN}/python" -m pip install -U "${PIP_BOILERPLATE[@]}" || exit
565 for i in ${PY_ENV_REQ}; do
566 pip_req=( "${pip_req[@]}" "-r" "$i" )
567 done
569 (
570 [ "$VERBOSE" = "1" ] && set -x
571 # shellcheck disable=SC2086
572 "${PY_ENV_BIN}/python" -m pip install "${pip_req[@]}" \
573 && sha256sum ${PY_ENV_REQ} > "${PY_ENV}/requirements.sha256"
574 )
575 fi
576 pyenv.OK
579_pyenv_OK=''
580pyenv.OK() {
582 # probes if pyenv exists and runs the script from pyenv.check
584 [ "$_pyenv_OK" == "OK" ] && return 0
586 if [ ! -f "${PY_ENV_BIN}/python" ]; then
587 build_msg PYENV "[virtualenv] missing ${PY_ENV_BIN}/python"
588 return 1
589 fi
591 if [ ! -f "${PY_ENV}/requirements.sha256" ] \
592 || ! sha256sum --check --status <"${PY_ENV}/requirements.sha256" 2>/dev/null; then
593 build_msg PYENV "[virtualenv] requirements.sha256 failed"
594 sed 's/^/ [virtualenv] - /' <"${PY_ENV}/requirements.sha256"
595 return 1
596 fi
598 pyenv.check \
599 | "${PY_ENV_BIN}/python" 2>&1 \
600 | prefix_stdout "${_Blue}PYENV ${_creset}[check] "
602 local err=${PIPESTATUS[1]}
603 if [ "$err" -ne "0" ]; then
604 build_msg PYENV "[check] python test failed"
605 return "$err"
606 fi
608 build_msg PYENV "OK"
609 _pyenv_OK="OK"
610 return 0
613pyenv.drop() {
615 build_msg PYENV "[virtualenv] drop ${PY_ENV}"
616 rm -rf "${PY_ENV}"
617 _pyenv_OK=''
621pyenv.check() {
623 # Prompts a python script with additional checks. Used by pyenv.OK to check
624 # if virtualenv is ready to install python objects. This function should be
625 # overwritten by the application script.
627 local imp=""
629 for i in "${PIP_BOILERPLATE[@]}"; do
630 imp="$imp, $i"
631 done
633 cat <<EOF
634import ${imp#,*}
636EOF
639pyenv.install() {
641 if ! pyenv.OK; then
642 py.clean > /dev/null
643 fi
644 if ! pyenv.install.OK > /dev/null; then
645 build_msg PYENV "[install] ${PYOBJECTS}"
646 if ! pyenv.OK >/dev/null; then
647 pyenv
648 fi
649 for i in ${PYOBJECTS}; do
650 build_msg PYENV "[install] pip install -e '$i${PY_SETUP_EXTRAS}'"
651 "${PY_ENV_BIN}/python" -m pip install -e "$i${PY_SETUP_EXTRAS}"
652 done
653 fi
654 pyenv.install.OK
657_pyenv_install_OK=''
658pyenv.install.OK() {
660 [ "$_pyenv_install_OK" == "OK" ] && return 0
662 local imp=""
663 local err=""
665 if [ "." = "${PYOBJECTS}" ]; then
666 imp="import $(basename "$(pwd)")"
667 else
668 # shellcheck disable=SC2086
669 for i in ${PYOBJECTS}; do imp="$imp, $i"; done
670 imp="import ${imp#,*} "
671 fi
672 (
673 [ "$VERBOSE" = "1" ] && set -x
674 "${PY_ENV_BIN}/python" -c "import sys; sys.path.pop(0); $imp;" 2>/dev/null
675 )
677 err=$?
678 if [ "$err" -ne "0" ]; then
679 build_msg PYENV "[install] python installation test failed"
680 return "$err"
681 fi
683 build_msg PYENV "[install] OK"
684 _pyenv_install_OK="OK"
685 return 0
688pyenv.uninstall() {
690 build_msg PYENV "[uninstall] ${PYOBJECTS}"
692 if [ "." = "${PYOBJECTS}" ]; then
693 pyenv.cmd python setup.py develop --uninstall 2>&1 \
694 | prefix_stdout "${_Blue}PYENV ${_creset}[pyenv.uninstall] "
695 else
696 pyenv.cmd python -m pip uninstall --yes ${PYOBJECTS} 2>&1 \
697 | prefix_stdout "${_Blue}PYENV ${_creset}[pyenv.uninstall] "
698 fi
702pyenv.cmd() {
703 pyenv.install
704 ( set -e
705 # shellcheck source=/dev/null
706 source "${PY_ENV_BIN}/activate"
707 [ "$VERBOSE" = "1" ] && set -x
708 "$@"
709 )
712# Sphinx doc
713# ----------
715GH_PAGES="build/gh-pages"
716DOCS_DIST="${DOCS_DIST:=dist/docs}"
717DOCS_BUILD="${DOCS_BUILD:=build/docs}"
719docs.html() {
720 build_msg SPHINX "HTML ./docs --> file://$(readlink -e "$(pwd)/$DOCS_DIST")"
721 pyenv.install
722 docs.prebuild
723 # shellcheck disable=SC2086
724 PATH="${PY_ENV_BIN}:${PATH}" pyenv.cmd sphinx-build \
725 ${SPHINX_VERBOSE} ${SPHINXOPTS} \
726 -b html -c ./docs -d "${DOCS_BUILD}/.doctrees" ./docs "${DOCS_DIST}"
727 dump_return $?
730docs.live() {
731 build_msg SPHINX "autobuild ./docs --> file://$(readlink -e "$(pwd)/$DOCS_DIST")"
732 pyenv.install
733 docs.prebuild
734 # shellcheck disable=SC2086
735 PATH="${PY_ENV_BIN}:${PATH}" pyenv.cmd sphinx-autobuild \
736 ${SPHINX_VERBOSE} ${SPHINXOPTS} --open-browser --host 0.0.0.0 \
737 -b html -c ./docs -d "${DOCS_BUILD}/.doctrees" ./docs "${DOCS_DIST}"
738 dump_return $?
741docs.clean() {
742 build_msg CLEAN "docs -- ${DOCS_BUILD} ${DOCS_DIST}"
743 # shellcheck disable=SC2115
744 rm -rf "${GH_PAGES}" "${DOCS_BUILD}" "${DOCS_DIST}"
745 dump_return $?
748docs.prebuild() {
749 # Dummy function to run some actions before sphinx-doc build gets started.
750 # This finction needs to be overwritten by the application script.
751 true
752 dump_return $?
755# shellcheck disable=SC2155
756docs.gh-pages() {
758 # The commit history in the gh-pages branch makes no sense, the history only
759 # inflates the repository unnecessarily. Therefore a *new orphan* branch
760 # is created each time we deploy on the gh-pages branch.
762 docs.clean
763 docs.prebuild
764 docs.html
766 [ "$VERBOSE" = "1" ] && set -x
767 local head="$(git rev-parse HEAD)"
768 local branch="$(git name-rev --name-only HEAD)"
769 local remote="$(git config branch."${branch}".remote)"
770 local remote_url="$(git config remote."${remote}".url)"
772 # prepare the *orphan* gh-pages working tree
773 (
774 git worktree remove -f "${GH_PAGES}"
775 git branch -D gh-pages
776 ) &> /dev/null || true
777 git worktree add --no-checkout "${GH_PAGES}" origin/master
779 pushd "${GH_PAGES}" &> /dev/null
780 git checkout --orphan gh-pages
781 git rm -rfq .
782 popd &> /dev/null
784 cp -r "${DOCS_DIST}"/* "${GH_PAGES}"/
785 touch "${GH_PAGES}/.nojekyll"
786 cat > "${GH_PAGES}/404.html" <<EOF
787<html><head><META http-equiv='refresh' content='0;URL=index.html'></head></html>
788EOF
790 pushd "${GH_PAGES}" &> /dev/null
791 git add --all .
792 git commit -q -m "gh-pages build from: ${branch}@${head} (${remote_url})"
793 git push -f "${remote}" gh-pages
794 popd &> /dev/null
796 set +x
797 build_msg GH-PAGES "deployed"
800# golang
801# ------
803go_is_available() {
805 # usage: go_is_available $SERVICE_USER && echo "go is installed!"
807 sudo -i -u "${1}" which go &>/dev/null
810install_go() {
812 # usage: install_go "${GO_PKG_URL}" "${GO_TAR}" "${SERVICE_USER}"
814 local _svcpr=" ${_Yellow}|${3}|${_creset} "
816 rst_title "Install Go in user's HOME" section
818 rst_para "download and install go binary .."
819 cache_download "${1}" "${2}"
821 tee_stderr 0.1 <<EOF | sudo -i -u "${3}" | prefix_stdout "$_svcpr"
822echo \$PATH
823echo \$GOPATH
824mkdir -p \$HOME/local
825rm -rf \$HOME/local/go
826tar -C \$HOME/local -xzf ${CACHE}/${2}
827EOF
828 sudo -i -u "${3}" <<EOF | prefix_stdout
829! which go >/dev/null && echo "ERROR - Go Installation not found in PATH!?!"
830which go >/dev/null && go version && echo "congratulations -- Go installation OK :)"
831EOF
834# system accounts
835# ---------------
837service_account_is_available() {
839 # usage: service_account_is_available "$SERVICE_USER" && echo "OK"
841 sudo -i -u "$1" echo \$HOME &>/dev/null
844drop_service_account() {
846 # usage: drop_service_account "${SERVICE_USER}"
848 rst_title "Drop ${1} HOME" section
849 if ask_yn "Do you really want to drop ${1} home folder?"; then
850 userdel -r -f "${1}" 2>&1 | prefix_stdout
851 else
852 rst_para "Leave HOME folder $(du -sh "${1}") unchanged."
853 fi
856interactive_shell(){
858 # usage: interactive_shell "${SERVICE_USER}"
860 echo -e "// exit with [${_BCyan}CTRL-D${_creset}]"
861 sudo -H -u "${1}" -i
865# systemd
866# -------
868SYSTEMD_UNITS="${SYSTEMD_UNITS:-/lib/systemd/system}"
870systemd_install_service() {
872 # usage: systemd_install_service "${SERVICE_NAME}" "${SERVICE_SYSTEMD_UNIT}"
874 rst_title "Install System-D Unit ${1}" section
875 echo
876 install_template "${2}" root root 644
877 wait_key
878 systemd_activate_service "${1}"
881systemd_remove_service() {
883 # usage: systemd_remove_service "${SERVICE_NAME}" "${SERVICE_SYSTEMD_UNIT}"
885 if ! ask_yn "Do you really want to deinstall systemd unit ${1}?"; then
886 return 42
887 fi
888 systemd_deactivate_service "${1}"
889 rm "${2}" 2>&1 | prefix_stdout
892systemd_activate_service() {
894 # usage: systemd_activate_service "${SERVICE_NAME}"
896 rst_title "Activate ${1} (service)" section
897 echo
898 tee_stderr <<EOF | bash 2>&1
899systemctl enable ${1}.service
900systemctl restart ${1}.service
901EOF
902 tee_stderr <<EOF | bash 2>&1
903systemctl status --no-pager ${1}.service
904EOF
907systemd_deactivate_service() {
909 # usage: systemd_deactivate_service "${SERVICE_NAME}"
911 rst_title "De-Activate ${1} (service)" section
912 echo
913 tee_stderr <<EOF | bash 2>&1 | prefix_stdout
914systemctl stop ${1}.service
915systemctl disable ${1}.service
916EOF
919systemd_restart_service() {
921 # usage: systemd_restart_service "${SERVICE_NAME}"
923 rst_title "Restart ${1} (service)" section
924 echo
925 tee_stderr <<EOF | bash 2>&1
926systemctl restart ${1}.service
927EOF
928 tee_stderr <<EOF | bash 2>&1
929systemctl status --no-pager ${1}.service
930EOF
934# nginx
935# -----
937nginx_distro_setup() {
938 # shellcheck disable=SC2034
940 NGINX_DEFAULT_SERVER=/etc/nginx/nginx.conf
942 # Including *location* directives from a dedicated config-folder into the
943 # server directive is, what fedora and centos (already) does.
944 NGINX_APPS_ENABLED="/etc/nginx/default.d"
946 # We add a apps-available folder and linking configurations into the
947 # NGINX_APPS_ENABLED folder. See also nginx_include_apps_enabled().
948 NGINX_APPS_AVAILABLE="/etc/nginx/default.apps-available"
950 case $DIST_ID-$DIST_VERS in
951 ubuntu-*|debian-*)
952 NGINX_PACKAGES="nginx"
953 NGINX_DEFAULT_SERVER=/etc/nginx/sites-available/default
954 ;;
955 arch-*)
956 NGINX_PACKAGES="nginx-mainline"
957 ;;
958 fedora-*|centos-7)
959 NGINX_PACKAGES="nginx"
960 ;;
961 *)
962 err_msg "$DIST_ID-$DIST_VERS: nginx not yet implemented"
963 ;;
964 esac
966nginx_distro_setup
968install_nginx(){
969 info_msg "installing nginx ..."
970 pkg_install "${NGINX_PACKAGES}"
971 case $DIST_ID-$DIST_VERS in
972 arch-*|fedora-*|centos-7)
973 systemctl enable nginx
974 systemctl start nginx
975 ;;
976 esac
979nginx_is_installed() {
980 command -v nginx &>/dev/null
983nginx_reload() {
985 info_msg "reload nginx .."
986 echo
987 if ! nginx -t; then
988 err_msg "testing nginx configuration failed"
989 return 42
990 fi
991 systemctl restart nginx
994nginx_install_app() {
996 # usage: nginx_install_app [<template option> ...] <myapp.conf>
997 #
998 # <template option>: see install_template
1000 local template_opts=()
1001 local pos_args=("$0")
1003 for i in "$@"; do
1004 case $i in
1005 -*) template_opts+=("$i");;
1006 *) pos_args+=("$i");;
1007 esac
1008 done
1010 nginx_include_apps_enabled "${NGINX_DEFAULT_SERVER}"
1012 install_template "${template_opts[@]}" \
1013 "${NGINX_APPS_AVAILABLE}/${pos_args[1]}" \
1014 root root 644
1015 nginx_enable_app "${pos_args[1]}"
1016 info_msg "installed nginx app: ${pos_args[1]}"
1019nginx_include_apps_enabled() {
1021 # Add the *NGINX_APPS_ENABLED* infrastruture to a nginx server block. Such
1022 # infrastruture is already known from fedora and centos, including apps (location
1023 # directives) from the /etc/nginx/default.d folder into the *default* nginx
1024 # server.
1026 # usage: nginx_include_apps_enabled <config-file>
1028 # config-file: Config file with server directive in.
1030 [[ -z $1 ]] && die_caller 42 "missing argument <config-file>"
1031 local server_conf="$1"
1033 # include /etc/nginx/default.d/*.conf;
1034 local include_directive="include ${NGINX_APPS_ENABLED}/*.conf;"
1035 local include_directive_re="^\s*include ${NGINX_APPS_ENABLED}/\*\.conf;"
1037 info_msg "checking existence: '${include_directive}' in file ${server_conf}"
1038 if grep "${include_directive_re}" "${server_conf}"; then
1039 info_msg "OK, already exists."
1040 return
1041 fi
1043 info_msg "add missing directive: '${include_directive}'"
1044 cp "${server_conf}" "${server_conf}.bak"
1047 local line
1048 local stage=0
1049 while IFS= read -r line
1050 do
1051 echo "$line"
1052 if [[ $stage = 0 ]]; then
1053 if [[ $line =~ ^[[:space:]]*server*[[:space:]]*\{ ]]; then
1054 stage=1
1055 fi
1056 fi
1058 if [[ $stage = 1 ]]; then
1059 echo " # Load configuration files for the default server block."
1060 echo " $include_directive"
1061 echo ""
1062 stage=2
1063 fi
1064 done < "${server_conf}.bak"
1065 ) > "${server_conf}"
1069nginx_remove_app() {
1071 # usage: nginx_remove_app <myapp.conf>
1073 info_msg "remove nginx app: $1"
1074 nginx_dissable_app "$1"
1075 rm -f "${NGINX_APPS_AVAILABLE}/$1"
1078nginx_enable_app() {
1080 # usage: nginx_enable_app <myapp.conf>
1082 local CONF="$1"
1084 info_msg "enable nginx app: ${CONF}"
1085 mkdir -p "${NGINX_APPS_ENABLED}"
1086 rm -f "${NGINX_APPS_ENABLED}/${CONF}"
1087 ln -s "${NGINX_APPS_AVAILABLE}/${CONF}" "${NGINX_APPS_ENABLED}/${CONF}"
1088 nginx_reload
1091nginx_dissable_app() {
1093 # usage: nginx_disable_app <myapp.conf>
1095 local CONF="$1"
1097 info_msg "disable nginx app: ${CONF}"
1098 rm -f "${NGINX_APPS_ENABLED}/${CONF}"
1099 nginx_reload
1103# Apache
1104# ------
1106apache_distro_setup() {
1107 # shellcheck disable=SC2034
1108 case $DIST_ID-$DIST_VERS in
1109 ubuntu-*|debian-*)
1110 # debian uses the /etc/apache2 path, while other distros use
1111 # the apache default at /etc/httpd
1112 APACHE_SITES_AVAILABLE="/etc/apache2/sites-available"
1113 APACHE_SITES_ENABLED="/etc/apache2/sites-enabled"
1114 APACHE_MODULES="/usr/lib/apache2/modules"
1115 APACHE_PACKAGES="apache2"
1116 ;;
1117 arch-*)
1118 APACHE_SITES_AVAILABLE="/etc/httpd/sites-available"
1119 APACHE_SITES_ENABLED="/etc/httpd/sites-enabled"
1120 APACHE_MODULES="modules"
1121 APACHE_PACKAGES="apache"
1122 ;;
1123 fedora-*|centos-7)
1124 APACHE_SITES_AVAILABLE="/etc/httpd/sites-available"
1125 APACHE_SITES_ENABLED="/etc/httpd/sites-enabled"
1126 APACHE_MODULES="modules"
1127 APACHE_PACKAGES="httpd"
1128 ;;
1129 *)
1130 err_msg "$DIST_ID-$DIST_VERS: apache not yet implemented"
1131 ;;
1132 esac
1135apache_distro_setup
1137install_apache(){
1138 info_msg "installing apache ..."
1139 pkg_install "$APACHE_PACKAGES"
1140 case $DIST_ID-$DIST_VERS in
1141 arch-*|fedora-*|centos-7)
1142 if ! grep "IncludeOptional sites-enabled" "/etc/httpd/conf/httpd.conf"; then
1143 echo "IncludeOptional sites-enabled/*.conf" >> "/etc/httpd/conf/httpd.conf"
1144 fi
1145 systemctl enable httpd
1146 systemctl start httpd
1147 ;;
1148 esac
1151apache_is_installed() {
1152 case $DIST_ID-$DIST_VERS in
1153 ubuntu-*|debian-*) (command -v apachectl) &>/dev/null;;
1154 arch-*) (command -v httpd) &>/dev/null;;
1155 fedora-*|centos-7) (command -v httpd) &>/dev/null;;
1156 esac
1159apache_reload() {
1161 info_msg "reload apache .."
1162 echo
1163 case $DIST_ID-$DIST_VERS in
1164 ubuntu-*|debian-*)
1165 sudo -H apachectl configtest
1166 sudo -H systemctl force-reload apache2
1167 ;;
1168 arch-*|fedora-*|centos-7)
1169 sudo -H httpd -t
1170 sudo -H systemctl force-reload httpd
1171 ;;
1172 esac
1175apache_install_site() {
1177 # usage: apache_install_site [<template option> ...] <mysite.conf>
1179 # <template option>: see install_template
1181 local template_opts=()
1182 local pos_args=("$0")
1184 for i in "$@"; do
1185 case $i in
1186 -*) template_opts+=("$i");;
1187 *) pos_args+=("$i");;
1188 esac
1189 done
1191 install_template "${template_opts[@]}" \
1192 "${APACHE_SITES_AVAILABLE}/${pos_args[1]}" \
1193 root root 644
1194 apache_enable_site "${pos_args[1]}"
1195 info_msg "installed apache site: ${pos_args[1]}"
1198apache_remove_site() {
1200 # usage: apache_remove_site <mysite.conf>
1202 info_msg "remove apache site: $1"
1203 apache_dissable_site "$1"
1204 rm -f "${APACHE_SITES_AVAILABLE}/$1"
1207apache_enable_site() {
1209 # usage: apache_enable_site <mysite.conf>
1211 local CONF="$1"
1213 info_msg "enable apache site: ${CONF}"
1215 case $DIST_ID-$DIST_VERS in
1216 ubuntu-*|debian-*)
1217 sudo -H a2ensite -q "${CONF}"
1218 ;;
1219 arch-*)
1220 mkdir -p "${APACHE_SITES_ENABLED}"
1221 rm -f "${APACHE_SITES_ENABLED}/${CONF}"
1222 ln -s "${APACHE_SITES_AVAILABLE}/${CONF}" "${APACHE_SITES_ENABLED}/${CONF}"
1223 ;;
1224 fedora-*|centos-7)
1225 mkdir -p "${APACHE_SITES_ENABLED}"
1226 rm -f "${APACHE_SITES_ENABLED}/${CONF}"
1227 ln -s "${APACHE_SITES_AVAILABLE}/${CONF}" "${APACHE_SITES_ENABLED}/${CONF}"
1228 ;;
1229 esac
1230 apache_reload
1233apache_dissable_site() {
1235 # usage: apache_disable_site <mysite.conf>
1237 local CONF="$1"
1239 info_msg "disable apache site: ${CONF}"
1241 case $DIST_ID-$DIST_VERS in
1242 ubuntu-*|debian-*)
1243 sudo -H a2dissite -q "${CONF}"
1244 ;;
1245 arch-*)
1246 rm -f "${APACHE_SITES_ENABLED}/${CONF}"
1247 ;;
1248 fedora-*|centos-7)
1249 rm -f "${APACHE_SITES_ENABLED}/${CONF}"
1250 ;;
1251 esac
1252 apache_reload
1255# uWSGI
1256# -----
1258uWSGI_SETUP="${uWSGI_SETUP:=/etc/uwsgi}"
1259uWSGI_USER=
1260uWSGI_GROUP=
1262# How distros manage uWSGI apps is very different. From uWSGI POV read:
1263# - https://uwsgi-docs.readthedocs.io/en/latest/Management.html
1265uWSGI_distro_setup() {
1266 case $DIST_ID-$DIST_VERS in
1267 ubuntu-*|debian-*)
1268 # init.d --> /usr/share/doc/uwsgi/README.Debian.gz
1269 # For uWSGI debian uses the LSB init process, this might be changed
1270 # one day, see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=833067
1271 uWSGI_APPS_AVAILABLE="${uWSGI_SETUP}/apps-available"
1272 uWSGI_APPS_ENABLED="${uWSGI_SETUP}/apps-enabled"
1273 uWSGI_PACKAGES="uwsgi"
1274 ;;
1275 arch-*)
1276 # systemd --> /usr/lib/systemd/system/uwsgi@.service
1277 # For uWSGI archlinux uses systemd template units, see
1278 # - http://0pointer.de/blog/projects/instances.html
1279 # - https://uwsgi-docs.readthedocs.io/en/latest/Systemd.html#one-service-per-app-in-systemd
1280 uWSGI_APPS_AVAILABLE="${uWSGI_SETUP}/apps-archlinux"
1281 uWSGI_APPS_ENABLED="${uWSGI_SETUP}"
1282 uWSGI_PACKAGES="uwsgi"
1283 ;;
1284 fedora-*|centos-7)
1285 # systemd --> /usr/lib/systemd/system/uwsgi.service
1286 # The unit file starts uWSGI in emperor mode (/etc/uwsgi.ini), see
1287 # - https://uwsgi-docs.readthedocs.io/en/latest/Emperor.html
1288 uWSGI_APPS_AVAILABLE="${uWSGI_SETUP}/apps-available"
1289 uWSGI_APPS_ENABLED="${uWSGI_SETUP}.d"
1290 uWSGI_PACKAGES="uwsgi"
1291 uWSGI_USER="uwsgi"
1292 uWSGI_GROUP="uwsgi"
1293 ;;
1294 *)
1295 err_msg "$DIST_ID-$DIST_VERS: uWSGI not yet implemented"
1296 ;;
1297esac
1300uWSGI_distro_setup
1302install_uwsgi(){
1303 info_msg "installing uwsgi ..."
1304 pkg_install "$uWSGI_PACKAGES"
1305 case $DIST_ID-$DIST_VERS in
1306 fedora-*|centos-7)
1307 # enable & start should be called once at uWSGI installation time
1308 systemctl enable uwsgi
1309 systemctl restart uwsgi
1310 ;;
1311 esac
1314uWSGI_restart() {
1316 # usage: uWSGI_restart() <myapp.ini>
1318 local CONF="$1"
1320 [[ -z $CONF ]] && die_caller 42 "missing argument <myapp.ini>"
1321 info_msg "restart uWSGI service"
1322 case $DIST_ID-$DIST_VERS in
1323 ubuntu-*|debian-*)
1324 # the 'service' method seems broken in that way, that it (re-)starts
1325 # the whole uwsgi process.
1326 service uwsgi restart "${CONF%.*}"
1327 ;;
1328 arch-*)
1329 # restart systemd template instance
1330 if uWSGI_app_available "${CONF}"; then
1331 systemctl restart "uwsgi@${CONF%.*}"
1332 else
1333 info_msg "[uWSGI:systemd-template] ${CONF} not installed (no need to restart)"
1334 fi
1335 ;;
1336 fedora-*|centos-7)
1337 # in emperor mode, just touch the file to restart
1338 if uWSGI_app_enabled "${CONF}"; then
1339 touch "${uWSGI_APPS_ENABLED}/${CONF}"
1340 # it seems, there is a polling time in between touch and restart
1341 # of the service.
1342 sleep 3
1343 else
1344 info_msg "[uWSGI:emperor] ${CONF} not installed (no need to restart)"
1345 fi
1346 ;;
1347 *)
1348 err_msg "$DIST_ID-$DIST_VERS: uWSGI not yet implemented"
1349 return 42
1350 ;;
1351 esac
1354uWSGI_prepare_app() {
1356 # usage: uWSGI_prepare_app <myapp.ini>
1358 [[ -z $1 ]] && die_caller 42 "missing argument <myapp.ini>"
1360 local APP="${1%.*}"
1362 case $DIST_ID-$DIST_VERS in
1363 fedora-*|centos-7)
1364 # in emperor mode, the uwsgi user is the owner of the sockets
1365 info_msg "prepare (uwsgi:uwsgi) /run/uwsgi/app/${APP}"
1366 mkdir -p "/run/uwsgi/app/${APP}"
1367 chown -R "uwsgi:uwsgi" "/run/uwsgi/app/${APP}"
1368 ;;
1369 *)
1370 info_msg "prepare (${SERVICE_USER}:${SERVICE_GROUP}) /run/uwsgi/app/${APP}"
1371 mkdir -p "/run/uwsgi/app/${APP}"
1372 chown -R "${SERVICE_USER}:${SERVICE_GROUP}" "/run/uwsgi/app/${APP}"
1373 ;;
1374 esac
1378uWSGI_app_available() {
1379 # usage: uWSGI_app_available <myapp.ini>
1380 local CONF="$1"
1382 [[ -z $CONF ]] && die_caller 42 "missing argument <myapp.ini>"
1383 [[ -f "${uWSGI_APPS_AVAILABLE}/${CONF}" ]]
1386uWSGI_install_app() {
1388 # usage: uWSGI_install_app [<template option> ...] <myapp.ini>
1390 # <template option>: see install_template
1392 local pos_args=("$0")
1394 for i in "$@"; do
1395 case $i in
1396 -*) template_opts+=("$i");;
1397 *) pos_args+=("$i");;
1398 esac
1399 done
1400 uWSGI_prepare_app "${pos_args[1]}"
1401 mkdir -p "${uWSGI_APPS_AVAILABLE}"
1402 install_template "${template_opts[@]}" \
1403 "${uWSGI_APPS_AVAILABLE}/${pos_args[1]}" \
1404 root root 644
1405 uWSGI_enable_app "${pos_args[1]}"
1406 uWSGI_restart "${pos_args[1]}"
1407 info_msg "uWSGI app: ${pos_args[1]} is installed"
1410uWSGI_remove_app() {
1412 # usage: uWSGI_remove_app <myapp.ini>
1414 local CONF="$1"
1416 [[ -z $CONF ]] && die_caller 42 "missing argument <myapp.ini>"
1417 info_msg "remove uWSGI app: ${CONF}"
1418 uWSGI_disable_app "${CONF}"
1419 uWSGI_restart "${CONF}"
1420 rm -f "${uWSGI_APPS_AVAILABLE}/${CONF}"
1423uWSGI_app_enabled() {
1424 # usage: uWSGI_app_enabled <myapp.ini>
1426 local exit_val=0
1427 local CONF="$1"
1429 [[ -z $CONF ]] && die_caller 42 "missing argument <myapp.ini>"
1430 case $DIST_ID-$DIST_VERS in
1431 ubuntu-*|debian-*)
1432 [[ -f "${uWSGI_APPS_ENABLED}/${CONF}" ]]
1433 exit_val=$?
1434 ;;
1435 arch-*)
1436 systemctl -q is-enabled "uwsgi@${CONF%.*}"
1437 exit_val=$?
1438 ;;
1439 fedora-*|centos-7)
1440 [[ -f "${uWSGI_APPS_ENABLED}/${CONF}" ]]
1441 exit_val=$?
1442 ;;
1443 *)
1444 # FIXME
1445 err_msg "$DIST_ID-$DIST_VERS: uWSGI not yet implemented"
1446 exit_val=1
1447 ;;
1448 esac
1449 return $exit_val
1452# shellcheck disable=SC2164
1453uWSGI_enable_app() {
1455 # usage: uWSGI_enable_app <myapp.ini>
1457 local CONF="$1"
1459 [[ -z $CONF ]] && die_caller 42 "missing argument <myapp.ini>"
1460 case $DIST_ID-$DIST_VERS in
1461 ubuntu-*|debian-*)
1462 mkdir -p "${uWSGI_APPS_ENABLED}"
1463 rm -f "${uWSGI_APPS_ENABLED}/${CONF}"
1464 ln -s "${uWSGI_APPS_AVAILABLE}/${CONF}" "${uWSGI_APPS_ENABLED}/${CONF}"
1465 info_msg "enabled uWSGI app: ${CONF} (restart required)"
1466 ;;
1467 arch-*)
1468 mkdir -p "${uWSGI_APPS_ENABLED}"
1469 rm -f "${uWSGI_APPS_ENABLED}/${CONF}"
1470 ln -s "${uWSGI_APPS_AVAILABLE}/${CONF}" "${uWSGI_APPS_ENABLED}/${CONF}"
1471 systemctl enable "uwsgi@${CONF%.*}"
1472 info_msg "enabled uWSGI app: ${CONF} (restart required)"
1473 ;;
1474 fedora-*|centos-7)
1475 mkdir -p "${uWSGI_APPS_ENABLED}"
1476 rm -f "${uWSGI_APPS_ENABLED}/${CONF}"
1477 ln -s "${uWSGI_APPS_AVAILABLE}/${CONF}" "${uWSGI_APPS_ENABLED}/${CONF}"
1478 chown "${uWSGI_USER}:${uWSGI_GROUP}" "${uWSGI_APPS_ENABLED}/${CONF}"
1479 info_msg "enabled uWSGI app: ${CONF}"
1480 ;;
1481 *)
1482 # FIXME
1483 err_msg "$DIST_ID-$DIST_VERS: uWSGI not yet implemented"
1484 ;;
1485 esac
1488uWSGI_disable_app() {
1490 # usage: uWSGI_disable_app <myapp.ini>
1492 local CONF="$1"
1494 [[ -z $CONF ]] && die_caller 42 "missing argument <myapp.ini>"
1495 case $DIST_ID-$DIST_VERS in
1496 ubuntu-*|debian-*)
1497 service uwsgi stop "${CONF%.*}"
1498 rm -f "${uWSGI_APPS_ENABLED}/${CONF}"
1499 info_msg "disabled uWSGI app: ${CONF} (restart uWSGI required)"
1500 ;;
1501 arch-*)
1502 systemctl stop "uwsgi@${CONF%.*}"
1503 systemctl disable "uwsgi@${CONF%.*}"
1504 rm -f "${uWSGI_APPS_ENABLED}/${CONF}"
1505 ;;
1506 fedora-*|centos-7)
1507 # in emperor mode, just remove the app.ini file
1508 rm -f "${uWSGI_APPS_ENABLED}/${CONF}"
1509 ;;
1510 *)
1511 # FIXME
1512 err_msg "$DIST_ID-$DIST_VERS: uWSGI not yet implemented"
1513 ;;
1514 esac
1517# distro's package manager
1518# ------------------------
1520_apt_pkg_info_is_updated=0
1522pkg_install() {
1524 # usage: TITEL='install foobar' pkg_install foopkg barpkg
1526 rst_title "${TITLE:-installation of packages}" section
1527 echo -e "\npackage(s)::\n"
1528 # shellcheck disable=SC2068
1529 echo " " $@ | $FMT
1531 if ! ask_yn "Should packages be installed?" Yn 30; then
1532 return 42
1533 fi
1534 case $DIST_ID in
1535 ubuntu|debian)
1536 if [[ $_apt_pkg_info_is_updated == 0 ]]; then
1537 export _apt_pkg_info_is_updated=1
1538 apt update
1539 fi
1540 # shellcheck disable=SC2068
1541 apt-get install -m -y $@
1542 ;;
1543 arch)
1544 # shellcheck disable=SC2068
1545 pacman -Sy --noconfirm $@
1546 ;;
1547 fedora)
1548 # shellcheck disable=SC2068
1549 dnf install -y $@
1550 ;;
1551 centos)
1552 # shellcheck disable=SC2068
1553 yum install -y $@
1554 ;;
1555 esac
1558pkg_remove() {
1560 # usage: TITEL='remove foobar' pkg_remove foopkg barpkg
1562 rst_title "${TITLE:-remove packages}" section
1563 echo -e "\npackage(s)::\n"
1564 # shellcheck disable=SC2068
1565 echo " " $@ | $FMT
1567 if ! ask_yn "Should packages be removed (purge)?" Yn 30; then
1568 return 42
1569 fi
1570 case $DIST_ID in
1571 ubuntu|debian)
1572 # shellcheck disable=SC2068
1573 apt-get purge --autoremove --ignore-missing -y $@
1574 ;;
1575 arch)
1576 # shellcheck disable=SC2068
1577 pacman -R --noconfirm $@
1578 ;;
1579 fedora)
1580 # shellcheck disable=SC2068
1581 dnf remove -y $@
1582 ;;
1583 centos)
1584 # shellcheck disable=SC2068
1585 yum remove -y $@
1586 ;;
1587 esac
1590pkg_is_installed() {
1592 # usage: pkg_is_install foopkg || pkg_install foopkg
1594 case $DIST_ID in
1595 ubuntu|debian)
1596 dpkg -l "$1" &> /dev/null
1597 return $?
1598 ;;
1599 arch)
1600 pacman -Qsq "$1" &> /dev/null
1601 return $?
1602 ;;
1603 fedora)
1604 dnf list -q --installed "$1" &> /dev/null
1605 return $?
1606 ;;
1607 centos)
1608 yum list -q --installed "$1" &> /dev/null
1609 return $?
1610 ;;
1611 esac
1614# git tooling
1615# -----------
1617# shellcheck disable=SC2164
1618git_clone() {
1620 # usage:
1622 # git_clone <url> <name> [<branch> [<user>]]
1623 # git_clone <url> <path> [<branch> [<user>]]
1625 # First form uses $CACHE/<name> as destination folder, second form clones
1626 # into <path>. If repository is allready cloned, pull from <branch> and
1627 # update working tree (if needed, the caller has to stash local changes).
1629 # git clone https://github.com/searx/searx searx-src origin/master searxlogin
1632 local url="$1"
1633 local dest="$2"
1634 local branch="$3"
1635 local user="$4"
1636 local bash_cmd="bash"
1637 local remote="origin"
1639 if [[ ! "${dest:0:1}" = "/" ]]; then
1640 dest="$CACHE/$dest"
1641 fi
1643 [[ -z $branch ]] && branch=master
1644 [[ -z $user ]] && [[ -n "${SUDO_USER}" ]] && user="${SUDO_USER}"
1645 [[ -n $user ]] && bash_cmd="sudo -H -u $user -i"
1647 if [[ -d "${dest}" ]] ; then
1648 info_msg "already cloned: $dest"
1649 tee_stderr 0.1 <<EOF | $bash_cmd 2>&1 | prefix_stdout " ${_Yellow}|$user|${_creset} "
1650cd "${dest}"
1651git checkout -m -B "$branch" --track "$remote/$branch"
1652git pull --all
1653EOF
1654 else
1655 info_msg "clone into: $dest"
1656 tee_stderr 0.1 <<EOF | $bash_cmd 2>&1 | prefix_stdout " ${_Yellow}|$user|${_creset} "
1657mkdir -p "$(dirname "$dest")"
1658cd "$(dirname "$dest")"
1659git clone --branch "$branch" --origin "$remote" "$url" "$(basename "$dest")"
1660EOF
1661 fi
1664# containers
1665# ----------
1667in_container() {
1668 # Test if shell runs in a container.
1670 # usage: in_container && echo "process running inside a LXC container"
1671 # in_container || echo "process is not running inside a LXC container"
1673 # sudo_or_exit
1674 # hint: Reads init process environment, therefore root access is required!
1675 # to be safe, take a look at the environment of process 1 (/sbin/init)
1676 # grep -qa 'container=lxc' /proc/1/environ
1678 # see lxc_init_container_env
1679 [[ -f /.lxcenv ]]
1682LXC_ENV_FOLDER=
1683if in_container; then
1684 # shellcheck disable=SC2034
1685 LXC_ENV_FOLDER="lxc-env/$(hostname)/"
1686 PY_ENV="${LXC_ENV_FOLDER}${PY_ENV}"
1687 PY_ENV_BIN="${LXC_ENV_FOLDER}${PY_ENV_BIN}"
1688 PYDIST="${LXC_ENV_FOLDER}${PYDIST}"
1689 PYBUILD="${LXC_ENV_FOLDER}${PYBUILD}"
1690 DOCS_DIST="${LXC_ENV_FOLDER}${DOCS_DIST}"
1691 DOCS_BUILD="${LXC_ENV_FOLDER}${DOCS_BUILD}"
1694lxc_init_container_env() {
1696 # usage: lxc_init_container_env <name>
1698 # Create a /.lxcenv file in the root folder. Call this once after the
1699 # container is inital started and before installing any boilerplate stuff.
1701 info_msg "create /.lxcenv in container $1"
1702 cat <<EOF | lxc exec "${1}" -- bash | prefix_stdout "[${_BBlue}${1}${_creset}] "
1703touch "/.lxcenv"
1704ls -l "/.lxcenv"
1705EOF
1708# apt packages
1709LXC_BASE_PACKAGES_debian="bash git build-essential python3 python3-venv"
1711# pacman packages
1712LXC_BASE_PACKAGES_arch="bash git base-devel python"
1714# dnf packages
1715LXC_BASE_PACKAGES_fedora="bash git @development-tools python"
1717# yum packages
1718LXC_BASE_PACKAGES_centos="bash git python3"
1720case $DIST_ID in
1721 ubuntu|debian) LXC_BASE_PACKAGES="${LXC_BASE_PACKAGES_debian}" ;;
1722 arch) LXC_BASE_PACKAGES="${LXC_BASE_PACKAGES_arch}" ;;
1723 fedora) LXC_BASE_PACKAGES="${LXC_BASE_PACKAGES_fedora}" ;;
1724 centos) LXC_BASE_PACKAGES="${LXC_BASE_PACKAGES_centos}" ;;
1725 *) err_msg "$DIST_ID-$DIST_VERS: pkg_install LXC_BASE_PACKAGES not yet implemented" ;;
1726esac
1728lxc_install_base_packages() {
1729 info_msg "install LXC_BASE_PACKAGES in container $1"
1730 case $DIST_ID in
1731 centos) yum groupinstall "Development Tools" -y ;;
1732 esac
1733 pkg_install "${LXC_BASE_PACKAGES}"
1737lxc_image_copy() {
1739 # usage: lxc_image_copy <remote image> <local image>
1741 # lxc_image_copy "images:ubuntu/20.04" "ubu2004"
1743 if lxc_image_exists "local:${LXC_SUITE[i+1]}"; then
1744 info_msg "image ${LXC_SUITE[i]} already copied --> ${LXC_SUITE[i+1]}"
1745 else
1746 info_msg "copy image locally ${LXC_SUITE[i]} --> ${LXC_SUITE[i+1]}"
1747 lxc image copy "${LXC_SUITE[i]}" local: \
1748 --alias "${LXC_SUITE[i+1]}" | prefix_stdout
1749 fi
1752lxc_init_container() {
1754 # usage: lxc_init_container <image name> <container name>
1756 local image_name="$1"
1757 local container_name="$2"
1759 if lxc info "${container_name}" &>/dev/null; then
1760 info_msg "container '${container_name}' already exists"
1761 else
1762 info_msg "create container instance: ${container_name}"
1763 lxc init "local:${image_name}" "${container_name}"
1764 fi
1767lxc_exists(){
1769 # usage: lxc_exists <name> || echo "container <name> does not exists"
1771 lxc info "$1" &>/dev/null
1774lxc_image_exists(){
1775 # usage: lxc_image_exists <alias> || echo "image <alias> does locally not exists"
1777 lxc image info "local:$1" &>/dev/null
1781lxc_delete_container() {
1783 # usage: lxc_delete_container <container-name>
1785 if lxc info "$1" &>/dev/null; then
1786 info_msg "stop & delete instance ${_BBlue}${1}${_creset}"
1787 lxc stop "$1" &>/dev/null
1788 lxc delete "$1" | prefix_stdout
1789 else
1790 warn_msg "instance '$1' does not exist / can't delete :o"
1791 fi
1794lxc_delete_local_image() {
1796 # usage: lxc_delete_local_image <container-name>
1798 info_msg "delete image 'local:$i'"
1799 lxc image delete "local:$i"
1803# IP
1804# --
1806global_IPs(){
1807 # usage: global_IPS
1809 # print list of host's SCOPE global addresses and adapters e.g::
1811 # $ global_IPs
1812 # enp4s0|192.168.1.127
1813 # lxdbr0|10.246.86.1
1814 # lxdbr0|fd42:8c58:2cd:b73f::1
1816 ip -o addr show | sed -nr 's/[0-9]*:\s*([a-z0-9]*).*inet[6]?\s*([a-z0-9.:]*).*scope global.*/\1|\2/p'
1819primary_ip() {
1821 case $DIST_ID in
1822 arch)
1823 ip -o addr show \
1824 | sed -nr 's/[0-9]*:\s*([a-z0-9]*).*inet[6]?\s*([a-z0-9.:]*).*scope global.*/\2/p' \
1825 | head -n 1
1826 ;;
1827 *) hostname -I | cut -d' ' -f1 ;;
1828 esac
1831# URL
1832# ---
1834url_replace_hostname(){
1836 # usage: url_replace_hostname <url> <new hostname>
1838 # to replace hostname by primary IP::
1840 # url_replace_hostname http://searx-ubu1604/morty $(primary_ip)
1841 # http://10.246.86.250/morty
1843 # shellcheck disable=SC2001
1844 echo "$1" | sed "s|\(http[s]*://\)[^/]*\(.*\)|\1$2\2|"