core_messages.sh 15 KB

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