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"
10SEARX_URL="${PUBLIC_URL:-http://$(uname -n)/searx}"
11source "${REPO_ROOT}/utils/lxc-searx.env"
12in_container && lxc_set_suite_env
14# ----------------------------------------------------------------------------
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
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
45APACHE_MORTY_SITE="morty.conf"
46NGINX_MORTY_SITE="morty.conf"
48# ----------------------------------------------------------------------------
50# ----------------------------------------------------------------------------
52 # shellcheck disable=SC1117
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]
68 start interactive shell from user ${SERVICE_USER}
70 all: complete setup of morty service
71 user: add/remove service user '$SERVICE_USER' ($SERVICE_HOME)
73 Update morty installation ($SERVICE_HOME)
75 activate and start service daemon (systemd unit)
77 stop and deactivate service daemon (systemd unit)
79 show service status and log
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}
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/"
102 echo " container URL (IPv4): http://${ip#*|}:3000/"
109 [[ -n ${1} ]] && err_msg "$1"
113 # shellcheck disable=SC1117
115To activate result and image proxy in searx, edit settings.yml (read:
116${DOCS_URL}/admin/morty.html)::
118 url : ${PUBLIC_URL_MORTY}
126 sudo install git wget curl \
129 local _usage="ERROR: unknown or missing $1 command $2"
132 --getenv) var="$2"; echo "${!var}"; exit 0;;
133 -h|--help) usage; exit 0;;
137 interactive_shell "${SERVICE_USER}"
145 *) usage "$_usage"; exit 42;;
148 rst_title "$SERVICE_NAME" part
153 *) usage "$_usage"; exit 42;;
158 morty) update_morty ;;
159 *) usage "$_usage"; exit 42;;
165 user) drop_service_account "${SERVICE_USER}" ;;
166 *) usage "$_usage"; exit 42;;
171 service) systemd_activate_service "${SERVICE_NAME}" ;;
172 *) usage "$_usage"; exit 42;;
177 service) systemd_deactivate_service "${SERVICE_NAME}" ;;
178 *) usage "$_usage"; exit 42;;
183 install) install_apache_site ;;
184 remove) remove_apache_site ;;
185 *) usage "$_usage"; exit 42;;
190 install) install_nginx_site ;;
191 remove) remove_nginx_site ;;
192 *) usage "$_usage"; exit 42;;
197 *) usage "$_usage"; exit 42;;
202 new-key) set_new_key ;;
203 debug-on) enable_debug ;;
204 debug-off) disable_debug ;;
205 *) usage "$_usage"; exit 42;;
208 *) usage "ERROR: unknown or missing command $1"; exit 42;;
214 MORTY_KEY="$(head -c 32 /dev/urandom | base64)"
216 rst_title "Install $SERVICE_NAME (service)"
219 install_go "${GO_PKG_URL}" "${GO_TAR}" "${SERVICE_USER}"
223 systemd_install_service "${SERVICE_NAME}" "${SERVICE_SYSTEMD_UNIT}"
225 if ! service_is_available "http://${MORTY_LISTEN}" ; then
226 err_msg "Morty does not listening on: http://${MORTY_LISTEN}"
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
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
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
245 if ask_yn "Do you want to inspect the installation?" Ny; then
252 rst_title "De-Install $SERVICE_NAME (service)"
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}"
264 rst_title "user $SERVICE_USER" section
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
271chown -R "$SERVICE_GROUP:$SERVICE_GROUP" "$SERVICE_HOME"
274 SERVICE_HOME="$(sudo -i -u "$SERVICE_USER" echo \$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
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
289morty_is_installed() {
290 [[ -f $SERVICE_HOME/go-apps/bin/morty ]]
293_svcpr=" ${_Yellow}|${SERVICE_USER}|${_creset} "
296 rst_title "Install morty in user's ~/go-apps" section
298 tee_stderr <<EOF | sudo -i -u "$SERVICE_USER" 2>&1 | prefix_stdout "$_svcpr"
299go get -v -u github.com/asciimoo/morty
301 tee_stderr <<EOF | sudo -i -u "$SERVICE_USER" 2>&1 | prefix_stdout "$_svcpr"
302cd \$GOPATH/src/github.com/asciimoo/morty
304go test -benchmem -bench .
309 rst_title "Update morty" section
311 tee_stderr <<EOF | sudo -i -u "$SERVICE_USER" 2>&1 | prefix_stdout "$_svcpr"
312go get -v -u github.com/asciimoo/morty
314 tee_stderr <<EOF | sudo -i -u "$SERVICE_USER" 2>&1 | prefix_stdout "$_svcpr"
315cd \$GOPATH/src/github.com/asciimoo/morty
317go test -benchmem -bench .
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}"
334 rst_title "service status & log"
338sourced ${DOT_CONFIG#"$REPO_ROOT/"} :
340 MORTY_LISTEN : ${MORTY_LISTEN}
344 if service_account_is_available "$SERVICE_USER"; then
345 info_msg "service account $SERVICE_USER available."
347 err_msg "service account $SERVICE_USER not available!"
349 if go_is_available "$SERVICE_USER"; then
350 info_msg "~$SERVICE_USER: go is installed"
352 err_msg "~$SERVICE_USER: go is not installed"
354 if morty_is_installed; then
355 info_msg "~$SERVICE_USER: morty app is installed"
357 err_msg "~$SERVICE_USER: morty app is not installed!"
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 .."
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."
373 if in_container; then
376 info_msg "public URL --> ${PUBLIC_URL_MORTY}"
377 info_msg "morty URL --> http://${MORTY_LISTEN}"
381 if ask_yn "Enable morty debug mode (needs reinstall of systemd service)?"; then
385 systemctl --no-pager -l status "${SERVICE_NAME}"
389 # shellcheck disable=SC2059
390 printf "// use ${_BCyan}CTRL-C${_creset} to stop monitoring the log"
395 journalctl -f -u "${SERVICE_NAME}"
398 if [[ $_debug_on == 1 ]]; then
399 FORCE_SELECTION=Y disable_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
411 info_msg "Disabling debug option needs to reinstall systemd service!"
412 set_service_env_debug false
417 rst_title "Set morty key"
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"
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
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!"
455remove_apache_site() {
457 rst_title "Remove Apache site $APACHE_MORTY_SITE"
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
468 apache_remove_site "$APACHE_MORTY_SITE"
471install_nginx_site() {
473 rst_title "Install nginx site $NGINX_MORTY_SITE"
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
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!"
502 rst_title "Remove nginx site $NGINX_MORTY_SITE"
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
513 nginx_remove_site "$NGINX_MORTY_SITE"
519 eval "echo \"$(< "${REPO_ROOT}/docs/build-templates/morty.rst")\""
521 echo -e "\n.. START install systemd unit"
525 .. group-tab:: systemd
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
535 # DIST_ID=${DIST_NAME%-*}
536 # DIST_VERS=${DIST_NAME#*-}
537 # [[ $DIST_VERS =~ $DIST_ID ]] && DIST_VERS=
544# ----------------------------------------------------------------------------
546# ----------------------------------------------------------------------------
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list