bash_functions.sh 4.3 KB

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