on engine-data help sign in
searx/utils/morty.sh
546 lines 16 kB history raw
1#!/usr/bin/env bash
2# -*- coding: utf-8; mode: sh indent-tabs-mode: nil -*-
3# SPDX-License-Identifier: AGPL-3.0-or-later
5# shellcheck source=utils/lib.sh
6source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
7# shellcheck source=utils/brand.env
8source "${REPO_ROOT}/utils/brand.env"
9source_dot_config
10SEARX_URL="${PUBLIC_URL:-http://$(uname -n)/searx}"
11source "${REPO_ROOT}/utils/lxc-searx.env"
12in_container && lxc_set_suite_env
14# ----------------------------------------------------------------------------
15# config
16# ----------------------------------------------------------------------------
18MORTY_LISTEN="${MORTY_LISTEN:-127.0.0.1:3000}"
19PUBLIC_URL_PATH_MORTY="${PUBLIC_URL_PATH_MORTY:-/morty/}"
21PUBLIC_URL_MORTY="${PUBLIC_URL_MORTY:-$(echo "$SEARX_URL" | sed -e's,^\(.*://[^/]*\).*,\1,g')${PUBLIC_URL_PATH_MORTY}}"
23# shellcheck disable=SC2034
24MORTY_TIMEOUT=5
26SERVICE_NAME="morty"
27SERVICE_USER="${SERVICE_USER:-${SERVICE_NAME}}"
28SERVICE_HOME_BASE="${SERVICE_HOME_BASE:-/usr/local}"
29SERVICE_HOME="${SERVICE_HOME_BASE}/${SERVICE_USER}"
30SERVICE_SYSTEMD_UNIT="${SYSTEMD_UNITS}/${SERVICE_NAME}.service"
31# shellcheck disable=SC2034
32SERVICE_GROUP="${SERVICE_USER}"
33# shellcheck disable=SC2034
34SERVICE_ENV_DEBUG=false
36GO_ENV="${SERVICE_HOME}/.go_env"
37GO_PKG_URL="https://dl.google.com/go/go1.13.5.linux-amd64.tar.gz"
38GO_TAR=$(basename "$GO_PKG_URL")
40# shellcheck disable=SC2034
41CONFIG_FILES=()
43# Apache Settings
45APACHE_MORTY_SITE="morty.conf"
46NGINX_MORTY_SITE="morty.conf"
48# ----------------------------------------------------------------------------
49usage() {
50# ----------------------------------------------------------------------------
52 # shellcheck disable=SC1117
53 cat <<EOF
54usage::
55 $(basename "$0") shell
56 $(basename "$0") install [all|user]
57 $(basename "$0") update [morty]
58 $(basename "$0") remove [all]
59 $(basename "$0") activate [service]
60 $(basename "$0") deactivate [service]
61 $(basename "$0") inspect [service]
62 $(basename "$0") option [debug-on|debug-off|new-key]
63 $(basename "$0") apache [install|remove]
64 $(basename "$0") nginx [install|remove]
65 $(basename "$0") info [searx]
67shell
68 start interactive shell from user ${SERVICE_USER}
69install / remove
70 all: complete setup of morty service
71 user: add/remove service user '$SERVICE_USER' ($SERVICE_HOME)
72update morty
73 Update morty installation ($SERVICE_HOME)
74activate service
75 activate and start service daemon (systemd unit)
76deactivate service
77 stop and deactivate service daemon (systemd unit)
78inspect service
79 show service status and log
80option
81 set one of the available options
82 :new-key: set new morty key
83apache : ${PUBLIC_URL_MORTY}
84 :install: apache site with a reverse proxy (ProxyPass)
85 :remove: apache site ${APACHE_MORTY_SITE}
86nginx (${PUBLIC_URL_MORTY})
87 :install: nginx site with a reverse proxy (ProxyPass)
88 :remove: nginx site ${NGINX_MORTY_SITE}
90If needed, set the environment variables in the '${DOT_CONFIG#"$REPO_ROOT/"}' file::
91 PUBLIC_URL_MORTY: ${PUBLIC_URL_MORTY}
92 MORTY_LISTEN: ${MORTY_LISTEN}
93 SERVICE_USER: ${SERVICE_USER}
94EOF
95 if in_container; then
96 # in containers the service is listening on 0.0.0.0 (see lxc-searx.env)
97 for ip in $(global_IPs) ; do
98 if [[ $ip =~ .*:.* ]]; then
99 echo " container URL (IPv6): http://[${ip#*|}]:3000/"
100 else
101 # IPv4:
102 echo " container URL (IPv4): http://${ip#*|}:3000/"
103 fi
104 done
105 fi
106 echo
107 info_searx
109 [[ -n ${1} ]] && err_msg "$1"
112info_searx() {
113 # shellcheck disable=SC1117
114 cat <<EOF
115To activate result and image proxy in searx, edit settings.yml (read:
116${DOCS_URL}/admin/morty.html)::
117 result_proxy:
118 url : ${PUBLIC_URL_MORTY}
119 server:
120 image_proxy : True
121EOF
124main() {
125 required_commands \
126 sudo install git wget curl \
127 || exit
129 local _usage="ERROR: unknown or missing $1 command $2"
131 case $1 in
132 --getenv) var="$2"; echo "${!var}"; exit 0;;
133 -h|--help) usage; exit 0;;
135 shell)
136 sudo_or_exit
137 interactive_shell "${SERVICE_USER}"
138 ;;
139 inspect)
140 case $2 in
141 service)
142 sudo_or_exit
143 inspect_service
144 ;;
145 *) usage "$_usage"; exit 42;;
146 esac ;;
147 install)
148 rst_title "$SERVICE_NAME" part
149 sudo_or_exit
150 case $2 in
151 all) install_all ;;
152 user) assert_user ;;
153 *) usage "$_usage"; exit 42;;
154 esac ;;
155 update)
156 sudo_or_exit
157 case $2 in
158 morty) update_morty ;;
159 *) usage "$_usage"; exit 42;;
160 esac ;;
161 remove)
162 sudo_or_exit
163 case $2 in
164 all) remove_all;;
165 user) drop_service_account "${SERVICE_USER}" ;;
166 *) usage "$_usage"; exit 42;;
167 esac ;;
168 activate)
169 sudo_or_exit
170 case $2 in
171 service) systemd_activate_service "${SERVICE_NAME}" ;;
172 *) usage "$_usage"; exit 42;;
173 esac ;;
174 deactivate)
175 sudo_or_exit
176 case $2 in
177 service) systemd_deactivate_service "${SERVICE_NAME}" ;;
178 *) usage "$_usage"; exit 42;;
179 esac ;;
180 apache)
181 sudo_or_exit
182 case $2 in
183 install) install_apache_site ;;
184 remove) remove_apache_site ;;
185 *) usage "$_usage"; exit 42;;
186 esac ;;
187 nginx)
188 sudo_or_exit
189 case $2 in
190 install) install_nginx_site ;;
191 remove) remove_nginx_site ;;
192 *) usage "$_usage"; exit 42;;
193 esac ;;
194 info)
195 case $2 in
196 searx) info_searx ;;
197 *) usage "$_usage"; exit 42;;
198 esac ;;
199 option)
200 sudo_or_exit
201 case $2 in
202 new-key) set_new_key ;;
203 debug-on) enable_debug ;;
204 debug-off) disable_debug ;;
205 *) usage "$_usage"; exit 42;;
206 esac ;;
207 doc) rst-doc ;;
208 *) usage "ERROR: unknown or missing command $1"; exit 42;;
209 esac
212install_all() {
214 MORTY_KEY="$(head -c 32 /dev/urandom | base64)"
216 rst_title "Install $SERVICE_NAME (service)"
217 assert_user
218 wait_key
219 install_go "${GO_PKG_URL}" "${GO_TAR}" "${SERVICE_USER}"
220 wait_key
221 install_morty
222 wait_key
223 systemd_install_service "${SERVICE_NAME}" "${SERVICE_SYSTEMD_UNIT}"
224 wait_key
225 if ! service_is_available "http://${MORTY_LISTEN}" ; then
226 err_msg "Morty does not listening on: http://${MORTY_LISTEN}"
227 fi
228 if apache_is_installed; then
229 info_msg "Apache is installed on this host."
230 if ask_yn "Do you want to install a reverse proxy (ProxyPass)" Yn; then
231 install_apache_site
232 fi
233 elif nginx_is_installed; then
234 info_msg "nginx is installed on this host."
235 if ask_yn "Do you want to install a reverse proxy (ProxyPass)" Yn; then
236 install_nginx_site
237 fi
238 fi
239 info_searx
240 if ask_yn "Add image and result proxy to searx settings.yml?" Yn; then
241 "${REPO_ROOT}/utils/searx.sh" option result-proxy "${PUBLIC_URL_MORTY}" "${MORTY_KEY}"
242 "${REPO_ROOT}/utils/searx.sh" option image-proxy-on
243 fi
245 if ask_yn "Do you want to inspect the installation?" Ny; then
246 inspect_service
247 fi
251remove_all() {
252 rst_title "De-Install $SERVICE_NAME (service)"
254 rst_para "\
255It goes without saying that this script can only be used to remove
256installations that were installed with this script."
258 if systemd_remove_service "${SERVICE_NAME}" "${SERVICE_SYSTEMD_UNIT}"; then
259 drop_service_account "${SERVICE_USER}"
260 fi
263assert_user() {
264 rst_title "user $SERVICE_USER" section
265 echo
266 tee_stderr 1 <<EOF | bash | prefix_stdout
267useradd --shell /bin/bash --system \
268 --home-dir "$SERVICE_HOME" \
269 --comment 'Web content sanitizer proxy' $SERVICE_USER
270mkdir "$SERVICE_HOME"
271chown -R "$SERVICE_GROUP:$SERVICE_GROUP" "$SERVICE_HOME"
272groups $SERVICE_USER
273EOF
274 SERVICE_HOME="$(sudo -i -u "$SERVICE_USER" echo \$HOME)"
275 export SERVICE_HOME
276 echo "export SERVICE_HOME=$SERVICE_HOME"
278 cat > "$GO_ENV" <<EOF
279export GOPATH=\$HOME/go-apps
280export PATH=\$PATH:\$HOME/local/go/bin:\$GOPATH/bin
281EOF
282 echo "Environment $GO_ENV has been setup."
284 tee_stderr <<EOF | sudo -i -u "$SERVICE_USER"
285grep -qFs -- 'source $GO_ENV' ~/.profile || echo 'source $GO_ENV' >> ~/.profile
286EOF
289morty_is_installed() {
290 [[ -f $SERVICE_HOME/go-apps/bin/morty ]]
293_svcpr=" ${_Yellow}|${SERVICE_USER}|${_creset} "
295install_morty() {
296 rst_title "Install morty in user's ~/go-apps" section
297 echo
298 tee_stderr <<EOF | sudo -i -u "$SERVICE_USER" 2>&1 | prefix_stdout "$_svcpr"
299go get -v -u github.com/asciimoo/morty
300EOF
301 tee_stderr <<EOF | sudo -i -u "$SERVICE_USER" 2>&1 | prefix_stdout "$_svcpr"
302cd \$GOPATH/src/github.com/asciimoo/morty
303go test
304go test -benchmem -bench .
305EOF
308update_morty() {
309 rst_title "Update morty" section
310 echo
311 tee_stderr <<EOF | sudo -i -u "$SERVICE_USER" 2>&1 | prefix_stdout "$_svcpr"
312go get -v -u github.com/asciimoo/morty
313EOF
314 tee_stderr <<EOF | sudo -i -u "$SERVICE_USER" 2>&1 | prefix_stdout "$_svcpr"
315cd \$GOPATH/src/github.com/asciimoo/morty
316go test
317go test -benchmem -bench .
318EOF
321set_service_env_debug() {
323 # usage: set_service_env_debug [false|true]
325 # shellcheck disable=SC2034
326 local SERVICE_ENV_DEBUG="${1:-false}"
327 if systemd_remove_service "${SERVICE_NAME}" "${SERVICE_SYSTEMD_UNIT}"; then
328 systemd_install_service "${SERVICE_NAME}" "${SERVICE_SYSTEMD_UNIT}"
329 fi
332inspect_service() {
334 rst_title "service status & log"
336 cat <<EOF
338sourced ${DOT_CONFIG#"$REPO_ROOT/"} :
340 MORTY_LISTEN : ${MORTY_LISTEN}
342EOF
344 if service_account_is_available "$SERVICE_USER"; then
345 info_msg "service account $SERVICE_USER available."
346 else
347 err_msg "service account $SERVICE_USER not available!"
348 fi
349 if go_is_available "$SERVICE_USER"; then
350 info_msg "~$SERVICE_USER: go is installed"
351 else
352 err_msg "~$SERVICE_USER: go is not installed"
353 fi
354 if morty_is_installed; then
355 info_msg "~$SERVICE_USER: morty app is installed"
356 else
357 err_msg "~$SERVICE_USER: morty app is not installed!"
358 fi
360 if ! service_is_available "http://${MORTY_LISTEN}" ; then
361 err_msg "Morty does not listening on: http://${MORTY_LISTEN}"
362 echo -e "${_Green}stop with [${_BCyan}CTRL-C${_Green}] or .."
363 wait_key
364 fi
366 if ! service_is_available "${PUBLIC_URL_MORTY}"; then
367 warn_msg "Public service at ${PUBLIC_URL_MORTY} is not available!"
368 if ! in_container; then
369 warn_msg "Check if public name is correct and routed or use the public IP from above."
370 fi
371 fi
373 if in_container; then
374 lxc_suite_info
375 else
376 info_msg "public URL --> ${PUBLIC_URL_MORTY}"
377 info_msg "morty URL --> http://${MORTY_LISTEN}"
378 fi
380 local _debug_on
381 if ask_yn "Enable morty debug mode (needs reinstall of systemd service)?"; then
382 enable_debug
383 _debug_on=1
384 else
385 systemctl --no-pager -l status "${SERVICE_NAME}"
386 fi
387 echo
389 # shellcheck disable=SC2059
390 printf "// use ${_BCyan}CTRL-C${_creset} to stop monitoring the log"
391 read -r -s -n1 -t 5
392 echo
393 while true; do
394 trap break 2
395 journalctl -f -u "${SERVICE_NAME}"
396 done
398 if [[ $_debug_on == 1 ]]; then
399 FORCE_SELECTION=Y disable_debug
400 fi
401 return 0
404enable_debug() {
405 warn_msg "Do not enable debug in production enviroments!!"
406 info_msg "Enabling debug option needs to reinstall systemd service!"
407 set_service_env_debug true
410disable_debug() {
411 info_msg "Disabling debug option needs to reinstall systemd service!"
412 set_service_env_debug false
416set_new_key() {
417 rst_title "Set morty key"
418 echo
420 MORTY_KEY="$(head -c 32 /dev/urandom | base64)"
421 info_msg "morty key: '${MORTY_KEY}'"
423 warn_msg "this will need to reinstall services .."
424 MSG="${_Green}press any [${_BCyan}KEY${_Green}] to continue // stop with [${_BCyan}CTRL-C${_creset}]" wait_key
426 systemd_install_service "${SERVICE_NAME}" "${SERVICE_SYSTEMD_UNIT}"
427 "${REPO_ROOT}/utils/searx.sh" option result-proxy "${PUBLIC_URL_MORTY}" "${MORTY_KEY}"
428 "${REPO_ROOT}/utils/searx.sh" option image-proxy-on
432install_apache_site() {
434 rst_title "Install Apache site $APACHE_MORTY_SITE"
436 rst_para "\
437This installs a reverse proxy (ProxyPass) into apache site (${APACHE_MORTY_SITE})"
439 ! apache_is_installed && err_msg "Apache is not installed."
441 if ! ask_yn "Do you really want to continue?" Yn; then
442 return
443 else
444 install_apache
445 fi
447 apache_install_site "${APACHE_MORTY_SITE}"
449 info_msg "testing public url .."
450 if ! service_is_available "${PUBLIC_URL_MORTY}"; then
451 err_msg "Public service at ${PUBLIC_URL_MORTY} is not available!"
452 fi
455remove_apache_site() {
457 rst_title "Remove Apache site $APACHE_MORTY_SITE"
459 rst_para "\
460This removes apache site ${APACHE_MORTY_SITE}."
462 ! apache_is_installed && err_msg "Apache is not installed."
464 if ! ask_yn "Do you really want to continue?" Yn; then
465 return
466 fi
468 apache_remove_site "$APACHE_MORTY_SITE"
471install_nginx_site() {
473 rst_title "Install nginx site $NGINX_MORTY_SITE"
475 rst_para "\
476This installs a reverse proxy (ProxyPass) into nginx site (${NGINX_MORTY_SITE})"
478 ! nginx_is_installed && err_msg "nginx is not installed."
480 if ! ask_yn "Do you really want to continue?" Yn; then
481 return
482 else
483 install_nginx
484 fi
486 "${REPO_ROOT}/utils/searx.sh" install uwsgi
488 # shellcheck disable=SC2034
489 SEARX_SRC=$("${REPO_ROOT}/utils/searx.sh" --getenv SEARX_SRC)
490 # shellcheck disable=SC2034
491 SEARX_URL_PATH=$("${REPO_ROOT}/utils/searx.sh" --getenv SEARX_URL_PATH)
492 nginx_install_app "${NGINX_MORTY_SITE}"
494 info_msg "testing public url .."
495 if ! service_is_available "${PUBLIC_URL_MORTY}"; then
496 err_msg "Public service at ${PUBLIC_URL_MORTY} is not available!"
497 fi
500remove_nginx_site() {
502 rst_title "Remove nginx site $NGINX_MORTY_SITE"
504 rst_para "\
505This removes nginx site ${NGINX_MORTY_SITE}."
507 ! nginx_is_installed && err_msg "nginx is not installed."
509 if ! ask_yn "Do you really want to continue?" Yn; then
510 return
511 fi
513 nginx_remove_site "$NGINX_MORTY_SITE"
517rst-doc() {
519 eval "echo \"$(< "${REPO_ROOT}/docs/build-templates/morty.rst")\""
521 echo -e "\n.. START install systemd unit"
522 cat <<EOF
523.. tabs::
525 .. group-tab:: systemd
527 .. code:: bash
529EOF
530 eval "echo \"$(< "${TEMPLATES}/${SERVICE_SYSTEMD_UNIT}")\"" | prefix_stdout " "
531 echo -e "\n.. END install systemd unit"
533 # for DIST_NAME in ubuntu-20.04 arch fedora centos; do
534 # (
535 # DIST_ID=${DIST_NAME%-*}
536 # DIST_VERS=${DIST_NAME#*-}
537 # [[ $DIST_VERS =~ $DIST_ID ]] && DIST_VERS=
538 # # ...
539 # )
540 # done
544# ----------------------------------------------------------------------------
545main "$@"
546# ----------------------------------------------------------------------------