bash_functions.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/bin/bash
  2. export DEBIAN_FRONTEND=noninteractive
  3. # If you are running a large number of installs back to back
  4. # (e.g. for FwBm development), then setting this variable
  5. # will cause apt-get and wget to use your proxy server. If
  6. # your proxy server has a cache for static content, this can
  7. # save you quite a lot of download time
  8. # export http_proxy=http://10.0.1.0:3128
  9. fw_get () {
  10. # Start a background process to print a dot every
  11. # 30 seconds (avoids travis-ci 10min timeout)
  12. while :;do sleep 30; echo -n .;done &
  13. # -no-verbose disables the big progress bars but keeps
  14. # other basic info
  15. #wget --no-verbose --no-check-certificate \
  16. # --trust-server-names "$@"
  17. # DEPRECATED - older versions of wget use SSLv3 for handshaking
  18. # and therefore don't work (Ubuntu12, for instance).
  19. # Use curl instead (-s means silent; -L means follow 3xx responses)
  20. curl -sL "$@"
  21. # Ensure the background job is killed if we are
  22. kill $!; trap 'kill $!' SIGTERM
  23. }
  24. fw_untar() {
  25. echo "Running 'tar xf $@'...please wait"
  26. tar xf "$@"
  27. echo "Removing compressed tar file"
  28. # use -f to avoid printing errors if they gave additional arguments
  29. rm -f "$@"
  30. }
  31. fw_unzip() {
  32. echo "Running 'unzip $@'...please wait"
  33. unzip -o -q "$@"
  34. echo "Removing compressed zip file"
  35. # use -f to avoid printing errors if they gave additional arguments
  36. rm -f "$@"
  37. }
  38. # Download *.deb file and install into IROOT
  39. # Cautions:
  40. # Without using sudo,
  41. # Does not download dependant packages.
  42. # script will be stuck and will not make progress. (e.g: CSharp/nancy)
  43. # Example: fw_apt_to_iroot <package> [<directory>]
  44. fw_apt_to_iroot() {
  45. DIR=${2:-$1}
  46. echo "Downloading $1 to $IROOT"
  47. sudo apt-get download $1
  48. echo "Extracting $1 to $DIR"
  49. sudo dpkg-deb -x $1*.deb "$IROOT/$DIR" && sudo rm $1*.deb
  50. }
  51. # Was there an error for the current dependency?
  52. FW_dep_error=0
  53. # Have we seen any errors?
  54. FW_any_errors=0
  55. fw_traperror () {
  56. depend=$1 # Dependency being installed
  57. err=$2 # error status
  58. line=$3 # Current line
  59. command="$4" # Bash command
  60. IFS=':' read -a funcstack <<< "$5" # Stack (function names)
  61. IFS=':' read -a bashstack <<< "$6" # Stack (file names)
  62. IFS=':' read -a linestack <<< "$7" # Stack (line numbers)
  63. FW_dep_error=1
  64. FW_any_errors=1
  65. wd=$(pwd)
  66. relative_wd=\$FWROOT${wd#$FWROOT}
  67. echo "ERROR: $(echo ${bashstack[1]#$FWROOT}): Command '$command' exited with status $err (dependency=$depend) (cwd=$relative_wd)"
  68. #echo " Function stack : ${funcstack[@]}"
  69. #echo " Bash source stack : ${bashstack[@]}"
  70. #echo " Bash line stack : ${linestack[@]}"
  71. }
  72. # Requires dependencies to come in order e.g. Nimrod before
  73. # Jester, etc. Users should be know this
  74. # fairly well (e.g. you can't use Yaf without PHP)
  75. fw_depends() {
  76. # Turn on errtrace (-E), so that our ERR
  77. # trap is passed on to any subshells
  78. set -E
  79. for depend in "$@"
  80. do
  81. depend=$(echo $depend | awk '{print tolower($0)}')
  82. echo Searching for $depend
  83. trap 'fw_traperror $depend $? $LINENO "$BASH_COMMAND" $(printf ":%s" ${FUNCNAME[@]}) $(printf ":%s" ${BASH_SOURCE[@]}) $(printf ":%s" ${BASH_LINENO[@]})' ERR
  84. retcode=0
  85. # Ensure we are inside the installer root for this framework
  86. pushd $IROOT
  87. wd=$(pwd)
  88. relative_wd=\$FWROOT${wd#$FWROOT}
  89. # Check that the prerequisites have been loaded
  90. RETCODE=$(fw_exists ${IROOT}/prerequisites.installed)
  91. [ "$RETCODE" == 0 ] || { \
  92. # Load environment variables
  93. echo Installing prerequisites
  94. source $FWROOT/toolset/setup/linux/prerequisites.sh
  95. touch $IROOT/prerequisites.installed; }
  96. # Find and run the installer.sh file for this dependency
  97. # Turn on some bash options before sourcing:
  98. # - (x) errtrace : Print commands before they are run
  99. # Note: A shebang is just a comment when you source a script,
  100. # so if you need to modify the default options use
  101. # `set -e` instead of `#!/bin/bash -e`
  102. installation_file=$( find ${FWROOT}/toolset/setup/linux -name ${depend}.sh )
  103. if [[ -n $installation_file ]]; then
  104. echo Installing dependency: $depend from $installation_file
  105. set -x
  106. . $installation_file
  107. else
  108. echo WARN: No installer found for $depend, attempting to install with 'apt-get'...
  109. sudo apt-get install -qqy -o Dpkg::Options::="--force-confold" -o Dpkg::Options::="--force-confdef" ${depend}
  110. # Return whence you came.
  111. popd
  112. continue
  113. fi
  114. set +x
  115. # Return whence you came.
  116. popd
  117. # For a sourced script to pass, all internal commands must return
  118. # non-zero. If you want to intentionally cause a failed install
  119. # message, just return a non-zero status from the sourced script
  120. if [ $FW_dep_error -ne 0 ]; then
  121. echo ERROR: $depend may not be installed properly
  122. # Reset variable for the next dependencies
  123. FW_dep_error=0
  124. else
  125. echo $depend is installed!
  126. fi
  127. done
  128. # Politely clean up our trap and trace
  129. set +E
  130. trap - ERR
  131. return $FW_any_errors
  132. }
  133. # Echo's 0 if file or directory exists
  134. # To be used with or || blocks, avoids triggering our ERR
  135. # trap with a return 1 statement
  136. fw_exists() {
  137. if [ -f $1 ] || [ -d $1 ]; then
  138. echo 0
  139. else
  140. echo 1
  141. fi
  142. }
  143. # Checks to see if an installation exists and sources the install
  144. # file if it does
  145. fw_installed() {
  146. if [ -f "$IROOT/$1.installed" ]; then
  147. source "$IROOT/$1.installed"
  148. return 0
  149. else
  150. return 1
  151. fi
  152. }