bash_functions.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. # Turn on bash tracing before sourcing installer files to
  64. # print commands before they run
  65. if [ -f $FWROOT/toolset/setup/linux/systools/${depend}.sh ]; then
  66. echo Installing system tool: $depend in $relative_wd
  67. set -x
  68. . $FWROOT/toolset/setup/linux/systools/${depend}.sh
  69. elif [ -f $FWROOT/toolset/setup/linux/languages/${depend}.sh ]; then
  70. echo Installing language: $depend in $relative_wd
  71. set -x
  72. . $FWROOT/toolset/setup/linux/languages/${depend}.sh
  73. elif [ -f $FWROOT/toolset/setup/linux/webservers/${depend}.sh ]; then
  74. echo Installing webserver: $depend in $relative_wd
  75. set -x
  76. . $FWROOT/toolset/setup/linux/webservers/${depend}.sh
  77. elif [ -f $FWROOT/toolset/setup/linux/frameworks/${depend}.sh ]; then
  78. echo Installing framework: $depend in $relative_wd
  79. set -x
  80. . $FWROOT/toolset/setup/linux/frameworks/${depend}.sh
  81. else
  82. echo WARN: No installer found for $depend
  83. continue
  84. fi
  85. set +x
  86. # For a sourced script to pass, all internal commands must return
  87. # non-zero. If you want to intentionally cause a failed install
  88. # message, just return a non-zero status from the sourced script
  89. if [ $FW_dep_error -ne 0 ]; then
  90. echo ERROR: $depend may not be installed properly
  91. # Reset variable for the next dependencies
  92. FW_dep_error=0
  93. else
  94. echo $depend is installed!
  95. fi
  96. done
  97. # Politely clean up our trap and trace
  98. set +E
  99. trap - ERR
  100. # Politely return to IROOT for later install.sh code
  101. cd $IROOT
  102. return $FW_any_errors
  103. }
  104. # Echo's 0 if file or directory exists
  105. # To be used with or || blocks, avoids triggering our ERR
  106. # trap with a return 1 statement
  107. fw_exists() {
  108. if [ -f $1 ] || [ -d $1 ]; then
  109. echo 0
  110. else
  111. echo 1
  112. fi
  113. }