lib.sh 40 KB

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