bash_functions.sh 3.2 KB

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