@@ -0,0 +1,1519 @@
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 ...
7+DIST_ID=$(source /etc/os-release; echo "$ID");
8+# shellcheck disable=SC2034
9+DIST_VERS=$(source /etc/os-release; echo "$VERSION_ID");
11+ADMIN_NAME="${ADMIN_NAME:-$(git config user.name)}"
12+ADMIN_NAME="${ADMIN_NAME:-$USER}"
14+ADMIN_EMAIL="${ADMIN_EMAIL:-$(git config user.email)}"
15+ADMIN_EMAIL="${ADMIN_EMAIL:-$USER@$(hostname)}"
17+if [[ -z "${REPO_ROOT}" ]]; then
18+ REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")
19+ while [ -h "${REPO_ROOT}" ] ; do
20+ REPO_ROOT=$(readlink "${REPO_ROOT}")
22+ REPO_ROOT=$(cd "${REPO_ROOT}/.." && pwd -P )
25+if [[ -z ${TEMPLATES} ]]; then
26+ TEMPLATES="${REPO_ROOT}/utils/templates"
29+if [[ -z "$CACHE" ]]; then
30+ CACHE="${REPO_ROOT}/cache"
33+if [[ -z ${DIFF_CMD} ]]; then
35+ if command -v colordiff >/dev/null; then
36+ DIFF_CMD="colordiff -u"
40+DOT_CONFIG="${DOT_CONFIG:-${REPO_ROOT}/.config.sh}"
43+ if [[ ! -e "${DOT_CONFIG}" ]]; then
44+ err_msg "configuration does not extsts at: ${DOT_CONFIG}"
47+ # shellcheck disable=SC1090
48+ source "${DOT_CONFIG}"
52+ # usage: sudo_or_exit
54+ if [ ! "$(id -u)" -eq 0 ]; then
55+ err_msg "this command requires root (sudo) privilege!" >&2
62+ # usage: required_commands [cmd1 ...]
65+ while [ -n "$1" ]; do
67+ if ! command -v "$1" &>/dev/null; then
68+ err_msg "missing command $1"
79+# shellcheck disable=SC2034
80+set_terminal_colors() {
82+ _creset='\e[0m' # reset all attributes
103+if [ ! -p /dev/stdout ]; then
110+if command -v fmt >/dev/null; then
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";;
124+ err_msg "invalid argument '${2}' in line $(caller)"
131+ # usage: RST_INDENT=1 rst_para "lorem ipsum ..."
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"
137+ echo -en "\n$*\n" | $FMT
142+ echo -e "${_BRed}ERROR:${_creset} ${BASH_SOURCE[1]}: line ${BASH_LINENO[0]}: ${2-died ${1-1}}" >&2;
147+ echo -e "${_BRed}ERROR:${_creset} ${BASH_SOURCE[2]}: line ${BASH_LINENO[1]}: ${FUNCNAME[1]}(): ${2-died ${1-1}}" >&2;
151+err_msg() { echo -e "${_BRed}ERROR:${_creset} $*" >&2; }
152+warn_msg() { echo -e "${_BBlue}WARN:${_creset} $*" >&2; }
153+info_msg() { echo -e "${_BYellow}INFO:${_creset} $*" >&2; }
156+ if [[ $(uname -s) != 'Darwin' ]]; then
157+ while read -r -n1 -t 0.1; do : ; done
162+ # usage: waitKEY [<timeout in sec>]
167+ [[ -z "$msg" ]] && msg="${_Green}** press any [${_BCyan}KEY${_Green}] to continue **${_creset}"
169+ [[ -n $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
170+ [[ -n $_t ]] && _t="-t $_t"
172+ # shellcheck disable=SC2086
179+ # usage: ask_yn <prompt-text> [Ny|Yn] [<timeout in sec>]
181+ local EXIT_YES=0 # exit status 0 --> successful
182+ local EXIT_NO=1 # exit status 1 --> error code
185+ [[ -n $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
186+ [[ -n $_t ]] && _t="-t $_t"
187+ case "${FORCE_SELECTION:-${2}}" in
188+ Y) return ${EXIT_YES} ;;
189+ N) return ${EXIT_NO} ;;
191+ local exit_val=${EXIT_YES}
192+ local choice="[${_BGreen}YES${_creset}/no]"
196+ local exit_val=${EXIT_NO}
197+ local choice="[${_BGreen}NO${_creset}/yes]"
204+ printf "$1 ${choice} "
205+ # shellcheck disable=SC2086
207+ if [[ -z $REPLY ]]; then
208+ printf "$default\n"; break
209+ elif [[ $REPLY =~ ^[Yy]$ ]]; then
210+ exit_val=${EXIT_YES}
213+ elif [[ $REPLY =~ ^[Nn]$ ]]; then
219+ err_msg "invalid choice"
228+ # tee_stderr 1 <<EOF | python -i
232+ # >>> print("hello")
236+ if [[ -n $1 ]] ; then _t="$1"; fi
238+ (while read -r line; do
239+ # shellcheck disable=SC2086
247+ # usage: <cmd> | prefix_stdout [prefix]
249+ local prefix="${_BYellow}-->|${_creset}"
251+ if [[ -n $1 ]] ; then prefix="$1"; fi
253+ # shellcheck disable=SC2162
254+ (while IFS= read line; do
255+ echo -e "${prefix}$line"
261+ # usage: append_line <line> <file>
263+ # Append line if not exists, create file if not exists. E.g::
265+ # append_line 'source ~/.foo' ~/bashrc
269+ grep -qFs -- "$LINE" "$FILE" || echo "$LINE" >> "$FILE"
274+ # usage: cache_download <url> <local-filename>
278+ if [[ -n ${SUDO_USER} ]]; then
279+ sudo -u "${SUDO_USER}" mkdir -p "${CACHE}"
284+ if [[ -f "${CACHE}/$2" ]] ; then
285+ info_msg "already cached: $1"
286+ info_msg " --> ${CACHE}/$2"
289+ if [[ ! -f "${CACHE}/$2" ]]; then
290+ info_msg "caching: $1"
291+ info_msg " --> ${CACHE}/$2"
292+ if [[ -n ${SUDO_USER} ]]; then
293+ sudo -u "${SUDO_USER}" wget --progress=bar -O "${CACHE}/$2" "$1" ; exit_value=$?
295+ wget --progress=bar -O "${CACHE}/$2" "$1" ; exit_value=$?
297+ if [[ ! $exit_value = 0 ]]; then
298+ err_msg "failed to download: $1"
305+ # usage: backup_file /path/to/file.foo
308+ stamp=$(date +"_%Y%m%d_%H%M%S")
309+ info_msg "create backup: ${1}${stamp}"
310+ cp -a "${1}" "${1}${stamp}"
317+ # DEFAULT_SELECT= 2 \
318+ # choose_one <name> "your selection?" "Coffee" "Coffee with milk"
320+ local default=${DEFAULT_SELECT-1}
322+ local env_name=$1 && shift
326+ [[ -n $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
327+ [[ -n $_t ]] && _t="-t $_t"
330+ echo -e "${_BGreen}Menu::${_creset}"
331+ for ((i=1; i<= $((max -1)); i++)); do
332+ if [[ "$i" == "$default" ]]; then
333+ echo -e " ${_BGreen}$i.${_creset}) ${list[$i]} [default]"
335+ echo -e " $i.) ${list[$i]}"
340+ printf "$1 [${_BGreen}$default${_creset}] "
342+ if (( 10 > max )); then
343+ # shellcheck disable=SC2086
346+ # shellcheck disable=SC2086,SC2229
350+ [[ $REPLY =~ ^-?[0-9]+$ ]] && (( REPLY > 0 )) && (( REPLY < max )) && break
353+ [[ -z $REPLY ]] && REPLY=$default && break
356+ err_msg "invalid choice"
358+ eval "$env_name"='${list[${REPLY}]}'
367+ # install_template [--no-eval] [--variant=<name>] \
368+ # {file} [{owner} [{group} [{chmod}]]]
370+ # E.g. the origin of variant 'raw' of /etc/updatedb.conf is::
372+ # ${TEMPLATES}/etc/updatedb.conf:raw
374+ # To install variant 'raw' of /etc/updatedb.conf without evaluated
375+ # replacements you can use::
377+ # install_template --variant=raw --no-eval \
378+ # /etc/updatedb.conf root root 644
383+ local pos_args=("$0")
387+ --no-eval) do_eval=0; shift ;;
388+ --variant=*) variant=":${i#*=}"; shift ;;
389+ *) pos_args+=("$i") ;;
393+ local dst="${pos_args[1]}"
394+ local template_origin="${TEMPLATES}${dst}${variant}"
395+ local template_file="${TEMPLATES}${dst}"
397+ local owner="${pos_args[2]-$(id -un)}"
398+ local group="${pos_args[3]-$(id -gn)}"
399+ local chmod="${pos_args[4]-644}"
401+ info_msg "install (eval=$do_eval): ${dst}"
402+ [[ -n $variant ]] && info_msg "variant --> ${variant}"
404+ if [[ ! -f "${template_origin}" ]] ; then
405+ err_msg "${template_origin} does not exists"
406+ err_msg "... can't install $dst"
411+ if [[ "$do_eval" == "1" ]]; then
412+ template_file="${CACHE}${dst}${variant}"
413+ info_msg "BUILD template ${template_file}"
414+ if [[ -n ${SUDO_USER} ]]; then
415+ sudo -u "${SUDO_USER}" mkdir -p "$(dirname "${template_file}")"
417+ mkdir -p "$(dirname "${template_file}")"
419+ # shellcheck disable=SC2086
420+ eval "echo \"$(cat ${template_origin})\"" > "${template_file}"
421+ if [[ -n ${SUDO_USER} ]]; then
422+ chown "${SUDO_USER}:${SUDO_USER}" "${template_file}"
425+ template_file=$template_origin
428+ mkdir -p "$(dirname "${dst}")"
430+ if [[ ! -f "${dst}" ]]; then
431+ info_msg "install: ${template_file}"
432+ sudo -H install -v -o "${owner}" -g "${group}" -m "${chmod}" \
433+ "${template_file}" "${dst}" | prefix_stdout
437+ if [[ -f "${dst}" ]] && cmp --silent "${template_file}" "${dst}" ; then
438+ info_msg "file ${dst} allready installed"
442+ info_msg "diffrent file ${dst} allready exists on this host"
445+ choose_one _reply "choose next step with file $dst" \
447+ "leave file unchanged" \
448+ "interactiv shell" \
453+ info_msg "install: ${template_file}"
454+ sudo -H install -v -o "${owner}" -g "${group}" -m "${chmod}" \
455+ "${template_file}" "${dst}" | prefix_stdout
458+ "leave file unchanged")
462+ echo -e "// edit ${_Red}${dst}${_creset} to your needs"
463+ echo -e "// exit with [${_BCyan}CTRL-D${_creset}]"
464+ sudo -H -u "${owner}" -i
465+ $DIFF_CMD "${dst}" "${template_file}"
467+ echo -e "// ${_BBlack}did you edit file ...${_creset}"
468+ echo -en "// ${_Red}${dst}${_creset}"
469+ if ask_yn "//${_BBlack}... to your needs?${_creset}"; then
474+ $DIFF_CMD "${dst}" "${template_file}" | prefix_stdout
480+service_is_available() {
482+ # usage: service_is_available <URL>
484+ [[ -z $1 ]] && die_caller 42 "missing argument <URL>"
486+ http_code=$(curl -H 'Cache-Control: no-cache' \
487+ --silent -o /dev/null --head --write-out '%{http_code}' --insecure \
490+ if [[ $exit_val = 0 ]]; then
491+ info_msg "got $http_code from ${URL}"
493+ case "$http_code" in
494+ 404|410|423) exit_val=$http_code;;
504+ # usage: go_is_available $SERVICE_USER && echo "go is installed!"
506+ sudo -i -u "${1}" which go &>/dev/null
511+ # usage: install_go "${GO_PKG_URL}" "${GO_TAR}" "${SERVICE_USER}"
513+ local _svcpr=" ${_Yellow}|${3}|${_creset} "
515+ rst_title "Install Go in user's HOME" section
517+ rst_para "download and install go binary .."
518+ cache_download "${1}" "${2}"
520+ tee_stderr 0.1 <<EOF | sudo -i -u "${3}" | prefix_stdout "$_svcpr"
523+mkdir -p \$HOME/local
524+rm -rf \$HOME/local/go
525+tar -C \$HOME/local -xzf ${CACHE}/${2}
527+ sudo -i -u "${3}" <<EOF | prefix_stdout
528+! which go >/dev/null && echo "ERROR - Go Installation not found in PATH!?!"
529+which go >/dev/null && go version && echo "congratulations -- Go installation OK :)"
536+service_account_is_available() {
538+ # usage: service_account_is_available "$SERVICE_USER" && echo "OK"
540+ sudo -i -u "$1" echo \$HOME &>/dev/null
543+drop_service_account() {
545+ # usage: drop_service_account "${SERVICE_USER}"
547+ rst_title "Drop ${1} HOME" section
548+ if ask_yn "Do you really want to drop ${1} home folder?"; then
549+ userdel -r -f "${1}" 2>&1 | prefix_stdout
551+ rst_para "Leave HOME folder $(du -sh "${1}") unchanged."
557+ # usage: interactive_shell "${SERVICE_USER}"
559+ echo -e "// exit with [${_BCyan}CTRL-D${_creset}]"
560+ sudo -H -u "${1}" -i
567+SYSTEMD_UNITS="${SYSTEMD_UNITS:-/lib/systemd/system}"
569+systemd_install_service() {
571+ # usage: systemd_install_service "${SERVICE_NAME}" "${SERVICE_SYSTEMD_UNIT}"
573+ rst_title "Install System-D Unit ${1}" section
575+ install_template "${2}" root root 644
577+ systemd_activate_service "${1}"
580+systemd_remove_service() {
582+ # usage: systemd_remove_service "${SERVICE_NAME}" "${SERVICE_SYSTEMD_UNIT}"
584+ if ! ask_yn "Do you really want to deinstall systemd unit ${1}?"; then
587+ systemd_deactivate_service "${1}"
588+ rm "${2}" 2>&1 | prefix_stdout
591+systemd_activate_service() {
593+ # usage: systemd_activate_service "${SERVICE_NAME}"
595+ rst_title "Activate ${1} (service)" section
597+ tee_stderr <<EOF | bash 2>&1
598+systemctl enable ${1}.service
599+systemctl restart ${1}.service
601+ tee_stderr <<EOF | bash 2>&1
602+systemctl status --no-pager ${1}.service
606+systemd_deactivate_service() {
608+ # usage: systemd_deactivate_service "${SERVICE_NAME}"
610+ rst_title "De-Activate ${1} (service)" section
612+ tee_stderr <<EOF | bash 2>&1 | prefix_stdout
613+systemctl stop ${1}.service
614+systemctl disable ${1}.service
618+systemd_restart_service() {
620+ # usage: systemd_restart_service "${SERVICE_NAME}"
622+ rst_title "Restart ${1} (service)" section
624+ tee_stderr <<EOF | bash 2>&1
625+systemctl restart ${1}.service
627+ tee_stderr <<EOF | bash 2>&1
628+systemctl status --no-pager ${1}.service
636+nginx_distro_setup() {
637+ # shellcheck disable=SC2034
639+ NGINX_DEFAULT_SERVER=/etc/nginx/nginx.conf
641+ # Including *location* directives from a dedicated config-folder into the
642+ # server directive is, what what fedora (already) does.
643+ NGINX_APPS_ENABLED="/etc/nginx/default.d"
645+ # We add a apps-available folder and linking configurations into the
646+ # NGINX_APPS_ENABLED folder. See also nginx_include_apps_enabled().
647+ NGINX_APPS_AVAILABLE="/etc/nginx/default.apps-available"
649+ case $DIST_ID-$DIST_VERS in
651+ NGINX_PACKAGES="nginx"
652+ NGINX_DEFAULT_SERVER=/etc/nginx/sites-available/default
655+ NGINX_PACKAGES="nginx-mainline"
658+ NGINX_PACKAGES="nginx"
661+ err_msg "$DIST_ID-$DIST_VERS: nginx not yet implemented"
668+ info_msg "installing nginx ..."
669+ pkg_install "${NGINX_PACKAGES}"
670+ case $DIST_ID-$DIST_VERS in
672+ systemctl enable nginx
673+ systemctl start nginx
678+nginx_is_installed() {
679+ command -v nginx &>/dev/null
684+ info_msg "reload nginx .."
687+ err_msg "testing nginx configuration failed"
690+ systemctl restart nginx
693+nginx_install_app() {
695+ # usage: nginx_install_app [<template option> ...] <myapp>
697+ # <template option>: see install_template
699+ local template_opts=()
700+ local pos_args=("$0")
704+ -*) template_opts+=("$i");;
705+ *) pos_args+=("$i");;
709+ nginx_include_apps_enabled "${NGINX_DEFAULT_SERVER}"
711+ install_template "${template_opts[@]}" \
712+ "${NGINX_APPS_AVAILABLE}/${pos_args[1]}" \
714+ nginx_enable_app "${pos_args[1]}"
715+ info_msg "installed nginx app: ${pos_args[1]}"
718+nginx_include_apps_enabled() {
720+ # Add the *NGINX_APPS_ENABLED* infrastruture to a nginx server block. Such
721+ # infrastruture is already known from fedora, including apps (location
722+ # directives) from the /etc/nginx/default.d folder into the *default* nginx
725+ # usage: nginx_include_apps_enabled <config-file>
727+ # config-file: Config file with server directive in.
729+ [[ -z $1 ]] && die_caller 42 "missing argument <config-file>"
730+ local server_conf="$1"
732+ # include /etc/nginx/default.d/*.conf;
733+ local include_directive="include ${NGINX_APPS_ENABLED}/*.conf;"
734+ local include_directive_re="^\s*include ${NGINX_APPS_ENABLED}/\*\.conf;"
736+ info_msg "checking existence: '${include_directive}' in file ${server_conf}"
737+ if grep "${include_directive_re}" "${server_conf}"; then
738+ info_msg "OK, already exists."
742+ info_msg "add missing directive: '${include_directive}'"
743+ cp "${server_conf}" "${server_conf}.bak"
748+ while IFS= read -r line
751+ if [[ $stage = 0 ]]; then
752+ if [[ $line =~ ^[[:space:]]*server*[[:space:]]*\{ ]]; then
757+ if [[ $stage = 1 ]]; then
758+ echo " # Load configuration files for the default server block."
759+ echo " $include_directive"
763+ done < "${server_conf}.bak"
764+ ) > "${server_conf}"
770+ # usage: nginx_remove_app <myapp.conf>
772+ info_msg "remove nginx app: $1"
773+ nginx_dissable_app "$1"
774+ rm -f "${NGINX_APPS_AVAILABLE}/$1"
779+ # usage: nginx_enable_app <myapp.conf>
783+ info_msg "enable nginx app: ${CONF}"
784+ mkdir -p "${NGINX_APPS_ENABLED}"
785+ rm -f "${NGINX_APPS_ENABLED}/${CONF}"
786+ ln -s "${NGINX_APPS_AVAILABLE}/${CONF}" "${NGINX_APPS_ENABLED}/${CONF}"
790+nginx_dissable_app() {
792+ # usage: nginx_disable_app <myapp.conf>
796+ info_msg "disable nginx app: ${CONF}"
797+ rm -f "${NGINX_APPS_ENABLED}/${CONF}"
805+apache_distro_setup() {
806+ # shellcheck disable=SC2034
807+ case $DIST_ID-$DIST_VERS in
809+ # debian uses the /etc/apache2 path, while other distros use
810+ # the apache default at /etc/httpd
811+ APACHE_SITES_AVAILABLE="/etc/apache2/sites-available"
812+ APACHE_SITES_ENABLED="/etc/apache2/sites-enabled"
813+ APACHE_MODULES="/usr/lib/apache2/modules"
814+ APACHE_PACKAGES="apache2"
817+ APACHE_SITES_AVAILABLE="/etc/httpd/sites-available"
818+ APACHE_SITES_ENABLED="/etc/httpd/sites-enabled"
819+ APACHE_MODULES="modules"
820+ APACHE_PACKAGES="apache"
823+ APACHE_SITES_AVAILABLE="/etc/httpd/sites-available"
824+ APACHE_SITES_ENABLED="/etc/httpd/sites-enabled"
825+ APACHE_MODULES="modules"
826+ APACHE_PACKAGES="httpd"
829+ err_msg "$DIST_ID-$DIST_VERS: apache not yet implemented"
837+ info_msg "installing apache ..."
838+ pkg_install "$APACHE_PACKAGES"
839+ case $DIST_ID-$DIST_VERS in
841+ if ! grep "IncludeOptional sites-enabled" "/etc/httpd/conf/httpd.conf"; then
842+ echo "IncludeOptional sites-enabled/*.conf" >> "/etc/httpd/conf/httpd.conf"
844+ systemctl enable httpd
845+ systemctl start httpd
850+apache_is_installed() {
851+ case $DIST_ID-$DIST_VERS in
852+ ubuntu-*|debian-*) (command -v apachectl) &>/dev/null;;
853+ arch-*) (command -v httpd) &>/dev/null;;
854+ fedora-*) (command -v httpd) &>/dev/null;;
860+ info_msg "reload apache .."
862+ case $DIST_ID-$DIST_VERS in
864+ sudo -H apachectl configtest
865+ sudo -H systemctl force-reload apache2
869+ sudo -H systemctl force-reload httpd
874+apache_install_site() {
876+ # usage: apache_install_site [<template option> ...] <mysite.conf>
878+ # <template option>: see install_template
880+ local template_opts=()
881+ local pos_args=("$0")
885+ -*) template_opts+=("$i");;
886+ *) pos_args+=("$i");;
890+ install_template "${template_opts[@]}" \
891+ "${APACHE_SITES_AVAILABLE}/${pos_args[1]}" \
893+ apache_enable_site "${pos_args[1]}"
894+ info_msg "installed apache site: ${pos_args[1]}"
897+apache_remove_site() {
899+ # usage: apache_remove_site <mysite.conf>
901+ info_msg "remove apache site: $1"
902+ apache_dissable_site "$1"
903+ rm -f "${APACHE_SITES_AVAILABLE}/$1"
906+apache_enable_site() {
908+ # usage: apache_enable_site <mysite.conf>
912+ info_msg "enable apache site: ${CONF}"
914+ case $DIST_ID-$DIST_VERS in
916+ sudo -H a2ensite -q "${CONF}"
919+ mkdir -p "${APACHE_SITES_ENABLED}"
920+ rm -f "${APACHE_SITES_ENABLED}/${CONF}"
921+ ln -s "${APACHE_SITES_AVAILABLE}/${CONF}" "${APACHE_SITES_ENABLED}/${CONF}"
924+ mkdir -p "${APACHE_SITES_ENABLED}"
925+ rm -f "${APACHE_SITES_ENABLED}/${CONF}"
926+ ln -s "${APACHE_SITES_AVAILABLE}/${CONF}" "${APACHE_SITES_ENABLED}/${CONF}"
932+apache_dissable_site() {
934+ # usage: apache_disable_site <mysite.conf>
938+ info_msg "disable apache site: ${CONF}"
940+ case $DIST_ID-$DIST_VERS in
942+ sudo -H a2dissite -q "${CONF}"
945+ rm -f "${APACHE_SITES_ENABLED}/${CONF}"
948+ rm -f "${APACHE_SITES_ENABLED}/${CONF}"
957+uWSGI_SETUP="${uWSGI_SETUP:=/etc/uwsgi}"
961+# How distros manage uWSGI apps is very different. From uWSGI POV read:
962+# - https://uwsgi-docs.readthedocs.io/en/latest/Management.html
964+uWSGI_distro_setup() {
965+ case $DIST_ID-$DIST_VERS in
967+ # init.d --> /usr/share/doc/uwsgi/README.Debian.gz
968+ # For uWSGI debian uses the LSB init process, this might be changed
969+ # one day, see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=833067
970+ uWSGI_APPS_AVAILABLE="${uWSGI_SETUP}/apps-available"
971+ uWSGI_APPS_ENABLED="${uWSGI_SETUP}/apps-enabled"
972+ uWSGI_PACKAGES="uwsgi"
975+ # systemd --> /usr/lib/systemd/system/uwsgi@.service
976+ # For uWSGI archlinux uses systemd template units, see
977+ # - http://0pointer.de/blog/projects/instances.html
978+ # - https://uwsgi-docs.readthedocs.io/en/latest/Systemd.html#one-service-per-app-in-systemd
979+ uWSGI_APPS_AVAILABLE="${uWSGI_SETUP}/apps-archlinux"
980+ uWSGI_APPS_ENABLED="${uWSGI_SETUP}"
981+ uWSGI_PACKAGES="uwsgi"
984+ # systemd --> /usr/lib/systemd/system/uwsgi.service
985+ # The unit file starts uWSGI in emperor mode (/etc/uwsgi.ini), see
986+ # - https://uwsgi-docs.readthedocs.io/en/latest/Emperor.html
987+ uWSGI_APPS_AVAILABLE="${uWSGI_SETUP}/apps-available"
988+ uWSGI_APPS_ENABLED="${uWSGI_SETUP}.d"
989+ uWSGI_PACKAGES="uwsgi"
994+ err_msg "$DIST_ID-$DIST_VERS: uWSGI not yet implemented"
1002+ info_msg "installing uwsgi ..."
1003+ pkg_install "$uWSGI_PACKAGES"
1004+ case $DIST_ID-$DIST_VERS in
1006+ # enable & start should be called once at uWSGI installation time
1007+ systemctl enable uwsgi
1008+ systemctl restart uwsgi
1015+ # usage: uWSGI_restart() <myapp.ini>
1019+ [[ -z $CONF ]] && die_caller 42 "missing argument <myapp.ini>"
1020+ info_msg "restart uWSGI service"
1021+ case $DIST_ID-$DIST_VERS in
1023+ # the 'service' method seems broken in that way, that it (re-)starts
1024+ # the whole uwsgi process.
1025+ service uwsgi restart "${CONF%.*}"
1028+ # restart systemd template instance
1029+ if uWSGI_app_available "${CONF}"; then
1030+ systemctl restart "uwsgi@${CONF%.*}"
1032+ info_msg "[uWSGI:systemd-template] ${CONF} not installed (no need to restart)"
1036+ # in emperor mode, just touch the file to restart
1037+ if uWSGI_app_enabled "${CONF}"; then
1038+ touch "${uWSGI_APPS_ENABLED}/${CONF}"
1039+ # it seems, there is a polling time in between touch and restart
1043+ info_msg "[uWSGI:emperor] ${CONF} not installed (no need to restart)"
1047+ err_msg "$DIST_ID-$DIST_VERS: uWSGI not yet implemented"
1053+uWSGI_prepare_app() {
1055+ # usage: uWSGI_prepare_app <myapp.ini>
1057+ [[ -z $1 ]] && die_caller 42 "missing argument <myapp.ini>"
1059+ local APP="${1%.*}"
1061+ case $DIST_ID-$DIST_VERS in
1063+ # in emperor mode, the uwsgi user is the owner of the sockets
1064+ info_msg "prepare (uwsgi:uwsgi) /run/uwsgi/app/${APP}"
1065+ mkdir -p "/run/uwsgi/app/${APP}"
1066+ chown -R "uwsgi:uwsgi" "/run/uwsgi/app/${APP}"
1069+ info_msg "prepare (${SERVICE_USER}:${SERVICE_GROUP}) /run/uwsgi/app/${APP}"
1070+ mkdir -p "/run/uwsgi/app/${APP}"
1071+ chown -R "${SERVICE_USER}:${SERVICE_GROUP}" "/run/uwsgi/app/${APP}"
1077+uWSGI_app_available() {
1078+ # usage: uWSGI_app_available <myapp.ini>
1081+ [[ -z $CONF ]] && die_caller 42 "missing argument <myapp.ini>"
1082+ [[ -f "${uWSGI_APPS_AVAILABLE}/${CONF}" ]]
1085+uWSGI_install_app() {
1087+ # usage: uWSGI_install_app [<template option> ...] <myapp.ini>
1089+ # <template option>: see install_template
1091+ local pos_args=("$0")
1095+ -*) template_opts+=("$i");;
1096+ *) pos_args+=("$i");;
1099+ uWSGI_prepare_app "${pos_args[1]}"
1100+ mkdir -p "${uWSGI_APPS_AVAILABLE}"
1101+ install_template "${template_opts[@]}" \
1102+ "${uWSGI_APPS_AVAILABLE}/${pos_args[1]}" \
1104+ uWSGI_enable_app "${pos_args[1]}"
1105+ uWSGI_restart "${pos_args[1]}"
1106+ info_msg "uWSGI app: ${pos_args[1]} is installed"
1109+uWSGI_remove_app() {
1111+ # usage: uWSGI_remove_app <myapp.ini>
1115+ [[ -z $CONF ]] && die_caller 42 "missing argument <myapp.ini>"
1116+ info_msg "remove uWSGI app: ${CONF}"
1117+ uWSGI_disable_app "${CONF}"
1118+ uWSGI_restart "${CONF}"
1119+ rm -f "${uWSGI_APPS_AVAILABLE}/${CONF}"
1122+uWSGI_app_enabled() {
1123+ # usage: uWSGI_app_enabled <myapp.ini>
1128+ [[ -z $CONF ]] && die_caller 42 "missing argument <myapp.ini>"
1129+ case $DIST_ID-$DIST_VERS in
1131+ [[ -f "${uWSGI_APPS_ENABLED}/${CONF}" ]]
1135+ systemctl -q is-enabled "uwsgi@${CONF%.*}"
1139+ [[ -f "${uWSGI_APPS_ENABLED}/${CONF}" ]]
1144+ err_msg "$DIST_ID-$DIST_VERS: uWSGI not yet implemented"
1151+# shellcheck disable=SC2164
1152+uWSGI_enable_app() {
1154+ # usage: uWSGI_enable_app <myapp.ini>
1158+ [[ -z $CONF ]] && die_caller 42 "missing argument <myapp.ini>"
1159+ case $DIST_ID-$DIST_VERS in
1161+ mkdir -p "${uWSGI_APPS_ENABLED}"
1162+ rm -f "${uWSGI_APPS_ENABLED}/${CONF}"
1163+ ln -s "${uWSGI_APPS_AVAILABLE}/${CONF}" "${uWSGI_APPS_ENABLED}/${CONF}"
1164+ info_msg "enabled uWSGI app: ${CONF} (restart required)"
1167+ mkdir -p "${uWSGI_APPS_ENABLED}"
1168+ rm -f "${uWSGI_APPS_ENABLED}/${CONF}"
1169+ ln -s "${uWSGI_APPS_AVAILABLE}/${CONF}" "${uWSGI_APPS_ENABLED}/${CONF}"
1170+ systemctl enable "uwsgi@${CONF%.*}"
1171+ info_msg "enabled uWSGI app: ${CONF} (restart required)"
1174+ mkdir -p "${uWSGI_APPS_ENABLED}"
1175+ rm -f "${uWSGI_APPS_ENABLED}/${CONF}"
1176+ ln -s "${uWSGI_APPS_AVAILABLE}/${CONF}" "${uWSGI_APPS_ENABLED}/${CONF}"
1177+ chown "${uWSGI_USER}:${uWSGI_GROUP}" "${uWSGI_APPS_ENABLED}/${CONF}"
1178+ info_msg "enabled uWSGI app: ${CONF}"
1182+ err_msg "$DIST_ID-$DIST_VERS: uWSGI not yet implemented"
1187+uWSGI_disable_app() {
1189+ # usage: uWSGI_disable_app <myapp.ini>
1193+ [[ -z $CONF ]] && die_caller 42 "missing argument <myapp.ini>"
1194+ case $DIST_ID-$DIST_VERS in
1196+ service uwsgi stop "${CONF%.*}"
1197+ rm -f "${uWSGI_APPS_ENABLED}/${CONF}"
1198+ info_msg "disabled uWSGI app: ${CONF} (restart uWSGI required)"
1201+ systemctl stop "uwsgi@${CONF%.*}"
1202+ systemctl disable "uwsgi@${CONF%.*}"
1203+ rm -f "${uWSGI_APPS_ENABLED}/${CONF}"
1206+ # in emperor mode, just remove the app.ini file
1207+ rm -f "${uWSGI_APPS_ENABLED}/${CONF}"
1211+ err_msg "$DIST_ID-$DIST_VERS: uWSGI not yet implemented"
1216+# distro's package manager
1217+# ------------------------
1219+_apt_pkg_info_is_updated=0
1223+ # usage: TITEL='install foobar' pkg_install foopkg barpkg
1225+ rst_title "${TITLE:-installation of packages}" section
1226+ echo -e "\npackage(s)::\n"
1227+ # shellcheck disable=SC2068
1230+ if ! ask_yn "Should packages be installed?" Yn 30; then
1235+ if [[ $_apt_pkg_info_is_updated == 0 ]]; then
1236+ export _apt_pkg_info_is_updated=1
1239+ # shellcheck disable=SC2068
1240+ apt-get install -m -y $@
1243+ # shellcheck disable=SC2068
1244+ pacman -Sy --noconfirm $@
1247+ # shellcheck disable=SC2068
1255+ # usage: TITEL='remove foobar' pkg_remove foopkg barpkg
1257+ rst_title "${TITLE:-remove packages}" section
1258+ echo -e "\npackage(s)::\n"
1259+ # shellcheck disable=SC2068
1262+ if ! ask_yn "Should packages be removed (purge)?" Yn 30; then
1267+ # shellcheck disable=SC2068
1268+ apt-get purge --autoremove --ignore-missing -y $@
1271+ # shellcheck disable=SC2068
1272+ pacman -R --noconfirm $@
1275+ # shellcheck disable=SC2068
1281+pkg_is_installed() {
1283+ # usage: pkg_is_install foopkg || pkg_install foopkg
1287+ dpkg -l "$1" &> /dev/null
1291+ pacman -Qsq "$1" &> /dev/null
1295+ dnf list -q --installed "$1" &> /dev/null
1304+# shellcheck disable=SC2164
1309+ # git_clone <url> <name> [<branch> [<user>]]
1310+ # git_clone <url> <path> [<branch> [<user>]]
1312+ # First form uses $CACHE/<name> as destination folder, second form clones
1313+ # into <path>. If repository is allready cloned, pull from <branch> and
1314+ # update working tree (if needed, the caller has to stash local changes).
1316+ # git clone https://github.com/asciimoo/searx searx-src origin/master searxlogin
1323+ local bash_cmd="bash"
1324+ local remote="origin"
1326+ if [[ ! "${dest:0:1}" = "/" ]]; then
1327+ dest="$CACHE/$dest"
1330+ [[ -z $branch ]] && branch=master
1331+ [[ -z $user ]] && [[ -n "${SUDO_USER}" ]] && user="${SUDO_USER}"
1332+ [[ -n $user ]] && bash_cmd="sudo -H -u $user -i"
1334+ if [[ -d "${dest}" ]] ; then
1335+ info_msg "already cloned: $dest"
1336+ tee_stderr 0.1 <<EOF | $bash_cmd 2>&1 | prefix_stdout " ${_Yellow}|$user|${_creset} "
1338+git checkout -m -B "$branch" --track "$remote/$branch"
1342+ info_msg "clone into: $dest"
1343+ tee_stderr 0.1 <<EOF | $bash_cmd 2>&1 | prefix_stdout " ${_Yellow}|$user|${_creset} "
1344+mkdir -p "$(dirname "$dest")"
1345+cd "$(dirname "$dest")"
1346+git clone --branch "$branch" --origin "$remote" "$url" "$(basename "$dest")"
1355+ # Test if shell runs in a container.
1357+ # usage: in_container && echo "process running inside a LXC container"
1358+ # in_container || echo "process is not running inside a LXC container"
1361+ # hint: Reads init process environment, therefore root access is required!
1362+ # to be safe, take a look at the environment of process 1 (/sbin/init)
1363+ # grep -qa 'container=lxc' /proc/1/environ
1365+ # see lxc_init_container_env
1370+if in_container; then
1371+ # shellcheck disable=SC2034
1372+ LXC_ENV_FOLDER="lxc/$(hostname)/"
1375+lxc_init_container_env() {
1377+ # usage: lxc_init_container_env <name>
1379+ # Create a /.lxcenv file in the root folder. Call this once after the
1380+ # container is inital started and before installing any boilerplate stuff.
1382+ info_msg "create /.lxcenv in container $1"
1383+ cat <<EOF | lxc exec "${1}" -- bash | prefix_stdout "[${_BBlue}${1}${_creset}] "
1390+LXC_BASE_PACKAGES_debian="bash git build-essential python3 virtualenv"
1393+LXC_BASE_PACKAGES_arch="bash git base-devel python python-virtualenv"
1396+LXC_BASE_PACKAGES_fedora="bash git @development-tools python virtualenv"
1399+ ubuntu|debian) LXC_BASE_PACKAGES="${LXC_BASE_PACKAGES_debian}" ;;
1400+ arch) LXC_BASE_PACKAGES="${LXC_BASE_PACKAGES_arch}" ;;
1401+ fedora) LXC_BASE_PACKAGES="${LXC_BASE_PACKAGES_fedora}" ;;
1402+ *) err_msg "$DIST_ID-$DIST_VERS: pkg_install LXC_BASE_PACKAGES not yet implemented" ;;
1405+lxc_install_base_packages() {
1406+ info_msg "install LXC_BASE_PACKAGES in container $1"
1407+ pkg_install "${LXC_BASE_PACKAGES}"
1413+ # usage: lxc_copy_image <remote image> <local image>
1415+ # lxc_copy_image "images:ubuntu/19.10" "ubu1910"
1417+ if lxc_image_exists "local:${LXC_SUITE[i+1]}"; then
1418+ info_msg "image ${LXC_SUITE[i]} already copied --> ${LXC_SUITE[i+1]}"
1420+ info_msg "copy image locally ${LXC_SUITE[i]} --> ${LXC_SUITE[i+1]}"
1421+ lxc image copy "${LXC_SUITE[i]}" local: \
1422+ --alias "${LXC_SUITE[i+1]}" | prefix_stdout
1426+lxc_init_container() {
1428+ # usage: lxc_init_container <image name> <container name>
1430+ local image_name="$1"
1431+ local container_name="$2"
1433+ if lxc info "${container_name}" &>/dev/null; then
1434+ info_msg "container '${container_name}' already exists"
1436+ info_msg "create container instance: ${container_name}"
1437+ lxc init "local:${image_name}" "${container_name}"
1443+ # usage: lxc_exists <name> || echo "container <name> does not exists"
1445+ lxc info "$1" &>/dev/null
1449+ # usage: lxc_image_exists <alias> || echo "image <alias> does locally not exists"
1451+ lxc image info "local:$1" &>/dev/null
1455+lxc_delete_container() {
1457+ # usage: lxc_delete_container <container-name>
1459+ if lxc info "$1" &>/dev/null; then
1460+ info_msg "stop & delete instance ${_BBlue}${1}${_creset}"
1461+ lxc stop "$1" &>/dev/null
1462+ lxc delete "$1" | prefix_stdout
1464+ warn_msg "instance '$1' does not exist / can't delete :o"
1468+lxc_delete_local_image() {
1470+ # usage: lxc_delete_local_image <container-name>
1472+ info_msg "delete image 'local:$i'"
1473+ lxc image delete "local:$i"
1481+ # usage: global_IPS
1483+ # print list of host's SCOPE global addresses and adapters e.g::
1486+ # enp4s0|192.168.1.127
1487+ # lxdbr0|10.246.86.1
1488+ # lxdbr0|fd42:8c58:2cd:b73f::1
1490+ ip -o addr show | sed -nr 's/[0-9]*:\s*([a-z0-9]*).*inet[6]?\s*([a-z0-9.:]*).*scope global.*/\1|\2/p'
1498+ | sed -nr 's/[0-9]*:\s*([a-z0-9]*).*inet[6]?\s*([a-z0-9.:]*).*scope global.*/\2/p' \
1501+ *) hostname -I | cut -d' ' -f1 ;;
1508+url_replace_hostname(){
1510+ # usage: url_replace_hostname <url> <new hostname>
1512+ # to replace hostname by primary IP::
1514+ # url_replace_hostname http://searx-ubu1604/morty $(primary_ip)
1515+ # http://10.246.86.250/morty
1517+ # shellcheck disable=SC2001
1518+ echo "$1" | sed "s|\(http[s]*://\)[^/]*\(.*\)|\1$2\2|"