bash_functions.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. # Ensure the background job is killed if we are
  17. kill $!; trap 'kill $!' SIGTERM
  18. }
  19. fw_untar() {
  20. echo "Running 'tar xf $@'...please wait"
  21. tar xf "$@"
  22. echo "Removing compressed tar file"
  23. # use -f to avoid printing errors if they gave additional arguments
  24. rm -f "$@"
  25. }
  26. fw_unzip() {
  27. echo "Running 'unzip $@'...please wait"
  28. unzip -o -q "$@"
  29. echo "Removing compressed zip file"
  30. # use -f to avoid printing errors if they gave additional arguments
  31. rm -f "$@"
  32. }
  33. # Download *.deb file and install into IROOT without using sudo
  34. # Does not download dependant packages
  35. #
  36. # Example: fw_apt_to_iroot <package> [<directory>]
  37. fw_apt_to_iroot() {
  38. DIR=${2:-$1}
  39. echo "Downloading $1 to $IROOT"
  40. apt-get download $1
  41. echo "Extracting $1 to $DIR"
  42. dpkg-deb -x $1*.deb "$IROOT/$DIR" && rm $1*.deb
  43. }
  44. # Was there an error for the current dependency?
  45. FW_dep_error=0
  46. # Have we seen any errors?
  47. FW_any_errors=0
  48. fw_traperror () {
  49. depend=$1 # Dependency being installed
  50. err=$2 # error status
  51. line=$3 # Current line
  52. command="$4" # Bash command
  53. IFS=':' read -a funcstack <<< "$5" # Stack (function names)
  54. IFS=':' read -a bashstack <<< "$6" # Stack (file names)
  55. IFS=':' read -a linestack <<< "$7" # Stack (line numbers)
  56. FW_dep_error=1
  57. FW_any_errors=1
  58. wd=$(pwd)
  59. relative_wd=\$FWROOT${wd#$FWROOT}
  60. echo "ERROR: $(echo ${bashstack[1]#$FWROOT}): Command '$command' exited with status $err (dependency=$depend) (cwd=$relative_wd)"
  61. #echo " Function stack : ${funcstack[@]}"
  62. #echo " Bash source stack : ${bashstack[@]}"
  63. #echo " Bash line stack : ${linestack[@]}"
  64. }
  65. # Requires dependencies to come in order e.g. Nimrod before
  66. # Jester, etc. Users should be know this
  67. # fairly well (e.g. you can't use Yaf without PHP)
  68. fw_depends() {
  69. # Turn on errtrace (-E), so that our ERR
  70. # trap is passed on to any subshells
  71. set -E
  72. for depend in "$@"
  73. do
  74. depend=$(echo $depend | awk '{print tolower($0)}')
  75. echo Searching for $depend
  76. trap 'fw_traperror $depend $? $LINENO "$BASH_COMMAND" $(printf ":%s" ${FUNCNAME[@]}) $(printf ":%s" ${BASH_SOURCE[@]}) $(printf ":%s" ${BASH_LINENO[@]})' ERR
  77. retcode=0
  78. # Ensure we are inside the installer root for this framework
  79. pushd $IROOT
  80. wd=$(pwd)
  81. relative_wd=\$FWROOT${wd#$FWROOT}
  82. # Find and run the installer.sh file for this dependency
  83. # Turn on some bash options before sourcing:
  84. # - (x) errtrace : Print commands before they are run
  85. # Note: A shebang is just a comment when you source a script,
  86. # so if you need to modify the default options use
  87. # `set -e` instead of `#!/bin/bash -e`
  88. if [ -f $FWROOT/toolset/setup/linux/systools/${depend}.sh ]; then
  89. echo Installing system tool: $depend in $relative_wd
  90. set -x
  91. . $FWROOT/toolset/setup/linux/systools/${depend}.sh
  92. elif [ -f $FWROOT/toolset/setup/linux/languages/${depend}.sh ]; then
  93. echo Installing language: $depend in $relative_wd
  94. set -x
  95. . $FWROOT/toolset/setup/linux/languages/${depend}.sh
  96. elif [ -f $FWROOT/toolset/setup/linux/webservers/${depend}.sh ]; then
  97. echo Installing webserver: $depend in $relative_wd
  98. set -x
  99. . $FWROOT/toolset/setup/linux/webservers/${depend}.sh
  100. elif [ -f $FWROOT/toolset/setup/linux/frameworks/${depend}.sh ]; then
  101. echo Installing framework: $depend in $relative_wd
  102. set -x
  103. . $FWROOT/toolset/setup/linux/frameworks/${depend}.sh
  104. else
  105. echo WARN: No installer found for $depend
  106. # Return whence you came.
  107. popd
  108. continue
  109. fi
  110. set +x
  111. # Return whence you came.
  112. popd
  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. return $FW_any_errors
  128. }
  129. # Echo's 0 if file or directory exists
  130. # To be used with or || blocks, avoids triggering our ERR
  131. # trap with a return 1 statement
  132. fw_exists() {
  133. if [ -f $1 ] || [ -d $1 ]; then
  134. echo 0
  135. else
  136. echo 1
  137. fi
  138. }