bash_functions.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/bin/bash
  2. # If you are running a large number of installs back to back
  3. # (e.g. for FwBm development), then setting this variable
  4. # will cause apt-get and wget to use your proxy server. If
  5. # your proxy server has a cache for static content, this can
  6. # save you quite a lot of download time
  7. # export http_proxy=http://10.0.1.0:3128
  8. fw_get () {
  9. wget --no-check-certificate --trust-server-names "$@"
  10. }
  11. fw_untar() {
  12. tar xvf "$@"
  13. }
  14. fw_unzip() {
  15. unzip "$@"
  16. }
  17. # Was there an error for the current dependency?
  18. FW_dep_error=0
  19. # Have we seen any errors?
  20. FW_any_errors=0
  21. fw_traperror () {
  22. depend=$1 # Dependency being installed
  23. err=$2 # error status
  24. line=$3 # Current line
  25. command="$4" # Bash command
  26. IFS=':' read -a funcstack <<< "$5" # Stack (function names)
  27. IFS=':' read -a bashstack <<< "$6" # Stack (file names)
  28. IFS=':' read -a linestack <<< "$7" # Stack (line numbers)
  29. FW_dep_error=1
  30. FW_any_errors=1
  31. echo "ERROR: $(echo ${bashstack[1]#$FWROOT}): Command '$command' exited with status $err (dependency=$depend)"
  32. #echo " Function stack : ${funcstack[@]}"
  33. #echo " Bash source stack : ${bashstack[@]}"
  34. #echo " Bash line stack : ${linestack[@]}"
  35. }
  36. # Requires dependencies to come in order e.g. Nimrod before
  37. # Jester, etc. Users should be know this
  38. # fairly well (e.g. you can't use Yaf without PHP)
  39. fw_depends() {
  40. # Turn on errtrace (-E), so that our ERR
  41. # trap is passed on to any subshells
  42. set -E
  43. for depend in "$@"
  44. do
  45. depend=$(echo $depend | awk '{print tolower($0)}')
  46. echo Searching for $depend
  47. trap 'fw_traperror $depend $? $LINENO "$BASH_COMMAND" $(printf ":%s" ${FUNCNAME[@]}) $(printf ":%s" ${BASH_SOURCE[@]}) $(printf ":%s" ${BASH_LINENO[@]})' ERR
  48. retcode=0
  49. if [ -f ../toolset/setup/linux/systools/${depend}.sh ]; then
  50. echo Installing system tool: $depend
  51. . ../toolset/setup/linux/systools/${depend}.sh
  52. elif [ -f ../toolset/setup/linux/languages/${depend}.sh ]; then
  53. echo Installing language: $depend
  54. . ../toolset/setup/linux/languages/${depend}.sh
  55. elif [ -f ../toolset/setup/linux/webservers/${depend}.sh ]; then
  56. echo Installing webserver: $depend
  57. . ../toolset/setup/linux/webservers/${depend}.sh
  58. elif [ -f ../toolset/setup/linux/frameworks/${depend}.sh ]; then
  59. echo Installing framework: $depend
  60. . ../toolset/setup/linux/frameworks/${depend}.sh
  61. else
  62. echo WARN: No installer found for $depend
  63. continue
  64. fi
  65. # For a sourced script to pass, all internal commands must return
  66. # non-zero. If you want to intentionally cause a failed install
  67. # message, just return a non-zero status from the sourced script
  68. if [ $FW_dep_error -ne 0 ]; then
  69. echo ERROR: $depend may not be installed properly
  70. # Reset variable for the next dependencies
  71. FW_dep_error=0
  72. else
  73. echo $depend is installed!
  74. fi
  75. done
  76. # Politely clean up our trap and trace
  77. set +E
  78. trap - ERR
  79. return $FW_any_errors
  80. }
  81. # Echo's 0 if file or directory exists
  82. # To be used with or || blocks, avoids triggering our ERR
  83. # trap with a return 1 statement
  84. fw_exists() {
  85. if [ -f $1 ] || [ -d $1 ]; then
  86. echo 0
  87. else
  88. echo 1
  89. fi
  90. }