bash_functions.sh 3.9 KB

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