lib.sh 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. #!/usr/bin/env bash
  2. # -*- coding: utf-8; mode: sh indent-tabs-mode: nil -*-
  3. # SPDX-License-Identifier: AGPL-3.0-or-later
  4. # shellcheck disable=SC2059,SC1117
  5. ADMIN_NAME="${ADMIN_NAME:-$(git config user.name)}"
  6. ADMIN_NAME="${ADMIN_NAME:-$USER}"
  7. ADMIN_EMAIL="${ADMIN_EMAIL:-$(git config user.email)}"
  8. ADMIN_EMAIL="${ADMIN_EMAIL:-$USER@$(hostname)}"
  9. if [[ -z "${REPO_ROOT}" ]]; then
  10. REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")
  11. while [ -h "${REPO_ROOT}" ] ; do
  12. REPO_ROOT=$(readlink "${REPO_ROOT}")
  13. done
  14. REPO_ROOT=$(cd "${REPO_ROOT}/.." && pwd -P )
  15. fi
  16. if [[ -z ${TEMPLATES} ]]; then
  17. TEMPLATES="${REPO_ROOT}/utils/templates"
  18. fi
  19. if [[ -z "$CACHE" ]]; then
  20. CACHE="${REPO_ROOT}/cache"
  21. fi
  22. if [[ -z ${DIFF_CMD} ]]; then
  23. DIFF_CMD="diff -u"
  24. if command -v colordiff >/dev/null; then
  25. DIFF_CMD="colordiff -u"
  26. fi
  27. fi
  28. DOT_CONFIG="${DOT_CONFIG:-${REPO_ROOT}/.config}"
  29. source_dot_config() {
  30. if [[ ! -e "$DOT_CONFIG" ]]; then
  31. info_msg "installing $DOT_CONFIG"
  32. cp "$(dirname "${BASH_SOURCE[0]}")/dot_config" "$DOT_CONFIG"
  33. if [[ ! -z ${SUDO_USER} ]]; then
  34. chown "${SUDO_USER}:${SUDO_USER}" "$DOT_CONFIG"
  35. fi
  36. fi
  37. # shellcheck disable=SC1090
  38. source "${REPO_ROOT}/.config"
  39. }
  40. sudo_or_exit() {
  41. # usage: sudo_or_exit
  42. if [ ! "$(id -u)" -eq 0 ]; then
  43. err_msg "this command requires root (sudo) privilege!" >&2
  44. exit 42
  45. fi
  46. }
  47. required_commands() {
  48. # usage: requires_commands [cmd1 ...]
  49. local exit_val=0
  50. while [ ! -z "$1" ]; do
  51. if ! command -v "$1" &>/dev/null; then
  52. err_msg "missing command $1"
  53. exit_val=42
  54. fi
  55. shift
  56. done
  57. return $exit_val
  58. }
  59. # colors
  60. # ------
  61. # shellcheck disable=SC2034
  62. set_terminal_colors() {
  63. _colors=8
  64. _creset='\e[0m' # reset all attributes
  65. _Black='\e[0;30m'
  66. _White='\e[1;37m'
  67. _Red='\e[0;31m'
  68. _Green='\e[0;32m'
  69. _Yellow='\e[0;33m'
  70. _Blue='\e[0;34m'
  71. _Violet='\e[0;35m'
  72. _Cyan='\e[0;36m'
  73. _BBlack='\e[1;30m'
  74. _BWhite='\e[1;37m'
  75. _BRed='\e[1;31m'
  76. _BGreen='\e[1;32m'
  77. _BYellow='\e[1;33m'
  78. _BBlue='\e[1;34m'
  79. _BPurple='\e[1;35m'
  80. _BCyan='\e[1;36m'
  81. }
  82. set_terminal_colors
  83. # reST
  84. # ----
  85. if command -v fmt >/dev/null; then
  86. export FMT="fmt -u"
  87. else
  88. export FMT="cat"
  89. fi
  90. rst_title() {
  91. # usage: rst_title <header-text> [part|chapter|section]
  92. case ${2-chapter} in
  93. part) printf "\n${_BGreen}${1//?/=}\n${_BCyan}${1}${_BGreen}\n${1//?/=}${_creset}\n";;
  94. chapter) printf "\n${_BCyan}${1}\n${_BGreen}${1//?/=}${_creset}\n";;
  95. section) printf "\n${_BCyan}${1}\n${_BGreen}${1//?/-}${_creset}\n";;
  96. *)
  97. err_msg "invalid argument '${2}' in line $(caller)"
  98. return 42
  99. ;;
  100. esac
  101. }
  102. rst_para() {
  103. # usage: RST_INDENT=1 rst_para "lorem ipsum ..."
  104. local prefix=''
  105. if ! [[ -z $RST_INDENT ]] && [[ $RST_INDENT -gt 0 ]]; then
  106. prefix="$(for i in $(seq 1 "$RST_INDENT"); do printf " "; done)"
  107. echo -en "\n$*\n" | $FMT | prefix_stdout "$prefix"
  108. else
  109. echo -en "\n$*\n" | $FMT
  110. fi
  111. }
  112. err_msg() { echo -e "${_BRed}ERROR:${_creset} $*" >&2; }
  113. warn_msg() { echo -e "${_BBlue}WARN:${_creset} $*" >&2; }
  114. info_msg() { echo -e "${_BYellow}INFO:${_creset} $*" >&2; }
  115. clean_stdin() {
  116. if [[ $(uname -s) != 'Darwin' ]]; then
  117. while read -r -n1 -t 0.1; do : ; done
  118. fi
  119. }
  120. wait_key(){
  121. # usage: waitKEY [<timeout in sec>]
  122. clean_stdin
  123. local _t=$1
  124. local msg="${MSG}"
  125. [[ -z "$msg" ]] && msg="${_Green}** press any [${_BCyan}KEY${_Green}] to continue **${_creset}"
  126. [[ ! -z $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
  127. [[ ! -z $_t ]] && _t="-t $_t"
  128. printf "$msg"
  129. # shellcheck disable=SC2086
  130. read -r -s -n1 $_t
  131. echo
  132. clean_stdin
  133. }
  134. ask_yn() {
  135. # usage: ask_yn <prompt-text> [Ny|Yn] [<timeout in sec>]
  136. local EXIT_YES=0 # exit status 0 --> successful
  137. local EXIT_NO=1 # exit status 1 --> error code
  138. local _t=$3
  139. [[ ! -z $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
  140. [[ ! -z $_t ]] && _t="-t $_t"
  141. case "${FORCE_SELECTION:-${2}}" in
  142. Y) return ${EXIT_YES} ;;
  143. N) return ${EXIT_NO} ;;
  144. Yn)
  145. local exit_val=${EXIT_YES}
  146. local choice="[${_BGreen}YES${_creset}/no]"
  147. local default="Yes"
  148. ;;
  149. *)
  150. local exit_val=${EXIT_NO}
  151. local choice="[${_BGreen}NO${_creset}/yes]"
  152. local default="No"
  153. ;;
  154. esac
  155. echo
  156. while true; do
  157. clean_stdin
  158. printf "$1 ${choice} "
  159. # shellcheck disable=SC2086
  160. read -r -n1 $_t
  161. if [[ -z $REPLY ]]; then
  162. printf "$default\n"; break
  163. elif [[ $REPLY =~ ^[Yy]$ ]]; then
  164. exit_val=${EXIT_YES}
  165. printf "\n"
  166. break
  167. elif [[ $REPLY =~ ^[Nn]$ ]]; then
  168. exit_val=${EXIT_NO}
  169. printf "\n"
  170. break
  171. fi
  172. _t=""
  173. err_msg "invalid choice"
  174. done
  175. clean_stdin
  176. return $exit_val
  177. }
  178. tee_stderr () {
  179. # usage::
  180. # tee_stderr 1 <<EOF | python -i
  181. # print("hello")
  182. # EOF
  183. # ...
  184. # >>> print("hello")
  185. # hello
  186. local _t="0";
  187. if [[ ! -z $1 ]] ; then _t="$1"; fi
  188. (while read -r line; do
  189. # shellcheck disable=SC2086
  190. sleep $_t
  191. echo -e "$line" >&2
  192. echo "$line"
  193. done)
  194. }
  195. prefix_stdout () {
  196. # usage: <cmd> | prefix_stdout [prefix]
  197. local prefix="${_BYellow}-->|${_creset}"
  198. if [[ ! -z $1 ]] ; then prefix="${_BYellow}$1${_creset}"; fi
  199. # shellcheck disable=SC2162
  200. (while IFS= read line; do
  201. echo -e "${prefix}$line"
  202. done)
  203. }
  204. append_line() {
  205. # usage: append_line <line> <file>
  206. #
  207. # Append line if not exists, create file if not exists. E.g::
  208. #
  209. # append_line 'source ~/.foo' ~/bashrc
  210. local LINE=$1
  211. local FILE=$2
  212. grep -qFs -- "$LINE" "$FILE" || echo "$LINE" >> "$FILE"
  213. }
  214. cache_download() {
  215. # usage: cache_download <url> <local-filename>
  216. local exit_value=0
  217. if [[ ! -z ${SUDO_USER} ]]; then
  218. sudo -u "${SUDO_USER}" mkdir -p "${CACHE}"
  219. else
  220. mkdir -p "${CACHE}"
  221. fi
  222. if [[ -f "${CACHE}/$2" ]] ; then
  223. info_msg "already cached: $1"
  224. info_msg " --> ${CACHE}/$2"
  225. fi
  226. if [[ ! -f "${CACHE}/$2" ]]; then
  227. info_msg "caching: $1"
  228. info_msg " --> ${CACHE}/$2"
  229. if [[ ! -z ${SUDO_USER} ]]; then
  230. sudo -u "${SUDO_USER}" wget --progress=bar -O "${CACHE}/$2" "$1" ; exit_value=$?
  231. else
  232. wget --progress=bar -O "${CACHE}/$2" "$1" ; exit_value=$?
  233. fi
  234. if [[ $exit_value = 0 ]]; then
  235. err_msg "failed to download: $1"
  236. fi
  237. fi
  238. }
  239. choose_one() {
  240. # usage:
  241. #
  242. # DEFAULT_SELECT= 2 \
  243. # choose_one <name> "your selection?" "Coffee" "Coffee with milk"
  244. local default=${DEFAULT_SELECT-1}
  245. local REPLY
  246. local env_name=$1 && shift
  247. local choice=$1;
  248. local max="${#@}"
  249. local _t
  250. [[ ! -z $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
  251. [[ ! -z $_t ]] && _t="-t $_t"
  252. list=("$@")
  253. echo -e "${_BGreen}Menu::${_creset}"
  254. for ((i=1; i<= $((max -1)); i++)); do
  255. if [[ "$i" == "$default" ]]; then
  256. echo -e " ${_BGreen}$i.${_creset}) ${list[$i]} [default]"
  257. else
  258. echo -e " $i.) ${list[$i]}"
  259. fi
  260. done
  261. while true; do
  262. clean_stdin
  263. printf "$1 [${_BGreen}$default${_creset}] "
  264. if (( 10 > max )); then
  265. # shellcheck disable=SC2086
  266. read -r -n1 $_t
  267. else
  268. # shellcheck disable=SC2086,SC2229
  269. read -r $_t
  270. fi
  271. # selection fits
  272. [[ $REPLY =~ ^-?[0-9]+$ ]] && (( REPLY > 0 )) && (( REPLY < max )) && break
  273. # take default
  274. [[ -z $REPLY ]] && REPLY=$default && break
  275. _t=""
  276. err_msg "invalid choice"
  277. done
  278. eval "$env_name"='${list[${REPLY}]}'
  279. echo
  280. clean_stdin
  281. }
  282. install_template() {
  283. # usage:
  284. #
  285. # install_template [--no-eval] [--variant=<name>] \
  286. # {file} [{owner} [{group} [{chmod}]]]
  287. #
  288. # E.g. the origin of variant 'raw' of /etc/updatedb.conf is::
  289. #
  290. # ${TEMPLATES}/etc/updatedb.conf:raw
  291. #
  292. # To install variant 'raw' of /etc/updatedb.conf without evaluated
  293. # replacements you can use::
  294. #
  295. # install_template --variant=raw --no-eval \
  296. # /etc/updatedb.conf root root 644
  297. local _reply=""
  298. local do_eval=1
  299. local variant=""
  300. local pos_args=("$0")
  301. for i in "$@"; do
  302. case $i in
  303. --no-eval) do_eval=0; shift ;;
  304. --variant=*) variant=":${i#*=}"; shift ;;
  305. *) pos_args+=("$i") ;;
  306. esac
  307. done
  308. local dst="${pos_args[1]}"
  309. local template_origin="${TEMPLATES}${dst}${variant}"
  310. local template_file="${TEMPLATES}${dst}"
  311. local owner="${pos_args[2]-$(id -un)}"
  312. local group="${pos_args[3]-$(id -gn)}"
  313. local chmod="${pos_args[4]-644}"
  314. info_msg "install (eval=$do_eval): ${dst}"
  315. [[ ! -z $variant ]] && info_msg "variant: ${variant}"
  316. if [[ ! -f "${template_origin}" ]] ; then
  317. err_msg "${template_origin} does not exists"
  318. err_msg "... can't install $dst"
  319. wait_key 30
  320. return 42
  321. fi
  322. if [[ "$do_eval" == "1" ]]; then
  323. template_file="${CACHE}${dst}${variant}"
  324. info_msg "BUILD template ${template_file}"
  325. if [[ ! -z ${SUDO_USER} ]]; then
  326. sudo -u "${SUDO_USER}" mkdir -p "$(dirname "${template_file}")"
  327. else
  328. mkdir -p "$(dirname "${template_file}")"
  329. fi
  330. # shellcheck disable=SC2086
  331. eval "echo \"$(cat ${template_origin})\"" > "${template_file}"
  332. if [[ ! -z ${SUDO_USER} ]]; then
  333. chown "${SUDO_USER}:${SUDO_USER}" "${template_file}"
  334. fi
  335. else
  336. template_file=$template_origin
  337. fi
  338. mkdir -p "$(dirname "${dst}")"
  339. if [[ ! -f "${dst}" ]]; then
  340. info_msg "install: ${template_file}"
  341. sudo -H install -v -o "${owner}" -g "${group}" -m "${chmod}" \
  342. "${template_file}" "${dst}" | prefix_stdout
  343. return $?
  344. fi
  345. if [[ -f "${dst}" ]] && cmp --silent "${template_file}" "${dst}" ; then
  346. info_msg "file ${dst} allready installed"
  347. return 0
  348. fi
  349. info_msg "diffrent file ${dst} allready exists on this host"
  350. while true; do
  351. choose_one _reply "choose next step with file $dst" \
  352. "replace file" \
  353. "leave file unchanged" \
  354. "interactiv shell" \
  355. "diff files"
  356. case $_reply in
  357. "replace file")
  358. info_msg "install: ${template_file}"
  359. sudo -H install -v -o "${owner}" -g "${group}" -m "${chmod}" \
  360. "${template_file}" "${dst}" | prefix_stdout
  361. break
  362. ;;
  363. "leave file unchanged")
  364. break
  365. ;;
  366. "interactiv shell")
  367. echo "// edit ${dst} to your needs"
  368. echo -e "// exit with [${_BCyan}CTRL-D${_creset}]"
  369. sudo -H -u "${owner}" -i
  370. $DIFF_CMD "${dst}" "${template_file}"
  371. echo
  372. echo "${_BBlack}did you edit file ...${_creset}"
  373. printf " ${template_file}"
  374. if ask_yn "... to your needs?"; then
  375. break
  376. fi
  377. ;;
  378. "diff files")
  379. $DIFF_CMD "${dst}" "${template_file}" | prefix_stdout
  380. esac
  381. done
  382. }
  383. service_is_available() {
  384. # usage: service_is_available <URL>
  385. local URL="$1"
  386. if [[ -z $URL ]]; then
  387. err_msg "service_is_available: missing arguments"
  388. return 42
  389. fi
  390. http_code=$(curl -H 'Cache-Control: no-cache' \
  391. --silent -o /dev/null --head --write-out '%{http_code}' --insecure \
  392. "${URL}")
  393. exit_val=$?
  394. if [[ $exit_val = 0 ]]; then
  395. info_msg "got $http_code from ${URL}"
  396. fi
  397. case "$http_code" in
  398. 404|410|423) exit_val=$http_code;;
  399. esac
  400. return "$exit_val"
  401. }
  402. # golang
  403. # ------
  404. go_is_available() {
  405. # usage: go_is_available $SERVICE_USER && echo "go is installed!"
  406. sudo -i -u "${1}" which go &>/dev/null
  407. }
  408. install_go() {
  409. # usage: install_go "${GO_PKG_URL}" "${GO_TAR}" "${SERVICE_USER}"
  410. local _svcpr=" |${3}| "
  411. rst_title "Install Go in user's HOME" section
  412. rst_para "download and install go binary .."
  413. cache_download "${1}" "${2}"
  414. tee_stderr 0.1 <<EOF | sudo -i -u "${3}" | prefix_stdout "$_svcpr"
  415. echo \$PATH
  416. echo \$GOPATH
  417. mkdir -p \$HOME/local
  418. rm -rf \$HOME/local/go
  419. tar -C \$HOME/local -xzf ${CACHE}/${2}
  420. EOF
  421. sudo -i -u "${3}" <<EOF | prefix_stdout
  422. ! which go >/dev/null && echo "ERROR - Go Installation not found in PATH!?!"
  423. which go >/dev/null && go version && echo "congratulations -- Go installation OK :)"
  424. EOF
  425. }
  426. # system accounts
  427. # ---------------
  428. service_account_is_available() {
  429. # usage: service_account_is_available "$SERVICE_USER" && echo "OK"
  430. sudo -i -u "$1" echo \$HOME &>/dev/null
  431. }
  432. drop_service_account() {
  433. # usage: drop_service_account "${SERVICE_USER}"
  434. rst_title "Drop ${1} HOME" section
  435. if ask_yn "Do you really want to drop ${1} home folder?"; then
  436. userdel -r -f "${1}" 2>&1 | prefix_stdout
  437. else
  438. rst_para "Leave HOME folder $(du -sh "${1}") unchanged."
  439. fi
  440. }
  441. interactive_shell(){
  442. # usage: interactive_shell "${SERVICE_USER}"
  443. echo -e "// exit with [${_BCyan}CTRL-D${_creset}]"
  444. sudo -H -u "${1}" -i
  445. }
  446. # systemd
  447. # -------
  448. SYSTEMD_UNITS="${SYSTEMD_UNITS:-/lib/systemd/system}"
  449. systemd_install_service() {
  450. # usage: systemd_install_service "${SERVICE_NAME}" "${SERVICE_SYSTEMD_UNIT}"
  451. rst_title "Install System-D Unit ${1}" section
  452. echo
  453. install_template "${2}" root root 644
  454. wait_key
  455. systemd_activate_service "${1}"
  456. }
  457. systemd_remove_service() {
  458. # usage: systemd_remove_service "${SERVICE_NAME}" "${SERVICE_SYSTEMD_UNIT}"
  459. if ! ask_yn "Do you really want to deinstall systemd unit ${1}?"; then
  460. return 42
  461. fi
  462. systemd_deactivate_service "${1}"
  463. rm "${2}" 2>&1 | prefix_stdout
  464. }
  465. systemd_activate_service() {
  466. # usage: systemd_activate_service "${SERVICE_NAME}"w
  467. rst_title "Activate ${1} (service)" section
  468. echo
  469. tee_stderr <<EOF | bash 2>&1
  470. systemctl enable ${1}.service
  471. systemctl restart ${1}.service
  472. EOF
  473. tee_stderr <<EOF | bash 2>&1
  474. systemctl status --no-pager ${1}.service
  475. EOF
  476. }
  477. systemd_deactivate_service() {
  478. # usage: systemd_deactivate_service "${SERVICE_NAME}"
  479. rst_title "De-Activate ${1} (service)" section
  480. echo
  481. tee_stderr <<EOF | bash 2>&1 | prefix_stdout
  482. systemctl stop ${1}.service
  483. systemctl disable ${1}.service
  484. EOF
  485. }
  486. # Apache
  487. # ------
  488. # FIXME: Arch Linux & RHEL should be added
  489. if [[ -z "${APACHE_SITES_AVAILABE}" ]]; then
  490. APACHE_SITES_AVAILABE="/etc/apache2/sites-available"
  491. fi
  492. apache_is_installed() {
  493. (command -v apachectl \
  494. && command -v a2ensite \
  495. && command -v a2dissite ) &>/dev/null
  496. }
  497. apache_reload() {
  498. info_msg "reload apache .."
  499. echo
  500. sudo -H apachectl configtest
  501. sudo -H service apache2 force-reload
  502. }
  503. apache_install_site() {
  504. # usage: apache_install_site [<template option> ...] <mysite.conf>
  505. #
  506. # <template option>: see install_template
  507. local template_opts=()
  508. local pos_args=("$0")
  509. for i in "$@"; do
  510. case $i in
  511. -*) template_opts+=("$i");;
  512. *) pos_args+=("$i");;
  513. esac
  514. done
  515. install_template "${template_opts[@]}" \
  516. "${APACHE_SITES_AVAILABE}/${pos_args[1]}" \
  517. root root 644
  518. apache_enable_site "${pos_args[1]}"
  519. info_msg "installed apache site: ${pos_args[1]}"
  520. }
  521. apache_remove_site() {
  522. # usage: apache_remove_site <mysite.conf>
  523. info_msg "remove apache site: $1"
  524. apache_dissable_site "$1"
  525. rm -f "${APACHE_SITES_AVAILABE}/$1"
  526. }
  527. apache_enable_site() {
  528. # usage: apache_enable_site <mysite.conf>
  529. info_msg "enable apache site: $1"
  530. sudo -H a2ensite -q "$1"
  531. apache_reload
  532. }
  533. apache_dissable_site() {
  534. # usage: apache_disable_site <mysite.conf>
  535. info_msg "disable apache site: $1"
  536. sudo -H a2dissite -q "$1"
  537. apache_reload
  538. }
  539. # uWSGI
  540. # -----
  541. uWSGI_SETUP="${uWSGI_SETUP:=/etc/uwsgi}"
  542. uWSGI_restart() {
  543. # usage: uWSGI_restart()
  544. info_msg "restart uWSGI service"
  545. systemctl restart uwsgi
  546. }
  547. uWSGI_app_available() {
  548. # usage: uWSGI_app_available <myapp.ini>
  549. local CONF="$1"
  550. if [[ -z $CONF ]]; then
  551. err_msg "uWSGI_app_available: missing arguments"
  552. return 42
  553. fi
  554. [[ -f "${uWSGI_SETUP}/apps-available/${CONF}" ]]
  555. }
  556. uWSGI_install_app() {
  557. # usage: uWSGI_install_app [<template option> ...] <myapp.ini>
  558. #
  559. # <template option>: see install_template
  560. local pos_args=("$0")
  561. for i in "$@"; do
  562. case $i in
  563. -*) template_opts+=("$i");;
  564. *) pos_args+=("$i");;
  565. esac
  566. done
  567. install_template "${template_opts[@]}" \
  568. "${uWSGI_SETUP}/apps-available/${pos_args[1]}" \
  569. root root 644
  570. uWSGI_enable_app "${pos_args[1]}"
  571. uWSGI_restart
  572. info_msg "installed uWSGI app: ${pos_args[1]}"
  573. }
  574. uWSGI_remove_app() {
  575. # usage: uWSGI_remove_app <myapp.ini>
  576. local CONF="$1"
  577. info_msg "remove uWSGI app: ${CONF}"
  578. uWSGI_disable_app "${CONF}"
  579. uWSGI_restart
  580. rm -f "${uWSGI_SETUP}/apps-available/${CONF}"
  581. }
  582. uWSGI_app_enabled() {
  583. # usage: uWSGI_app_enabled <myapp.ini>
  584. local CONF="$1"
  585. if [[ -z $CONF ]]; then
  586. err_msg "uWSGI_app_enabled: missing arguments"
  587. return 42
  588. fi
  589. [[ -f "${uWSGI_SETUP}/apps-enabled/${CONF}" ]]
  590. }
  591. # shellcheck disable=SC2164
  592. uWSGI_enable_app() {
  593. # usage: uWSGI_enable_app <myapp.ini>
  594. local CONF="$1"
  595. if [[ -z $CONF ]]; then
  596. err_msg "uWSGI_enable_app: missing arguments"
  597. return 42
  598. fi
  599. pushd "${uWSGI_SETUP}/apps-enabled" >/dev/null
  600. rm -f "$CONF"
  601. # shellcheck disable=SC2226
  602. ln -s "../apps-available/${CONF}"
  603. info_msg "enabled uWSGI app: ${CONF} (restart uWSGI required)"
  604. popd >/dev/null
  605. }
  606. uWSGI_disable_app() {
  607. # usage: uWSGI_disable_app <myapp.ini>
  608. local CONF="$1"
  609. if [[ -z $CONF ]]; then
  610. err_msg "uWSGI_enable_app: missing arguments"
  611. return 42
  612. fi
  613. rm -f "${uWSGI_SETUP}/apps-enabled/${CONF}"
  614. # FIXME: restart uwsgi service won't stop wsgi forked processes of user searx.
  615. # I had to kill them manually here ...
  616. pkill -f "${uWSGI_SETUP}/apps-enabled/${CONF}" -9
  617. info_msg "disabled uWSGI app: ${CONF} (restart uWSGI required)"
  618. }
  619. # distro's package manager
  620. # ------------------------
  621. #
  622. # FIXME: Arch Linux & RHEL should be added
  623. #
  624. pkg_install() {
  625. # usage: TITEL='install foobar' pkg_install foopkg barpkg
  626. rst_title "${TITLE:-installation of packages}" section
  627. echo -en "\npackage(s)::\n\n $*\n" | $FMT
  628. if ! ask_yn "Should packages be installed?" Yn 30; then
  629. return 42
  630. fi
  631. # shellcheck disable=SC2068
  632. apt-get install -y $@
  633. }
  634. pkg_remove() {
  635. # usage: TITEL='remove foobar' pkg_remove foopkg barpkg
  636. rst_title "${TITLE:-remove packages}" section
  637. echo -en "\npackage(s)::\n\n $*\n" | $FMT
  638. if ! ask_yn "Should packages be removed (purge)?" Yn 30; then
  639. return 42
  640. fi
  641. apt-get purge --autoremove --ignore-missing -y "$@"
  642. }
  643. pkg_is_installed() {
  644. # usage: pkg_is_install foopkg || pkg_install foopkg
  645. dpkg -l "$1" &> /dev/null
  646. return $?
  647. }
  648. # git tooling
  649. # -----------
  650. # shellcheck disable=SC2164
  651. git_clone() {
  652. # usage:
  653. #
  654. # git_clone <url> <name> [<branch> [<user>]]
  655. # git_clone <url> <path> [<branch> [<user>]]
  656. #
  657. # First form uses $CACHE/<name> as destination folder, second form clones
  658. # into <path>. If repository is allready cloned, pull from <branch> and
  659. # update working tree (if needed, the caller has to stash local changes).
  660. #
  661. # git clone https://github.com/asciimoo/searx searx-src origin/master searxlogin
  662. #
  663. local url="$1"
  664. local dest="$2"
  665. local branch="$3"
  666. local user="$4"
  667. local bash_cmd="bash"
  668. local remote="origin"
  669. if [[ ! "${dest:0:1}" = "/" ]]; then
  670. dest="$CACHE/$dest"
  671. fi
  672. [[ -z $branch ]] && branch=master
  673. [[ -z $user ]] && [[ ! -z "${SUDO_USER}" ]] && user="${SUDO_USER}"
  674. [[ ! -z $user ]] && bash_cmd="sudo -H -u $user -i"
  675. if [[ -d "${dest}" ]] ; then
  676. info_msg "already cloned: $dest"
  677. tee_stderr 0.1 <<EOF | $bash_cmd 2>&1 | prefix_stdout " |$user| "
  678. cd "${dest}"
  679. git checkout -m -B "$branch" --track "$remote/$branch"
  680. git pull --all
  681. EOF
  682. else
  683. info_msg "clone into: $dest"
  684. tee_stderr 0.1 <<EOF | $bash_cmd 2>&1 | prefix_stdout " |$user| "
  685. mkdir -p "$(dirname "$dest")"
  686. cd "$(dirname "$dest")"
  687. git clone --branch "$branch" --origin "$remote" "$url" "$(basename "$dest")"
  688. EOF
  689. fi
  690. }