bash_functions.sh 5.4 KB

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