utils/filtron.sh: generalize systemd, accounts and golang tasks

Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
This commit is contained in:
Markus Heiser 2020-02-01 16:59:27 +01:00
parent 56a93ee770
commit 709ac51d33
3 changed files with 134 additions and 116 deletions

View file

@ -109,7 +109,7 @@ main() {
shell) shell)
sudo_or_exit sudo_or_exit
interactive_shell interactive_shell "${SERVICE_USER}"
;; ;;
inspect) inspect)
case $2 in case $2 in
@ -136,19 +136,19 @@ main() {
sudo_or_exit sudo_or_exit
case $2 in case $2 in
all) remove_all;; all) remove_all;;
user) remove_user ;; user) drop_service_account "${SERVICE_USER}" ;;
*) usage "$_usage"; exit 42;; *) usage "$_usage"; exit 42;;
esac ;; esac ;;
activate) activate)
sudo_or_exit sudo_or_exit
case $2 in case $2 in
service) activate_service ;; service) systemd_activate_service "${SERVICE_NAME}" ;;
*) usage "$_usage"; exit 42;; *) usage "$_usage"; exit 42;;
esac ;; esac ;;
deactivate) deactivate)
sudo_or_exit sudo_or_exit
case $2 in case $2 in
service) deactivate_service ;; service) systemd_deactivate_service "${SERVICE_NAME}" ;;
*) usage "$_usage"; exit 42;; *) usage "$_usage"; exit 42;;
esac ;; esac ;;
apache) apache)
@ -174,11 +174,11 @@ install_all() {
rst_title "Install $SERVICE_NAME (service)" rst_title "Install $SERVICE_NAME (service)"
assert_user assert_user
wait_key wait_key
install_go install_go "${GO_PKG_URL}" "${GO_TAR}" "${SERVICE_USER}"
wait_key wait_key
install_filtron install_filtron
wait_key wait_key
install_service systemd_install_service "${SERVICE_NAME}" "${SERVICE_SYSTEMD_UNIT}"
wait_key wait_key
echo echo
if ! service_is_available "http://${FILTRON_LISTEN}" ; then if ! service_is_available "http://${FILTRON_LISTEN}" ; then
@ -203,56 +203,15 @@ remove_all() {
It goes without saying that this script can only be used to remove It goes without saying that this script can only be used to remove
installations that were installed with this script." installations that were installed with this script."
remove_service systemd_remove_service "${SERVICE_NAME}" "${SERVICE_SYSTEMD_UNIT}"
wait_key wait_key
remove_user drop_service_account "${SERVICE_USER}"
rm -r "$FILTRON_ETC" 2>&1 | prefix_stdout rm -r "$FILTRON_ETC" 2>&1 | prefix_stdout
if service_is_available "${PUBLIC_URL}"; then if service_is_available "${PUBLIC_URL}"; then
MSG="** Don't forget to remove your public site! (${PUBLIC_URL}) **" wait_key 10 MSG="** Don't forget to remove your public site! (${PUBLIC_URL}) **" wait_key 10
fi fi
} }
install_service() {
rst_title "Install System-D Unit ${SERVICE_NAME}.service" section
echo
install_template "${SERVICE_SYSTEMD_UNIT}" root root 644
wait_key
activate_service
}
remove_service() {
if ! ask_yn "Do you really want to deinstall $SERVICE_NAME?"; then
return
fi
deactivate_service
rm "${SERVICE_SYSTEMD_UNIT}" 2>&1 | prefix_stdout
}
activate_service() {
rst_title "Activate $SERVICE_NAME (service)" section
echo
tee_stderr <<EOF | bash 2>&1
systemctl enable $SERVICE_NAME.service
systemctl restart $SERVICE_NAME.service
EOF
tee_stderr <<EOF | bash 2>&1
systemctl status --no-pager $SERVICE_NAME.service
EOF
}
deactivate_service() {
rst_title "De-Activate $SERVICE_NAME (service)" section
echo
tee_stderr <<EOF | bash 2>&1 | prefix_stdout
systemctl stop $SERVICE_NAME.service
systemctl disable $SERVICE_NAME.service
EOF
}
user_is_available() {
sudo -i -u "$SERVICE_USER" echo \$HOME &>/dev/null
}
assert_user() { assert_user() {
rst_title "user $SERVICE_USER" section rst_title "user $SERVICE_USER" section
echo echo
@ -277,44 +236,6 @@ grep -qFs -- 'source $GO_ENV' ~/.profile || echo 'source $GO_ENV' >> ~/.profile
EOF EOF
} }
remove_user() {
rst_title "Drop $SERVICE_USER HOME" section
if ask_yn "Do you really want to drop $SERVICE_USER home folder?"; then
userdel -r -f "$SERVICE_USER" 2>&1 | prefix_stdout
else
rst_para "Leave HOME folder $(du -sh "$SERVICE_HOME") unchanged."
fi
}
interactive_shell(){
echo "// exit with ${_BCyan}CTRL-D${_creset}"
sudo -H -u ${SERVICE_USER} -i
}
_service_prefix=" |$SERVICE_USER| "
go_is_available() {
sudo -i -u "$SERVICE_USER" which go &>/dev/null
}
install_go() {
rst_title "Install Go in user's HOME" section
rst_para "download and install go binary .."
cache_download "${GO_PKG_URL}" "${GO_TAR}"
tee_stderr 0.1 <<EOF | sudo -i -u "$SERVICE_USER" | prefix_stdout "$_service_prefix"
echo \$PATH
echo \$GOPATH
mkdir -p \$HOME/local
rm -rf \$HOME/local/go
tar -C \$HOME/local -xzf ${CACHE}/${GO_TAR}
EOF
sudo -i -u "$SERVICE_USER" <<EOF | prefix_stdout
! which go >/dev/null && echo "ERROR - Go Installation not found in PATH!?!"
which go >/dev/null && go version && echo "congratulations -- Go installation OK :)"
EOF
}
filtron_is_installed() { filtron_is_installed() {
[[ -f $SERVICE_HOME/go-apps/bin/filtron ]] [[ -f $SERVICE_HOME/go-apps/bin/filtron ]]
@ -356,12 +277,12 @@ EOF
apache_is_installed && info_msg "Apache is installed." apache_is_installed && info_msg "Apache is installed."
if user_is_available; then if service_account_is_available "$SERVICE_USER"; then
info_msg "service account $SERVICE_USER available." info_msg "service account $SERVICE_USER available."
else else
err_msg "service account $SERVICE_USER not available!" err_msg "service account $SERVICE_USER not available!"
fi fi
if go_is_available; then if go_is_available "$SERVICE_USER"; then
info_msg "~$SERVICE_USER: go is installed" info_msg "~$SERVICE_USER: go is installed"
else else
err_msg "~$SERVICE_USER: go is not installed" err_msg "~$SERVICE_USER: go is not installed"

View file

@ -25,10 +25,6 @@ if [[ -z "$CACHE" ]]; then
CACHE="${REPO_ROOT}/cache" CACHE="${REPO_ROOT}/cache"
fi fi
if [[ -z "$SYSTEMD_UNITS" ]]; then
SYSTEMD_UNITS="/lib/systemd/system"
fi
if [[ -z ${DIFF_CMD} ]]; then if [[ -z ${DIFF_CMD} ]]; then
DIFF_CMD="diff -u" DIFF_CMD="diff -u"
if command -v colordiff >/dev/null; then if command -v colordiff >/dev/null; then
@ -477,6 +473,125 @@ service_is_available() {
return "$exit_val" return "$exit_val"
} }
# golang
# ------
go_is_available() {
# usage: go_is_available $SERVICE_USER && echo "go is installed!"
sudo -i -u "${1}" which go &>/dev/null
}
install_go() {
# usage: install_go "${GO_PKG_URL}" "${GO_TAR}" "${SERVICE_USER}"
local _service_prefix=" |${3}| "
rst_title "Install Go in user's HOME" section
rst_para "download and install go binary .."
cache_download "${1}" "${2}"
tee_stderr 0.1 <<EOF | sudo -i -u "${3}" | prefix_stdout "$_service_prefix"
echo \$PATH
echo \$GOPATH
mkdir -p \$HOME/local
rm -rf \$HOME/local/go
tar -C \$HOME/local -xzf ${CACHE}/${2}
EOF
sudo -i -u "${3}" <<EOF | prefix_stdout
! which go >/dev/null && echo "ERROR - Go Installation not found in PATH!?!"
which go >/dev/null && go version && echo "congratulations -- Go installation OK :)"
EOF
}
# system accounts
# ---------------
service_account_is_available() {
# usage: service_account_is_available "$SERVICE_USER" && echo "OK"
sudo -i -u "$1" echo \$HOME &>/dev/null
}
drop_service_account() {
# usage: drop_service_account "${SERVICE_USER}"
rst_title "Drop ${1} HOME" section
if ask_yn "Do you really want to drop ${1} home folder?"; then
userdel -r -f "${1}" 2>&1 | prefix_stdout
else
rst_para "Leave HOME folder $(du -sh "${1}") unchanged."
fi
}
interactive_shell(){
# usage: interactive_shell "${SERVICE_USER}"
echo "// exit with ${_BCyan}CTRL-D${_creset}"
sudo -H -u "${1}" -i
}
# systemd
# -------
SYSTEMD_UNITS="${SYSTEMD_UNITS:-/lib/systemd/system}"
systemd_install_service() {
# usage: systemd_install_service "${SERVICE_NAME}" "${SERVICE_SYSTEMD_UNIT}"
rst_title "Install System-D Unit ${1}" section
echo
install_template "${2}" root root 644
wait_key
systemd_activate_service "${1}"
}
systemd_remove_service() {
# usage: systemd_remove_service "${SERVICE_NAME}" "${SERVICE_SYSTEMD_UNIT}"
if ! ask_yn "Do you really want to deinstall ${1}?"; then
return
fi
systemd_deactivate_service "${1}"
rm "${2}" 2>&1 | prefix_stdout
}
systemd_activate_service() {
# usage: systemd_activate_service "${SERVICE_NAME}"w
rst_title "Activate ${1} (service)" section
echo
tee_stderr <<EOF | bash 2>&1
systemctl enable ${1}.service
systemctl restart ${1}.service
EOF
tee_stderr <<EOF | bash 2>&1
systemctl status --no-pager ${1}.service
EOF
}
systemd_deactivate_service() {
# usage: systemd_deactivate_service "${SERVICE_NAME}"
rst_title "De-Activate ${1} (service)" section
echo
tee_stderr <<EOF | bash 2>&1 | prefix_stdout
systemctl stop ${1}.service
systemctl disable ${1}.service
EOF
}
# Apache # Apache
# ------ # ------

View file

@ -124,7 +124,7 @@ main() {
shell) shell)
sudo_or_exit sudo_or_exit
interactive_shell interactive_shell "${SERVICE_USER}"
;; ;;
inspect) inspect)
case $2 in case $2 in
@ -153,7 +153,7 @@ main() {
sudo_or_exit sudo_or_exit
case $2 in case $2 in
all) remove_all;; all) remove_all;;
user) remove_user ;; user) drop_service_account "${SERVICE_USER}";;
pyenv) remove_pyenv ;; pyenv) remove_pyenv ;;
searx-src) remove_searx ;; searx-src) remove_searx ;;
*) usage "$_usage"; exit 42;; *) usage "$_usage"; exit 42;;
@ -250,7 +250,7 @@ cp -f ${SEARX_SETTINGS}.backup ${SEARX_SETTINGS}
EOF EOF
;; ;;
"start interactiv shell") "start interactiv shell")
interactive_shell interactive_shell "${SERVICE_USER}"
;; ;;
esac esac
chown "${SERVICE_USER}:${SERVICE_USER}" "${SEARX_SETTINGS}" chown "${SERVICE_USER}:${SERVICE_USER}" "${SEARX_SETTINGS}"
@ -275,16 +275,12 @@ installations that were installed with this script."
fi fi
remove_searx_uwsgi remove_searx_uwsgi
wait_key wait_key
remove_user drop_service_account "${SERVICE_USER}"
if service_is_available "${PUBLIC_URL}"; then if service_is_available "${PUBLIC_URL}"; then
MSG="** Don't forgett to remove your public site! (${PUBLIC_URL}) **" wait_key 10 MSG="** Don't forgett to remove your public site! (${PUBLIC_URL}) **" wait_key 10
fi fi
} }
user_is_available() {
sudo -i -u "$SERVICE_USER" echo \$HOME &>/dev/null
}
assert_user() { assert_user() {
rst_title "user $SERVICE_USER" section rst_title "user $SERVICE_USER" section
echo echo
@ -299,15 +295,6 @@ EOF
#echo "export SERVICE_HOME=$SERVICE_HOME" #echo "export SERVICE_HOME=$SERVICE_HOME"
} }
remove_user() {
rst_title "Drop $SERVICE_USER HOME" section
if ask_yn "Do you really want to drop $SERVICE_USER home folder?"; then
userdel -r -f "$SERVICE_USER" 2>&1 | prefix_stdout
else
rst_para "Leave HOME folder $(du -sh "$SERVICE_HOME") unchanged."
fi
}
clone_is_available() { clone_is_available() {
[[ -f "$SEARX_SETTINGS" ]] [[ -f "$SEARX_SETTINGS" ]]
} }
@ -445,11 +432,6 @@ deactivate_service() {
uWSGI_restart uWSGI_restart
} }
interactive_shell(){
echo "// exit with ${_BCyan}CTRL-D${_creset}"
sudo -H -u ${SERVICE_USER} -i
}
git_diff() { git_diff() {
sudo -H -u "${SERVICE_USER}" -i <<EOF sudo -H -u "${SERVICE_USER}" -i <<EOF
cd ${SEARX_REPO_FOLDER} cd ${SEARX_REPO_FOLDER}
@ -491,7 +473,7 @@ EOF
apache_is_installed && info_msg "Apache is installed." apache_is_installed && info_msg "Apache is installed."
if user_is_available; then if service_account_is_available "$SERVICE_USER"; then
info_msg "Service account $SERVICE_USER exists." info_msg "Service account $SERVICE_USER exists."
else else
err_msg "Service account $SERVICE_USER does not exists!" err_msg "Service account $SERVICE_USER does not exists!"