core_messages.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. #!/bin/bash
  2. # LinuxGSM core_messages.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Defines on-screen messages such as [ OK ] and how script logs look.
  7. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. # nl: new line: message is following by a new line.
  9. # eol: end of line: message is placed at the end of the current line.
  10. fn_ansi_loader() {
  11. # carriage return.
  12. creeol="\r"
  13. if [ "${ansi}" != "off" ]; then
  14. # echo colors
  15. default="\e[0m"
  16. black="\e[30m"
  17. red="\e[31m"
  18. lightred="\e[91m"
  19. green="\e[32m"
  20. lightgreen="\e[92m"
  21. yellow="\e[33m"
  22. lightyellow="\e[93m"
  23. blue="\e[34m"
  24. lightblue="\e[94m"
  25. magenta="\e[35m"
  26. lightmagenta="\e[95m"
  27. cyan="\e[36m"
  28. lightcyan="\e[96m"
  29. darkgrey="\e[90m"
  30. lightgrey="\e[37m"
  31. white="\e[97m"
  32. # erase to end of line.
  33. creeol+="\033[K"
  34. fi
  35. # carriage return & erase to end of line.
  36. creeol="\r\033[K"
  37. bold="\e[1m"
  38. dim="\e[2m"
  39. italic="\e[3m"
  40. underline="\e[4m"
  41. reverse="\e[7m"
  42. }
  43. fn_sleep_time() {
  44. sleep "0.1"
  45. }
  46. fn_sleep_time_05() {
  47. sleep "0.5"
  48. }
  49. fn_sleep_time_1() {
  50. sleep "1"
  51. }
  52. fn_sleep_time_5() {
  53. sleep "5"
  54. }
  55. fn_sleep_time_10() {
  56. sleep "10"
  57. }
  58. # Log display
  59. ########################
  60. ## Feb 28 14:56:58 ut99-server: Monitor:
  61. fn_script_log() {
  62. if [ -d "${lgsmlogdir}" ]; then
  63. if [ -n "${commandname}" ]; then
  64. echo -e "$(date '+%b %d %H:%M:%S.%3N') ${selfname}: ${commandname}: ${1}" >> "${lgsmlog}"
  65. else
  66. echo -e "$(date '+%b %d %H:%M:%S.%3N') ${selfname}: ${1}" >> "${lgsmlog}"
  67. fi
  68. fi
  69. }
  70. ## Feb 28 14:56:58 ut99-server: Monitor: PASS:
  71. fn_script_log_pass() {
  72. fn_script_log "PASS: ${1}"
  73. exitcode=0
  74. }
  75. ## Feb 28 14:56:58 ut99-server: Monitor: FATAL:
  76. fn_script_log_fail() {
  77. fn_script_log "FAIL: ${1}"
  78. exitcode=1
  79. }
  80. ## Feb 28 14:56:58 ut99-server: Monitor: ERROR:
  81. fn_script_log_error() {
  82. fn_script_log "ERROR: ${1}"
  83. exitcode=2
  84. }
  85. ## Feb 28 14:56:58 ut99-server: Monitor: WARN:
  86. fn_script_log_warn() {
  87. fn_script_log "WARN: ${1}"
  88. exitcode=3
  89. }
  90. ## Feb 28 14:56:58 ut99-server: Monitor: INFO:
  91. fn_script_log_info() {
  92. fn_script_log "INFO: ${1}"
  93. }
  94. # On-Screen - Automated functions
  95. ##################################
  96. fn_print() {
  97. echo -en "$*${default}"
  98. }
  99. fn_print_nl() {
  100. echo -e "$*${default}"
  101. }
  102. # Helper function to print messages with a specific format and color
  103. fn_print_message() {
  104. local type="$1"
  105. local color="$2"
  106. local message="$3"
  107. if [ "${commandaction}" ]; then
  108. echo -en "${bold}${creeol}[${color} ${type} ${default}]${default} ${commandaction} ${selfname}: ${message}${default}"
  109. else
  110. echo -en "${bold}${cree}[${color} ${type} ${default}]${default} ${message}${default}"
  111. fi
  112. fn_sleep_time
  113. }
  114. fn_print_message_nl() {
  115. local type="$1"
  116. local color="$2"
  117. local message="$3"
  118. if [ "${commandaction}" ]; then
  119. echo -e "${bold}${creeol}[${color} ${type} ${default}]${default} ${commandaction} ${selfname}: ${message}${default}"
  120. else
  121. echo -e "${bold}${creeol}[${color} ${type} ${default}]${default} ${message}${default}"
  122. fi
  123. fn_sleep_time
  124. }
  125. # [ .... ]
  126. fn_print_dots() {
  127. fn_print_message "...." "${default}" "$*"
  128. fn_sleep_time_05
  129. }
  130. fn_print_dots_nl() {
  131. fn_print_message_nl "...." "${default}" "$*"
  132. fn_sleep_time_05
  133. }
  134. # [ OK ]
  135. fn_print_ok() {
  136. fn_print_message " OK " "${green}" "$*"
  137. }
  138. fn_print_ok_nl() {
  139. fn_print_message_nl " OK " "${green}" "$*"
  140. }
  141. # [ FAIL ]
  142. fn_print_fail() {
  143. fn_print_message "FAIL" "${red}" "$*"
  144. }
  145. fn_print_fail_nl() {
  146. fn_print_message_nl "FAIL" "${red}" "$*"
  147. }
  148. # [ ERROR ]
  149. fn_print_error() {
  150. fn_print_message "ERROR" "${red}" "$*"
  151. }
  152. fn_print_error_nl() {
  153. fn_print_message_nl "ERROR" "${red}" "$*"
  154. }
  155. # [ WARN ]
  156. fn_print_warn() {
  157. fn_print_message "WARN" "${lightyellow}" "$*"
  158. }
  159. fn_print_warn_nl() {
  160. fn_print_message_nl "WARN" "${lightyellow}" "$*"
  161. }
  162. # [ INFO ]
  163. fn_print_info() {
  164. fn_print_message "INFO" "${cyan}" "$*"
  165. }
  166. fn_print_info_nl() {
  167. fn_print_message_nl "INFO" "${cyan}" "$*"
  168. }
  169. # [ SKIP ]
  170. fn_print_skip() {
  171. fn_print_message "SKIP" "${cyan}" "$*"
  172. }
  173. fn_print_skip_nl() {
  174. fn_print_message_nl "SKIP" "${cyan}" "$*"
  175. }
  176. # [ START ]
  177. fn_print_start() {
  178. fn_print_message "START" "${lightgreen}" "$*"
  179. }
  180. fn_print_start_nl() {
  181. fn_print_message_nl "START" "${lightgreen}" "$*"
  182. }
  183. # On-Screen - Interactive messages
  184. ##################################
  185. # Separator is different for details.
  186. fn_messages_separator() {
  187. if [ "${commandname}" == "DETAILS" ]; then
  188. # Fall back to 80 columns when there is no terminal (e.g. cron or Docker); bash 5.3+ rejects an empty printf width.
  189. local columns="${COLUMNS:-$(tput cols 2> /dev/null)}"
  190. printf '%*s\n' "${columns:-80}" '' | tr ' ' =
  191. else
  192. echo -e "${bold}=================================${default}"
  193. fn_sleep_time
  194. fi
  195. }
  196. # No More Room in Hell Debug
  197. # =================================
  198. fn_print_header() {
  199. echo -e ""
  200. echo -e "${bold}${lightyellow}${gamename} ${commandaction}${default}"
  201. fn_messages_separator
  202. }
  203. # Success!
  204. fn_print_success() {
  205. echo -en "${green}Success!${default} $*${default}"
  206. fn_sleep_time
  207. }
  208. fn_print_success_nl() {
  209. echo -e "${green}Success!${default} $*${default}"
  210. fn_sleep_time
  211. }
  212. # Failure!
  213. fn_print_failure() {
  214. echo -en "${red}Failure!${default} $*${default}"
  215. fn_sleep_time
  216. }
  217. fn_print_failure_nl() {
  218. echo -e "${red}Failure!${default} $*${default}"
  219. fn_sleep_time
  220. }
  221. # Error!
  222. fn_print_error2() {
  223. echo -en "${red}Error!${default} $*${default}"
  224. fn_sleep_time
  225. }
  226. fn_print_error2_nl() {
  227. echo -e "${red}Error!${default} $*${default}"
  228. fn_sleep_time
  229. }
  230. # Warning!
  231. fn_print_warning() {
  232. echo -en "${lightyellow}Warning!${default} $*${default}"
  233. fn_sleep_time
  234. }
  235. fn_print_warning_nl() {
  236. echo -e "${lightyellow}Warning!${default} $*${default}"
  237. fn_sleep_time
  238. }
  239. # Information!
  240. fn_print_information() {
  241. echo -en "${cyan}Information!${default} $*${default}"
  242. fn_sleep_time
  243. }
  244. fn_print_information_nl() {
  245. echo -e "${cyan}Information!${default} $*${default}"
  246. fn_sleep_time
  247. }
  248. # Skip!
  249. fn_print_skip2() {
  250. echo -en "${cyan}Skip!${default} $*${default}"
  251. fn_sleep_time
  252. }
  253. fn_print_skip2_nl() {
  254. echo -e "${cyan}Skip!${default} $*${default}"
  255. fn_sleep_time
  256. }
  257. # Y/N Prompt
  258. fn_prompt_yn() {
  259. echo -e ""
  260. local prompt="$1"
  261. local initial="$2"
  262. if [ "${initial}" == "Y" ]; then
  263. prompt+=" [Y/n] "
  264. elif [ "${initial}" == "N" ]; then
  265. prompt+=" [y/N] "
  266. else
  267. prompt+=" [y/n] "
  268. fi
  269. while true; do
  270. if ! read -e -i "${initial}" -p "${prompt}" -r yn && [ -z "${yn}" ]; then
  271. echo -e ""
  272. fn_print_error_nl "Unable to read an answer: no input available"
  273. fn_script_log_error "Unable to read an answer to \"${1}\": no input available"
  274. # shellcheck disable=SC2034 # Used by core_exit.sh.
  275. exitcode=2
  276. core_exit.sh
  277. fi
  278. case "${yn}" in
  279. [Yy] | [Yy][Ee][Ss]) return 0 ;;
  280. [Nn] | [Nn][Oo]) return 1 ;;
  281. *) echo -e "Please answer yes or no." ;;
  282. esac
  283. done
  284. }
  285. # Prompt for message
  286. fn_prompt_message() {
  287. while true; do
  288. unset prompt
  289. local prompt="$1"
  290. if ! read -e -p "${prompt}" -r answer && [ -z "${answer}" ]; then
  291. return 1
  292. fi
  293. if fn_prompt_yn "Continue" Y; then
  294. break
  295. fi
  296. done
  297. echo "${answer}"
  298. }
  299. # On-Screen End of Line
  300. ##################################
  301. # YES
  302. fn_print_yes_eol() {
  303. echo -en " ... ${cyan}YES${default}"
  304. fn_sleep_time
  305. }
  306. fn_print_yes_eol_nl() {
  307. echo -e " ... ${cyan}YES${default}"
  308. fn_sleep_time
  309. }
  310. # NO
  311. fn_print_no_eol() {
  312. echo -en " ... ${red}NO${default}"
  313. fn_sleep_time
  314. }
  315. fn_print_no_eol_nl() {
  316. echo -e " ... ${red}NO${default}"
  317. fn_sleep_time
  318. }
  319. # OK
  320. fn_print_ok_eol() {
  321. echo -en " ... ${green}OK${default}"
  322. fn_sleep_time
  323. }
  324. fn_print_ok_eol_nl() {
  325. echo -e " ... ${green}OK${default}"
  326. fn_sleep_time
  327. }
  328. # FAIL
  329. fn_print_fail_eol() {
  330. echo -en " ... ${red}FAIL${default}"
  331. fn_sleep_time
  332. }
  333. fn_print_fail_eol_nl() {
  334. echo -e " ... ${red}FAIL${default}"
  335. fn_sleep_time
  336. }
  337. # ERROR
  338. fn_print_error_eol() {
  339. echo -en " ... ${red}ERROR${default}"
  340. fn_sleep_time
  341. }
  342. fn_print_error_eol_nl() {
  343. echo -e " ... ${red}ERROR${default}"
  344. fn_sleep_time
  345. }
  346. # WAIT
  347. fn_print_wait_eol() {
  348. echo -en " ... ${cyan}WAIT${default}"
  349. fn_sleep_time
  350. }
  351. fn_print_wait_eol_nl() {
  352. echo -e " ... ${cyan}WAIT${default}"
  353. fn_sleep_time
  354. }
  355. # WARN
  356. fn_print_warn_eol() {
  357. echo -en " ... ${lightyellow}WARN${default}"
  358. fn_sleep_time
  359. }
  360. fn_print_warn_eol_nl() {
  361. echo -e " ... ${lightyellow}WARN${default}"
  362. fn_sleep_time
  363. }
  364. # INFO
  365. fn_print_info_eol() {
  366. echo -en " ... ${cyan}INFO${default}"
  367. fn_sleep_time
  368. }
  369. fn_print_info_eol_nl() {
  370. echo -e " ... ${cyan}INFO${default}"
  371. fn_sleep_time
  372. }
  373. # QUERYING
  374. fn_print_querying_eol() {
  375. echo -en " ... ${cyan}QUERYING${default}"
  376. fn_sleep_time_1
  377. }
  378. fn_print_querying_eol_nl() {
  379. echo -e " ... ${cyan}QUERYING${default}"
  380. fn_sleep_time_1
  381. }
  382. # CHECKING
  383. fn_print_checking_eol() {
  384. echo -en " ... ${cyan}CHECKING${default}"
  385. fn_sleep_time_1
  386. }
  387. fn_print_checking_eol_nl() {
  388. echo -e " ... ${cyan}CHECKING${default}"
  389. fn_sleep_time_1
  390. }
  391. # DELAY
  392. fn_print_delay_eol() {
  393. echo -en " ... ${green}DELAY${default}"
  394. fn_sleep_time_1
  395. }
  396. fn_print_delay_eol_nl() {
  397. echo -e " ... ${green}DELAY${default}"
  398. fn_sleep_time_1
  399. }
  400. # CANCELED
  401. fn_print_canceled_eol() {
  402. echo -en " ... ${lightyellow}CANCELED${default}"
  403. fn_sleep_time_1
  404. }
  405. fn_print_canceled_eol_nl() {
  406. echo -e " ... ${lightyellow}CANCELED${default}"
  407. fn_sleep_time_1
  408. }
  409. # REMOVED
  410. fn_print_removed_eol() {
  411. echo -en " ... ${red}REMOVED${default}"
  412. fn_sleep_time_1
  413. }
  414. fn_print_removed_eol_nl() {
  415. echo -e " ... ${red}REMOVED${default}"
  416. fn_sleep_time_1
  417. }
  418. # UPDATE
  419. fn_print_update_eol() {
  420. echo -en " ... ${lightblue}UPDATE${default}"
  421. fn_sleep_time
  422. }
  423. fn_print_update_eol_nl() {
  424. echo -e " ... ${lightblue}UPDATE${default}"
  425. fn_sleep_time
  426. }
  427. # SKIP
  428. fn_print_skip_eol() {
  429. echo -en " ... ${cyan}SKIP${default}"
  430. fn_sleep_time
  431. }
  432. fn_print_skip_eol_nl() {
  433. echo -e " ... ${cyan}SKIP${default}"
  434. fn_sleep_time
  435. }
  436. fn_print_ascii_logo() {
  437. echo -e ""
  438. echo -e " mdMMMMbm"
  439. echo -e " mMMMMMMMMMMm"
  440. echo -e " mMMMMMMMMMMMMm"
  441. echo -e " mMMMMMMMMMMMMMMm"
  442. echo -e " hMMMV^VMMV^VMMMh"
  443. echo -e " MMMMM MM MMMMM"
  444. echo -e " hMMs vv sMMh"
  445. echo -e " hMMM: :MMMh"
  446. echo -e " .hMMMh hMMMh."
  447. echo -e " -dMMMh ${lightgrey}__${default} hMMMd-"
  448. echo -e " :mMMMs ${lightgrey}||${default} sMMMm:"
  449. echo -e " :MMMM+ ${lightgrey}||${default} ${red}_${default} +NMMN:"
  450. echo -e " .mMMM+ ${lightgrey}========${default} +MMMm."
  451. echo -e " yMMMy ${darkgrey}##############${default} yMMMy"
  452. echo -e " mMMM: ${darkgrey}##############${default} :MMMm"
  453. echo -e " mMM ${lightyellow}nn${default} ${lightyellow}nn${default} ${lightyellow}nn${default} ${lightyellow}nn${default} MMm"
  454. echo -e " o ${lightyellow}nNNNNNNNn${default} ${lightyellow}nNNNNNNNn${default} o"
  455. echo -e " ${lightyellow}nNNNNNNNNNn${default} ${lightyellow}nNNNNNNNNNn${default}"
  456. echo -e " ${lightyellow}nNNNNNNNNNNN${default} ${lightyellow}NNNNNNNNNNNn${default}"
  457. echo -e " ${lightyellow}+NNNNNNNNN:${default} ${lightyellow}:NNNNNNNNN+${default}"
  458. echo -e " ${lightyellow}nNNNNNNN${default} /\ ${lightyellow}NNNNNNNn${default}"
  459. echo -e " ${lightyellow}nnnnn${default} db ${lightyellow}nnnnn${default}"
  460. echo -e ""
  461. echo -e "${lightyellow}888${default} ${lightyellow}d8b${default} ${default}.d8888b. .d8888b. 888b d888"
  462. echo -e "${lightyellow}888 Y8P ${default}d88P Y88b d88P Y88b 8888b d8888"
  463. echo -e "${lightyellow}888${default} ${default}888${default} 888 Y88b. 88888b.d88888"
  464. echo -e "${lightyellow}888${default} ${lightyellow}888${default} ${lightyellow}88888b.${default} ${lightyellow}888${default} ${lightyellow}888${default} ${lightyellow}888${default} ${lightyellow}888${default} 888 Y888b. 888Y88888P888"
  465. echo -e "${lightyellow}888${default} ${lightyellow}888${default} ${lightyellow}888${default} ${lightyellow}88b${default} ${lightyellow}888${default} ${lightyellow}888${default} ${lightyellow}Y8bd8P${default} 888 88888 Y88b. 888 Y888P 888"
  466. echo -e "${lightyellow}888${default} ${lightyellow}888${default} ${lightyellow}888${default} ${lightyellow}888${default} ${lightyellow}888${default} ${lightyellow}888${default} ${lightyellow}X88K${default} 888 888 888 888 Y8P 888"
  467. echo -e "${lightyellow}888${default} ${lightyellow}888${default} ${lightyellow}888${default} ${lightyellow}888${default} ${lightyellow}Y88b${default} ${lightyellow}88Y${default} ${lightyellow}.d8pq8b.${default} Y88b d88P Y88b d88P 888 * 888"
  468. echo -e "${lightyellow}LinuxGSM${default} ${lightyellow}888${default} ${lightyellow}888${default} ${lightyellow}888${default} ${lightyellow}Y8888Y${default} ${lightyellow}888${default} ${lightyellow}888${default} Y2012P88 Y8888P 888 888"
  469. echo -e ""
  470. }
  471. fn_print_restart_warning() {
  472. fn_print_warn "${selfname} will be restarted"
  473. fn_script_log_warn "${selfname} will be restarted"
  474. totalseconds=3
  475. for seconds in {3..1}; do
  476. fn_print_warn "${selfname} will be restarted: ${totalseconds}"
  477. totalseconds=$((totalseconds - 1))
  478. fn_sleep_time_1
  479. if [ "${seconds}" == "0" ]; then
  480. break
  481. fi
  482. done
  483. fn_print_warn_nl "${selfname} will be restarted"
  484. }
  485. # Functions below are used to ensure that logs and UI correctly reflect the command it is actually running.
  486. # Useful when a command has to call upon another command causing the other command to overwrite commandname variables
  487. # Used to remember the command that ran first.
  488. fn_firstcommand_set() {
  489. if [ -z "${firstcommandname}" ]; then
  490. firstcommandname="${commandname}"
  491. firstcommandaction="${commandaction}"
  492. fi
  493. }
  494. # Used to reset commandname variables to the command the script ran first.
  495. fn_firstcommand_reset() {
  496. commandname="${firstcommandname}"
  497. commandaction="${firstcommandaction}"
  498. }