bash_functions.sh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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
  38. # Cautions:
  39. # Without using sudo,
  40. # Does not download dependant packages.
  41. # script will be stuck and will not make progress. (e.g: CSharp/nancy)
  42. # Example: fw_apt_to_iroot <package> [<directory>]
  43. fw_apt_to_iroot() {
  44. DIR=${2:-$1}
  45. echo "Downloading $1 to $IROOT"
  46. sudo apt-get download $1
  47. echo "Extracting $1 to $DIR"
  48. sudo dpkg-deb -x $1*.deb "$IROOT/$DIR" && sudo rm $1*.deb
  49. }
  50. # Was there an error for the current dependency?
  51. FW_dep_error=0
  52. # Have we seen any errors?
  53. FW_any_errors=0
  54. fw_traperror () {
  55. depend=$1 # Dependency being installed
  56. err=$2 # error status
  57. line=$3 # Current line
  58. command="$4" # Bash command
  59. IFS=':' read -a funcstack <<< "$5" # Stack (function names)
  60. IFS=':' read -a bashstack <<< "$6" # Stack (file names)
  61. IFS=':' read -a linestack <<< "$7" # Stack (line numbers)
  62. FW_dep_error=1
  63. FW_any_errors=1
  64. wd=$(pwd)
  65. relative_wd=\$FWROOT${wd#$FWROOT}
  66. echo "ERROR: $(echo ${bashstack[1]#$FWROOT}): Command '$command' exited with status $err (dependency=$depend) (cwd=$relative_wd)"
  67. #echo " Function stack : ${funcstack[@]}"
  68. #echo " Bash source stack : ${bashstack[@]}"
  69. #echo " Bash line stack : ${linestack[@]}"
  70. }
  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. # Check that the prerequisites have been loaded
  89. RETCODE=$(fw_exists ${IROOT}/prerequisites.installed)
  90. [ "$RETCODE" == 0 ] || { \
  91. # Load environment variables
  92. echo Installing prerequisites
  93. source $FWROOT/toolset/setup/linux/prerequisites.sh
  94. touch $IROOT/prerequisites.installed; }
  95. # Find and run the installer.sh file for this dependency
  96. # Turn on some bash options before sourcing:
  97. # - (x) errtrace : Print commands before they are run
  98. # Note: A shebang is just a comment when you source a script,
  99. # so if you need to modify the default options use
  100. # `set -e` instead of `#!/bin/bash -e`
  101. if [ -f $FWROOT/toolset/setup/linux/systools/${depend}.sh ]; then
  102. echo Installing system tool: $depend in $relative_wd
  103. set -x
  104. . $FWROOT/toolset/setup/linux/systools/${depend}.sh
  105. elif [ -f $FWROOT/toolset/setup/linux/languages/${depend}.sh ]; then
  106. echo Installing language: $depend in $relative_wd
  107. set -x
  108. . $FWROOT/toolset/setup/linux/languages/${depend}.sh
  109. elif [ -f $FWROOT/toolset/setup/linux/webservers/${depend}.sh ]; then
  110. echo Installing webserver: $depend in $relative_wd
  111. set -x
  112. . $FWROOT/toolset/setup/linux/webservers/${depend}.sh
  113. elif [ -f $FWROOT/toolset/setup/linux/frameworks/${depend}.sh ]; then
  114. echo Installing framework: $depend in $relative_wd
  115. set -x
  116. . $FWROOT/toolset/setup/linux/frameworks/${depend}.sh
  117. elif [ -f $FWROOT/toolset/setup/linux/databases/${depend}.sh ]; then
  118. echo Installing database: $depend in $relative_wd
  119. set -x
  120. . $FWROOT/toolset/setup/linux/databases/${depend}.sh
  121. else
  122. echo WARN: No installer found for $depend, attempting to install with 'apt-get'...
  123. sudo apt-get install -o Dpkg::Options::="--force-confold --force-confdef" --force-yes ${depend}
  124. # Return whence you came.
  125. popd
  126. continue
  127. fi
  128. set +x
  129. # Return whence you came.
  130. popd
  131. # For a sourced script to pass, all internal commands must return
  132. # non-zero. If you want to intentionally cause a failed install
  133. # message, just return a non-zero status from the sourced script
  134. if [ $FW_dep_error -ne 0 ]; then
  135. echo ERROR: $depend may not be installed properly
  136. # Reset variable for the next dependencies
  137. FW_dep_error=0
  138. else
  139. echo $depend is installed!
  140. fi
  141. done
  142. # Politely clean up our trap and trace
  143. set +E
  144. trap - ERR
  145. return $FW_any_errors
  146. }
  147. # Echo's 0 if file or directory exists
  148. # To be used with or || blocks, avoids triggering our ERR
  149. # trap with a return 1 statement
  150. fw_exists() {
  151. if [ -f $1 ] || [ -d $1 ]; then
  152. echo 0
  153. else
  154. echo 1
  155. fi
  156. }