bash_functions.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. echo "Running 'tar xf $@'...please wait"
  13. tar xf "$@"
  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. wd=$(pwd)
  33. relative_wd=\$FWROOT${wd#$FWROOT}
  34. echo "ERROR: $(echo ${bashstack[1]#$FWROOT}): Command '$command' exited with status $err (dependency=$depend) (cwd=$relative_wd)"
  35. #echo " Function stack : ${funcstack[@]}"
  36. #echo " Bash source stack : ${bashstack[@]}"
  37. #echo " Bash line stack : ${linestack[@]}"
  38. }
  39. # Requires dependencies to come in order e.g. Nimrod before
  40. # Jester, etc. Users should be know this
  41. # fairly well (e.g. you can't use Yaf without PHP)
  42. fw_depends() {
  43. # Turn on errtrace (-E), so that our ERR
  44. # trap is passed on to any subshells
  45. set -E
  46. for depend in "$@"
  47. do
  48. depend=$(echo $depend | awk '{print tolower($0)}')
  49. echo Searching for $depend
  50. trap 'fw_traperror $depend $? $LINENO "$BASH_COMMAND" $(printf ":%s" ${FUNCNAME[@]}) $(printf ":%s" ${BASH_SOURCE[@]}) $(printf ":%s" ${BASH_LINENO[@]})' ERR
  51. retcode=0
  52. # Ensure we are inside the installer root for this framework
  53. cd $IROOT
  54. wd=$(pwd)
  55. relative_wd=\$FWROOT${wd#$FWROOT}
  56. if [ -f $FWROOT/toolset/setup/linux/systools/${depend}.sh ]; then
  57. echo Installing system tool: $depend in $relative_wd
  58. . $FWROOT/toolset/setup/linux/systools/${depend}.sh
  59. elif [ -f $FWROOT/toolset/setup/linux/languages/${depend}.sh ]; then
  60. echo Installing language: $depend in $relative_wd
  61. . $FWROOT/toolset/setup/linux/languages/${depend}.sh
  62. elif [ -f $FWROOT/toolset/setup/linux/webservers/${depend}.sh ]; then
  63. echo Installing webserver: $depend in $relative_wd
  64. . $FWROOT/toolset/setup/linux/webservers/${depend}.sh
  65. elif [ -f $FWROOT/toolset/setup/linux/frameworks/${depend}.sh ]; then
  66. echo Installing framework: $depend in $relative_wd
  67. . $FWROOT/toolset/setup/linux/frameworks/${depend}.sh
  68. else
  69. echo WARN: No installer found for $depend
  70. continue
  71. fi
  72. # For a sourced script to pass, all internal commands must return
  73. # non-zero. If you want to intentionally cause a failed install
  74. # message, just return a non-zero status from the sourced script
  75. if [ $FW_dep_error -ne 0 ]; then
  76. echo ERROR: $depend may not be installed properly
  77. # Reset variable for the next dependencies
  78. FW_dep_error=0
  79. else
  80. echo $depend is installed!
  81. fi
  82. done
  83. # Politely clean up our trap and trace
  84. set +E
  85. trap - ERR
  86. # Politely return to IROOT for later install.sh code
  87. cd $IROOT
  88. return $FW_any_errors
  89. }
  90. # Echo's 0 if file or directory exists
  91. # To be used with or || blocks, avoids triggering our ERR
  92. # trap with a return 1 statement
  93. fw_exists() {
  94. if [ -f $1 ] || [ -d $1 ]; then
  95. echo 0
  96. else
  97. echo 1
  98. fi
  99. }