core_messages.sh 15 KB

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