From 7167a7ee36e9198c01b5d5c45d9801d9d225d2de Mon Sep 17 00:00:00 2001 From: Markus Heiser Date: Tue, 29 Jun 2021 21:36:32 +0200 Subject: [PATCH] [mod] utils/.searx.sh: add commands: install [dot-config|init-src] This patch implements two new commands: install dot-config : - copy ./config.sh to ${SEARX_SRC} - implemented in new function install_DOT_CONFIG install init-src: - sync files (SEARX_SRC_INIT_FILES) with ${SEARX_SRC} - implemented in new function init_SEARX_SRC() new functions: verify_continue_install() and prompt_installation_status() These functions are used in installation procedures to get the status of the installation procedure. The status is based on the existing function: usage: install_searx_get_state Prompts a string indicating the status of the installation procedure missing-searx-clone: There is no clone at ${SEARX_SRC} missing-searx-pyenv: There is no pyenv in ${SEARX_PYENV} installer-modified: There are files modified locally in the installer (clone), see ${SEARX_SRC_INIT_FILES} description. python-installed: Scripts can be executed in instance's environment - user: ${SERVICE_USER} - pyenv: ${SEARX_PYENV} Signed-off-by: Markus Heiser --- utils/searx.sh | 183 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 140 insertions(+), 43 deletions(-) diff --git a/utils/searx.sh b/utils/searx.sh index e8c2720ec..cfe6cbcda 100755 --- a/utils/searx.sh +++ b/utils/searx.sh @@ -141,7 +141,7 @@ usage() { cat < /dev/null } +prompt_installation_status(){ + local _state + _state="$(install_searx_get_state)" + case $_state in + missing-searx-clone) + info_msg "${_BBlue}(status: $(install_searx_get_state))${_creset}" + return 0 + ;; + *) + warn_msg "SearXNG instance already installed at: $SEARX_SRC" + warn_msg "status: ${_BBlue}$(install_searx_get_state)${_creset} " + return 42 + ;; + esac +} + +verify_continue_install(){ + if ! prompt_installation_status; then + MSG="[${_BCyan}KEY${_creset}] to continue installation / [${_BCyan}CTRL-C${_creset}] to exit" \ + wait_key + fi +} + +init_SEARX_SRC(){ + rst_title "Update instance: ${SEARX_SRC}/" section + + if ! clone_is_available; then + err_msg "you have to install searx first" + return 1 + fi + + init_SEARX_SRC_INIT_FILES + + if [ ${#SEARX_SRC_INIT_FILES[*]} -eq 0 ]; then + info_msg "no files registered in SEARX_SRC_INIT_FILES" + return 2 + fi + + echo + echo "Manipulating files like settings.yml can break existing installation!" + echo "Update instance with file(s) from: ${REPO_ROOT}" + echo + for i in "${SEARX_SRC_INIT_FILES[@]}"; do + echo "- $i" + done + if ! ask_yn "Do you really want to update these files in the instance?" Yn; then + return 42 + fi + for fname in "${SEARX_SRC_INIT_FILES[@]}"; do + while true; do + choose_one _reply "choose next step with file ${fname}" \ + "leave file unchanged" \ + "replace file" \ + "diff files" \ + "interactive shell" + + case $_reply in + "leave file unchanged") + break + ;; + "replace file") + info_msg "copy: ${REPO_ROOT}/${fname} --> ${SEARX_SRC}/${fname}" + cp "${REPO_ROOT}/${fname}" "${SEARX_SRC}/${fname}" + break + ;; + "diff files") + $DIFF_CMD "${SEARX_SRC}/${fname}" "${REPO_ROOT}/${fname}" + ;; + "interactive shell") + backup_file "${SEARX_SRC}/${fname}" + echo -e "// edit ${_Red}${dst}${_creset} to your needs" + echo -e "// exit with [${_BCyan}CTRL-D${_creset}]" + sudo -H -u "${SERVICE_USER}" -i + $DIFF_CMD "${SEARX_SRC}/${fname}" "${REPO_ROOT}/${fname}" + echo + echo -e "// ${_BBlack}did you edit file ...${_creset}" + echo -en "// ${_Red}${dst}${_creset}" + if ask_yn "//${_BBlack}... to your needs?${_creset}"; then + break + fi + ;; + esac + done + done +} + +install_DOT_CONFIG(){ + rst_title "Update instance: ${SEARX_SRC}/.config.sh" section + + if cmp --silent "${REPO_ROOT}/.config.sh" "${SEARX_SRC}/.config.sh"; then + info_msg "${SEARX_SRC}/.config.sh is up to date" + return 0 + fi + + diff "${REPO_ROOT}/.config.sh" "${SEARX_SRC}/.config.sh" + if ! ask_yn "Do you want to copy file .config.sh into instance?" Yn; then + return 42 + fi + backup_file "${SEARX_SRC}/.config.sh" + cp "${REPO_ROOT}/.config.sh" "${SEARX_SRC}/.config.sh" +} + install_settings() { rst_title "${SEARX_SETTINGS_PATH}" section + if ! clone_is_available; then err_msg "you have to install searx first" exit 42 fi + mkdir -p "$(dirname "${SEARX_SETTINGS_PATH}")" - - if [[ ! -f "${SEARX_SETTINGS_PATH}" ]]; then - info_msg "install settings ${SEARX_SETTINGS_TEMPLATE}" - info_msg " --> ${SEARX_SETTINGS_PATH}" - cp "${SEARX_SETTINGS_TEMPLATE}" "${SEARX_SETTINGS_PATH}" - configure_searx - return - fi - - rst_para "Diff between origin's setting file (+) and current (-):" - echo "${SEARX_SETTINGS_PATH}" "${SEARX_SETTINGS_TEMPLATE}" - $DIFF_CMD "${SEARX_SETTINGS_PATH}" "${SEARX_SETTINGS_TEMPLATE}" - - local action - choose_one action "What should happen to the settings file? " \ - "keep configuration unchanged" \ - "use origin settings" \ - "start interactive shell" - case $action in - "keep configuration unchanged") - info_msg "leave settings file unchanged" - ;; - "use origin settings") - backup_file "${SEARX_SETTINGS_PATH}" - info_msg "install origin settings" - cp "${SEARX_SETTINGS_TEMPLATE}" "${SEARX_SETTINGS_PATH}" - ;; - "start interactive shell") - backup_file "${SEARX_SETTINGS_PATH}" - echo -e "// exit with [${_BCyan}CTRL-D${_creset}]" - sudo -H -i - rst_para 'Diff between new setting file (-) and current (+):' - echo - $DIFF_CMD "${SEARX_SETTINGS_TEMPLATE}" "${SEARX_SETTINGS_PATH}" - wait_key - ;; - esac + install_template \ + "${SEARX_SETTINGS_PATH}" \ + "${SERVICE_USER}" "${SERVICE_GROUP}" + configure_searx } remove_settings() {