bash_functions.sh 4.4 KB

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