@@ -0,0 +1,349 @@
2+# -*- coding: utf-8; mode: sh -*-
3+# shellcheck disable=SC2119
5+# shellcheck source=utils/lib.sh
6+source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
8+# ----------------------------------------------------------------------------
10+# ----------------------------------------------------------------------------
13+SERVICE_USER="${SERVICE_NAME}"
14+# shellcheck disable=SC2034
15+SERVICE_GROUP="${SERVICE_USER}"
16+SERVICE_HOME="/home/${SERVICE_USER}"
18+SEARX_GIT_URL="https://github.com/asciimoo/searx.git"
19+SEARX_GIT_BRANCH="origin/master"
21+# FIXME: Arch Linux & RHEL should be added
24+libapache2-mod-uwsgi uwsgi uwsgi-plugin-python3 \
25+ git build-essential libxslt-dev python3-dev python3-babel zlib1g-dev \
26+ libffi-dev libssl-dev"
28+SEARX_VENV="${SEARX_HOME}/searx-venv"
29+SEARX_SRC="${SEARX_HOME}/searx-src"
30+SEARX_SETTINGS="${SEARX_SRC}/searx/settings.yml"
31+SEARX_INSTANCE_NAME="${SEARX_INSTANCE_NAME:-searx@$(uname -n)}"
32+SEARX_UWSGI_APP="${uWSGI_SETUP}/apps-available/searx.ini"
34+# shellcheck disable=SC2034
39+# shellcheck disable=SC2034
40+CONFIG_BACKUP_ENCRYPTED=(
44+# ----------------------------------------------------------------------------
46+# ----------------------------------------------------------------------------
48+ # shellcheck disable=SC1117
53+ $(basename "$0") shell
54+ $(basename "$0") install [all|user]
55+ $(basename "$0") update [searx]
56+ $(basename "$0") remove [all]
57+ $(basename "$0") activate [service]
58+ $(basename "$0") deactivate [service]
59+ $(basename "$0") show [service]
62+ start interactive shell from user ${SERVICE_USER}
64+ complete setup of searx service
66+ Update searx installation of user ${SERVICE_USER}
68+ activate and start service daemon (systemd unit)
70+ stop and deactivate service daemon (systemd unit)
72+ add service user '$SERVICE_USER' at $SERVICE_HOME
74+ show service status and log
76+ [ ! -z ${1+x} ] && echo -e "$1"
80+ rst_title "$SERVICE_NAME" part
82+ local _usage="ERROR: unknown or missing $1 command $2"
86+ -h|--help) usage; exit 0;;
98+ *) usage "$_usage"; exit 42;;
104+ user) assert_user ;;
105+ *) usage "$_usage"; exit 42;;
110+ searx) update_searx;;
111+ *) usage "$_usage"; exit 42;;
117+ user) remove_user ;;
118+ *) usage "$_usage"; exit 42;;
123+ service) activate_service ;;
124+ *) usage "$_usage"; exit 42;;
129+ service) deactivate_service ;;
130+ *) usage "$_usage"; exit 42;;
132+ *) usage "ERROR: unknown or missing command $1"; exit 42;;
136+_service_prefix=" |$SERVICE_USER| "
139+ rst_title "Install $SERVICE_NAME (service)"
140+ pkg_install "$SEARX_APT_PACKAGES"
156+ # install_apache_site
158+ # info_msg "searX --> https://${SEARX_APACHE_DOMAIN}${SEARX_APACHE_URL}"
163+ rst_title "Update searx instance"
166+ tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
168+cp -f ${SEARX_SETTINGS} ${SEARX_SETTINGS}.backup
169+git stash push -m "BACKUP -- 'update server' at ($(date))"
170+git checkout -b "$(basename "$SEARX_GIT_BRANCH")" --track "$SEARX_GIT_BRANCH"
171+git pull "$SEARX_GIT_BRANCH"
172+${SEARX_SRC}/manage.sh update_packages
176+ rst_title "${SEARX_SETTINGS}" section
177+ rstBlock 'Diff between new setting file (<) and backup (>):'
179+ diff "$SEARX_SETTINGS}" "${SEARX_SETTINGS}.backup"
182+ choose_one action "What should happen to the settings file? " \
183+ "keep new configuration" \
184+ "revert to the old configuration (backup file)" \
185+ "start interactiv shell"
187+ "keep new configuration")
188+ info_msg "continue using new settings file"
190+ "revert to the old configuration (backup file)")
191+ tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
192+cp -f ${SEARX_SETTINGS}.backup ${SEARX_SETTINGS}
195+ "start interactiv shell")
199+ chown "${SERVICE_USER}:${SERVICE_USER}" "${SEARX_SETTINGS}"
201+ # shellcheck disable=SC2016
202+ rst_para 'Diff between local modified settings (<) and $SEARX_GIT_BRANCH branch (>):'
210+ rst_title "De-Install $SERVICE_NAME (service)"
217+ rst_title "user $SERVICE_USER" section
219+ tee_stderr 1 <<EOF | bash | prefix_stdout
220+sudo -H adduser --shell /bin/bash --system --home "$SERVICE_HOME" \
221+ --disabled-password --group --gecos 'searx' $SERVICE_USER
222+sudo -H usermod -a -G shadow $SERVICE_USER
225+ #SERVICE_HOME="$(sudo -i -u "$SERVICE_USER" echo \$HOME)"
226+ #export SERVICE_HOME
227+ #echo "export SERVICE_HOME=$SERVICE_HOME"
231+ rst_title "Drop $SERVICE_USER HOME" section
232+ if ask_yn "Do you really want to drop $SERVICE_USER home folder?"; then
233+ userdel -r -f "$SERVICE_USER" 2>&1 | prefix_stdout
235+ rst_para "Leave HOME folder $(du -sh "$SERVICE_HOME") unchanged."
239+# shellcheck disable=SC2164
241+ rst_title "Clone searx sources" section
243+ git_clone "$SEARX_GIT_URL" "$SEARX_SRC" \
244+ "$SEARX_GIT_BRANCH" "$SERVICE_USER"
246+ pushd "${SEARX_SRC}" > /dev/null
247+ tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
249+git config user.email "$ADMIN_EMAIL"
250+git config user.name "$ADMIN_NAME"
251+git checkout "$SEARX_GIT_BRANCH"
257+ rst_title "Create virtualenv (python)" section
259+ rst_para "Create venv in ${SEARX_VENV} and install needed python packages."
261+ tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
262+rm -rf "${SEARX_VENV}"
263+python3 -m venv "${SEARX_VENV}"
264+. ${SEARX_VENV}/bin/activate
265+${SEARX_SRC}/manage.sh update_packages
267+ tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
268+grep -qFs -- 'source ${SEARX_VENV}/bin/activate' ~/.profile \
269+ || echo 'source ${SEARX_VENV}/bin/activate' >> ~/.profile
275+ rst_title "Configure searx" section
276+ rst_para "Setup searx config located at $SEARX_SETTINGS"
278+ tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
280+sed -i -e "s/ultrasecretkey/$(openssl rand -hex 16)/g" "$SEARX_SETTINGS"
281+sed -i -e "s/{instance_name}/${SEARX_INSTANCE_NAME}/g" "$SEARX_SETTINGS"
286+ rstHeading "Testing searx instance localy" section
288+ tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
290+sed -i -e "s/debug : False/debug : True/g" "$SEARX_SETTINGS"
291+timeout 5 python3 searx/webapp.py &
293+curl --location --verbose --head --insecure http://127.0.0.1:8888/
294+sed -i -e "s/debug : True/debug : False/g" "$SEARX_SETTINGS"
299+install_searx_uwsgi() {
300+ rst_title "Install searx's uWSGI app (searx.ini)" section
302+ uWSGI_install_app "$SEARX_UWSGI_APP"
305+remove_searx_uwsgi() {
306+ rst_title "Remove searx's uWSGI app (searx.ini)" section
308+ uWSGI_remove_app "$SEARX_UWSGI_APP"
311+activate_service () {
312+ rst_title "Activate $SERVICE_NAME (service)" section
313+ uWSGI_enable_app "$SEARX_UWSGI_APP"
316+deactivate_service () {
317+ rst_title "De-Activate $SERVICE_NAME (service)" section
318+ uWSGI_disable_app "$SEARX_UWSGI_APP"
322+ echo "// exit with CTRL-D"
323+ sudo -H -u "${SERVICE_USER}" -i
327+ sudo -H -u "${SERVICE_USER}" -i <<EOF
328+cd ${SEARX_REPO_FOLDER}
334+ rst_title "service status & log"
336+ systemctl status uwsgi.service
338+ read -r -s -n1 -t 5 -p "// use CTRL-C to stop monitoring the log"
342+ journalctl -f -u uwsgi.service
347+# ----------------------------------------------------------------------------
349+# ----------------------------------------------------------------------------