core_messages.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. # [ START ]
  170. fn_print_start() {
  171. fn_print_message "START" "${lightgreen}" "$*"
  172. }
  173. fn_print_start_nl() {
  174. fn_print_message_nl "START" "${lightgreen}" "$*"
  175. }
  176. # On-Screen - Interactive messages
  177. ##################################
  178. # Separator is different for details.
  179. fn_messages_separator() {
  180. if [ "${commandname}" == "DETAILS" ]; then
  181. printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' =
  182. else
  183. echo -e "${bold}=================================${default}"
  184. fn_sleep_time
  185. fi
  186. }
  187. # No More Room in Hell Debug
  188. # =================================
  189. fn_print_header() {
  190. echo -e ""
  191. echo -e "${bold}${lightyellow}${gamename} ${commandaction}${default}"
  192. fn_messages_separator
  193. }
  194. # Success!
  195. fn_print_success() {
  196. echo -en "${green}Success!${default} $*${default}"
  197. fn_sleep_time
  198. }
  199. fn_print_success_nl() {
  200. echo -e "${green}Success!${default} $*${default}"
  201. fn_sleep_time
  202. }
  203. # Failure!
  204. fn_print_failure() {
  205. echo -en "${red}Failure!${default} $*${default}"
  206. fn_sleep_time
  207. }
  208. fn_print_failure_nl() {
  209. echo -e "${red}Failure!${default} $*${default}"
  210. fn_sleep_time
  211. }
  212. # Error!
  213. fn_print_error2() {
  214. echo -en "${red}Error!${default} $*${default}"
  215. fn_sleep_time
  216. }
  217. fn_print_error2_nl() {
  218. echo -e "${red}Error!${default} $*${default}"
  219. fn_sleep_time
  220. }
  221. # Warning!
  222. fn_print_warning() {
  223. echo -en "${lightyellow}Warning!${default} $*${default}"
  224. fn_sleep_time
  225. }
  226. fn_print_warning_nl() {
  227. echo -e "${lightyellow}Warning!${default} $*${default}"
  228. fn_sleep_time
  229. }
  230. # Information!
  231. fn_print_information() {
  232. echo -en "${cyan}Information!${default} $*${default}"
  233. fn_sleep_time
  234. }
  235. fn_print_information_nl() {
  236. echo -e "${cyan}Information!${default} $*${default}"
  237. fn_sleep_time
  238. }
  239. # Y/N Prompt
  240. fn_prompt_yn() {
  241. echo -e ""
  242. local prompt="$1"
  243. local initial="$2"
  244. if [ "${initial}" == "Y" ]; then
  245. prompt+=" [Y/n] "
  246. elif [ "${initial}" == "N" ]; then
  247. prompt+=" [y/N] "
  248. else
  249. prompt+=" [y/n] "
  250. fi
  251. while true; do
  252. read -e -i "${initial}" -p "${prompt}" -r yn
  253. case "${yn}" in
  254. [Yy] | [Yy][Ee][Ss]) return 0 ;;
  255. [Nn] | [Nn][Oo]) return 1 ;;
  256. *) echo -e "Please answer yes or no." ;;
  257. esac
  258. done
  259. }
  260. # Prompt for message
  261. fn_prompt_message() {
  262. while true; do
  263. unset prompt
  264. local prompt="$1"
  265. read -e -p "${prompt}" -r answer
  266. if fn_prompt_yn "Continue" Y; then
  267. break
  268. fi
  269. done
  270. echo "${answer}"
  271. }
  272. # On-Screen End of Line
  273. ##################################
  274. # YES
  275. fn_print_yes_eol() {
  276. echo -en " ... ${cyan}YES${default}"
  277. fn_sleep_time
  278. }
  279. fn_print_yes_eol_nl() {
  280. echo -e " ... ${cyan}YES${default}"
  281. fn_sleep_time
  282. }
  283. # NO
  284. fn_print_no_eol() {
  285. echo -en " ... ${red}NO${default}"
  286. fn_sleep_time
  287. }
  288. fn_print_no_eol_nl() {
  289. echo -e " ... ${red}NO${default}"
  290. fn_sleep_time
  291. }
  292. # OK
  293. fn_print_ok_eol() {
  294. echo -en " ... ${green}OK${default}"
  295. fn_sleep_time
  296. }
  297. fn_print_ok_eol_nl() {
  298. echo -e " ... ${green}OK${default}"
  299. fn_sleep_time
  300. }
  301. # FAIL
  302. fn_print_fail_eol() {
  303. echo -en " ... ${red}FAIL${default}"
  304. fn_sleep_time
  305. }
  306. fn_print_fail_eol_nl() {
  307. echo -e " ... ${red}FAIL${default}"
  308. fn_sleep_time
  309. }
  310. # ERROR
  311. fn_print_error_eol() {
  312. echo -en " ... ${red}ERROR${default}"
  313. fn_sleep_time
  314. }
  315. fn_print_error_eol_nl() {
  316. echo -e " ... ${red}ERROR${default}"
  317. fn_sleep_time
  318. }
  319. # WAIT
  320. fn_print_wait_eol() {
  321. echo -en " ... ${cyan}WAIT${default}"
  322. fn_sleep_time
  323. }
  324. fn_print_wait_eol_nl() {
  325. echo -e " ... ${cyan}WAIT${default}"
  326. fn_sleep_time
  327. }
  328. # WARN
  329. fn_print_warn_eol() {
  330. echo -en " ... ${lightyellow}WARN${default}"
  331. fn_sleep_time
  332. }
  333. fn_print_warn_eol_nl() {
  334. echo -e " ... ${lightyellow}WARN${default}"
  335. fn_sleep_time
  336. }
  337. # INFO
  338. fn_print_info_eol() {
  339. echo -en " ... ${cyan}INFO${default}"
  340. fn_sleep_time
  341. }
  342. fn_print_info_eol_nl() {
  343. echo -e " ... ${cyan}INFO${default}"
  344. fn_sleep_time
  345. }
  346. # QUERYING
  347. fn_print_querying_eol() {
  348. echo -en " ... ${cyan}QUERYING${default}"
  349. fn_sleep_time_1
  350. }
  351. fn_print_querying_eol_nl() {
  352. echo -e " ... ${cyan}QUERYING${default}"
  353. fn_sleep_time_1
  354. }
  355. # CHECKING
  356. fn_print_checking_eol() {
  357. echo -en " ... ${cyan}CHECKING${default}"
  358. fn_sleep_time_1
  359. }
  360. fn_print_checking_eol_nl() {
  361. echo -e " ... ${cyan}CHECKING${default}"
  362. fn_sleep_time_1
  363. }
  364. # DELAY
  365. fn_print_delay_eol() {
  366. echo -en " ... ${green}DELAY${default}"
  367. fn_sleep_time_1
  368. }
  369. fn_print_delay_eol_nl() {
  370. echo -e " ... ${green}DELAY${default}"
  371. fn_sleep_time_1
  372. }
  373. # CANCELED
  374. fn_print_canceled_eol() {
  375. echo -en " ... ${lightyellow}CANCELED${default}"
  376. fn_sleep_time_1
  377. }
  378. fn_print_canceled_eol_nl() {
  379. echo -e " ... ${lightyellow}CANCELED${default}"
  380. fn_sleep_time_1
  381. }
  382. # REMOVED
  383. fn_print_removed_eol() {
  384. echo -en " ... ${red}REMOVED${default}"
  385. fn_sleep_time_1
  386. }
  387. fn_print_removed_eol_nl() {
  388. echo -e " ... ${red}REMOVED${default}"
  389. fn_sleep_time_1
  390. }
  391. # UPDATE
  392. fn_print_update_eol() {
  393. echo -en " ... ${lightblue}UPDATE${default}"
  394. fn_sleep_time
  395. }
  396. fn_print_update_eol_nl() {
  397. echo -e " ... ${lightblue}UPDATE${default}"
  398. fn_sleep_time
  399. }
  400. # SKIP
  401. fn_print_skip_eol() {
  402. echo -en " ... ${cyan}SKIP${default}"
  403. fn_sleep_time
  404. }
  405. fn_print_skip_eol_nl() {
  406. echo -e " ... ${cyan}SKIP${default}"
  407. fn_sleep_time
  408. }
  409. fn_print_ascii_logo() {
  410. echo -e ""
  411. echo -e " mdMMMMbm"
  412. echo -e " mMMMMMMMMMMm"
  413. echo -e " mMMMMMMMMMMMMm"
  414. echo -e " mMMMMMMMMMMMMMMm"
  415. echo -e " hMMMV^VMMV^VMMMh"
  416. echo -e " MMMMM MM MMMMM"
  417. echo -e " hMMs vv sMMh"
  418. echo -e " hMMM: :MMMh"
  419. echo -e " .hMMMh hMMMh."
  420. echo -e " -dMMMh ${lightgrey}__${default} hMMMd-"
  421. echo -e " :mMMMs ${lightgrey}||${default} sMMMm:"
  422. echo -e " :MMMM+ ${lightgrey}||${default} ${red}_${default} +NMMN:"
  423. echo -e " .mMMM+ ${lightgrey}========${default} +MMMm."
  424. echo -e " yMMMy ${darkgrey}##############${default} yMMMy"
  425. echo -e " mMMM: ${darkgrey}##############${default} :MMMm"
  426. echo -e " mMM ${lightyellow}nn${default} ${lightyellow}nn${default} ${lightyellow}nn${default} ${lightyellow}nn${default} MMm"
  427. echo -e " o ${lightyellow}nNNNNNNNn${default} ${lightyellow}nNNNNNNNn${default} o"
  428. echo -e " ${lightyellow}nNNNNNNNNNn${default} ${lightyellow}nNNNNNNNNNn${default}"
  429. echo -e " ${lightyellow}nNNNNNNNNNNN${default} ${lightyellow}NNNNNNNNNNNn${default}"
  430. echo -e " ${lightyellow}+NNNNNNNNN:${default} ${lightyellow}:NNNNNNNNN+${default}"
  431. echo -e " ${lightyellow}nNNNNNNN${default} /\ ${lightyellow}NNNNNNNn${default}"
  432. echo -e " ${lightyellow}nnnnn${default} db ${lightyellow}nnnnn${default}"
  433. echo -e ""
  434. echo -e "${lightyellow}888${default} ${lightyellow}d8b${default} ${default}.d8888b. .d8888b. 888b d888"
  435. echo -e "${lightyellow}888 Y8P ${default}d88P Y88b d88P Y88b 8888b d8888"
  436. echo -e "${lightyellow}888${default} ${default}888${default} 888 Y88b. 88888b.d88888"
  437. 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"
  438. 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"
  439. 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"
  440. 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"
  441. 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"
  442. echo -e ""
  443. }
  444. fn_print_restart_warning() {
  445. fn_print_warn "${selfname} will be restarted"
  446. fn_script_log_warn "${selfname} will be restarted"
  447. totalseconds=3
  448. for seconds in {3..1}; do
  449. fn_print_warn "${selfname} will be restarted: ${totalseconds}"
  450. totalseconds=$((totalseconds - 1))
  451. fn_sleep_time_1
  452. if [ "${seconds}" == "0" ]; then
  453. break
  454. fi
  455. done
  456. fn_print_warn_nl "${selfname} will be restarted"
  457. }
  458. # Functions below are used to ensure that logs and UI correctly reflect the command it is actually running.
  459. # Useful when a command has to call upon another command causing the other command to overrite commandname variables
  460. # Used to remember the command that ran first.
  461. fn_firstcommand_set() {
  462. if [ -z "${firstcommandname}" ]; then
  463. firstcommandname="${commandname}"
  464. firstcommandaction="${commandaction}"
  465. fi
  466. }
  467. # Used to reset commandname variables to the command the script ran first.
  468. fn_firstcommand_reset() {
  469. commandname="${firstcommandname}"
  470. commandaction="${firstcommandaction}"
  471. }