bash_functions.sh 4.6 KB

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