lib.sh 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684
  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. # ubuntu, debian, arch, fedora, centos ...
  6. DIST_ID=$(
  7. source /etc/os-release
  8. echo "$ID"
  9. )
  10. # shellcheck disable=SC2034
  11. DIST_VERS=$(
  12. source /etc/os-release
  13. echo "$VERSION_ID"
  14. )
  15. # shellcheck disable=SC2034
  16. DIST_VERSION_CODENAME=$(
  17. source /etc/os-release
  18. echo "$VERSION_CODENAME"
  19. )
  20. ADMIN_NAME="${ADMIN_NAME:-$(git config user.name)}"
  21. ADMIN_NAME="${ADMIN_NAME:-$USER}"
  22. ADMIN_EMAIL="${ADMIN_EMAIL:-$(git config user.email)}"
  23. ADMIN_EMAIL="${ADMIN_EMAIL:-$USER@$(hostname)}"
  24. if [[ -z "${REPO_ROOT}" ]]; then
  25. REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")
  26. while [ -h "${REPO_ROOT}" ]; do
  27. REPO_ROOT=$(readlink "${REPO_ROOT}")
  28. done
  29. REPO_ROOT=$(cd "${REPO_ROOT}/.." && pwd -P)
  30. fi
  31. if [[ -z ${TEMPLATES} ]]; then
  32. TEMPLATES="${REPO_ROOT}/utils/templates"
  33. fi
  34. if [[ -z "$CACHE" ]]; then
  35. CACHE="${REPO_ROOT}/cache"
  36. fi
  37. if [[ -z ${DIFF_CMD} ]]; then
  38. DIFF_CMD="diff -u"
  39. if command -v colordiff >/dev/null; then
  40. DIFF_CMD="colordiff -u"
  41. fi
  42. fi
  43. DOT_CONFIG="${DOT_CONFIG:-${REPO_ROOT}/.config.sh}"
  44. source_dot_config() {
  45. if [[ ! -e "${DOT_CONFIG}" ]]; then
  46. err_msg "configuration does not exists at: ${DOT_CONFIG}"
  47. return 42
  48. fi
  49. # shellcheck disable=SC1090
  50. source "${DOT_CONFIG}"
  51. }
  52. sudo_or_exit() {
  53. # usage: sudo_or_exit
  54. if [ ! "$(id -u)" -eq 0 ]; then
  55. err_msg "this command requires root (sudo) privilege!" >&2
  56. exit 42
  57. fi
  58. }
  59. required_commands() {
  60. # usage: required_commands [cmd1 ...]
  61. local exit_val=0
  62. while [ -n "$1" ]; do
  63. if ! command -v "$1" &>/dev/null; then
  64. err_msg "missing command $1"
  65. exit_val=42
  66. fi
  67. shift
  68. done
  69. return $exit_val
  70. }
  71. # colors
  72. # ------
  73. # shellcheck disable=SC2034
  74. set_terminal_colors() {
  75. # https://en.wikipedia.org/wiki/ANSI_escape_code
  76. # CSI (Control Sequence Introducer) sequences
  77. _show_cursor='\e[?25h'
  78. _hide_cursor='\e[?25l'
  79. # SGR (Select Graphic Rendition) parameters
  80. _creset='\e[0m' # reset all attributes
  81. # original specification only had 8 colors
  82. _colors=8
  83. _Black='\e[0;30m'
  84. _White='\e[1;37m'
  85. _Red='\e[0;31m'
  86. _Green='\e[0;32m'
  87. _Yellow='\e[0;33m'
  88. _Blue='\e[0;94m'
  89. _Violet='\e[0;35m'
  90. _Cyan='\e[0;36m'
  91. _BBlack='\e[1;30m'
  92. _BWhite='\e[1;37m'
  93. _BRed='\e[1;31m'
  94. _BGreen='\e[1;32m'
  95. _BYellow='\e[1;33m'
  96. _BBlue='\e[1;94m'
  97. _BPurple='\e[1;35m'
  98. _BCyan='\e[1;36m'
  99. }
  100. if [ ! -p /dev/stdout ] && [ ! "$TERM" = 'dumb' ] && [ ! "$TERM" = 'unknown' ]; then
  101. set_terminal_colors
  102. fi
  103. # reST
  104. # ----
  105. if command -v fmt >/dev/null; then
  106. export FMT="fmt -u"
  107. else
  108. export FMT="cat"
  109. fi
  110. rst_title() {
  111. # usage: rst_title <header-text> [part|chapter|section]
  112. case ${2-chapter} in
  113. part) printf "\n${_BGreen}${1//?/=}${_creset}\n${_BCyan}${1}${_creset}\n${_BGreen}${1//?/=}${_creset}\n" ;;
  114. chapter) printf "\n${_BCyan}${1}${_creset}\n${_BGreen}${1//?/=}${_creset}\n" ;;
  115. section) printf "\n${_BCyan}${1}${_creset}\n${_BGreen}${1//?/-}${_creset}\n" ;;
  116. *)
  117. err_msg "invalid argument '${2}' in line $(caller)"
  118. return 42
  119. ;;
  120. esac
  121. }
  122. rst_para() {
  123. # usage: RST_INDENT=1 rst_para "lorem ipsum ..."
  124. local prefix=''
  125. if [[ -n $RST_INDENT ]] && [[ $RST_INDENT -gt 0 ]]; then
  126. prefix="$(for i in $(seq 1 "$RST_INDENT"); do printf " "; done)"
  127. echo -en "\n$*\n" | $FMT | prefix_stdout "$prefix"
  128. else
  129. echo -en "\n$*\n" | $FMT
  130. fi
  131. }
  132. die() {
  133. echo -e "${_BRed}ERROR:${_creset} ${BASH_SOURCE[1]}: line ${BASH_LINENO[0]}: ${2-died ${1-1}}" >&2
  134. exit "${1-1}"
  135. }
  136. die_caller() {
  137. echo -e "${_BRed}ERROR:${_creset} ${BASH_SOURCE[2]}: line ${BASH_LINENO[1]}: ${FUNCNAME[1]}(): ${2-died ${1-1}}" >&2
  138. exit "${1-1}"
  139. }
  140. err_msg() { echo -e "${_BRed}ERROR:${_creset} $*" >&2; }
  141. warn_msg() { echo -e "${_BBlue}WARN:${_creset} $*" >&2; }
  142. info_msg() { echo -e "${_BYellow}INFO:${_creset} $*" >&2; }
  143. build_msg() {
  144. local tag="$1 "
  145. shift
  146. echo -e "${_Blue}${tag:0:10}${_creset}$*"
  147. }
  148. dump_return() {
  149. # Use this as last command in your function to prompt an ERROR message if
  150. # the exit code is not zero.
  151. local err=$1
  152. [ "$err" -ne "0" ] && err_msg "${FUNCNAME[1]} exit with error ($err)"
  153. return "$err"
  154. }
  155. clean_stdin() {
  156. if [[ $(uname -s) != 'Darwin' ]]; then
  157. while read -r -n1 -t 0.1; do :; done
  158. fi
  159. }
  160. wait_key() {
  161. # usage: wait_key [<timeout in sec>]
  162. clean_stdin
  163. local _t=$1
  164. local msg="${MSG}"
  165. [[ -z "$msg" ]] && msg="${_Green}** press any [${_BCyan}KEY${_Green}] to continue **${_creset}"
  166. [[ -n $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
  167. [[ -n $_t ]] && _t="-t $_t"
  168. printf "$msg"
  169. # shellcheck disable=SC2229
  170. # shellcheck disable=SC2086
  171. read -r -s -n1 $_t || true
  172. echo
  173. clean_stdin
  174. }
  175. ask_yn() {
  176. # usage: ask_yn <prompt-text> [Ny|Yn] [<timeout in sec>]
  177. local EXIT_YES=0 # exit status 0 --> successful
  178. local EXIT_NO=1 # exit status 1 --> error code
  179. local _t=$3
  180. [[ -n $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
  181. [[ -n $_t ]] && _t="-t $_t"
  182. case "${FORCE_SELECTION:-${2}}" in
  183. Y) return ${EXIT_YES} ;;
  184. N) return ${EXIT_NO} ;;
  185. Yn)
  186. local exit_val=${EXIT_YES}
  187. local choice="[${_BGreen}YES${_creset}/no]"
  188. local default="Yes"
  189. ;;
  190. *)
  191. local exit_val=${EXIT_NO}
  192. local choice="[${_BGreen}NO${_creset}/yes]"
  193. local default="No"
  194. ;;
  195. esac
  196. echo
  197. while true; do
  198. clean_stdin
  199. printf "$1 ${choice} "
  200. # shellcheck disable=SC2086,SC2229
  201. read -r -n1 $_t
  202. if [[ -z $REPLY ]]; then
  203. printf "$default\n"
  204. break
  205. elif [[ $REPLY =~ ^[Yy]$ ]]; then
  206. exit_val=${EXIT_YES}
  207. printf "\n"
  208. break
  209. elif [[ $REPLY =~ ^[Nn]$ ]]; then
  210. exit_val=${EXIT_NO}
  211. printf "\n"
  212. break
  213. fi
  214. _t=""
  215. err_msg "invalid choice"
  216. done
  217. clean_stdin
  218. return $exit_val
  219. }
  220. tee_stderr() {
  221. # usage::
  222. # tee_stderr 1 <<EOF | python -i
  223. # print("hello")
  224. # EOF
  225. # ...
  226. # >>> print("hello")
  227. # hello
  228. local _t="0"
  229. if [[ -n $1 ]]; then _t="$1"; fi
  230. (while read -r line; do
  231. # shellcheck disable=SC2086,SC2229
  232. sleep $_t
  233. echo -e "$line" >&2
  234. echo "$line"
  235. done)
  236. }
  237. prefix_stdout() {
  238. # usage: <cmd> | prefix_stdout [prefix]
  239. local prefix="${_BYellow}-->|${_creset}"
  240. if [[ -n $1 ]]; then prefix="$1"; fi
  241. # shellcheck disable=SC2162
  242. (while IFS= read line; do
  243. echo -e "${prefix}$line"
  244. done)
  245. # some piped commands hide the cursor, show cursory when the stream ends
  246. echo -en "$_show_cursor"
  247. }
  248. append_line() {
  249. # usage: append_line <line> <file>
  250. #
  251. # Append line if not exists, create file if not exists. E.g::
  252. #
  253. # append_line 'source ~/.foo' ~/bashrc
  254. local LINE=$1
  255. local FILE=$2
  256. grep -qFs -- "$LINE" "$FILE" || echo "$LINE" >>"$FILE"
  257. }
  258. cache_download() {
  259. # usage: cache_download <url> <local-filename>
  260. local exit_value=0
  261. if [[ -n ${SUDO_USER} ]]; then
  262. sudo -u "${SUDO_USER}" mkdir -p "${CACHE}"
  263. else
  264. mkdir -p "${CACHE}"
  265. fi
  266. if [[ -f "${CACHE}/$2" ]]; then
  267. info_msg "already cached: $1"
  268. info_msg " --> ${CACHE}/$2"
  269. fi
  270. if [[ ! -f "${CACHE}/$2" ]]; then
  271. info_msg "caching: $1"
  272. info_msg " --> ${CACHE}/$2"
  273. if [[ -n ${SUDO_USER} ]]; then
  274. sudo -u "${SUDO_USER}" wget --progress=bar -O "${CACHE}/$2" "$1"
  275. exit_value=$?
  276. else
  277. wget --progress=bar -O "${CACHE}/$2" "$1"
  278. exit_value=$?
  279. fi
  280. if [[ ! $exit_value = 0 ]]; then
  281. err_msg "failed to download: $1"
  282. fi
  283. fi
  284. }
  285. backup_file() {
  286. # usage: backup_file /path/to/file.foo
  287. local stamp
  288. stamp=$(date +"_%Y%m%d_%H%M%S")
  289. info_msg "create backup: ${1}${stamp}"
  290. cp -a "${1}" "${1}${stamp}"
  291. }
  292. choose_one() {
  293. # usage:
  294. #
  295. # DEFAULT_SELECT= 2 \
  296. # choose_one <name> "your selection?" "Coffee" "Coffee with milk"
  297. local default=${DEFAULT_SELECT-1}
  298. local REPLY
  299. local env_name=$1 && shift
  300. local choice=$1
  301. local max="${#@}"
  302. local _t
  303. [[ -n $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
  304. [[ -n $_t ]] && _t="-t $_t"
  305. list=("$@")
  306. echo -e "${_BGreen}Menu::${_creset}"
  307. for ((i = 1; i <= $((max - 1)); i++)); do
  308. if [[ "$i" == "$default" ]]; then
  309. echo -e " ${_BGreen}$i.${_creset}) ${list[$i]} [default]"
  310. else
  311. echo -e " $i.) ${list[$i]}"
  312. fi
  313. done
  314. while true; do
  315. clean_stdin
  316. printf "$1 [${_BGreen}$default${_creset}] "
  317. if ((10 > max)); then
  318. # shellcheck disable=SC2086,SC2229
  319. read -r -n1 $_t
  320. else
  321. # shellcheck disable=SC2086,SC2229
  322. read -r $_t
  323. fi
  324. # selection fits
  325. [[ $REPLY =~ ^-?[0-9]+$ ]] && ((REPLY > 0)) && ((REPLY < max)) && break
  326. # take default
  327. [[ -z $REPLY ]] && REPLY=$default && break
  328. _t=""
  329. err_msg "invalid choice"
  330. done
  331. eval "$env_name"='${list[${REPLY}]}'
  332. echo
  333. clean_stdin
  334. }
  335. install_template() {
  336. # usage:
  337. #
  338. # install_template [--no-eval] [--variant=<name>] \
  339. # {file} [{owner} [{group} [{chmod}]]]
  340. #
  341. # E.g. the origin of variant 'raw' of /etc/updatedb.conf is::
  342. #
  343. # ${TEMPLATES}/etc/updatedb.conf:raw
  344. #
  345. # To install variant 'raw' of /etc/updatedb.conf without evaluated
  346. # replacements you can use::
  347. #
  348. # install_template --variant=raw --no-eval \
  349. # /etc/updatedb.conf root root 644
  350. local _reply=""
  351. local do_eval=1
  352. local variant=""
  353. local pos_args=("$0")
  354. for i in "$@"; do
  355. case $i in
  356. --no-eval)
  357. do_eval=0
  358. shift
  359. ;;
  360. --variant=*)
  361. variant=":${i#*=}"
  362. shift
  363. ;;
  364. *) pos_args+=("$i") ;;
  365. esac
  366. done
  367. local dst="${pos_args[1]}"
  368. local template_origin="${TEMPLATES}${dst}${variant}"
  369. local template_file="${TEMPLATES}${dst}"
  370. local owner="${pos_args[2]-$(id -un)}"
  371. local group="${pos_args[3]-$(id -gn)}"
  372. local chmod="${pos_args[4]-644}"
  373. info_msg "install (eval=$do_eval): ${dst}"
  374. [[ -n $variant ]] && info_msg "variant --> ${variant}"
  375. if [[ ! -f "${template_origin}" ]]; then
  376. err_msg "${template_origin} does not exists"
  377. err_msg "... can't install $dst"
  378. wait_key 30
  379. return 42
  380. fi
  381. if [[ "$do_eval" == "1" ]]; then
  382. template_file="${CACHE}${dst}${variant}"
  383. info_msg "BUILD ${template_file}"
  384. info_msg "BUILD using template ${template_origin}"
  385. if [[ -n ${SUDO_USER} ]]; then
  386. sudo -u "${SUDO_USER}" mkdir -p "$(dirname "${template_file}")"
  387. else
  388. mkdir -p "$(dirname "${template_file}")"
  389. fi
  390. # shellcheck disable=SC2086
  391. eval "echo \"$(cat ${template_origin})\"" >"${template_file}"
  392. if [[ -n ${SUDO_USER} ]]; then
  393. chown "${SUDO_USER}:${SUDO_USER}" "${template_file}"
  394. fi
  395. else
  396. template_file=$template_origin
  397. fi
  398. mkdir -p "$(dirname "${dst}")"
  399. if [[ ! -f "${dst}" ]]; then
  400. info_msg "install: ${template_file}"
  401. sudo -H install -v -o "${owner}" -g "${group}" -m "${chmod}" \
  402. "${template_file}" "${dst}" | prefix_stdout
  403. return $?
  404. fi
  405. if [[ -f "${dst}" ]] && cmp --silent "${template_file}" "${dst}"; then
  406. info_msg "file ${dst} already installed"
  407. return 0
  408. fi
  409. info_msg "different file ${dst} already exists on this host"
  410. while true; do
  411. choose_one _reply "choose next step with file $dst" \
  412. "replace file" \
  413. "leave file unchanged" \
  414. "interactive shell" \
  415. "diff files"
  416. case $_reply in
  417. "replace file")
  418. info_msg "install: ${template_file}"
  419. sudo -H install -v -o "${owner}" -g "${group}" -m "${chmod}" \
  420. "${template_file}" "${dst}" | prefix_stdout
  421. break
  422. ;;
  423. "leave file unchanged")
  424. break
  425. ;;
  426. "interactive shell")
  427. echo -e "// edit ${_Red}${dst}${_creset} to your needs"
  428. echo -e "// exit with [${_BCyan}CTRL-D${_creset}]"
  429. sudo -H -u "${owner}" -i
  430. $DIFF_CMD "${dst}" "${template_file}"
  431. echo
  432. echo -e "// ${_BBlack}did you edit file ...${_creset}"
  433. echo -en "// ${_Red}${dst}${_creset}"
  434. if ask_yn "//${_BBlack}... to your needs?${_creset}"; then
  435. break
  436. fi
  437. ;;
  438. "diff files")
  439. $DIFF_CMD "${dst}" "${template_file}" | prefix_stdout
  440. ;;
  441. esac
  442. done
  443. }
  444. service_is_available() {
  445. # usage: service_is_available <URL>
  446. [[ -z $1 ]] && die_caller 42 "missing argument <URL>"
  447. local URL="$1"
  448. http_code=$(curl -H 'Cache-Control: no-cache' \
  449. --silent -o /dev/null --head --write-out '%{http_code}' --insecure \
  450. "${URL}")
  451. exit_val=$?
  452. if [[ $exit_val = 0 ]]; then
  453. info_msg "got $http_code from ${URL}"
  454. fi
  455. case "$http_code" in
  456. 404 | 410 | 423) exit_val=$http_code ;;
  457. esac
  458. return "$exit_val"
  459. }
  460. # python
  461. # ------
  462. PY="${PY:=3}"
  463. PYTHON="${PYTHON:=python$PY}"
  464. PY_ENV="${PY_ENV:=local/py${PY}}"
  465. PY_ENV_BIN="${PY_ENV}/bin"
  466. PY_ENV_REQ="${PY_ENV_REQ:=${REPO_ROOT}/requirements*.txt}"
  467. # List of python packages (folders) or modules (files) installed by command:
  468. # pyenv.install
  469. PYOBJECTS="${PYOBJECTS:=.}"
  470. # folder where the python distribution takes place
  471. PYDIST="${PYDIST:=dist}"
  472. # folder where the intermediate build files take place
  473. PYBUILD="${PYBUILD:=build/py${PY}}"
  474. # https://www.python.org/dev/peps/pep-0508/#extras
  475. #PY_SETUP_EXTRAS='[develop,test]'
  476. PY_SETUP_EXTRAS="${PY_SETUP_EXTRAS:=[develop,test]}"
  477. PIP_BOILERPLATE=(pip wheel setuptools)
  478. # shellcheck disable=SC2120
  479. pyenv() {
  480. # usage: pyenv [vtenv_opts ...]
  481. #
  482. # vtenv_opts: see 'pip install --help'
  483. #
  484. # Builds virtualenv with 'requirements*.txt' (PY_ENV_REQ) installed. The
  485. # virtualenv will be reused by validating sha256sum of the requirement
  486. # files.
  487. required_commands \
  488. sha256sum "${PYTHON}" ||
  489. exit
  490. local pip_req=()
  491. if ! pyenv.OK >/dev/null; then
  492. rm -f "${PY_ENV}/${PY_ENV_REQ}.sha256"
  493. pyenv.drop >/dev/null
  494. build_msg PYENV "[virtualenv] installing ${PY_ENV_REQ} into ${PY_ENV}"
  495. "${PYTHON}" -m venv "$@" "${PY_ENV}"
  496. "${PY_ENV_BIN}/python" -m pip install -U "${PIP_BOILERPLATE[@]}"
  497. for i in ${PY_ENV_REQ}; do
  498. pip_req=("${pip_req[@]}" "-r" "$i")
  499. done
  500. (
  501. [ "$VERBOSE" = "1" ] && set -x
  502. # shellcheck disable=SC2086
  503. "${PY_ENV_BIN}/python" -m pip install "${pip_req[@]}" &&
  504. sha256sum ${PY_ENV_REQ} >"${PY_ENV}/requirements.sha256"
  505. )
  506. fi
  507. pyenv.OK
  508. }
  509. _pyenv_OK=''
  510. pyenv.OK() {
  511. # probes if pyenv exists and runs the script from pyenv.check
  512. [ "$_pyenv_OK" == "OK" ] && return 0
  513. if [ ! -f "${PY_ENV_BIN}/python" ]; then
  514. build_msg PYENV "[virtualenv] missing ${PY_ENV_BIN}/python"
  515. return 1
  516. fi
  517. if [ ! -f "${PY_ENV}/requirements.sha256" ] ||
  518. ! sha256sum -c "${PY_ENV}/requirements.sha256" >/dev/null 2>&1; then
  519. build_msg PYENV "[virtualenv] requirements.sha256 failed"
  520. sed 's/^/ [virtualenv] - /' <"${PY_ENV}/requirements.sha256"
  521. return 1
  522. fi
  523. if [ "$VERBOSE" = "1" ]; then
  524. pyenv.check |
  525. "${PY_ENV_BIN}/python" 2>&1 |
  526. prefix_stdout "${_Blue}PYENV ${_creset}[check] "
  527. else
  528. pyenv.check | "${PY_ENV_BIN}/python" 1>/dev/null
  529. fi
  530. local err=${PIPESTATUS[1]}
  531. if [ "$err" -ne "0" ]; then
  532. build_msg PYENV "[check] python test failed"
  533. return "$err"
  534. fi
  535. [ "$VERBOSE" = "1" ] && build_msg PYENV "OK"
  536. _pyenv_OK="OK"
  537. return 0
  538. }
  539. pyenv.drop() {
  540. build_msg PYENV "[virtualenv] drop ${PY_ENV}"
  541. rm -rf "${PY_ENV}"
  542. _pyenv_OK=''
  543. }
  544. pyenv.check() {
  545. # Prompts a python script with additional checks. Used by pyenv.OK to check
  546. # if virtualenv is ready to install python objects. This function should be
  547. # overwritten by the application script.
  548. local imp=""
  549. for i in "${PIP_BOILERPLATE[@]}"; do
  550. imp="$imp, $i"
  551. done
  552. cat <<EOF
  553. import ${imp#,*}
  554. EOF
  555. }
  556. pyenv.install() {
  557. if ! pyenv.OK; then
  558. py.clean >/dev/null
  559. fi
  560. if ! pyenv.install.OK >/dev/null; then
  561. build_msg PYENV "[install] ${PYOBJECTS}"
  562. if ! pyenv.OK >/dev/null; then
  563. pyenv
  564. fi
  565. for i in ${PYOBJECTS}; do
  566. build_msg PYENV "[install] pip install --use-pep517 --no-build-isolation -e '$i${PY_SETUP_EXTRAS}'"
  567. "${PY_ENV_BIN}/python" -m pip install --use-pep517 --no-build-isolation -e "$i${PY_SETUP_EXTRAS}"
  568. done
  569. fi
  570. pyenv.install.OK
  571. }
  572. _pyenv_install_OK=''
  573. pyenv.install.OK() {
  574. [ "$_pyenv_install_OK" == "OK" ] && return 0
  575. local imp=""
  576. local err=""
  577. if [ "." = "${PYOBJECTS}" ]; then
  578. imp="import $(basename "$(pwd)")"
  579. else
  580. # shellcheck disable=SC2086
  581. for i in ${PYOBJECTS}; do imp="$imp, $i"; done
  582. imp="import ${imp#,*} "
  583. fi
  584. (
  585. [ "$VERBOSE" = "1" ] && set -x
  586. "${PY_ENV_BIN}/python" -c "import sys; sys.path.pop(0); $imp;" 2>/dev/null
  587. )
  588. err=$?
  589. if [ "$err" -ne "0" ]; then
  590. build_msg PYENV "[install] python installation test failed"
  591. return "$err"
  592. fi
  593. build_msg PYENV "[install] OK"
  594. _pyenv_install_OK="OK"
  595. return 0
  596. }
  597. pyenv.uninstall() {
  598. build_msg PYENV "[uninstall] ${PYOBJECTS}"
  599. if [ "." = "${PYOBJECTS}" ]; then
  600. pyenv.cmd python setup.py develop --uninstall 2>&1 |
  601. prefix_stdout "${_Blue}PYENV ${_creset}[pyenv.uninstall] "
  602. else
  603. # shellcheck disable=SC2086
  604. pyenv.cmd python -m pip uninstall --yes ${PYOBJECTS} 2>&1 |
  605. prefix_stdout "${_Blue}PYENV ${_creset}[pyenv.uninstall] "
  606. fi
  607. }
  608. pyenv.cmd() {
  609. pyenv.install
  610. (
  611. set -e
  612. # shellcheck source=/dev/null
  613. source "${PY_ENV_BIN}/activate"
  614. [ "$VERBOSE" = "1" ] && set -x
  615. "$@"
  616. )
  617. }
  618. pyenv.activate() {
  619. pyenv.install
  620. # shellcheck source=/dev/null
  621. source "${PY_ENV_BIN}/activate"
  622. }
  623. # Sphinx doc
  624. # ----------
  625. GH_PAGES="build/gh-pages"
  626. DOCS_DIST="${DOCS_DIST:=dist/docs}"
  627. DOCS_BUILD="${DOCS_BUILD:=build/docs}"
  628. docs.html() {
  629. build_msg SPHINX "HTML ./docs --> file://$(readlink -e "$(pwd)/$DOCS_DIST")"
  630. pyenv.install
  631. docs.prebuild
  632. # shellcheck disable=SC2086
  633. PATH="${PY_ENV_BIN}:${PATH}" pyenv.cmd sphinx-build \
  634. ${SPHINX_VERBOSE} ${SPHINXOPTS} \
  635. -b html -c ./docs -d "${DOCS_BUILD}/.doctrees" ./docs "${DOCS_DIST}"
  636. dump_return $?
  637. }
  638. docs.live() {
  639. build_msg SPHINX "autobuild ./docs --> file://$(readlink -e "$(pwd)/$DOCS_DIST")"
  640. pyenv.install
  641. docs.prebuild
  642. # shellcheck disable=SC2086
  643. PATH="${PY_ENV_BIN}:${PATH}" pyenv.cmd sphinx-autobuild \
  644. ${SPHINX_VERBOSE} ${SPHINXOPTS} --open-browser --host 0.0.0.0 \
  645. -b html -c ./docs -d "${DOCS_BUILD}/.doctrees" ./docs "${DOCS_DIST}"
  646. dump_return $?
  647. }
  648. docs.clean() {
  649. build_msg CLEAN "docs -- ${DOCS_BUILD} ${DOCS_DIST}"
  650. # shellcheck disable=SC2115
  651. rm -rf "${GH_PAGES}" "${DOCS_BUILD}" "${DOCS_DIST}"
  652. dump_return $?
  653. }
  654. docs.prebuild() {
  655. # Dummy function to run some actions before sphinx-doc build gets started.
  656. # This function needs to be overwritten by the application script.
  657. true
  658. dump_return $?
  659. }
  660. # shellcheck disable=SC2155
  661. docs.gh-pages() {
  662. # The commit history in the gh-pages branch makes no sense, the history only
  663. # inflates the repository unnecessarily. Therefore a *new orphan* branch
  664. # is created each time we deploy on the gh-pages branch.
  665. docs.clean
  666. docs.prebuild
  667. docs.html
  668. [ "$VERBOSE" = "1" ] && set -x
  669. local head="$(git rev-parse HEAD)"
  670. local branch="$(git name-rev --name-only HEAD)"
  671. local remote="$(git config branch."${branch}".remote)"
  672. local remote_url="$(git config remote."${remote}".url)"
  673. build_msg GH-PAGES "prepare folder: ${GH_PAGES}"
  674. build_msg GH-PAGES "remote of the gh-pages branch: ${remote} / ${remote_url}"
  675. build_msg GH-PAGES "current branch: ${branch}"
  676. # prepare the *orphan* gh-pages working tree
  677. (
  678. git worktree remove -f "${GH_PAGES}"
  679. git branch -D gh-pages
  680. ) &>/dev/null || true
  681. git worktree add --no-checkout "${GH_PAGES}" "${remote}/master"
  682. pushd "${GH_PAGES}" &>/dev/null
  683. git checkout --orphan gh-pages
  684. git rm -rfq .
  685. popd &>/dev/null
  686. cp -r "${DOCS_DIST}"/* "${GH_PAGES}"/
  687. touch "${GH_PAGES}/.nojekyll"
  688. cat >"${GH_PAGES}/404.html" <<EOF
  689. <html><head><META http-equiv='refresh' content='0;URL=index.html'></head></html>
  690. EOF
  691. pushd "${GH_PAGES}" &>/dev/null
  692. git add --all .
  693. git commit -q -m "gh-pages build from: ${branch}@${head} (${remote_url})"
  694. git push -f "${remote}" gh-pages
  695. popd &>/dev/null
  696. set +x
  697. build_msg GH-PAGES "deployed"
  698. }
  699. # system accounts
  700. # ---------------
  701. service_account_is_available() {
  702. # usage: service_account_is_available "$SERVICE_USER" && echo "OK"
  703. sudo -i -u "$1" echo \$HOME &>/dev/null
  704. }
  705. drop_service_account() {
  706. # usage: drop_service_account "${SERVICE_USER}"
  707. rst_title "Drop ${1} HOME" section
  708. if ask_yn "Do you really want to drop ${1} home folder?"; then
  709. userdel -r -f "${1}" 2>&1 | prefix_stdout
  710. else
  711. rst_para "Leave HOME folder $(du -sh "${1}") unchanged."
  712. fi
  713. }
  714. interactive_shell() {
  715. # usage: interactive_shell "${SERVICE_USER}"
  716. echo -e "// exit with [${_BCyan}CTRL-D${_creset}]"
  717. sudo -H -u "${1}" -i
  718. }
  719. # systemd
  720. # -------
  721. SYSTEMD_UNITS="${SYSTEMD_UNITS:-/lib/systemd/system}"
  722. systemd_install_service() {
  723. # usage: systemd_install_service "${SERVICE_NAME}" "${SERVICE_SYSTEMD_UNIT}"
  724. rst_title "Install System-D Unit ${1}" section
  725. echo
  726. install_template "${2}" root root 644
  727. wait_key
  728. systemd_activate_service "${1}"
  729. }
  730. systemd_remove_service() {
  731. # usage: systemd_remove_service "${SERVICE_NAME}" "${SERVICE_SYSTEMD_UNIT}"
  732. if ! ask_yn "Do you really want to deinstall systemd unit ${1}?"; then
  733. return 42
  734. fi
  735. systemd_deactivate_service "${1}"
  736. rm "${2}" 2>&1 | prefix_stdout
  737. }
  738. systemd_activate_service() {
  739. # usage: systemd_activate_service "${SERVICE_NAME}"
  740. rst_title "Activate ${1} (service)" section
  741. echo
  742. tee_stderr <<EOF | bash 2>&1
  743. systemctl enable ${1}.service
  744. systemctl restart ${1}.service
  745. EOF
  746. tee_stderr <<EOF | bash 2>&1
  747. systemctl status --no-pager ${1}.service
  748. EOF
  749. }
  750. systemd_deactivate_service() {
  751. # usage: systemd_deactivate_service "${SERVICE_NAME}"
  752. rst_title "De-Activate ${1} (service)" section
  753. echo
  754. tee_stderr <<EOF | bash 2>&1 | prefix_stdout
  755. systemctl stop ${1}.service
  756. systemctl disable ${1}.service
  757. EOF
  758. }
  759. systemd_restart_service() {
  760. # usage: systemd_restart_service "${SERVICE_NAME}"
  761. rst_title "Restart ${1} (service)" section
  762. echo
  763. tee_stderr <<EOF | bash 2>&1
  764. systemctl restart ${1}.service
  765. EOF
  766. tee_stderr <<EOF | bash 2>&1
  767. systemctl status --no-pager ${1}.service
  768. EOF
  769. }
  770. # nginx
  771. # -----
  772. nginx_distro_setup() {
  773. # shellcheck disable=SC2034
  774. NGINX_DEFAULT_SERVER=/etc/nginx/nginx.conf
  775. # Including *location* directives from a dedicated config-folder into the
  776. # server directive is, what fedora and centos (already) does.
  777. NGINX_APPS_ENABLED="/etc/nginx/default.d"
  778. # We add a apps-available folder and linking configurations into the
  779. # NGINX_APPS_ENABLED folder. See also nginx_include_apps_enabled().
  780. NGINX_APPS_AVAILABLE="/etc/nginx/default.apps-available"
  781. case $DIST_ID-$DIST_VERS in
  782. ubuntu-* | debian-*)
  783. NGINX_PACKAGES="nginx"
  784. NGINX_DEFAULT_SERVER=/etc/nginx/sites-available/default
  785. ;;
  786. arch-*)
  787. NGINX_PACKAGES="nginx-mainline"
  788. ;;
  789. fedora-* | centos-7)
  790. NGINX_PACKAGES="nginx"
  791. ;;
  792. *)
  793. err_msg "$DIST_ID-$DIST_VERS: nginx not yet implemented"
  794. ;;
  795. esac
  796. }
  797. install_nginx() {
  798. info_msg "installing nginx ..."
  799. pkg_install "${NGINX_PACKAGES}"
  800. case $DIST_ID-$DIST_VERS in
  801. arch-* | fedora-* | centos-7)
  802. systemctl enable nginx
  803. systemctl start nginx
  804. ;;
  805. esac
  806. }
  807. nginx_is_installed() {
  808. command -v nginx &>/dev/null
  809. }
  810. nginx_reload() {
  811. info_msg "reload nginx .."
  812. echo
  813. if ! nginx -t; then
  814. err_msg "testing nginx configuration failed"
  815. return 42
  816. fi
  817. systemctl restart nginx
  818. }
  819. nginx_install_app() {
  820. # usage: nginx_install_app [<template option> ...] <myapp.conf>
  821. #
  822. # <template option>: see install_template
  823. local template_opts=()
  824. local pos_args=("$0")
  825. for i in "$@"; do
  826. case $i in
  827. -*) template_opts+=("$i") ;;
  828. *) pos_args+=("$i") ;;
  829. esac
  830. done
  831. nginx_include_apps_enabled "${NGINX_DEFAULT_SERVER}"
  832. install_template "${template_opts[@]}" \
  833. "${NGINX_APPS_AVAILABLE}/${pos_args[1]}" \
  834. root root 644
  835. nginx_enable_app "${pos_args[1]}"
  836. info_msg "installed nginx app: ${pos_args[1]}"
  837. }
  838. nginx_include_apps_enabled() {
  839. # Add the *NGINX_APPS_ENABLED* infrastructure to a nginx server block. Such
  840. # infrastructure is already known from fedora and centos, including apps (location
  841. # directives) from the /etc/nginx/default.d folder into the *default* nginx
  842. # server.
  843. # usage: nginx_include_apps_enabled <config-file>
  844. #
  845. # config-file: Config file with server directive in.
  846. [[ -z $1 ]] && die_caller 42 "missing argument <config-file>"
  847. local server_conf="$1"
  848. # include /etc/nginx/default.d/*.conf;
  849. local include_directive="include ${NGINX_APPS_ENABLED}/*.conf;"
  850. local include_directive_re="^\s*include ${NGINX_APPS_ENABLED}/\*\.conf;"
  851. info_msg "checking existence: '${include_directive}' in file ${server_conf}"
  852. if grep "${include_directive_re}" "${server_conf}"; then
  853. info_msg "OK, already exists."
  854. return
  855. fi
  856. info_msg "add missing directive: '${include_directive}'"
  857. cp "${server_conf}" "${server_conf}.bak"
  858. (
  859. local line
  860. local stage=0
  861. while IFS= read -r line; do
  862. echo "$line"
  863. if [[ $stage = 0 ]]; then
  864. if [[ $line =~ ^[[:space:]]*server*[[:space:]]*\{ ]]; then
  865. stage=1
  866. fi
  867. fi
  868. if [[ $stage = 1 ]]; then
  869. echo " # Load configuration files for the default server block."
  870. echo " $include_directive"
  871. echo ""
  872. stage=2
  873. fi
  874. done <"${server_conf}.bak"
  875. ) >"${server_conf}"
  876. }
  877. nginx_remove_app() {
  878. # usage: nginx_remove_app <myapp.conf>
  879. info_msg "remove nginx app: $1"
  880. nginx_disable_app "$1"
  881. rm -f "${NGINX_APPS_AVAILABLE}/$1"
  882. }
  883. nginx_enable_app() {
  884. # usage: nginx_enable_app <myapp.conf>
  885. local CONF="$1"
  886. info_msg "enable nginx app: ${CONF}"
  887. mkdir -p "${NGINX_APPS_ENABLED}"
  888. rm -f "${NGINX_APPS_ENABLED}/${CONF}"
  889. ln -s "${NGINX_APPS_AVAILABLE}/${CONF}" "${NGINX_APPS_ENABLED}/${CONF}"
  890. nginx_reload
  891. }
  892. nginx_disable_app() {
  893. # usage: nginx_disable_app <myapp.conf>
  894. local CONF="$1"
  895. info_msg "disable nginx app: ${CONF}"
  896. rm -f "${NGINX_APPS_ENABLED}/${CONF}"
  897. nginx_reload
  898. }
  899. # Apache
  900. # ------
  901. apache_distro_setup() {
  902. # shellcheck disable=SC2034
  903. case $DIST_ID-$DIST_VERS in
  904. ubuntu-* | debian-*)
  905. # debian uses the /etc/apache2 path, while other distros use
  906. # the apache default at /etc/httpd
  907. APACHE_SITES_AVAILABLE="/etc/apache2/sites-available"
  908. APACHE_SITES_ENABLED="/etc/apache2/sites-enabled"
  909. APACHE_MODULES="/usr/lib/apache2/modules"
  910. APACHE_PACKAGES="apache2"
  911. ;;
  912. arch-*)
  913. APACHE_SITES_AVAILABLE="/etc/httpd/sites-available"
  914. APACHE_SITES_ENABLED="/etc/httpd/sites-enabled"
  915. APACHE_MODULES="modules"
  916. APACHE_PACKAGES="apache"
  917. ;;
  918. fedora-* | centos-7)
  919. APACHE_SITES_AVAILABLE="/etc/httpd/sites-available"
  920. APACHE_SITES_ENABLED="/etc/httpd/sites-enabled"
  921. APACHE_MODULES="modules"
  922. APACHE_PACKAGES="httpd mod_ssl"
  923. ;;
  924. *)
  925. err_msg "$DIST_ID-$DIST_VERS: apache not yet implemented"
  926. ;;
  927. esac
  928. }
  929. install_apache() {
  930. info_msg "installing apache ..."
  931. pkg_install "$APACHE_PACKAGES"
  932. case $DIST_ID-$DIST_VERS in
  933. arch-* | fedora-* | centos-7)
  934. if ! grep "IncludeOptional sites-enabled" "/etc/httpd/conf/httpd.conf"; then
  935. echo "IncludeOptional sites-enabled/*.conf" >>"/etc/httpd/conf/httpd.conf"
  936. fi
  937. systemctl enable httpd
  938. systemctl start httpd
  939. ;;
  940. esac
  941. }
  942. apache_is_installed() {
  943. case $DIST_ID-$DIST_VERS in
  944. ubuntu-* | debian-*) (command -v apachectl) &>/dev/null ;;
  945. arch-*) (command -v httpd) &>/dev/null ;;
  946. fedora-* | centos-7) (command -v httpd) &>/dev/null ;;
  947. esac
  948. }
  949. apache_reload() {
  950. info_msg "reload apache .."
  951. echo
  952. case $DIST_ID-$DIST_VERS in
  953. ubuntu-* | debian-*)
  954. sudo -H apachectl configtest
  955. sudo -H systemctl force-reload apache2
  956. ;;
  957. arch-* | fedora-* | centos-7)
  958. sudo -H httpd -t
  959. sudo -H systemctl force-reload httpd
  960. ;;
  961. esac
  962. }
  963. apache_install_site() {
  964. # usage: apache_install_site [<template option> ...] <mysite.conf>
  965. #
  966. # <template option>: see install_template
  967. local template_opts=()
  968. local pos_args=("$0")
  969. for i in "$@"; do
  970. case $i in
  971. -*) template_opts+=("$i") ;;
  972. *) pos_args+=("$i") ;;
  973. esac
  974. done
  975. install_template "${template_opts[@]}" \
  976. "${APACHE_SITES_AVAILABLE}/${pos_args[1]}" \
  977. root root 644
  978. apache_enable_site "${pos_args[1]}"
  979. info_msg "installed apache site: ${pos_args[1]}"
  980. }
  981. apache_remove_site() {
  982. # usage: apache_remove_site <mysite.conf>
  983. info_msg "remove apache site: $1"
  984. apache_disable_site "$1"
  985. rm -f "${APACHE_SITES_AVAILABLE}/$1"
  986. }
  987. apache_enable_site() {
  988. # usage: apache_enable_site <mysite.conf>
  989. local CONF="$1"
  990. info_msg "enable apache site: ${CONF}"
  991. case $DIST_ID-$DIST_VERS in
  992. ubuntu-* | debian-*)
  993. sudo -H a2ensite -q "${CONF}"
  994. ;;
  995. arch-*)
  996. mkdir -p "${APACHE_SITES_ENABLED}"
  997. rm -f "${APACHE_SITES_ENABLED}/${CONF}"
  998. ln -s "${APACHE_SITES_AVAILABLE}/${CONF}" "${APACHE_SITES_ENABLED}/${CONF}"
  999. ;;
  1000. fedora-* | centos-7)
  1001. mkdir -p "${APACHE_SITES_ENABLED}"
  1002. rm -f "${APACHE_SITES_ENABLED}/${CONF}"
  1003. ln -s "${APACHE_SITES_AVAILABLE}/${CONF}" "${APACHE_SITES_ENABLED}/${CONF}"
  1004. ;;
  1005. esac
  1006. apache_reload
  1007. }
  1008. apache_disable_site() {
  1009. # usage: apache_disable_site <mysite.conf>
  1010. local CONF="$1"
  1011. info_msg "disable apache site: ${CONF}"
  1012. case $DIST_ID-$DIST_VERS in
  1013. ubuntu-* | debian-*)
  1014. sudo -H a2dissite -q "${CONF}"
  1015. ;;
  1016. arch-*)
  1017. rm -f "${APACHE_SITES_ENABLED}/${CONF}"
  1018. ;;
  1019. fedora-* | centos-7)
  1020. rm -f "${APACHE_SITES_ENABLED}/${CONF}"
  1021. ;;
  1022. esac
  1023. apache_reload
  1024. }
  1025. # uWSGI
  1026. # -----
  1027. uWSGI_SETUP="${uWSGI_SETUP:=/etc/uwsgi}"
  1028. # How distros manage uWSGI apps is very different. From uWSGI POV read:
  1029. # - https://uwsgi-docs.readthedocs.io/en/latest/Management.html
  1030. uWSGI_distro_setup() {
  1031. case $DIST_ID-$DIST_VERS in
  1032. ubuntu-* | debian-*)
  1033. # init.d --> /usr/share/doc/uwsgi/README.Debian.gz
  1034. # For uWSGI debian uses the LSB init process, this might be changed
  1035. # one day, see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=833067
  1036. uWSGI_APPS_AVAILABLE="${uWSGI_SETUP}/apps-available"
  1037. uWSGI_APPS_ENABLED="${uWSGI_SETUP}/apps-enabled"
  1038. uWSGI_PACKAGES="uwsgi"
  1039. ;;
  1040. arch-*)
  1041. # systemd --> /usr/lib/systemd/system/uwsgi@.service
  1042. # For uWSGI archlinux uses systemd template units, see
  1043. # - http://0pointer.de/blog/projects/instances.html
  1044. # - https://uwsgi-docs.readthedocs.io/en/latest/Systemd.html#one-service-per-app-in-systemd
  1045. uWSGI_APPS_AVAILABLE="${uWSGI_SETUP}/apps-archlinux"
  1046. uWSGI_APPS_ENABLED="${uWSGI_SETUP}"
  1047. uWSGI_PACKAGES="uwsgi"
  1048. ;;
  1049. fedora-* | centos-7)
  1050. # systemd --> /usr/lib/systemd/system/uwsgi.service
  1051. # Fedora runs uWSGI in emperor-tyrant mode: in Tyrant mode the
  1052. # Emperor will run the vassal using the UID/GID of the vassal
  1053. # configuration file [1] (user and group of the app .ini file).
  1054. # There are some quirks about additional POSIX groups in uWSGI
  1055. # 2.0.x, read at least: https://github.com/unbit/uwsgi/issues/2099
  1056. uWSGI_APPS_AVAILABLE="${uWSGI_SETUP}/apps-available"
  1057. uWSGI_APPS_ENABLED="${uWSGI_SETUP}.d"
  1058. uWSGI_PACKAGES="uwsgi"
  1059. ;;
  1060. *)
  1061. err_msg "$DIST_ID-$DIST_VERS: uWSGI not yet implemented"
  1062. ;;
  1063. esac
  1064. }
  1065. install_uwsgi() {
  1066. info_msg "installing uwsgi ..."
  1067. pkg_install "$uWSGI_PACKAGES"
  1068. case $DIST_ID-$DIST_VERS in
  1069. fedora-* | centos-7)
  1070. # enable & start should be called once at uWSGI installation time
  1071. systemctl enable uwsgi
  1072. systemctl restart uwsgi
  1073. ;;
  1074. esac
  1075. }
  1076. uWSGI_restart() {
  1077. # usage: uWSGI_restart() <myapp.ini>
  1078. local CONF="$1"
  1079. [[ -z $CONF ]] && die_caller 42 "missing argument <myapp.ini>"
  1080. info_msg "restart uWSGI service"
  1081. case $DIST_ID-$DIST_VERS in
  1082. ubuntu-* | debian-*)
  1083. # the 'service' method seems broken in that way, that it (re-)starts
  1084. # the whole uwsgi process.
  1085. service uwsgi restart "${CONF%.*}"
  1086. ;;
  1087. arch-*)
  1088. # restart systemd template instance
  1089. if uWSGI_app_available "${CONF}"; then
  1090. systemctl restart "uwsgi@${CONF%.*}"
  1091. else
  1092. info_msg "[uWSGI:systemd-template] ${CONF} not installed (no need to restart)"
  1093. fi
  1094. ;;
  1095. fedora-* | centos-7)
  1096. # in emperor mode, just touch the file to restart
  1097. if uWSGI_app_enabled "${CONF}"; then
  1098. touch "${uWSGI_APPS_ENABLED}/${CONF}"
  1099. # it seems, there is a polling time in between touch and restart
  1100. # of the service.
  1101. sleep 3
  1102. else
  1103. info_msg "[uWSGI:emperor] ${CONF} not installed (no need to restart)"
  1104. fi
  1105. ;;
  1106. *)
  1107. err_msg "$DIST_ID-$DIST_VERS: uWSGI not yet implemented"
  1108. return 42
  1109. ;;
  1110. esac
  1111. }
  1112. uWSGI_app_available() {
  1113. # usage: uWSGI_app_available <myapp.ini>
  1114. local CONF="$1"
  1115. [[ -z $CONF ]] && die_caller 42 "missing argument <myapp.ini>"
  1116. [[ -f "${uWSGI_APPS_AVAILABLE}/${CONF}" ]]
  1117. }
  1118. uWSGI_install_app() {
  1119. # usage: uWSGI_install_app [<template option> ...] <myapp.ini> [{owner} [{group} [{chmod}]]]
  1120. #
  1121. # <template option>: see install_template
  1122. local pos_args=("$0")
  1123. for i in "$@"; do
  1124. case $i in
  1125. -*) template_opts+=("$i") ;;
  1126. *) pos_args+=("$i") ;;
  1127. esac
  1128. done
  1129. mkdir -p "${uWSGI_APPS_AVAILABLE}"
  1130. install_template "${template_opts[@]}" \
  1131. "${uWSGI_APPS_AVAILABLE}/${pos_args[1]}" \
  1132. "${pos_args[2]:-root}" "${pos_args[3]:-root}" "${pos_args[4]:-644}"
  1133. uWSGI_enable_app "${pos_args[1]}"
  1134. uWSGI_restart "${pos_args[1]}"
  1135. info_msg "uWSGI app: ${pos_args[1]} is installed"
  1136. }
  1137. uWSGI_remove_app() {
  1138. # usage: uWSGI_remove_app <myapp.ini>
  1139. local CONF="$1"
  1140. [[ -z $CONF ]] && die_caller 42 "missing argument <myapp.ini>"
  1141. info_msg "remove uWSGI app: ${CONF}"
  1142. uWSGI_disable_app "${CONF}"
  1143. uWSGI_restart "${CONF}"
  1144. rm -f "${uWSGI_APPS_AVAILABLE}/${CONF}"
  1145. }
  1146. uWSGI_app_enabled() {
  1147. # usage: uWSGI_app_enabled <myapp.ini>
  1148. local exit_val=0
  1149. local CONF="$1"
  1150. [[ -z $CONF ]] && die_caller 42 "missing argument <myapp.ini>"
  1151. case $DIST_ID-$DIST_VERS in
  1152. ubuntu-* | debian-*)
  1153. [[ -f "${uWSGI_APPS_ENABLED}/${CONF}" ]]
  1154. exit_val=$?
  1155. ;;
  1156. arch-*)
  1157. systemctl -q is-enabled "uwsgi@${CONF%.*}"
  1158. exit_val=$?
  1159. ;;
  1160. fedora-* | centos-7)
  1161. [[ -f "${uWSGI_APPS_ENABLED}/${CONF}" ]]
  1162. exit_val=$?
  1163. ;;
  1164. *)
  1165. # FIXME
  1166. err_msg "$DIST_ID-$DIST_VERS: uWSGI not yet implemented"
  1167. exit_val=1
  1168. ;;
  1169. esac
  1170. return $exit_val
  1171. }
  1172. # shellcheck disable=SC2164
  1173. uWSGI_enable_app() {
  1174. # usage: uWSGI_enable_app <myapp.ini>
  1175. local CONF="$1"
  1176. [[ -z $CONF ]] && die_caller 42 "missing argument <myapp.ini>"
  1177. case $DIST_ID-$DIST_VERS in
  1178. ubuntu-* | debian-*)
  1179. mkdir -p "${uWSGI_APPS_ENABLED}"
  1180. rm -f "${uWSGI_APPS_ENABLED}/${CONF}"
  1181. ln -s "${uWSGI_APPS_AVAILABLE}/${CONF}" "${uWSGI_APPS_ENABLED}/${CONF}"
  1182. info_msg "enabled uWSGI app: ${CONF} (restart required)"
  1183. ;;
  1184. arch-*)
  1185. mkdir -p "${uWSGI_APPS_ENABLED}"
  1186. rm -f "${uWSGI_APPS_ENABLED}/${CONF}"
  1187. ln -s "${uWSGI_APPS_AVAILABLE}/${CONF}" "${uWSGI_APPS_ENABLED}/${CONF}"
  1188. systemctl enable "uwsgi@${CONF%.*}"
  1189. info_msg "enabled uWSGI app: ${CONF} (restart required)"
  1190. ;;
  1191. fedora-* | centos-7)
  1192. mkdir -p "${uWSGI_APPS_ENABLED}"
  1193. rm -f "${uWSGI_APPS_ENABLED}/${CONF}"
  1194. ln -s "${uWSGI_APPS_AVAILABLE}/${CONF}" "${uWSGI_APPS_ENABLED}/${CONF}"
  1195. info_msg "enabled uWSGI app: ${CONF}"
  1196. ;;
  1197. *)
  1198. # FIXME
  1199. err_msg "$DIST_ID-$DIST_VERS: uWSGI not yet implemented"
  1200. ;;
  1201. esac
  1202. }
  1203. uWSGI_disable_app() {
  1204. # usage: uWSGI_disable_app <myapp.ini>
  1205. local CONF="$1"
  1206. [[ -z $CONF ]] && die_caller 42 "missing argument <myapp.ini>"
  1207. case $DIST_ID-$DIST_VERS in
  1208. ubuntu-* | debian-*)
  1209. service uwsgi stop "${CONF%.*}"
  1210. rm -f "${uWSGI_APPS_ENABLED}/${CONF}"
  1211. info_msg "disabled uWSGI app: ${CONF} (restart uWSGI required)"
  1212. ;;
  1213. arch-*)
  1214. systemctl stop "uwsgi@${CONF%.*}"
  1215. systemctl disable "uwsgi@${CONF%.*}"
  1216. rm -f "${uWSGI_APPS_ENABLED}/${CONF}"
  1217. ;;
  1218. fedora-* | centos-7)
  1219. # in emperor mode, just remove the app.ini file
  1220. rm -f "${uWSGI_APPS_ENABLED}/${CONF}"
  1221. ;;
  1222. *)
  1223. # FIXME
  1224. err_msg "$DIST_ID-$DIST_VERS: uWSGI not yet implemented"
  1225. ;;
  1226. esac
  1227. }
  1228. # distro's package manager
  1229. # ------------------------
  1230. _apt_pkg_info_is_updated=0
  1231. pkg_install() {
  1232. # usage: TITLE='install foobar' pkg_install foopkg barpkg
  1233. rst_title "${TITLE:-installation of packages}" section
  1234. echo -e "\npackage(s)::\n"
  1235. # shellcheck disable=SC2068
  1236. echo " " $@ | $FMT
  1237. if ! ask_yn "Should packages be installed?" Yn 30; then
  1238. return 42
  1239. fi
  1240. case $DIST_ID in
  1241. ubuntu | debian)
  1242. if [[ $_apt_pkg_info_is_updated == 0 ]]; then
  1243. export _apt_pkg_info_is_updated=1
  1244. apt update
  1245. fi
  1246. # shellcheck disable=SC2068
  1247. apt-get install -m -y $@
  1248. ;;
  1249. arch)
  1250. # shellcheck disable=SC2068
  1251. pacman --noprogressbar -Sy --noconfirm --needed $@
  1252. ;;
  1253. fedora)
  1254. # shellcheck disable=SC2068
  1255. dnf install -y $@
  1256. ;;
  1257. centos)
  1258. # shellcheck disable=SC2068
  1259. yum install -y $@
  1260. ;;
  1261. esac
  1262. }
  1263. pkg_remove() {
  1264. # usage: TITLE='remove foobar' pkg_remove foopkg barpkg
  1265. rst_title "${TITLE:-remove packages}" section
  1266. echo -e "\npackage(s)::\n"
  1267. # shellcheck disable=SC2068
  1268. echo " " $@ | $FMT
  1269. if ! ask_yn "Should packages be removed (purge)?" Yn 30; then
  1270. return 42
  1271. fi
  1272. case $DIST_ID in
  1273. ubuntu | debian)
  1274. # shellcheck disable=SC2068
  1275. apt-get purge --autoremove --ignore-missing -y $@
  1276. ;;
  1277. arch)
  1278. # shellcheck disable=SC2068
  1279. pacman --noprogressbar -R --noconfirm $@
  1280. ;;
  1281. fedora)
  1282. # shellcheck disable=SC2068
  1283. dnf remove -y $@
  1284. ;;
  1285. centos)
  1286. # shellcheck disable=SC2068
  1287. yum remove -y $@
  1288. ;;
  1289. esac
  1290. }
  1291. pkg_is_installed() {
  1292. # usage: pkg_is_install foopkg || pkg_install foopkg
  1293. case $DIST_ID in
  1294. ubuntu | debian)
  1295. dpkg -l "$1" &>/dev/null
  1296. return $?
  1297. ;;
  1298. arch)
  1299. pacman -Qsq "$1" &>/dev/null
  1300. return $?
  1301. ;;
  1302. fedora)
  1303. dnf list -q --installed "$1" &>/dev/null
  1304. return $?
  1305. ;;
  1306. centos)
  1307. yum list -q --installed "$1" &>/dev/null
  1308. return $?
  1309. ;;
  1310. esac
  1311. }
  1312. # git tooling
  1313. # -----------
  1314. # shellcheck disable=SC2164
  1315. git_clone() {
  1316. # usage:
  1317. #
  1318. # git_clone <url> <name> [<branch> [<user>]]
  1319. # git_clone <url> <path> [<branch> [<user>]]
  1320. #
  1321. # First form uses $CACHE/<name> as destination folder, second form clones
  1322. # into <path>. If repository is already cloned, pull from <branch> and
  1323. # update working tree (if needed, the caller has to stash local changes).
  1324. #
  1325. # git clone https://github.com/searxng/searxng searx-src origin/master searxlogin
  1326. #
  1327. local url="$1"
  1328. local dest="$2"
  1329. local branch="$3"
  1330. local user="$4"
  1331. local bash_cmd="bash"
  1332. local remote="origin"
  1333. if [[ ! "${dest:0:1}" = "/" ]]; then
  1334. dest="$CACHE/$dest"
  1335. fi
  1336. [[ -z $branch ]] && branch=master
  1337. [[ -z $user ]] && [[ -n "${SUDO_USER}" ]] && user="${SUDO_USER}"
  1338. [[ -n $user ]] && bash_cmd="sudo -H -u $user -i"
  1339. if [[ -d "${dest}" ]]; then
  1340. info_msg "already cloned: $dest"
  1341. tee_stderr 0.1 <<EOF | $bash_cmd 2>&1 | prefix_stdout " ${_Yellow}|$user|${_creset} "
  1342. cd "${dest}"
  1343. git checkout -m -B "$branch" --track "$remote/$branch"
  1344. git pull --all
  1345. EOF
  1346. else
  1347. info_msg "clone into: $dest"
  1348. tee_stderr 0.1 <<EOF | $bash_cmd 2>&1 | prefix_stdout " ${_Yellow}|$user|${_creset} "
  1349. mkdir -p "$(dirname "$dest")"
  1350. cd "$(dirname "$dest")"
  1351. git clone --branch "$branch" --origin "$remote" "$url" "$(basename "$dest")"
  1352. EOF
  1353. fi
  1354. }
  1355. # IP
  1356. # --
  1357. global_IPs() {
  1358. # usage: global_IPS
  1359. #
  1360. # print list of host's SCOPE global addresses and adapters e.g::
  1361. #
  1362. # $ global_IPs
  1363. # enp4s0|192.168.1.127
  1364. # lxdbr0|10.246.86.1
  1365. # lxdbr0|fd42:8c58:2cd:b73f::1
  1366. ip -o addr show | sed -nr 's/[0-9]*:\s*([a-z0-9]*).*inet[6]?\s*([a-z0-9.:]*).*scope global.*/\1|\2/p'
  1367. }
  1368. primary_ip() {
  1369. case $DIST_ID in
  1370. arch)
  1371. ip -o addr show |
  1372. sed -nr 's/[0-9]*:\s*([a-z0-9]*).*inet[6]?\s*([a-z0-9.:]*).*scope global.*/\2/p' |
  1373. head -n 1
  1374. ;;
  1375. *) hostname -I | cut -d' ' -f1 ;;
  1376. esac
  1377. }
  1378. # URL
  1379. # ---
  1380. url_replace_hostname() {
  1381. # usage: url_replace_hostname <url> <new hostname>
  1382. # to replace hostname by primary IP::
  1383. #
  1384. # url_replace_hostname http://searx-ubu1604/example $(primary_ip)
  1385. # http://10.246.86.250/example
  1386. # shellcheck disable=SC2001
  1387. echo "$1" | sed "s|\(http[s]*://\)[^/]*\(.*\)|\1$2\2|"
  1388. }