@@ -1,5 +1,5 @@
11#!/usr/bin/env bash
2−# -*- coding: utf-8; mode: sh -*-
2+# -*- coding: utf-8; mode: sh indent-tabs-mode: nil -*-
33# shellcheck disable=SC2119
44
55# shellcheck source=utils/lib.sh
@@ -15,8 +15,11 @@ SERVICE_USER="${SERVICE_NAME}"
1515SERVICE_GROUP="${SERVICE_USER}"
1616SERVICE_HOME="/home/${SERVICE_USER}"
1717
18+# shellcheck disable=SC2034
19+SEARX_URL="127.0.0.1:8888"
20+
1821SEARX_GIT_URL="https://github.com/asciimoo/searx.git"
19−SEARX_GIT_BRANCH="origin/master"
22+SEARX_GIT_BRANCH="master"
2023
2124# FIXME: Arch Linux & RHEL should be added
2225
@@ -25,8 +28,8 @@ libapache2-mod-uwsgi uwsgi uwsgi-plugin-python3 \
2528 git build-essential libxslt-dev python3-dev python3-babel zlib1g-dev \
2629 libffi-dev libssl-dev"
2730
28−SEARX_VENV="${SEARX_HOME}/searx-venv"
29−SEARX_SRC="${SEARX_HOME}/searx-src"
31+SEARX_PYENV="${SERVICE_HOME}/searx-pyenv"
32+SEARX_SRC="${SERVICE_HOME}/searx-src"
3033SEARX_SETTINGS="${SEARX_SRC}/searx/settings.yml"
3134SEARX_INSTANCE_NAME="${SEARX_INSTANCE_NAME:-searx@$(uname -n)}"
3235SEARX_UWSGI_APP="${uWSGI_SETUP}/apps-available/searx.ini"
@@ -51,25 +54,26 @@ usage(){
5154usage:
5255
5356 $(basename "$0") shell
54− $(basename "$0") install [all|user]
57+ $(basename "$0") install [all|user|pyenv|searx-src]
5558 $(basename "$0") update [searx]
56− $(basename "$0") remove [all]
59+ $(basename "$0") remove [all|user|pyenv|searx-src]
5760 $(basename "$0") activate [service]
5861 $(basename "$0") deactivate [service]
5962 $(basename "$0") show [service]
6063
6164shell
6265 start interactive shell from user ${SERVICE_USER}
63−install / remove all
64− complete setup of searx service
66+install / remove
67+ all: complete (de-) installation of searx service
68+ user: add/remove service user '$SERVICE_USER' at $SERVICE_HOME
69+ searx-src: clone $SEARX_GIT_URL
70+ pyenv: create/remove virtualenv (python) in $SEARX_PYENV
6571update searx
6672 Update searx installation of user ${SERVICE_USER}
6773activate
6874 activate and start service daemon (systemd unit)
6975deactivate service
7076 stop and deactivate service daemon (systemd unit)
71−install user
72− add service user '$SERVICE_USER' at $SERVICE_HOME
7377show service
7478 show service status and log
7579EOF
@@ -102,6 +106,8 @@ main(){
102106 case $2 in
103107 all) install_all ;;
104108 user) assert_user ;;
109+ pyenv) create_pyenv ;;
110+ searx-src) clone_searx ;;
105111 *) usage "$_usage"; exit 42;;
106112 esac ;;
107113 update)
@@ -115,6 +121,8 @@ main(){
115121 case $2 in
116122 all) remove_all;;
117123 user) remove_user ;;
124+ pyenv) remove_pyenv ;;
125+ searx-src) remove_searx ;;
118126 *) usage "$_usage"; exit 42;;
119127 esac ;;
120128 activate)
@@ -143,7 +151,7 @@ install_all() {
143151 wait_key
144152 clone_searx
145153 wait_key
146− create_venv
154+ create_pyenv
147155 wait_key
148156 configure_searx
149157 wait_key
@@ -167,7 +175,7 @@ update_searx() {
167175cd ${SEARX_SRC}
168176cp -f ${SEARX_SETTINGS} ${SEARX_SETTINGS}.backup
169177git stash push -m "BACKUP -- 'update server' at ($(date))"
170−git checkout -b "$(basename "$SEARX_GIT_BRANCH")" --track "$SEARX_GIT_BRANCH"
178+git checkout -b $SEARX_GIT_BRANCH" --track "$SEARX_GIT_BRANCH"
171179git pull "$SEARX_GIT_BRANCH"
172180${SEARX_SRC}/manage.sh update_packages
173181EOF
@@ -185,16 +193,16 @@ EOF
185193 "start interactiv shell"
186194 case $action in
187195 "keep new configuration")
188− info_msg "continue using new settings file"
189− ;;
196+ info_msg "continue using new settings file"
197+ ;;
190198 "revert to the old configuration (backup file)")
191199 tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
192200cp -f ${SEARX_SETTINGS}.backup ${SEARX_SETTINGS}
193201EOF
194− ;;
195− "start interactiv shell")
196− interactive_shell
197− ;;
202+ ;;
203+ "start interactiv shell")
204+ interactive_shell
205+ ;;
198206 esac
199207 chown "${SERVICE_USER}:${SERVICE_USER}" "${SEARX_SETTINGS}"
200208
@@ -208,7 +216,10 @@ EOF
208216
209217remove_all() {
210218 rst_title "De-Install $SERVICE_NAME (service)"
211− remove_service
219+ if ! ask_yn "Do you really want to deinstall $SERVICE_NAME?"; then
220+ return
221+ fi
222+ remove_searx_uwsgi
212223 wait_key
213224 remove_user
214225}
@@ -240,35 +251,71 @@ remove_user() {
240251clone_searx(){
241252 rst_title "Clone searx sources" section
242253 echo
254+ SERVICE_HOME="$(sudo -i -u "$SERVICE_USER" echo \$HOME 2>/dev/null)"
255+ if [[ ! "${SERVICE_HOME}" ]]; then
256+ err_msg "to clone searx sources, user $SERVICE_USER hast to be created first"
257+ return 42
258+ fi
259+ export SERVICE_HOME
260+
243261 git_clone "$SEARX_GIT_URL" "$SEARX_SRC" \
244− "$SEARX_GIT_BRANCH" "$SERVICE_USER"
262+ "$SEARX_GIT_BRANCH" "$SERVICE_USER"
245263
246264 pushd "${SEARX_SRC}" > /dev/null
247265 tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
248266cd "${SEARX_SRC}"
249267git config user.email "$ADMIN_EMAIL"
250268git config user.name "$ADMIN_NAME"
251−git checkout "$SEARX_GIT_BRANCH"
269+git config --list
252270EOF
253271 popd > /dev/null
254272}
255273
256−create_venv(){
257− rst_title "Create virtualenv (python)" section
274+remove_searx() {
275+ rst_title "Drop searx sources" section
276+ if ask_yn "Do you really want to drop searx sources ($SEARX_SRC)?"; then
277+ rm -rf "$SEARX_SRC"
278+ else
279+ rst_para "Leave searx sources unchanged."
280+ fi
281+}
258282
259− rst_para "Create venv in ${SEARX_VENV} and install needed python packages."
283+create_pyenv(){
284+ rst_title "Create virtualenv (python)" section
260285 echo
286+ if [[ ! -f "${SEARX_SRC}/manage.sh" ]]; then
287+ err_msg "to create pyenv for searx, searx has to be cloned first"
288+ return 42
289+ fi
290+ info_msg "create pyenv in ${SEARX_PYENV}"
261291 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
292+rm -rf "${SEARX_PYENV}"
293+python3 -m venv "${SEARX_PYENV}"
294+grep -qFs -- 'source ${SEARX_PYENV}/bin/activate' ~/.profile \
295+ || echo 'source ${SEARX_PYENV}/bin/activate' >> ~/.profile
296+EOF
297+ info_msg "inspect python's virtual environment"
298+ tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
299+command -v python && python --version
266300EOF
301+ wait_key
302+ info_msg "install needed python packages"
267303 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
304+${SEARX_SRC}/manage.sh update_packages
270305EOF
306+}
271307
308+remove_pyenv(){
309+ rst_title "Remove virtualenv (python)" section
310+ if ! ask_yn "Do you really want to drop ${SEARX_PYENV} ?"; then
311+ return
312+ fi
313+ info_msg "remove pyenv activation from ~/.profile"
314+ tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
315+grep -v 'source ${SEARX_PYENV}/bin/activate' ~/.profile > ~/.profile.##
316+mv ~/.profile.## ~/.profile
317+EOF
318+ rm -rf "${SEARX_PYENV}"
272319}
273320
274321configure_searx(){
@@ -283,7 +330,7 @@ EOF
283330}
284331
285332test_local_searx(){
286− rstHeading "Testing searx instance localy" section
333+ rst_title "Testing searx instance localy" section
287334 echo
288335 tee_stderr 0.1 <<EOF | sudo -H -u "${SERVICE_USER}" -i 2>&1 | prefix_stdout "$_service_prefix"
289336cd ${SEARX_SRC}
@@ -293,7 +340,6 @@ sleep 1
293340curl --location --verbose --head --insecure http://127.0.0.1:8888/
294341sed -i -e "s/debug : True/debug : False/g" "$SEARX_SETTINGS"
295342EOF
296− waitKEY
297343}
298344
299345install_searx_uwsgi() {