bash_functions.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. cd $IROOT
  84. wd=$(pwd)
  85. relative_wd=\$FWROOT${wd#$FWROOT}
  86. # Find and run the installer.sh file for this dependency
  87. # Turn on some bash options before sourcing:
  88. # - (x) errtrace : Print commands before they are run
  89. # Note: A shebang is just a comment when you source a script,
  90. # so if you need to modify the default options use
  91. # `set -e` instead of `#!/bin/bash -e`
  92. if [ -f $FWROOT/toolset/setup/linux/systools/${depend}.sh ]; then
  93. echo Installing system tool: $depend in $relative_wd
  94. set -x
  95. . $FWROOT/toolset/setup/linux/systools/${depend}.sh
  96. elif [ -f $FWROOT/toolset/setup/linux/languages/${depend}.sh ]; then
  97. echo Installing language: $depend in $relative_wd
  98. set -x
  99. . $FWROOT/toolset/setup/linux/languages/${depend}.sh
  100. elif [ -f $FWROOT/toolset/setup/linux/webservers/${depend}.sh ]; then
  101. echo Installing webserver: $depend in $relative_wd
  102. set -x
  103. . $FWROOT/toolset/setup/linux/webservers/${depend}.sh
  104. elif [ -f $FWROOT/toolset/setup/linux/frameworks/${depend}.sh ]; then
  105. echo Installing framework: $depend in $relative_wd
  106. set -x
  107. . $FWROOT/toolset/setup/linux/frameworks/${depend}.sh
  108. else
  109. echo WARN: No installer found for $depend
  110. continue
  111. fi
  112. set +x
  113. # For a sourced script to pass, all internal commands must return
  114. # non-zero. If you want to intentionally cause a failed install
  115. # message, just return a non-zero status from the sourced script
  116. if [ $FW_dep_error -ne 0 ]; then
  117. echo ERROR: $depend may not be installed properly
  118. # Reset variable for the next dependencies
  119. FW_dep_error=0
  120. else
  121. echo $depend is installed!
  122. fi
  123. done
  124. # Politely clean up our trap and trace
  125. set +E
  126. trap - ERR
  127. # Politely return to IROOT for later install.sh code
  128. cd $IROOT
  129. return $FW_any_errors
  130. }
  131. # Echo's 0 if file or directory exists
  132. # To be used with or || blocks, avoids triggering our ERR
  133. # trap with a return 1 statement
  134. fw_exists() {
  135. if [ -f $1 ] || [ -d $1 ]; then
  136. echo 0
  137. else
  138. echo 1
  139. fi
  140. }