lib.sh 21 KB

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