bash_functions.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. wd=$(pwd)
  51. relative_wd=\$FWROOT${wd#$FWROOT}
  52. if [ -f $FWROOT/toolset/setup/linux/systools/${depend}.sh ]; then
  53. echo Installing system tool: $depend in $relative_wd
  54. . $FWROOT/toolset/setup/linux/systools/${depend}.sh
  55. elif [ -f $FWROOT/toolset/setup/linux/languages/${depend}.sh ]; then
  56. echo Installing language: $depend in $relative_wd
  57. . $FWROOT/toolset/setup/linux/languages/${depend}.sh
  58. elif [ -f $FWROOT/toolset/setup/linux/webservers/${depend}.sh ]; then
  59. echo Installing webserver: $depend in $relative_wd
  60. . $FWROOT/toolset/setup/linux/webservers/${depend}.sh
  61. elif [ -f $FWROOT/toolset/setup/linux/frameworks/${depend}.sh ]; then
  62. echo Installing framework: $depend in $relative_wd
  63. . $FWROOT/toolset/setup/linux/frameworks/${depend}.sh
  64. else
  65. echo WARN: No installer found for $depend
  66. continue
  67. fi
  68. # For a sourced script to pass, all internal commands must return
  69. # non-zero. If you want to intentionally cause a failed install
  70. # message, just return a non-zero status from the sourced script
  71. if [ $FW_dep_error -ne 0 ]; then
  72. echo ERROR: $depend may not be installed properly
  73. # Reset variable for the next dependencies
  74. FW_dep_error=0
  75. else
  76. echo $depend is installed!
  77. fi
  78. done
  79. # Politely clean up our trap and trace
  80. set +E
  81. trap - ERR
  82. return $FW_any_errors
  83. }
  84. # Echo's 0 if file or directory exists
  85. # To be used with or || blocks, avoids triggering our ERR
  86. # trap with a return 1 statement
  87. fw_exists() {
  88. if [ -f $1 ] || [ -d $1 ]; then
  89. echo 0
  90. else
  91. echo 1
  92. fi
  93. }