bash_functions.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. # Find and run the installer.sh file for this dependency
  90. # Turn on some bash options before sourcing:
  91. # - (x) errtrace : Print commands before they are run
  92. # Note: A shebang is just a comment when you source a script,
  93. # so if you need to modify the default options use
  94. # `set -e` instead of `#!/bin/bash -e`
  95. installation_file=$( find ${FWROOT}/toolset/setup/linux -name ${depend}.sh )
  96. if [[ -n $installation_file ]]; then
  97. echo Installing dependency: $depend from $installation_file
  98. set -x
  99. . $installation_file
  100. else
  101. echo WARN: No installer found for $depend, attempting to install with 'apt-get'...
  102. sudo apt-get install -qqy -o Dpkg::Options::="--force-confold" -o Dpkg::Options::="--force-confdef" ${depend}
  103. # Return whence you came.
  104. popd
  105. continue
  106. fi
  107. set +x
  108. # Return whence you came.
  109. popd
  110. # For a sourced script to pass, all internal commands must return
  111. # non-zero. If you want to intentionally cause a failed install
  112. # message, just return a non-zero status from the sourced script
  113. if [ $FW_dep_error -ne 0 ]; then
  114. echo ERROR: $depend may not be installed properly
  115. # Reset variable for the next dependencies
  116. FW_dep_error=0
  117. else
  118. echo $depend is installed!
  119. fi
  120. done
  121. # Politely clean up our trap and trace
  122. set +E
  123. trap - ERR
  124. return $FW_any_errors
  125. }
  126. # Echo's 0 if file or directory exists
  127. # To be used with or || blocks, avoids triggering our ERR
  128. # trap with a return 1 statement
  129. fw_exists() {
  130. if [ -f $1 ] || [ -d $1 ]; then
  131. echo 0
  132. else
  133. echo 1
  134. fi
  135. }
  136. # Checks to see if an installation exists and sources the install
  137. # file if it does
  138. fw_installed() {
  139. if [ -f "$IROOT/$1.installed" ]; then
  140. source "$IROOT/$1.installed"
  141. return 0
  142. else
  143. return 1
  144. fi
  145. }