lib.sh 35 KB

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