2020-01-08 17:13:22 +00:00
|
|
|
#!/usr/bin/env bash
|
2020-01-16 13:01:38 +00:00
|
|
|
# -*- coding: utf-8; mode: sh indent-tabs-mode: nil -*-
|
2020-01-29 19:00:50 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
# shellcheck disable=SC2059,SC1117
|
2020-01-08 17:13:22 +00:00
|
|
|
|
2020-01-14 18:26:54 +00:00
|
|
|
ADMIN_NAME="${ADMIN_NAME:-$(git config user.name)}"
|
|
|
|
ADMIN_NAME="${ADMIN_NAME:-$USER}"
|
|
|
|
|
|
|
|
ADMIN_EMAIL="${ADMIN_EMAIL:-$(git config user.email)}"
|
|
|
|
ADMIN_EMAIL="${ADMIN_EMAIL:-$USER@$(hostname)}"
|
|
|
|
|
2020-01-08 17:13:22 +00:00
|
|
|
if [[ -z "${REPO_ROOT}" ]]; then
|
|
|
|
REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")
|
|
|
|
while [ -h "${REPO_ROOT}" ] ; do
|
|
|
|
REPO_ROOT=$(readlink "${REPO_ROOT}")
|
|
|
|
done
|
|
|
|
REPO_ROOT=$(cd "${REPO_ROOT}/.." && pwd -P )
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -z ${TEMPLATES} ]]; then
|
|
|
|
TEMPLATES="${REPO_ROOT}/utils/templates"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -z "$CACHE" ]]; then
|
|
|
|
CACHE="${REPO_ROOT}/cache"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -z "$SYSTEMD_UNITS" ]]; then
|
2020-01-08 18:21:07 +00:00
|
|
|
SYSTEMD_UNITS="/lib/systemd/system"
|
2020-01-08 17:13:22 +00:00
|
|
|
fi
|
|
|
|
|
2020-01-09 15:25:05 +00:00
|
|
|
if [[ -z ${DIFF_CMD} ]]; then
|
|
|
|
DIFF_CMD="diff -u"
|
|
|
|
if command -v colordiff >/dev/null; then
|
|
|
|
DIFF_CMD="colordiff -u"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2020-01-29 19:00:50 +00:00
|
|
|
DOT_CONFIG="${DOT_CONFIG:-${REPO_ROOT}/.config}"
|
|
|
|
|
|
|
|
source_dot_config() {
|
|
|
|
if [[ ! -e "$DOT_CONFIG" ]]; then
|
|
|
|
info_msg "installing $DOT_CONFIG"
|
|
|
|
cp "$(dirname "${BASH_SOURCE[0]}")/dot_config" "$DOT_CONFIG"
|
|
|
|
if [[ ! -z ${SUDO_USER} ]]; then
|
|
|
|
chown "${SUDO_USER}:${SUDO_USER}" "$DOT_CONFIG"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
# shellcheck disable=SC1090
|
|
|
|
source "${REPO_ROOT}/.config"
|
|
|
|
}
|
|
|
|
|
2020-01-08 17:13:22 +00:00
|
|
|
sudo_or_exit() {
|
|
|
|
# usage: sudo_or_exit
|
|
|
|
|
|
|
|
if [ ! "$(id -u)" -eq 0 ]; then
|
|
|
|
err_msg "this command requires root (sudo) privilege!" >&2
|
|
|
|
exit 42
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-01-29 19:00:50 +00:00
|
|
|
required_commands() {
|
|
|
|
|
|
|
|
# usage: requires_commands [cmd1 ...]
|
|
|
|
|
|
|
|
local exit_val=0
|
|
|
|
while [ ! -z "$1" ]; do
|
|
|
|
|
|
|
|
if ! command -v "$1" &>/dev/null; then
|
|
|
|
err_msg "missing command $1"
|
|
|
|
exit_val=42
|
|
|
|
fi
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
return $exit_val
|
|
|
|
}
|
|
|
|
|
2020-01-08 17:13:22 +00:00
|
|
|
rst_title() {
|
|
|
|
# usage: rst_title <header-text> [part|chapter|section]
|
|
|
|
|
|
|
|
case ${2-chapter} in
|
|
|
|
part) printf "\n${1//?/=}\n$1\n${1//?/=}\n";;
|
|
|
|
chapter) printf "\n${1}\n${1//?/=}\n";;
|
|
|
|
section) printf "\n${1}\n${1//?/-}\n";;
|
|
|
|
*)
|
|
|
|
err_msg "invalid argument '${2}' in line $(caller)"
|
|
|
|
return 42
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
if command -v fmt >/dev/null; then
|
|
|
|
export FMT="fmt -u"
|
|
|
|
else
|
|
|
|
export FMT="cat"
|
|
|
|
fi
|
|
|
|
|
|
|
|
rst_para() {
|
|
|
|
# usage: RST_INDENT=1 rst_para "lorem ipsum ..."
|
|
|
|
local prefix=''
|
|
|
|
if ! [[ -z $RST_INDENT ]] && [[ $RST_INDENT -gt 0 ]]; then
|
2020-01-09 15:25:05 +00:00
|
|
|
prefix="$(for i in $(seq 1 "$RST_INDENT"); do printf " "; done)"
|
|
|
|
echo -en "\n$*\n" | $FMT | prefix_stdout "$prefix"
|
2020-01-08 17:13:22 +00:00
|
|
|
else
|
2020-01-09 15:25:05 +00:00
|
|
|
echo -en "\n$*\n" | $FMT
|
2020-01-08 17:13:22 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
err_msg() { echo -e "ERROR: $*" >&2; }
|
|
|
|
warn_msg() { echo -e "WARN: $*" >&2; }
|
|
|
|
info_msg() { echo -e "INFO: $*"; }
|
|
|
|
|
2020-01-09 15:25:05 +00:00
|
|
|
clean_stdin() {
|
|
|
|
if [[ $(uname -s) != 'Darwin' ]]; then
|
2020-01-29 19:00:50 +00:00
|
|
|
while read -r -n1 -t 0.1; do : ; done
|
2020-01-09 15:25:05 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-01-08 17:13:22 +00:00
|
|
|
wait_key(){
|
|
|
|
# usage: waitKEY [<timeout in sec>]
|
|
|
|
|
2020-01-09 15:25:05 +00:00
|
|
|
clean_stdin
|
2020-01-08 17:13:22 +00:00
|
|
|
local _t=$1
|
2020-01-30 18:55:51 +00:00
|
|
|
local msg="${MSG:-** press any [KEY] to continue **}"
|
2020-01-08 17:13:22 +00:00
|
|
|
[[ ! -z $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
|
|
|
|
[[ ! -z $_t ]] && _t="-t $_t"
|
|
|
|
# shellcheck disable=SC2086
|
2020-01-30 18:55:51 +00:00
|
|
|
read -r -s -n1 $_t -p "$msg"
|
2020-01-08 17:13:22 +00:00
|
|
|
echo
|
2020-01-09 15:25:05 +00:00
|
|
|
clean_stdin
|
2020-01-08 17:13:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ask_yn() {
|
|
|
|
# usage: ask_yn <prompt-text> [Ny|Yn] [<timeout in sec>]
|
|
|
|
|
|
|
|
local EXIT_YES=0 # exit status 0 --> successful
|
|
|
|
local EXIT_NO=1 # exit status 1 --> error code
|
|
|
|
|
|
|
|
local _t=$3
|
|
|
|
[[ ! -z $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
|
|
|
|
[[ ! -z $_t ]] && _t="-t $_t"
|
|
|
|
case "${2}" in
|
|
|
|
Yn)
|
|
|
|
local exit_val=${EXIT_YES}
|
|
|
|
local choice="[YES/no]"
|
|
|
|
local default="Yes"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
local exit_val=${EXIT_NO}
|
|
|
|
local choice="[NO/yes]"
|
|
|
|
local default="No"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
echo
|
|
|
|
while true; do
|
2020-01-14 18:26:54 +00:00
|
|
|
clean_stdin
|
2020-01-08 17:13:22 +00:00
|
|
|
printf "$1 ${choice} "
|
|
|
|
# shellcheck disable=SC2086
|
2020-01-29 19:00:50 +00:00
|
|
|
read -r -n1 $_t
|
2020-01-08 17:13:22 +00:00
|
|
|
if [[ -z $REPLY ]]; then
|
|
|
|
printf "$default\n"; break
|
|
|
|
elif [[ $REPLY =~ ^[Yy]$ ]]; then
|
|
|
|
exit_val=${EXIT_YES}
|
|
|
|
printf "\n"
|
|
|
|
break
|
|
|
|
elif [[ $REPLY =~ ^[Nn]$ ]]; then
|
|
|
|
exit_val=${EXIT_NO}
|
|
|
|
printf "\n"
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
_t=""
|
|
|
|
err_msg "invalid choice"
|
|
|
|
done
|
2020-01-09 15:25:05 +00:00
|
|
|
clean_stdin
|
2020-01-08 17:13:22 +00:00
|
|
|
return $exit_val
|
|
|
|
}
|
|
|
|
|
|
|
|
tee_stderr () {
|
|
|
|
|
|
|
|
# usage::
|
|
|
|
# tee_stderr 1 <<EOF | python -i
|
|
|
|
# print("hello")
|
|
|
|
# EOF
|
|
|
|
# ...
|
|
|
|
# >>> print("hello")
|
|
|
|
# hello
|
|
|
|
|
|
|
|
local _t="0";
|
|
|
|
if [[ ! -z $1 ]] ; then _t="$1"; fi
|
|
|
|
|
2020-01-29 19:00:50 +00:00
|
|
|
(while read -r line; do
|
2020-01-08 17:13:22 +00:00
|
|
|
# shellcheck disable=SC2086
|
|
|
|
sleep $_t
|
|
|
|
echo -e "$line" >&2
|
|
|
|
echo "$line"
|
|
|
|
done)
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix_stdout () {
|
|
|
|
# usage: <cmd> | prefix_stdout [prefix]
|
|
|
|
|
2020-01-09 15:25:05 +00:00
|
|
|
local prefix=" | "
|
2020-01-08 17:13:22 +00:00
|
|
|
|
|
|
|
if [[ ! -z $1 ]] ; then prefix="$1"; fi
|
|
|
|
|
2020-01-29 19:00:50 +00:00
|
|
|
# shellcheck disable=SC2162
|
2020-01-08 17:13:22 +00:00
|
|
|
(while IFS= read line; do
|
|
|
|
echo -e "${prefix}$line"
|
|
|
|
done)
|
|
|
|
}
|
|
|
|
|
|
|
|
append_line() {
|
|
|
|
|
|
|
|
# usage: append_line <line> <file>
|
|
|
|
#
|
|
|
|
# Append line if not exists, create file if not exists. E.g::
|
|
|
|
#
|
|
|
|
# append_line 'source ~/.foo' ~/bashrc
|
|
|
|
|
|
|
|
local LINE=$1
|
|
|
|
local FILE=$2
|
|
|
|
grep -qFs -- "$LINE" "$FILE" || echo "$LINE" >> "$FILE"
|
|
|
|
}
|
|
|
|
|
|
|
|
cache_download() {
|
|
|
|
|
|
|
|
# usage: cache_download <url> <local-filename>
|
|
|
|
|
|
|
|
local exit_value=0
|
|
|
|
|
|
|
|
if [[ ! -z ${SUDO_USER} ]]; then
|
|
|
|
sudo -u "${SUDO_USER}" mkdir -p "${CACHE}"
|
|
|
|
else
|
|
|
|
mkdir -p "${CACHE}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -f "${CACHE}/$2" ]] ; then
|
|
|
|
info_msg "already cached: $1"
|
|
|
|
info_msg " --> ${CACHE}/$2"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ ! -f "${CACHE}/$2" ]]; then
|
|
|
|
info_msg "caching: $1"
|
|
|
|
info_msg " --> ${CACHE}/$2"
|
|
|
|
if [[ ! -z ${SUDO_USER} ]]; then
|
|
|
|
sudo -u "${SUDO_USER}" wget --progress=bar -O "${CACHE}/$2" "$1" ; exit_value=$?
|
|
|
|
else
|
|
|
|
wget --progress=bar -O "${CACHE}/$2" "$1" ; exit_value=$?
|
|
|
|
fi
|
2020-01-29 19:00:50 +00:00
|
|
|
if [[ $exit_value = 0 ]]; then
|
2020-01-08 17:13:22 +00:00
|
|
|
err_msg "failed to download: $1"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
choose_one() {
|
|
|
|
|
|
|
|
# usage:
|
|
|
|
#
|
|
|
|
# DEFAULT_SELECT= 2 \
|
|
|
|
# choose_one <name> "your selection?" "Coffee" "Coffee with milk"
|
|
|
|
|
|
|
|
local default=${DEFAULT_SELECT-1}
|
|
|
|
local REPLY
|
|
|
|
local env_name=$1 && shift
|
|
|
|
local choice=$1;
|
|
|
|
local max="${#@}"
|
|
|
|
local _t
|
|
|
|
[[ ! -z $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
|
|
|
|
[[ ! -z $_t ]] && _t="-t $_t"
|
|
|
|
|
|
|
|
list=("$@")
|
|
|
|
echo -e "Menu::"
|
2020-01-29 19:00:50 +00:00
|
|
|
for ((i=1; i<= $((max -1)); i++)); do
|
2020-01-08 17:13:22 +00:00
|
|
|
if [[ "$i" == "$default" ]]; then
|
|
|
|
echo -e " $i.) ${list[$i]} [default]"
|
|
|
|
else
|
|
|
|
echo -e " $i.) ${list[$i]}"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
while true; do
|
2020-01-14 18:26:54 +00:00
|
|
|
clean_stdin
|
2020-01-08 17:13:22 +00:00
|
|
|
printf "$1 [$default] "
|
|
|
|
|
2020-01-29 19:00:50 +00:00
|
|
|
if (( 10 > max )); then
|
2020-01-08 17:13:22 +00:00
|
|
|
# shellcheck disable=SC2086
|
2020-01-29 19:00:50 +00:00
|
|
|
read -r -n1 $_t
|
2020-01-08 17:13:22 +00:00
|
|
|
else
|
|
|
|
# shellcheck disable=SC2086,SC2229
|
2020-01-29 19:00:50 +00:00
|
|
|
read -r $_t
|
2020-01-08 17:13:22 +00:00
|
|
|
fi
|
|
|
|
# selection fits
|
2020-01-29 19:00:50 +00:00
|
|
|
[[ $REPLY =~ ^-?[0-9]+$ ]] && (( REPLY > 0 )) && (( REPLY < max )) && break
|
2020-01-08 17:13:22 +00:00
|
|
|
|
|
|
|
# take default
|
|
|
|
[[ -z $REPLY ]] && REPLY=$default && break
|
|
|
|
|
|
|
|
_t=""
|
|
|
|
err_msg "invalid choice"
|
|
|
|
done
|
2020-01-16 13:01:38 +00:00
|
|
|
eval "$env_name"='${list[${REPLY}]}'
|
2020-01-08 17:13:22 +00:00
|
|
|
echo
|
2020-01-09 15:25:05 +00:00
|
|
|
clean_stdin
|
2020-01-08 17:13:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
install_template() {
|
|
|
|
|
|
|
|
# usage:
|
|
|
|
#
|
2020-01-21 17:38:57 +00:00
|
|
|
# install_template [--no-eval] [--variant=<name>] \
|
|
|
|
# {file} [{owner} [{group} [{chmod}]]]
|
2020-01-08 17:13:22 +00:00
|
|
|
#
|
2020-01-21 17:38:57 +00:00
|
|
|
# E.g. the origin of variant 'raw' of /etc/updatedb.conf is::
|
|
|
|
#
|
|
|
|
# ${TEMPLATES}/etc/updatedb.conf:raw
|
|
|
|
#
|
|
|
|
# To install variant 'raw' of /etc/updatedb.conf without evaluated
|
|
|
|
# replacements you can use::
|
|
|
|
#
|
|
|
|
# install_template --variant=raw --no-eval \
|
|
|
|
# /etc/updatedb.conf root root 644
|
2020-01-08 17:13:22 +00:00
|
|
|
|
|
|
|
local _reply=""
|
2020-01-21 17:38:57 +00:00
|
|
|
local do_eval=1
|
|
|
|
local variant=""
|
|
|
|
local pos_args=("$0")
|
|
|
|
|
|
|
|
for i in "$@"; do
|
|
|
|
case $i in
|
|
|
|
--no-eval) do_eval=0; shift ;;
|
|
|
|
--variant=*) variant=":${i#*=}"; shift ;;
|
|
|
|
*) pos_args+=("$i") ;;
|
|
|
|
esac
|
|
|
|
done
|
2020-01-08 17:13:22 +00:00
|
|
|
|
2020-01-21 17:38:57 +00:00
|
|
|
local dst="${pos_args[1]}"
|
|
|
|
local template_origin="${TEMPLATES}${dst}${variant}"
|
|
|
|
local template_file="${TEMPLATES}${dst}"
|
2020-01-08 17:13:22 +00:00
|
|
|
|
2020-01-21 17:38:57 +00:00
|
|
|
local owner="${pos_args[2]-$(id -un)}"
|
|
|
|
local group="${pos_args[3]-$(id -gn)}"
|
|
|
|
local chmod="${pos_args[4]-644}"
|
|
|
|
|
|
|
|
info_msg "install (eval=$do_eval): ${dst}"
|
|
|
|
[[ ! -z $variant ]] && info_msg "variant: ${variant}"
|
|
|
|
|
|
|
|
if [[ ! -f "${template_origin}" ]] ; then
|
|
|
|
err_msg "${template_origin} does not exists"
|
|
|
|
err_msg "... can't install $dst"
|
2020-01-08 17:13:22 +00:00
|
|
|
wait_key 30
|
|
|
|
return 42
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "$do_eval" == "1" ]]; then
|
2020-01-21 17:38:57 +00:00
|
|
|
template_file="${CACHE}${dst}${variant}"
|
2020-01-08 17:13:22 +00:00
|
|
|
info_msg "BUILD template ${template_file}"
|
2020-01-21 17:38:57 +00:00
|
|
|
if [[ ! -z ${SUDO_USER} ]]; then
|
|
|
|
sudo -u "${SUDO_USER}" mkdir -p "$(dirname "${template_file}")"
|
2020-01-08 17:13:22 +00:00
|
|
|
else
|
2020-01-21 17:38:57 +00:00
|
|
|
mkdir -p "$(dirname "${template_file}")"
|
2020-01-08 17:13:22 +00:00
|
|
|
fi
|
2020-01-21 17:38:57 +00:00
|
|
|
# shellcheck disable=SC2086
|
|
|
|
eval "echo \"$(cat ${template_origin})\"" > "${template_file}"
|
|
|
|
if [[ ! -z ${SUDO_USER} ]]; then
|
|
|
|
chown "${SUDO_USER}:${SUDO_USER}" "${template_file}"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
template_file=$template_origin
|
2020-01-08 17:13:22 +00:00
|
|
|
fi
|
|
|
|
|
2020-01-08 18:21:07 +00:00
|
|
|
mkdir -p "$(dirname "${dst}")"
|
|
|
|
|
2020-01-09 15:25:05 +00:00
|
|
|
if [[ ! -f "${dst}" ]]; then
|
|
|
|
info_msg "install: ${template_file}"
|
|
|
|
sudo -H install -v -o "${owner}" -g "${group}" -m "${chmod}" \
|
|
|
|
"${template_file}" "${dst}" | prefix_stdout
|
|
|
|
return $?
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -f "${dst}" ]] && cmp --silent "${template_file}" "${dst}" ; then
|
|
|
|
info_msg "file ${dst} allready installed"
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
2020-01-21 17:38:57 +00:00
|
|
|
info_msg "diffrent file ${dst} allready exists on this host"
|
2020-01-09 15:25:05 +00:00
|
|
|
|
|
|
|
while true; do
|
2020-01-08 17:13:22 +00:00
|
|
|
choose_one _reply "choose next step with file $dst" \
|
|
|
|
"replace file" \
|
2020-01-09 15:25:05 +00:00
|
|
|
"leave file unchanged" \
|
|
|
|
"interactiv shell" \
|
|
|
|
"diff files"
|
2020-01-08 17:13:22 +00:00
|
|
|
|
|
|
|
case $_reply in
|
|
|
|
"replace file")
|
|
|
|
info_msg "install: ${template_file}"
|
|
|
|
sudo -H install -v -o "${owner}" -g "${group}" -m "${chmod}" \
|
2020-01-08 18:21:07 +00:00
|
|
|
"${template_file}" "${dst}" | prefix_stdout
|
2020-01-14 18:26:54 +00:00
|
|
|
break
|
2020-01-08 17:13:22 +00:00
|
|
|
;;
|
|
|
|
"leave file unchanged")
|
2020-01-09 15:25:05 +00:00
|
|
|
break
|
2020-01-08 17:13:22 +00:00
|
|
|
;;
|
|
|
|
"interactiv shell")
|
2020-01-09 15:25:05 +00:00
|
|
|
echo "// edit ${dst} to your needs"
|
|
|
|
echo "// exit with CTRL-D"
|
2020-01-08 17:13:22 +00:00
|
|
|
sudo -H -u "${owner}" -i
|
2020-01-09 15:25:05 +00:00
|
|
|
$DIFF_CMD "${dst}" "${template_file}"
|
2020-01-21 17:38:57 +00:00
|
|
|
echo
|
|
|
|
echo "did you edit file ..."
|
|
|
|
printf " ${template_file}"
|
|
|
|
if ask_yn "... to your needs?"; then
|
2020-01-14 18:26:54 +00:00
|
|
|
break
|
|
|
|
fi
|
2020-01-09 15:25:05 +00:00
|
|
|
;;
|
|
|
|
"diff files")
|
|
|
|
$DIFF_CMD "${dst}" "${template_file}" | prefix_stdout
|
2020-01-08 17:13:22 +00:00
|
|
|
esac
|
2020-01-09 15:25:05 +00:00
|
|
|
done
|
2020-01-08 17:13:22 +00:00
|
|
|
}
|
2020-01-14 18:26:54 +00:00
|
|
|
|
2020-01-29 19:00:50 +00:00
|
|
|
|
|
|
|
service_is_available() {
|
|
|
|
|
|
|
|
# usage: service_is_available <URL>
|
|
|
|
|
|
|
|
local URL="$1"
|
|
|
|
if [[ -z $URL ]]; then
|
|
|
|
err_msg "service_is_available: missing arguments"
|
|
|
|
return 42
|
|
|
|
fi
|
|
|
|
|
|
|
|
http_code=$(curl -H 'Cache-Control: no-cache' \
|
|
|
|
--silent -o /dev/null --head --write-out '%{http_code}' --insecure \
|
|
|
|
"${URL}")
|
|
|
|
exit_val=$?
|
|
|
|
if [[ $exit_val = 0 ]]; then
|
|
|
|
info_msg "got $http_code from ${URL}"
|
|
|
|
fi
|
2020-01-30 18:55:51 +00:00
|
|
|
case "$http_code" in
|
|
|
|
404|410|423) exit_val=$http_code;;
|
|
|
|
esac
|
2020-01-31 14:53:53 +00:00
|
|
|
return "$exit_val"
|
2020-01-29 19:00:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-20 18:08:56 +00:00
|
|
|
# Apache
|
|
|
|
# ------
|
|
|
|
|
|
|
|
# FIXME: Arch Linux & RHEL should be added
|
|
|
|
|
|
|
|
if [[ -z "${APACHE_SITES_AVAILABE}" ]]; then
|
|
|
|
APACHE_SITES_AVAILABE="/etc/apache2/sites-available"
|
|
|
|
fi
|
|
|
|
|
|
|
|
apache_is_installed() {
|
|
|
|
(command -v apachectl \
|
|
|
|
&& command -v a2ensite \
|
|
|
|
&& command -v a2dissite ) &>/dev/null
|
|
|
|
}
|
|
|
|
|
|
|
|
apache_reload() {
|
|
|
|
|
|
|
|
info_msg "reload apache .."
|
|
|
|
echo
|
|
|
|
sudo -H apachectl configtest
|
|
|
|
sudo -H service apache2 force-reload
|
|
|
|
}
|
|
|
|
|
|
|
|
apache_install_site() {
|
|
|
|
|
2020-01-21 17:38:57 +00:00
|
|
|
# usage: apache_install_site [<template option> ...] <mysite.conf>
|
|
|
|
#
|
|
|
|
# <template option>: see install_template
|
2020-01-20 18:08:56 +00:00
|
|
|
|
2020-01-21 17:38:57 +00:00
|
|
|
local template_opts=()
|
|
|
|
local pos_args=("$0")
|
2020-01-20 18:08:56 +00:00
|
|
|
|
2020-01-21 17:38:57 +00:00
|
|
|
for i in "$@"; do
|
|
|
|
case $i in
|
|
|
|
-*) template_opts+=("$i");;
|
|
|
|
*) pos_args+=("$i");;
|
|
|
|
esac
|
|
|
|
done
|
2020-01-20 18:08:56 +00:00
|
|
|
|
2020-01-21 17:38:57 +00:00
|
|
|
install_template "${template_opts[@]}" \
|
|
|
|
"${APACHE_SITES_AVAILABE}/${pos_args[1]}" \
|
|
|
|
root root 644
|
2020-01-20 18:08:56 +00:00
|
|
|
|
2020-01-21 17:38:57 +00:00
|
|
|
apache_enable_site "${pos_args[1]}"
|
|
|
|
info_msg "installed apache site: ${pos_args[1]}"
|
2020-01-20 18:08:56 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 19:00:50 +00:00
|
|
|
apache_remove_site() {
|
|
|
|
|
|
|
|
# usage: apache_remove_site <mysite.conf>
|
|
|
|
|
|
|
|
info_msg "remove apache site: $1"
|
|
|
|
apache_dissable_site "$1"
|
|
|
|
rm -f "${APACHE_SITES_AVAILABE}/$1"
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:08:56 +00:00
|
|
|
apache_enable_site() {
|
2020-01-29 19:00:50 +00:00
|
|
|
|
|
|
|
# usage: apache_enable_site <mysite.conf>
|
|
|
|
|
|
|
|
info_msg "enable apache site: $1"
|
2020-01-20 18:08:56 +00:00
|
|
|
sudo -H a2ensite -q "$1"
|
|
|
|
apache_reload
|
|
|
|
}
|
|
|
|
|
|
|
|
apache_dissable_site() {
|
2020-01-29 19:00:50 +00:00
|
|
|
|
|
|
|
# usage: apache_disable_site <mysite.conf>
|
|
|
|
|
|
|
|
info_msg "disable apache site: $1"
|
2020-01-20 18:08:56 +00:00
|
|
|
sudo -H a2dissite -q "$1"
|
|
|
|
apache_reload
|
|
|
|
}
|
2020-01-14 18:26:54 +00:00
|
|
|
|
|
|
|
# uWSGI
|
|
|
|
# -----
|
|
|
|
|
|
|
|
uWSGI_SETUP="${uWSGI_SETUP:=/etc/uwsgi}"
|
|
|
|
|
|
|
|
uWSGI_restart() {
|
|
|
|
|
|
|
|
# usage: uWSGI_restart()
|
|
|
|
|
|
|
|
info_msg "restart uWSGI service"
|
2020-01-29 19:00:50 +00:00
|
|
|
systemctl restart uwsgi
|
2020-01-14 18:26:54 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 15:55:05 +00:00
|
|
|
uWSGI_app_available() {
|
|
|
|
# usage: uWSGI_app_available <myapp.ini>
|
|
|
|
local CONF="$1"
|
|
|
|
if [[ -z $CONF ]]; then
|
|
|
|
err_msg "uWSGI_app_available: missing arguments"
|
|
|
|
return 42
|
|
|
|
fi
|
|
|
|
[[ -f "${uWSGI_SETUP}/apps-available/${CONF}" ]]
|
|
|
|
}
|
|
|
|
|
2020-01-14 18:26:54 +00:00
|
|
|
uWSGI_install_app() {
|
|
|
|
|
2020-01-21 17:38:57 +00:00
|
|
|
# usage: uWSGI_install_app [<template option> ...] <myapp.ini>
|
|
|
|
#
|
|
|
|
# <template option>: see install_template
|
2020-01-14 18:26:54 +00:00
|
|
|
|
2020-01-27 18:08:40 +00:00
|
|
|
local pos_args=("$0")
|
|
|
|
|
2020-01-21 17:38:57 +00:00
|
|
|
for i in "$@"; do
|
|
|
|
case $i in
|
|
|
|
-*) template_opts+=("$i");;
|
|
|
|
*) pos_args+=("$i");;
|
|
|
|
esac
|
|
|
|
done
|
2020-01-14 18:26:54 +00:00
|
|
|
|
2020-01-21 17:38:57 +00:00
|
|
|
install_template "${template_opts[@]}" \
|
|
|
|
"${uWSGI_SETUP}/apps-available/${pos_args[1]}" \
|
|
|
|
root root 644
|
2020-01-14 18:26:54 +00:00
|
|
|
|
2020-01-21 17:38:57 +00:00
|
|
|
uWSGI_enable_app "${pos_args[1]}"
|
2020-01-14 18:26:54 +00:00
|
|
|
uWSGI_restart
|
2020-01-21 17:38:57 +00:00
|
|
|
info_msg "installed uWSGI app: ${pos_args[1]}"
|
2020-01-14 18:26:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uWSGI_remove_app() {
|
|
|
|
|
2020-01-20 15:55:05 +00:00
|
|
|
# usage: uWSGI_remove_app <myapp.ini>
|
2020-01-14 18:26:54 +00:00
|
|
|
|
2020-01-20 15:55:05 +00:00
|
|
|
local CONF="$1"
|
2020-01-29 19:00:50 +00:00
|
|
|
info_msg "remove uWSGI app: ${CONF}"
|
2020-01-20 15:55:05 +00:00
|
|
|
uWSGI_disable_app "${CONF}"
|
2020-01-14 18:26:54 +00:00
|
|
|
uWSGI_restart
|
2020-01-20 15:55:05 +00:00
|
|
|
rm -f "${uWSGI_SETUP}/apps-available/${CONF}"
|
|
|
|
}
|
|
|
|
|
|
|
|
uWSGI_app_enabled() {
|
|
|
|
# usage: uWSGI_app_enabled <myapp.ini>
|
|
|
|
local CONF="$1"
|
|
|
|
if [[ -z $CONF ]]; then
|
|
|
|
err_msg "uWSGI_app_enabled: missing arguments"
|
|
|
|
return 42
|
|
|
|
fi
|
|
|
|
[[ -f "${uWSGI_SETUP}/apps-enabled/${CONF}" ]]
|
2020-01-14 18:26:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# shellcheck disable=SC2164
|
|
|
|
uWSGI_enable_app() {
|
|
|
|
|
2020-01-20 15:55:05 +00:00
|
|
|
# usage: uWSGI_enable_app <myapp.ini>
|
2020-01-14 18:26:54 +00:00
|
|
|
|
2020-01-20 15:55:05 +00:00
|
|
|
local CONF="$1"
|
2020-01-14 18:26:54 +00:00
|
|
|
if [[ -z $CONF ]]; then
|
2020-01-20 15:55:05 +00:00
|
|
|
err_msg "uWSGI_enable_app: missing arguments"
|
2020-01-14 18:26:54 +00:00
|
|
|
return 42
|
|
|
|
fi
|
|
|
|
pushd "${uWSGI_SETUP}/apps-enabled" >/dev/null
|
2020-01-20 15:55:05 +00:00
|
|
|
rm -f "$CONF"
|
2020-01-14 18:26:54 +00:00
|
|
|
# shellcheck disable=SC2226
|
2020-01-20 15:55:05 +00:00
|
|
|
ln -s "../apps-available/${CONF}"
|
|
|
|
info_msg "enabled uWSGI app: ${CONF} (restart uWSGI required)"
|
2020-01-14 18:26:54 +00:00
|
|
|
popd >/dev/null
|
|
|
|
}
|
|
|
|
|
|
|
|
uWSGI_disable_app() {
|
|
|
|
|
2020-01-20 15:55:05 +00:00
|
|
|
# usage: uWSGI_disable_app <myapp.ini>
|
2020-01-14 18:26:54 +00:00
|
|
|
|
2020-01-20 15:55:05 +00:00
|
|
|
local CONF="$1"
|
2020-01-14 18:26:54 +00:00
|
|
|
if [[ -z $CONF ]]; then
|
2020-01-20 15:55:05 +00:00
|
|
|
err_msg "uWSGI_enable_app: missing arguments"
|
2020-01-14 18:26:54 +00:00
|
|
|
return 42
|
|
|
|
fi
|
2020-01-20 15:55:05 +00:00
|
|
|
rm -f "${uWSGI_SETUP}/apps-enabled/${CONF}"
|
2020-01-29 19:00:50 +00:00
|
|
|
# FIXME: restart uwsgi service won't stop wsgi forked processes of user searx.
|
|
|
|
# I had to kill them manually here ...
|
|
|
|
pkill -f "${uWSGI_SETUP}/apps-enabled/${CONF}" -9
|
2020-01-20 15:55:05 +00:00
|
|
|
info_msg "disabled uWSGI app: ${CONF} (restart uWSGI required)"
|
2020-01-14 18:26:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# distro's package manager
|
|
|
|
# ------------------------
|
|
|
|
#
|
|
|
|
# FIXME: Arch Linux & RHEL should be added
|
|
|
|
#
|
|
|
|
|
|
|
|
pkg_install() {
|
|
|
|
|
|
|
|
# usage: TITEL='install foobar' pkg_install foopkg barpkg
|
|
|
|
|
|
|
|
rst_title "${TITLE:-installation of packages}" section
|
|
|
|
echo -en "\npackage(s)::\n\n $*\n" | $FMT
|
|
|
|
|
|
|
|
if ! ask_yn "Should packages be installed?" Yn 30; then
|
|
|
|
return 42
|
|
|
|
fi
|
|
|
|
# shellcheck disable=SC2068
|
|
|
|
apt-get install -y $@
|
|
|
|
}
|
|
|
|
|
|
|
|
pkg_remove() {
|
|
|
|
|
|
|
|
# usage: TITEL='remove foobar' pkg_remove foopkg barpkg
|
|
|
|
|
|
|
|
rst_title "${TITLE:-remove packages}" section
|
|
|
|
echo -en "\npackage(s)::\n\n $*\n" | $FMT
|
|
|
|
|
|
|
|
if ! ask_yn "Should packages be removed (purge)?" Yn 30; then
|
|
|
|
return 42
|
|
|
|
fi
|
|
|
|
apt-get purge --autoremove --ignore-missing -y "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
pkg_is_installed() {
|
|
|
|
|
|
|
|
# usage: pkg_is_install foopkg || pkg_install foopkg
|
|
|
|
|
|
|
|
dpkg -l "$1" &> /dev/null
|
|
|
|
return $?
|
|
|
|
}
|
|
|
|
|
|
|
|
# git tooling
|
|
|
|
# -----------
|
|
|
|
|
|
|
|
# shellcheck disable=SC2164
|
|
|
|
git_clone() {
|
|
|
|
|
|
|
|
# usage:
|
|
|
|
#
|
|
|
|
# git_clone <url> <name> [<branch> [<user>]]
|
|
|
|
# git_clone <url> <path> [<branch> [<user>]]
|
|
|
|
#
|
|
|
|
# First form uses $CACHE/<name> as destination folder, second form clones
|
2020-01-16 13:01:38 +00:00
|
|
|
# into <path>. If repository is allready cloned, pull from <branch> and
|
2020-01-14 18:26:54 +00:00
|
|
|
# update working tree (if needed, the caller has to stash local changes).
|
|
|
|
#
|
|
|
|
# git clone https://github.com/asciimoo/searx searx-src origin/master searxlogin
|
|
|
|
#
|
|
|
|
|
|
|
|
local url="$1"
|
|
|
|
local dest="$2"
|
|
|
|
local branch="$3"
|
|
|
|
local user="$4"
|
2020-01-16 13:01:38 +00:00
|
|
|
local bash_cmd="bash"
|
|
|
|
local remote="origin"
|
2020-01-14 18:26:54 +00:00
|
|
|
|
|
|
|
if [[ ! "${dest:0:1}" = "/" ]]; then
|
|
|
|
dest="$CACHE/$dest"
|
|
|
|
fi
|
|
|
|
|
|
|
|
[[ -z $branch ]] && branch=master
|
|
|
|
[[ -z $user ]] && [[ ! -z "${SUDO_USER}" ]] && user="${SUDO_USER}"
|
2020-01-16 13:01:38 +00:00
|
|
|
[[ ! -z $user ]] && bash_cmd="sudo -H -u $user -i"
|
2020-01-14 18:26:54 +00:00
|
|
|
|
|
|
|
if [[ -d "${dest}" ]] ; then
|
|
|
|
info_msg "already cloned: $dest"
|
2020-01-16 13:01:38 +00:00
|
|
|
tee_stderr 0.1 <<EOF | $bash_cmd 2>&1 | prefix_stdout " |$user| "
|
|
|
|
cd "${dest}"
|
|
|
|
git checkout -m -B "$branch" --track "$remote/$branch"
|
|
|
|
git pull --all
|
|
|
|
EOF
|
2020-01-14 18:26:54 +00:00
|
|
|
else
|
|
|
|
info_msg "clone into: $dest"
|
2020-01-16 13:01:38 +00:00
|
|
|
tee_stderr 0.1 <<EOF | $bash_cmd 2>&1 | prefix_stdout " |$user| "
|
|
|
|
mkdir -p "$(dirname "$dest")"
|
|
|
|
cd "$(dirname "$dest")"
|
|
|
|
git clone --branch "$branch" --origin "$remote" "$url" "$(basename "$dest")"
|
|
|
|
EOF
|
2020-01-14 18:26:54 +00:00
|
|
|
fi
|
|
|
|
}
|