core_messages.sh 15 KB

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