bash_functions.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. # Makes it available in subshells
  20. export -f fw_get
  21. fw_untar() {
  22. echo "Running 'tar xf $@'...please wait"
  23. tar xf "$@"
  24. echo "Removing compressed tar file"
  25. # use -f to avoid printing errors if they gave additional arguments
  26. rm -f "$@"
  27. }
  28. export -f fw_untar
  29. fw_unzip() {
  30. echo "Running 'unzip $@'...please wait"
  31. unzip -o -q "$@"
  32. echo "Removing compressed zip file"
  33. # use -f to avoid printing errors if they gave additional arguments
  34. rm -f "$@"
  35. }
  36. export -f fw_unzip
  37. # Download *.deb file and install into IROOT without using sudo
  38. # Does not download dependant packages
  39. #
  40. # Example: fw_apt_to_iroot <package> [<directory>]
  41. fw_apt_to_iroot() {
  42. DIR=${2:-$1}
  43. echo "Downloading $1 to $IROOT"
  44. apt-get download $1
  45. echo "Extracting $1 to $DIR"
  46. dpkg-deb -x $1*.deb "$IROOT/$DIR" && rm $1*.deb
  47. }
  48. export -f
  49. # Was there an error for the current dependency?
  50. FW_dep_error=0
  51. # Have we seen any errors?
  52. FW_any_errors=0
  53. fw_traperror () {
  54. depend=$1 # Dependency being installed
  55. err=$2 # error status
  56. line=$3 # Current line
  57. command="$4" # Bash command
  58. IFS=':' read -a funcstack <<< "$5" # Stack (function names)
  59. IFS=':' read -a bashstack <<< "$6" # Stack (file names)
  60. IFS=':' read -a linestack <<< "$7" # Stack (line numbers)
  61. FW_dep_error=1
  62. FW_any_errors=1
  63. wd=$(pwd)
  64. relative_wd=\$FWROOT${wd#$FWROOT}
  65. echo "ERROR: $(echo ${bashstack[1]#$FWROOT}): Command '$command' exited with status $err (dependency=$depend) (cwd=$relative_wd)"
  66. #echo " Function stack : ${funcstack[@]}"
  67. #echo " Bash source stack : ${bashstack[@]}"
  68. #echo " Bash line stack : ${linestack[@]}"
  69. }
  70. export -f fw_traperror
  71. # Requires dependencies to come in order e.g. Nimrod before
  72. # Jester, etc. Users should be know this
  73. # fairly well (e.g. you can't use Yaf without PHP)
  74. fw_depends() {
  75. # Turn on errtrace (-E), so that our ERR
  76. # trap is passed on to any subshells
  77. set -E
  78. for depend in "$@"
  79. do
  80. depend=$(echo $depend | awk '{print tolower($0)}')
  81. echo Searching for $depend
  82. trap 'fw_traperror $depend $? $LINENO "$BASH_COMMAND" $(printf ":%s" ${FUNCNAME[@]}) $(printf ":%s" ${BASH_SOURCE[@]}) $(printf ":%s" ${BASH_LINENO[@]})' ERR
  83. retcode=0
  84. # Ensure we are inside the installer root for this framework
  85. pushd $IROOT
  86. wd=$(pwd)
  87. relative_wd=\$FWROOT${wd#$FWROOT}
  88. # Find and run the installer.sh file for this dependency
  89. # Turn on some bash options before sourcing:
  90. # - (x) errtrace : Print commands before they are run
  91. # Note: A shebang is just a comment when you source a script,
  92. # so if you need to modify the default options use
  93. # `set -e` instead of `#!/bin/bash -e`
  94. if [ -f $FWROOT/toolset/setup/linux/systools/${depend}.sh ]; then
  95. echo Installing system tool: $depend in $relative_wd
  96. set -x
  97. . $FWROOT/toolset/setup/linux/systools/${depend}.sh
  98. elif [ -f $FWROOT/toolset/setup/linux/languages/${depend}.sh ]; then
  99. echo Installing language: $depend in $relative_wd
  100. set -x
  101. . $FWROOT/toolset/setup/linux/languages/${depend}.sh
  102. elif [ -f $FWROOT/toolset/setup/linux/webservers/${depend}.sh ]; then
  103. echo Installing webserver: $depend in $relative_wd
  104. set -x
  105. . $FWROOT/toolset/setup/linux/webservers/${depend}.sh
  106. elif [ -f $FWROOT/toolset/setup/linux/frameworks/${depend}.sh ]; then
  107. echo Installing framework: $depend in $relative_wd
  108. set -x
  109. . $FWROOT/toolset/setup/linux/frameworks/${depend}.sh
  110. else
  111. echo WARN: No installer found for $depend
  112. # Return whence you came.
  113. popd
  114. continue
  115. fi
  116. set +x
  117. # Return whence you came.
  118. popd
  119. # For a sourced script to pass, all internal commands must return
  120. # non-zero. If you want to intentionally cause a failed install
  121. # message, just return a non-zero status from the sourced script
  122. if [ $FW_dep_error -ne 0 ]; then
  123. echo ERROR: $depend may not be installed properly
  124. # Reset variable for the next dependencies
  125. FW_dep_error=0
  126. else
  127. echo $depend is installed!
  128. fi
  129. done
  130. # Politely clean up our trap and trace
  131. set +E
  132. trap - ERR
  133. return $FW_any_errors
  134. }
  135. export -f fw_depends
  136. # Echo's 0 if file or directory exists
  137. # To be used with or || blocks, avoids triggering our ERR
  138. # trap with a return 1 statement
  139. fw_exists() {
  140. if [ -f $1 ] || [ -d $1 ]; then
  141. echo 0
  142. else
  143. echo 1
  144. fi
  145. }
  146. export -f fw_exists