lib.sh 45 KB

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