lib.sh 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #!/usr/bin/env bash
  2. # -*- coding: utf-8; mode: sh -*-
  3. # shellcheck disable=SC2059,SC1117,SC2162,SC2004
  4. if [[ -z "${REPO_ROOT}" ]]; then
  5. REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")
  6. while [ -h "${REPO_ROOT}" ] ; do
  7. REPO_ROOT=$(readlink "${REPO_ROOT}")
  8. done
  9. REPO_ROOT=$(cd "${REPO_ROOT}/.." && pwd -P )
  10. fi
  11. if [[ -z ${TEMPLATES} ]]; then
  12. TEMPLATES="${REPO_ROOT}/utils/templates"
  13. fi
  14. if [[ -z "$CACHE" ]]; then
  15. CACHE="${REPO_ROOT}/cache"
  16. fi
  17. if [[ -z "$SYSTEMD_UNITS" ]]; then
  18. SYSTEMD_UNITS="/lib/systemd/system"
  19. fi
  20. if [[ -z ${DIFF_CMD} ]]; then
  21. DIFF_CMD="diff -u"
  22. if command -v colordiff >/dev/null; then
  23. DIFF_CMD="colordiff -u"
  24. fi
  25. fi
  26. sudo_or_exit() {
  27. # usage: sudo_or_exit
  28. if [ ! "$(id -u)" -eq 0 ]; then
  29. err_msg "this command requires root (sudo) privilege!" >&2
  30. exit 42
  31. fi
  32. }
  33. rst_title() {
  34. # usage: rst_title <header-text> [part|chapter|section]
  35. case ${2-chapter} in
  36. part) printf "\n${1//?/=}\n$1\n${1//?/=}\n";;
  37. chapter) printf "\n${1}\n${1//?/=}\n";;
  38. section) printf "\n${1}\n${1//?/-}\n";;
  39. *)
  40. err_msg "invalid argument '${2}' in line $(caller)"
  41. return 42
  42. ;;
  43. esac
  44. }
  45. if command -v fmt >/dev/null; then
  46. export FMT="fmt -u"
  47. else
  48. export FMT="cat"
  49. fi
  50. rst_para() {
  51. # usage: RST_INDENT=1 rst_para "lorem ipsum ..."
  52. local prefix=''
  53. if ! [[ -z $RST_INDENT ]] && [[ $RST_INDENT -gt 0 ]]; then
  54. prefix="$(for i in $(seq 1 "$RST_INDENT"); do printf " "; done)"
  55. echo -en "\n$*\n" | $FMT | prefix_stdout "$prefix"
  56. else
  57. echo -en "\n$*\n" | $FMT
  58. fi
  59. }
  60. err_msg() { echo -e "ERROR: $*" >&2; }
  61. warn_msg() { echo -e "WARN: $*" >&2; }
  62. info_msg() { echo -e "INFO: $*"; }
  63. clean_stdin() {
  64. if [[ $(uname -s) != 'Darwin' ]]; then
  65. while $(read -n1 -t 0.1); do : ; done
  66. fi
  67. }
  68. wait_key(){
  69. # usage: waitKEY [<timeout in sec>]
  70. clean_stdin
  71. local _t=$1
  72. [[ ! -z $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
  73. [[ ! -z $_t ]] && _t="-t $_t"
  74. # shellcheck disable=SC2086
  75. read -s -n1 $_t -p "** press any [KEY] to continue **"
  76. echo
  77. clean_stdin
  78. }
  79. ask_yn() {
  80. # usage: ask_yn <prompt-text> [Ny|Yn] [<timeout in sec>]
  81. local EXIT_YES=0 # exit status 0 --> successful
  82. local EXIT_NO=1 # exit status 1 --> error code
  83. local _t=$3
  84. [[ ! -z $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
  85. [[ ! -z $_t ]] && _t="-t $_t"
  86. case "${2}" in
  87. Yn)
  88. local exit_val=${EXIT_YES}
  89. local choice="[YES/no]"
  90. local default="Yes"
  91. ;;
  92. *)
  93. local exit_val=${EXIT_NO}
  94. local choice="[NO/yes]"
  95. local default="No"
  96. ;;
  97. esac
  98. echo
  99. while true; do
  100. clean_stdin
  101. printf "$1 ${choice} "
  102. # shellcheck disable=SC2086
  103. read -n1 $_t
  104. if [[ -z $REPLY ]]; then
  105. printf "$default\n"; break
  106. elif [[ $REPLY =~ ^[Yy]$ ]]; then
  107. exit_val=${EXIT_YES}
  108. printf "\n"
  109. break
  110. elif [[ $REPLY =~ ^[Nn]$ ]]; then
  111. exit_val=${EXIT_NO}
  112. printf "\n"
  113. break
  114. fi
  115. _t=""
  116. err_msg "invalid choice"
  117. done
  118. clean_stdin
  119. return $exit_val
  120. }
  121. tee_stderr () {
  122. # usage::
  123. # tee_stderr 1 <<EOF | python -i
  124. # print("hello")
  125. # EOF
  126. # ...
  127. # >>> print("hello")
  128. # hello
  129. local _t="0";
  130. if [[ ! -z $1 ]] ; then _t="$1"; fi
  131. (while read line; do
  132. # shellcheck disable=SC2086
  133. sleep $_t
  134. echo -e "$line" >&2
  135. echo "$line"
  136. done)
  137. }
  138. prefix_stdout () {
  139. # usage: <cmd> | prefix_stdout [prefix]
  140. local prefix=" | "
  141. if [[ ! -z $1 ]] ; then prefix="$1"; fi
  142. (while IFS= read line; do
  143. echo -e "${prefix}$line"
  144. done)
  145. }
  146. append_line() {
  147. # usage: append_line <line> <file>
  148. #
  149. # Append line if not exists, create file if not exists. E.g::
  150. #
  151. # append_line 'source ~/.foo' ~/bashrc
  152. local LINE=$1
  153. local FILE=$2
  154. grep -qFs -- "$LINE" "$FILE" || echo "$LINE" >> "$FILE"
  155. }
  156. cache_download() {
  157. # usage: cache_download <url> <local-filename>
  158. local exit_value=0
  159. if [[ ! -z ${SUDO_USER} ]]; then
  160. sudo -u "${SUDO_USER}" mkdir -p "${CACHE}"
  161. else
  162. mkdir -p "${CACHE}"
  163. fi
  164. if [[ -f "${CACHE}/$2" ]] ; then
  165. info_msg "already cached: $1"
  166. info_msg " --> ${CACHE}/$2"
  167. fi
  168. if [[ ! -f "${CACHE}/$2" ]]; then
  169. info_msg "caching: $1"
  170. info_msg " --> ${CACHE}/$2"
  171. if [[ ! -z ${SUDO_USER} ]]; then
  172. sudo -u "${SUDO_USER}" wget --progress=bar -O "${CACHE}/$2" "$1" ; exit_value=$?
  173. else
  174. wget --progress=bar -O "${CACHE}/$2" "$1" ; exit_value=$?
  175. fi
  176. if $exit_value; then
  177. err_msg "failed to download: $1"
  178. fi
  179. fi
  180. }
  181. choose_one() {
  182. # usage:
  183. #
  184. # DEFAULT_SELECT= 2 \
  185. # choose_one <name> "your selection?" "Coffee" "Coffee with milk"
  186. local default=${DEFAULT_SELECT-1}
  187. local REPLY
  188. local env_name=$1 && shift
  189. local choice=$1;
  190. local max="${#@}"
  191. local _t
  192. [[ ! -z $FORCE_TIMEOUT ]] && _t=$FORCE_TIMEOUT
  193. [[ ! -z $_t ]] && _t="-t $_t"
  194. list=("$@")
  195. echo -e "Menu::"
  196. for ((i=1; i<= $(($max -1)); i++)); do
  197. if [[ "$i" == "$default" ]]; then
  198. echo -e " $i.) ${list[$i]} [default]"
  199. else
  200. echo -e " $i.) ${list[$i]}"
  201. fi
  202. done
  203. while true; do
  204. clean_stdin
  205. printf "$1 [$default] "
  206. if (( 10 > $max )); then
  207. # shellcheck disable=SC2086
  208. read -n1 $_t
  209. else
  210. # shellcheck disable=SC2086,SC2229
  211. read $_t
  212. fi
  213. # selection fits
  214. [[ $REPLY =~ ^-?[0-9]+$ ]] && (( $REPLY > 0 )) && (( $REPLY < $max )) && break
  215. # take default
  216. [[ -z $REPLY ]] && REPLY=$default && break
  217. _t=""
  218. err_msg "invalid choice"
  219. done
  220. echo
  221. clean_stdin
  222. eval "$env_name"='${list[${REPLY}]}'
  223. }
  224. install_template() {
  225. # usage:
  226. #
  227. # install_template [--no-eval] {file} [{owner} [{group} [{chmod}]]]
  228. #
  229. # install_template --no-eval /etc/updatedb.conf root root 644
  230. local do_eval=1
  231. if [[ "$1" == "--no-eval" ]]; then
  232. do_eval=0; shift
  233. fi
  234. local dst="${1}"
  235. local owner=${2-$(id -un)}
  236. local group=${3-$(id -gn)}
  237. local chmod=${4-644}
  238. local _reply=""
  239. info_msg "install: ${dst}"
  240. if [[ ! -f "${TEMPLATES}${dst}" ]] ; then
  241. err_msg "${TEMPLATES}${dst} does not exists"
  242. err_msg "... can't install $dst / exit installation with error 42"
  243. wait_key 30
  244. return 42
  245. fi
  246. local template_file="${TEMPLATES}${dst}"
  247. if [[ "$do_eval" == "1" ]]; then
  248. info_msg "BUILD template ${template_file}"
  249. if [[ -f "${TEMPLATES}${dst}" ]] ; then
  250. template_file="${CACHE}${dst}"
  251. mkdir -p "$(dirname "${template_file}")"
  252. # shellcheck disable=SC2086
  253. eval "echo \"$(cat ${TEMPLATES}${dst})\"" > "${template_file}"
  254. else
  255. err_msg "failed ${template_file}"
  256. return 42
  257. fi
  258. fi
  259. mkdir -p "$(dirname "${dst}")"
  260. if [[ ! -f "${dst}" ]]; then
  261. info_msg "install: ${template_file}"
  262. sudo -H install -v -o "${owner}" -g "${group}" -m "${chmod}" \
  263. "${template_file}" "${dst}" | prefix_stdout
  264. return $?
  265. fi
  266. if [[ -f "${dst}" ]] && cmp --silent "${template_file}" "${dst}" ; then
  267. info_msg "file ${dst} allready installed"
  268. return 0
  269. fi
  270. info_msg "file ${dst} allready exists on this host"
  271. while true; do
  272. choose_one _reply "choose next step with file $dst" \
  273. "replace file" \
  274. "leave file unchanged" \
  275. "interactiv shell" \
  276. "diff files"
  277. case $_reply in
  278. "replace file")
  279. info_msg "install: ${template_file}"
  280. sudo -H install -v -o "${owner}" -g "${group}" -m "${chmod}" \
  281. "${template_file}" "${dst}" | prefix_stdout
  282. break
  283. ;;
  284. "leave file unchanged")
  285. break
  286. ;;
  287. "interactiv shell")
  288. echo "// edit ${dst} to your needs"
  289. echo "// exit with CTRL-D"
  290. sudo -H -u "${owner}" -i
  291. $DIFF_CMD "${dst}" "${template_file}"
  292. if ask_yn "did you edit ${template_file} to your needs?"; then
  293. break
  294. fi
  295. ;;
  296. "diff files")
  297. $DIFF_CMD "${dst}" "${template_file}" | prefix_stdout
  298. esac
  299. done
  300. }