lib.sh 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510
  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 ...
  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 extsts 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: waitKEY [<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. http_code=$(curl -H 'Cache-Control: no-cache' \
  405. --silent -o /dev/null --head --write-out '%{http_code}' --insecure \
  406. "${URL}")
  407. exit_val=$?
  408. if [[ $exit_val = 0 ]]; then
  409. info_msg "got $http_code from ${URL}"
  410. fi
  411. case "$http_code" in
  412. 404|410|423) exit_val=$http_code;;
  413. esac
  414. return "$exit_val"
  415. }
  416. # golang
  417. # ------
  418. go_is_available() {
  419. # usage: go_is_available $SERVICE_USER && echo "go is installed!"
  420. sudo -i -u "${1}" which go &>/dev/null
  421. }
  422. install_go() {
  423. # usage: install_go "${GO_PKG_URL}" "${GO_TAR}" "${SERVICE_USER}"
  424. local _svcpr=" ${_Yellow}|${3}|${_creset} "
  425. rst_title "Install Go in user's HOME" section
  426. rst_para "download and install go binary .."
  427. cache_download "${1}" "${2}"
  428. tee_stderr 0.1 <<EOF | sudo -i -u "${3}" | prefix_stdout "$_svcpr"
  429. echo \$PATH
  430. echo \$GOPATH
  431. mkdir -p \$HOME/local
  432. rm -rf \$HOME/local/go
  433. tar -C \$HOME/local -xzf ${CACHE}/${2}
  434. EOF
  435. sudo -i -u "${3}" <<EOF | prefix_stdout
  436. ! which go >/dev/null && echo "ERROR - Go Installation not found in PATH!?!"
  437. which go >/dev/null && go version && echo "congratulations -- Go installation OK :)"
  438. EOF
  439. }
  440. # system accounts
  441. # ---------------
  442. service_account_is_available() {
  443. # usage: service_account_is_available "$SERVICE_USER" && echo "OK"
  444. sudo -i -u "$1" echo \$HOME &>/dev/null
  445. }
  446. drop_service_account() {
  447. # usage: drop_service_account "${SERVICE_USER}"
  448. rst_title "Drop ${1} HOME" section
  449. if ask_yn "Do you really want to drop ${1} home folder?"; then
  450. userdel -r -f "${1}" 2>&1 | prefix_stdout
  451. else
  452. rst_para "Leave HOME folder $(du -sh "${1}") unchanged."
  453. fi
  454. }
  455. interactive_shell(){
  456. # usage: interactive_shell "${SERVICE_USER}"
  457. echo -e "// exit with [${_BCyan}CTRL-D${_creset}]"
  458. sudo -H -u "${1}" -i
  459. }
  460. # systemd
  461. # -------
  462. SYSTEMD_UNITS="${SYSTEMD_UNITS:-/lib/systemd/system}"
  463. systemd_install_service() {
  464. # usage: systemd_install_service "${SERVICE_NAME}" "${SERVICE_SYSTEMD_UNIT}"
  465. rst_title "Install System-D Unit ${1}" section
  466. echo
  467. install_template "${2}" root root 644
  468. wait_key
  469. systemd_activate_service "${1}"
  470. }
  471. systemd_remove_service() {
  472. # usage: systemd_remove_service "${SERVICE_NAME}" "${SERVICE_SYSTEMD_UNIT}"
  473. if ! ask_yn "Do you really want to deinstall systemd unit ${1}?"; then
  474. return 42
  475. fi
  476. systemd_deactivate_service "${1}"
  477. rm "${2}" 2>&1 | prefix_stdout
  478. }
  479. systemd_activate_service() {
  480. # usage: systemd_activate_service "${SERVICE_NAME}"
  481. rst_title "Activate ${1} (service)" section
  482. echo
  483. tee_stderr <<EOF | bash 2>&1
  484. systemctl enable ${1}.service
  485. systemctl restart ${1}.service
  486. EOF
  487. tee_stderr <<EOF | bash 2>&1
  488. systemctl status --no-pager ${1}.service
  489. EOF
  490. }
  491. systemd_deactivate_service() {
  492. # usage: systemd_deactivate_service "${SERVICE_NAME}"
  493. rst_title "De-Activate ${1} (service)" section
  494. echo
  495. tee_stderr <<EOF | bash 2>&1 | prefix_stdout
  496. systemctl stop ${1}.service
  497. systemctl disable ${1}.service
  498. EOF
  499. }
  500. systemd_restart_service() {
  501. # usage: systemd_restart_service "${SERVICE_NAME}"
  502. rst_title "Restart ${1} (service)" section
  503. echo
  504. tee_stderr <<EOF | bash 2>&1
  505. systemctl restart ${1}.service
  506. EOF
  507. tee_stderr <<EOF | bash 2>&1
  508. systemctl status --no-pager ${1}.service
  509. EOF
  510. }
  511. # nginx
  512. # -----
  513. nginx_distro_setup() {
  514. # shellcheck disable=SC2034
  515. NGINX_DEFAULT_SERVER=/etc/nginx/nginx.conf
  516. # Including *location* directives from a dedicated config-folder into the
  517. # server directive is, what what fedora (already) does.
  518. NGINX_APPS_ENABLED="/etc/nginx/default.d"
  519. # We add a apps-available folder and linking configurations into the
  520. # NGINX_APPS_ENABLED folder. See also nginx_include_apps_enabled().
  521. NGINX_APPS_AVAILABLE="/etc/nginx/default.apps-available"
  522. case $DIST_ID-$DIST_VERS in
  523. ubuntu-*|debian-*)
  524. NGINX_PACKAGES="nginx"
  525. NGINX_DEFAULT_SERVER=/etc/nginx/sites-available/default
  526. ;;
  527. arch-*)
  528. NGINX_PACKAGES="nginx-mainline"
  529. ;;
  530. fedora-*)
  531. NGINX_PACKAGES="nginx"
  532. ;;
  533. *)
  534. err_msg "$DIST_ID-$DIST_VERS: nginx not yet implemented"
  535. ;;
  536. esac
  537. }
  538. nginx_distro_setup
  539. install_nginx(){
  540. info_msg "installing nginx ..."
  541. pkg_install "${NGINX_PACKAGES}"
  542. case $DIST_ID-$DIST_VERS in
  543. arch-*|fedora-*)
  544. systemctl enable nginx
  545. systemctl start nginx
  546. ;;
  547. esac
  548. }
  549. nginx_is_installed() {
  550. command -v nginx &>/dev/null
  551. }
  552. nginx_reload() {
  553. info_msg "reload nginx .."
  554. echo
  555. if ! nginx -t; then
  556. err_msg "testing nginx configuration failed"
  557. return 42
  558. fi
  559. systemctl restart nginx
  560. }
  561. nginx_install_app() {
  562. # usage: nginx_install_app [<template option> ...] <myapp>
  563. #
  564. # <template option>: see install_template
  565. local template_opts=()
  566. local pos_args=("$0")
  567. for i in "$@"; do
  568. case $i in
  569. -*) template_opts+=("$i");;
  570. *) pos_args+=("$i");;
  571. esac
  572. done
  573. nginx_include_apps_enabled "${NGINX_DEFAULT_SERVER}"
  574. install_template "${template_opts[@]}" \
  575. "${NGINX_APPS_AVAILABLE}/${pos_args[1]}" \
  576. root root 644
  577. nginx_enable_app "${pos_args[1]}"
  578. info_msg "installed nginx app: ${pos_args[1]}"
  579. }
  580. nginx_include_apps_enabled() {
  581. # Add the *NGINX_APPS_ENABLED* infrastruture to a nginx server block. Such
  582. # infrastruture is already known from fedora, including apps (location
  583. # directives) from the /etc/nginx/default.d folder into the *default* nginx
  584. # server.
  585. # usage: nginx_include_apps_enabled <config-file>
  586. #
  587. # config-file: Config file with server directive in.
  588. [[ -z $1 ]] && die_caller 42 "missing argument <config-file>"
  589. local server_conf="$1"
  590. # include /etc/nginx/default.d/*.conf;
  591. local include_directive="include ${NGINX_APPS_ENABLED}/*.conf;"
  592. local include_directive_re="^\s*include ${NGINX_APPS_ENABLED}/\*\.conf;"
  593. info_msg "checking existence: '${include_directive}' in file ${server_conf}"
  594. if grep "${include_directive_re}" "${server_conf}"; then
  595. info_msg "OK, already exists."
  596. return
  597. fi
  598. info_msg "add missing directive: '${include_directive}'"
  599. cp "${server_conf}" "${server_conf}.bak"
  600. (
  601. local line
  602. local stage=0
  603. while IFS= read -r line
  604. do
  605. echo "$line"
  606. if [[ $stage = 0 ]]; then
  607. if [[ $line =~ ^[[:space:]]*server*[[:space:]]*\{ ]]; then
  608. stage=1
  609. fi
  610. fi
  611. if [[ $stage = 1 ]]; then
  612. echo " # Load configuration files for the default server block."
  613. echo " $include_directive"
  614. echo ""
  615. stage=2
  616. fi
  617. done < "${server_conf}.bak"
  618. ) > "${server_conf}"
  619. }
  620. nginx_remove_app() {
  621. # usage: nginx_remove_app <myapp.conf>
  622. info_msg "remove nginx app: $1"
  623. nginx_dissable_app "$1"
  624. rm -f "${NGINX_APPS_AVAILABLE}/$1"
  625. }
  626. nginx_enable_app() {
  627. # usage: nginx_enable_app <myapp.conf>
  628. local CONF="$1"
  629. info_msg "enable nginx app: ${CONF}"
  630. mkdir -p "${NGINX_APPS_ENABLED}"
  631. rm -f "${NGINX_APPS_ENABLED}/${CONF}"
  632. ln -s "${NGINX_APPS_AVAILABLE}/${CONF}" "${NGINX_APPS_ENABLED}/${CONF}"
  633. nginx_reload
  634. }
  635. nginx_dissable_app() {
  636. # usage: nginx_disable_app <myapp.conf>
  637. local CONF="$1"
  638. info_msg "disable nginx app: ${CONF}"
  639. rm -f "${NGINX_APPS_ENABLED}/${CONF}"
  640. nginx_reload
  641. }
  642. # Apache
  643. # ------
  644. apache_distro_setup() {
  645. # shellcheck disable=SC2034
  646. case $DIST_ID-$DIST_VERS in
  647. ubuntu-*|debian-*)
  648. # debian uses the /etc/apache2 path, while other distros use
  649. # the apache default at /etc/httpd
  650. APACHE_SITES_AVAILABLE="/etc/apache2/sites-available"
  651. APACHE_SITES_ENABLED="/etc/apache2/sites-enabled"
  652. APACHE_MODULES="/usr/lib/apache2/modules"
  653. APACHE_PACKAGES="apache2"
  654. ;;
  655. arch-*)
  656. APACHE_SITES_AVAILABLE="/etc/httpd/sites-available"
  657. APACHE_SITES_ENABLED="/etc/httpd/sites-enabled"
  658. APACHE_MODULES="modules"
  659. APACHE_PACKAGES="apache"
  660. ;;
  661. fedora-*)
  662. APACHE_SITES_AVAILABLE="/etc/httpd/sites-available"
  663. APACHE_SITES_ENABLED="/etc/httpd/sites-enabled"
  664. APACHE_MODULES="modules"
  665. APACHE_PACKAGES="httpd"
  666. ;;
  667. *)
  668. err_msg "$DIST_ID-$DIST_VERS: apache not yet implemented"
  669. ;;
  670. esac
  671. }
  672. apache_distro_setup
  673. install_apache(){
  674. info_msg "installing apache ..."
  675. pkg_install "$APACHE_PACKAGES"
  676. case $DIST_ID-$DIST_VERS in
  677. arch-*|fedora-*)
  678. if ! grep "IncludeOptional sites-enabled" "/etc/httpd/conf/httpd.conf"; then
  679. echo "IncludeOptional sites-enabled/*.conf" >> "/etc/httpd/conf/httpd.conf"
  680. fi
  681. systemctl enable httpd
  682. systemctl start httpd
  683. ;;
  684. esac
  685. }
  686. apache_is_installed() {
  687. case $DIST_ID-$DIST_VERS in
  688. ubuntu-*|debian-*) (command -v apachectl) &>/dev/null;;
  689. arch-*) (command -v httpd) &>/dev/null;;
  690. fedora-*) (command -v httpd) &>/dev/null;;
  691. esac
  692. }
  693. apache_reload() {
  694. info_msg "reload apache .."
  695. echo
  696. case $DIST_ID-$DIST_VERS in
  697. ubuntu-*|debian-*)
  698. sudo -H apachectl configtest
  699. sudo -H systemctl force-reload apache2
  700. ;;
  701. arch-*| fedora-*)
  702. sudo -H httpd -t
  703. sudo -H systemctl force-reload httpd
  704. ;;
  705. esac
  706. }
  707. apache_install_site() {
  708. # usage: apache_install_site [<template option> ...] <mysite.conf>
  709. #
  710. # <template option>: see install_template
  711. local template_opts=()
  712. local pos_args=("$0")
  713. for i in "$@"; do
  714. case $i in
  715. -*) template_opts+=("$i");;
  716. *) pos_args+=("$i");;
  717. esac
  718. done
  719. install_template "${template_opts[@]}" \
  720. "${APACHE_SITES_AVAILABLE}/${pos_args[1]}" \
  721. root root 644
  722. apache_enable_site "${pos_args[1]}"
  723. info_msg "installed apache site: ${pos_args[1]}"
  724. }
  725. apache_remove_site() {
  726. # usage: apache_remove_site <mysite.conf>
  727. info_msg "remove apache site: $1"
  728. apache_dissable_site "$1"
  729. rm -f "${APACHE_SITES_AVAILABLE}/$1"
  730. }
  731. apache_enable_site() {
  732. # usage: apache_enable_site <mysite.conf>
  733. local CONF="$1"
  734. info_msg "enable apache site: ${CONF}"
  735. case $DIST_ID-$DIST_VERS in
  736. ubuntu-*|debian-*)
  737. sudo -H a2ensite -q "${CONF}"
  738. ;;
  739. arch-*)
  740. mkdir -p "${APACHE_SITES_ENABLED}"
  741. rm -f "${APACHE_SITES_ENABLED}/${CONF}"
  742. ln -s "${APACHE_SITES_AVAILABLE}/${CONF}" "${APACHE_SITES_ENABLED}/${CONF}"
  743. ;;
  744. fedora-*)
  745. mkdir -p "${APACHE_SITES_ENABLED}"
  746. rm -f "${APACHE_SITES_ENABLED}/${CONF}"
  747. ln -s "${APACHE_SITES_AVAILABLE}/${CONF}" "${APACHE_SITES_ENABLED}/${CONF}"
  748. ;;
  749. esac
  750. apache_reload
  751. }
  752. apache_dissable_site() {
  753. # usage: apache_disable_site <mysite.conf>
  754. local CONF="$1"
  755. info_msg "disable apache site: ${CONF}"
  756. case $DIST_ID-$DIST_VERS in
  757. ubuntu-*|debian-*)
  758. sudo -H a2dissite -q "${CONF}"
  759. ;;
  760. arch-*)
  761. rm -f "${APACHE_SITES_ENABLED}/${CONF}"
  762. ;;
  763. fedora-*)
  764. rm -f "${APACHE_SITES_ENABLED}/${CONF}"
  765. ;;
  766. esac
  767. apache_reload
  768. }
  769. # uWSGI
  770. # -----
  771. uWSGI_SETUP="${uWSGI_SETUP:=/etc/uwsgi}"
  772. uWSGI_USER=
  773. uWSGI_GROUP=
  774. # How distros manage uWSGI apps is very different. From uWSGI POV read:
  775. # - https://uwsgi-docs.readthedocs.io/en/latest/Management.html
  776. uWSGI_distro_setup() {
  777. case $DIST_ID-$DIST_VERS in
  778. ubuntu-*|debian-*)
  779. # init.d --> /usr/share/doc/uwsgi/README.Debian.gz
  780. # For uWSGI debian uses the LSB init process, this might be changed
  781. # one day, see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=833067
  782. uWSGI_APPS_AVAILABLE="${uWSGI_SETUP}/apps-available"
  783. uWSGI_APPS_ENABLED="${uWSGI_SETUP}/apps-enabled"
  784. ;;
  785. arch-*)
  786. # systemd --> /usr/lib/systemd/system/uwsgi@.service
  787. # For uWSGI archlinux uses systemd template units, see
  788. # - http://0pointer.de/blog/projects/instances.html
  789. # - https://uwsgi-docs.readthedocs.io/en/latest/Systemd.html#one-service-per-app-in-systemd
  790. uWSGI_APPS_AVAILABLE="${uWSGI_SETUP}/apps-archlinux"
  791. uWSGI_APPS_ENABLED="${uWSGI_SETUP}"
  792. ;;
  793. fedora-*)
  794. # systemd --> /usr/lib/systemd/system/uwsgi.service
  795. # The unit file starts uWSGI in emperor mode (/etc/uwsgi.ini), see
  796. # - https://uwsgi-docs.readthedocs.io/en/latest/Emperor.html
  797. uWSGI_APPS_AVAILABLE="${uWSGI_SETUP}/apps-available"
  798. uWSGI_APPS_ENABLED="${uWSGI_SETUP}.d"
  799. uWSGI_USER="uwsgi"
  800. uWSGI_GROUP="uwsgi"
  801. ;;
  802. *)
  803. err_msg "$DIST_ID-$DIST_VERS: uWSGI not yet implemented"
  804. ;;
  805. esac
  806. }
  807. uWSGI_distro_setup
  808. uWSGI_restart() {
  809. # usage: uWSGI_restart() <myapp.ini>
  810. local CONF="$1"
  811. if [[ -z $CONF ]]; then
  812. err_msg "uWSGI_restart: missing arguments"
  813. return 42
  814. fi
  815. info_msg "restart uWSGI service"
  816. case $DIST_ID-$DIST_VERS in
  817. ubuntu-*|debian-*)
  818. # the 'service' method seems broken in that way, that it (re-)starts
  819. # the whole uwsgi process.
  820. service uwsgi restart "${CONF%.*}"
  821. ;;
  822. arch-*)
  823. # restart systemd template instance
  824. if uWSGI_app_available "${CONF}"; then
  825. systemctl restart "uwsgi@${CONF%.*}"
  826. else
  827. info_msg "[uWSGI:systemd-template] ${CONF} not installed (no need to restart)"
  828. fi
  829. ;;
  830. fedora-*)
  831. # in emperor mode, just touch the file to restart
  832. if uWSGI_app_enabled "${CONF}"; then
  833. touch "${uWSGI_APPS_ENABLED}/${CONF}"
  834. else
  835. info_msg "[uWSGI:emperor] ${CONF} not installed (no need to restart)"
  836. fi
  837. ;;
  838. *)
  839. err_msg "$DIST_ID-$DIST_VERS: uWSGI not yet implemented"
  840. return 42
  841. ;;
  842. esac
  843. }
  844. uWSGI_prepare_app() {
  845. # usage: uWSGI_prepare_app <myapp.ini>
  846. local APP="${1%.*}"
  847. if [[ -z $APP ]]; then
  848. err_msg "uWSGI_prepare_app: missing arguments"
  849. return 42
  850. fi
  851. case $DIST_ID-$DIST_VERS in
  852. fedora-*)
  853. # in emperor mode, the uwsgi user is the owner of the sockets
  854. info_msg "prepare (uwsgi:uwsgi) /run/uwsgi/app/${APP}"
  855. mkdir -p "/run/uwsgi/app/${APP}"
  856. chown -R "uwsgi:uwsgi" "/run/uwsgi/app/${APP}"
  857. ;;
  858. *)
  859. info_msg "prepare (${SERVICE_USER}:${SERVICE_GROUP}) /run/uwsgi/app/${APP}"
  860. mkdir -p "/run/uwsgi/app/${APP}"
  861. chown -R "${SERVICE_USER}:${SERVICE_GROUP}" "/run/uwsgi/app/${APP}"
  862. ;;
  863. esac
  864. }
  865. uWSGI_app_available() {
  866. # usage: uWSGI_app_available <myapp.ini>
  867. local CONF="$1"
  868. if [[ -z $CONF ]]; then
  869. err_msg "uWSGI_app_available: missing arguments"
  870. return 42
  871. fi
  872. [[ -f "${uWSGI_APPS_AVAILABLE}/${CONF}" ]]
  873. }
  874. uWSGI_install_app() {
  875. # usage: uWSGI_install_app [<template option> ...] <myapp.ini>
  876. #
  877. # <template option>: see install_template
  878. local pos_args=("$0")
  879. for i in "$@"; do
  880. case $i in
  881. -*) template_opts+=("$i");;
  882. *) pos_args+=("$i");;
  883. esac
  884. done
  885. uWSGI_prepare_app "${pos_args[1]}"
  886. mkdir -p "${uWSGI_APPS_AVAILABLE}"
  887. install_template "${template_opts[@]}" \
  888. "${uWSGI_APPS_AVAILABLE}/${pos_args[1]}" \
  889. root root 644
  890. uWSGI_enable_app "${pos_args[1]}"
  891. uWSGI_restart "${pos_args[1]}"
  892. info_msg "uWSGI app: ${pos_args[1]} is installed"
  893. }
  894. uWSGI_remove_app() {
  895. # usage: uWSGI_remove_app <myapp.ini>
  896. local CONF="$1"
  897. info_msg "remove uWSGI app: ${CONF}"
  898. uWSGI_disable_app "${CONF}"
  899. uWSGI_restart "${CONF}"
  900. rm -f "${uWSGI_APPS_AVAILABLE}/${CONF}"
  901. }
  902. uWSGI_app_enabled() {
  903. # usage: uWSGI_app_enabled <myapp.ini>
  904. local CONF="$1"
  905. local exit_val=0
  906. if [[ -z $CONF ]]; then
  907. err_msg "uWSGI_app_enabled: missing arguments"
  908. return 42
  909. fi
  910. case $DIST_ID-$DIST_VERS in
  911. ubuntu-*|debian-*)
  912. [[ -f "${uWSGI_APPS_ENABLED}/${CONF}" ]]
  913. exit_val=$?
  914. ;;
  915. arch-*)
  916. systemctl -q is-enabled "uwsgi@${CONF%.*}"
  917. exit_val=$?
  918. ;;
  919. fedora-*)
  920. [[ -f "${uWSGI_APPS_ENABLED}/${CONF}" ]]
  921. exit_val=$?
  922. ;;
  923. *)
  924. # FIXME
  925. err_msg "$DIST_ID-$DIST_VERS: uWSGI not yet implemented"
  926. exit_val=1
  927. ;;
  928. esac
  929. return $exit_val
  930. }
  931. # shellcheck disable=SC2164
  932. uWSGI_enable_app() {
  933. # usage: uWSGI_enable_app <myapp.ini>
  934. local CONF="$1"
  935. if [[ -z $CONF ]]; then
  936. err_msg "uWSGI_enable_app: missing arguments"
  937. return 42
  938. fi
  939. case $DIST_ID-$DIST_VERS in
  940. ubuntu-*|debian-*)
  941. mkdir -p "${uWSGI_APPS_ENABLED}"
  942. rm -f "${uWSGI_APPS_ENABLED}/${CONF}"
  943. ln -s "${uWSGI_APPS_AVAILABLE}/${CONF}" "${uWSGI_APPS_ENABLED}/${CONF}"
  944. info_msg "enabled uWSGI app: ${CONF} (restart required)"
  945. ;;
  946. arch-*)
  947. mkdir -p "${uWSGI_APPS_ENABLED}"
  948. rm -f "${uWSGI_APPS_ENABLED}/${CONF}"
  949. ln -s "${uWSGI_APPS_AVAILABLE}/${CONF}" "${uWSGI_APPS_ENABLED}/${CONF}"
  950. systemctl enable "uwsgi@${CONF%.*}"
  951. info_msg "enabled uWSGI app: ${CONF} (restart required)"
  952. ;;
  953. fedora-*)
  954. mkdir -p "${uWSGI_APPS_ENABLED}"
  955. rm -f "${uWSGI_APPS_ENABLED}/${CONF}"
  956. ln -s "${uWSGI_APPS_AVAILABLE}/${CONF}" "${uWSGI_APPS_ENABLED}/${CONF}"
  957. chown "${uWSGI_USER}:${uWSGI_GROUP}" "${uWSGI_APPS_ENABLED}/${CONF}"
  958. info_msg "enabled uWSGI app: ${CONF}"
  959. ;;
  960. *)
  961. # FIXME
  962. err_msg "$DIST_ID-$DIST_VERS: uWSGI not yet implemented"
  963. ;;
  964. esac
  965. }
  966. uWSGI_disable_app() {
  967. # usage: uWSGI_disable_app <myapp.ini>
  968. local CONF="$1"
  969. if [[ -z $CONF ]]; then
  970. err_msg "uWSGI_disable_app: missing arguments"
  971. return 42
  972. fi
  973. case $DIST_ID-$DIST_VERS in
  974. ubuntu-*|debian-*)
  975. service uwsgi stop "${CONF%.*}"
  976. rm -f "${uWSGI_APPS_ENABLED}/${CONF}"
  977. info_msg "disabled uWSGI app: ${CONF} (restart uWSGI required)"
  978. ;;
  979. arch-*)
  980. systemctl stop "uwsgi@${CONF%.*}"
  981. systemctl disable "uwsgi@${CONF%.*}"
  982. rm -f "${uWSGI_APPS_ENABLED}/${CONF}"
  983. ;;
  984. fedora-*)
  985. # in emperor mode, just remove the app.ini file
  986. rm -f "${uWSGI_APPS_ENABLED}/${CONF}"
  987. ;;
  988. *)
  989. # FIXME
  990. err_msg "$DIST_ID-$DIST_VERS: uWSGI not yet implemented"
  991. ;;
  992. esac
  993. }
  994. # distro's package manager
  995. # ------------------------
  996. pkg_install() {
  997. # usage: TITEL='install foobar' pkg_install foopkg barpkg
  998. rst_title "${TITLE:-installation of packages}" section
  999. echo -e "\npackage(s)::\n"
  1000. # shellcheck disable=SC2068
  1001. echo " " $@ | $FMT
  1002. if ! ask_yn "Should packages be installed?" Yn 30; then
  1003. return 42
  1004. fi
  1005. case $DIST_ID in
  1006. ubuntu|debian)
  1007. # shellcheck disable=SC2068
  1008. apt-get install -m -y $@
  1009. ;;
  1010. arch)
  1011. # shellcheck disable=SC2068
  1012. pacman -Sy --noconfirm $@
  1013. ;;
  1014. fedora)
  1015. # shellcheck disable=SC2068
  1016. dnf install -y $@
  1017. ;;
  1018. esac
  1019. }
  1020. pkg_remove() {
  1021. # usage: TITEL='remove foobar' pkg_remove foopkg barpkg
  1022. rst_title "${TITLE:-remove packages}" section
  1023. echo -e "\npackage(s)::\n"
  1024. # shellcheck disable=SC2068
  1025. echo " " $@ | $FMT
  1026. if ! ask_yn "Should packages be removed (purge)?" Yn 30; then
  1027. return 42
  1028. fi
  1029. case $DIST_ID in
  1030. ubuntu|debian)
  1031. # shellcheck disable=SC2068
  1032. apt-get purge --autoremove --ignore-missing -y $@
  1033. ;;
  1034. arch)
  1035. # shellcheck disable=SC2068
  1036. pacman -R --noconfirm $@
  1037. ;;
  1038. fedora)
  1039. # shellcheck disable=SC2068
  1040. dnf remove -y $@
  1041. ;;
  1042. esac
  1043. }
  1044. pkg_is_installed() {
  1045. # usage: pkg_is_install foopkg || pkg_install foopkg
  1046. case $DIST_ID in
  1047. ubuntu|debian)
  1048. dpkg -l "$1" &> /dev/null
  1049. return $?
  1050. ;;
  1051. arch)
  1052. pacman -Qsq "$1" &> /dev/null
  1053. return $?
  1054. ;;
  1055. fedora)
  1056. dnf list -q --installed "$1" &> /dev/null
  1057. return $?
  1058. ;;
  1059. esac
  1060. }
  1061. # git tooling
  1062. # -----------
  1063. # shellcheck disable=SC2164
  1064. git_clone() {
  1065. # usage:
  1066. #
  1067. # git_clone <url> <name> [<branch> [<user>]]
  1068. # git_clone <url> <path> [<branch> [<user>]]
  1069. #
  1070. # First form uses $CACHE/<name> as destination folder, second form clones
  1071. # into <path>. If repository is allready cloned, pull from <branch> and
  1072. # update working tree (if needed, the caller has to stash local changes).
  1073. #
  1074. # git clone https://github.com/asciimoo/searx searx-src origin/master searxlogin
  1075. #
  1076. local url="$1"
  1077. local dest="$2"
  1078. local branch="$3"
  1079. local user="$4"
  1080. local bash_cmd="bash"
  1081. local remote="origin"
  1082. if [[ ! "${dest:0:1}" = "/" ]]; then
  1083. dest="$CACHE/$dest"
  1084. fi
  1085. [[ -z $branch ]] && branch=master
  1086. [[ -z $user ]] && [[ -n "${SUDO_USER}" ]] && user="${SUDO_USER}"
  1087. [[ -n $user ]] && bash_cmd="sudo -H -u $user -i"
  1088. if [[ -d "${dest}" ]] ; then
  1089. info_msg "already cloned: $dest"
  1090. tee_stderr 0.1 <<EOF | $bash_cmd 2>&1 | prefix_stdout " ${_Yellow}|$user|${_creset} "
  1091. cd "${dest}"
  1092. git checkout -m -B "$branch" --track "$remote/$branch"
  1093. git pull --all
  1094. EOF
  1095. else
  1096. info_msg "clone into: $dest"
  1097. tee_stderr 0.1 <<EOF | $bash_cmd 2>&1 | prefix_stdout " ${_Yellow}|$user|${_creset} "
  1098. mkdir -p "$(dirname "$dest")"
  1099. cd "$(dirname "$dest")"
  1100. git clone --branch "$branch" --origin "$remote" "$url" "$(basename "$dest")"
  1101. EOF
  1102. fi
  1103. }
  1104. # containers
  1105. # ----------
  1106. in_container() {
  1107. # Test if shell runs in a container.
  1108. #
  1109. # usage: in_container && echo "process running inside a LXC container"
  1110. # in_container || echo "process is not running inside a LXC container"
  1111. #
  1112. # sudo_or_exit
  1113. # hint: Reads init process environment, therefore root access is required!
  1114. # to be safe, take a look at the environment of process 1 (/sbin/init)
  1115. # grep -qa 'container=lxc' /proc/1/environ
  1116. # see lxc_init_container_env
  1117. [[ -f /.lxcenv ]]
  1118. }
  1119. LXC_ENV_FOLDER=
  1120. if in_container; then
  1121. # shellcheck disable=SC2034
  1122. LXC_ENV_FOLDER="lxc/$(hostname)/"
  1123. fi
  1124. lxc_init_container_env() {
  1125. # usage: lxc_init_container_env <name>
  1126. # Create a /.lxcenv file in the root folder. Call this once after the
  1127. # container is inital started and before installing any boilerplate stuff.
  1128. info_msg "create /.lxcenv in container $1"
  1129. cat <<EOF | lxc exec "${1}" -- bash | prefix_stdout "[${_BBlue}${1}${_creset}] "
  1130. touch "/.lxcenv"
  1131. ls -l "/.lxcenv"
  1132. EOF
  1133. }
  1134. # apt packages
  1135. LXC_BASE_PACKAGES_debian="bash git build-essential python3 virtualenv"
  1136. # pacman packages
  1137. LXC_BASE_PACKAGES_arch="bash git base-devel python python-virtualenv"
  1138. # dnf packages
  1139. LXC_BASE_PACKAGES_fedora="bash git @development-tools python virtualenv"
  1140. case $DIST_ID in
  1141. ubuntu|debian) LXC_BASE_PACKAGES="${LXC_BASE_PACKAGES_debian}" ;;
  1142. arch) LXC_BASE_PACKAGES="${LXC_BASE_PACKAGES_arch}" ;;
  1143. fedora) LXC_BASE_PACKAGES="${LXC_BASE_PACKAGES_fedora}" ;;
  1144. *) err_msg "$DIST_ID-$DIST_VERS: pkg_install LXC_BASE_PACKAGES not yet implemented" ;;
  1145. esac
  1146. lxc_install_base_packages() {
  1147. info_msg "install LXC_BASE_PACKAGES in container $1"
  1148. pkg_install "${LXC_BASE_PACKAGES}"
  1149. }
  1150. lxc_image_copy() {
  1151. # usage: lxc_copy_image <remote image> <local image>
  1152. #
  1153. # lxc_copy_image "images:ubuntu/19.10" "ubu1910"
  1154. if lxc_image_exists "local:${LXC_SUITE[i+1]}"; then
  1155. info_msg "image ${LXC_SUITE[i]} already copied --> ${LXC_SUITE[i+1]}"
  1156. else
  1157. info_msg "copy image locally ${LXC_SUITE[i]} --> ${LXC_SUITE[i+1]}"
  1158. lxc image copy "${LXC_SUITE[i]}" local: \
  1159. --alias "${LXC_SUITE[i+1]}" | prefix_stdout
  1160. fi
  1161. }
  1162. lxc_init_container() {
  1163. # usage: lxc_init_container <image name> <container name>
  1164. local image_name="$1"
  1165. local container_name="$2"
  1166. if lxc info "${container_name}" &>/dev/null; then
  1167. info_msg "container '${container_name}' already exists"
  1168. else
  1169. info_msg "create container instance: ${container_name}"
  1170. lxc init "local:${image_name}" "${container_name}"
  1171. fi
  1172. }
  1173. lxc_exists(){
  1174. # usage: lxc_exists <name> || echo "container <name> does not exists"
  1175. lxc info "$1" &>/dev/null
  1176. }
  1177. lxc_image_exists(){
  1178. # usage: lxc_image_exists <alias> || echo "image <alias> does locally not exists"
  1179. lxc image info "local:$1" &>/dev/null
  1180. }
  1181. lxc_delete_container() {
  1182. # usage: lxc_delete_container <container-name>
  1183. if lxc info "$1" &>/dev/null; then
  1184. info_msg "stop & delete instance ${_BBlue}${1}${_creset}"
  1185. lxc stop "$1" &>/dev/null
  1186. lxc delete "$1" | prefix_stdout
  1187. else
  1188. warn_msg "instance '$1' does not exist / can't delete :o"
  1189. fi
  1190. }
  1191. lxc_delete_local_image() {
  1192. # usage: lxc_delete_local_image <container-name>
  1193. info_msg "delete image 'local:$i'"
  1194. lxc image delete "local:$i"
  1195. }
  1196. # IP
  1197. # --
  1198. global_IPs(){
  1199. # usage: global_IPS
  1200. #
  1201. # print list of host's SCOPE global addresses and adapters e.g::
  1202. #
  1203. # $ global_IPs
  1204. # enp4s0|192.168.1.127
  1205. # lxdbr0|10.246.86.1
  1206. # lxdbr0|fd42:8c58:2cd:b73f::1
  1207. ip -o addr show | sed -nr 's/[0-9]*:\s*([a-z0-9]*).*inet[6]?\s*([a-z0-9.:]*).*scope global.*/\1|\2/p'
  1208. }
  1209. primary_ip() {
  1210. case $DIST_ID in
  1211. arch)
  1212. ip -o addr show \
  1213. | sed -nr 's/[0-9]*:\s*([a-z0-9]*).*inet[6]?\s*([a-z0-9.:]*).*scope global.*/\2/p' \
  1214. | head -n 1
  1215. ;;
  1216. *) hostname -I | cut -d' ' -f1 ;;
  1217. esac
  1218. }
  1219. # URL
  1220. # ---
  1221. url_replace_hostname(){
  1222. # usage: url_replace_hostname <url> <new hostname>
  1223. # to replace hostname by primary IP::
  1224. #
  1225. # url_replace_hostname http://searx-ubu1604/morty $(primary_ip)
  1226. # http://10.246.86.250/morty
  1227. # shellcheck disable=SC2001
  1228. echo "$1" | sed "s|\(http[s]*://\)[^/]*\(.*\)|\1$2\2|"
  1229. }