function.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #!/bin/bash
  2. # provides some general-purpose script functions
  3. #
  4. # Copyright (C) 2004 Eric Marchionni, Patrik Rayo
  5. # Zuercher Hochschule Winterthur
  6. #
  7. # This program is free software; you can redistribute it and/or modify it
  8. # under the terms of the GNU General Public License as published by the
  9. # Free Software Foundation; either version 2 of the License, or (at your
  10. # option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  11. #
  12. # This program is distributed in the hope that it will be useful, but
  13. # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14. # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  15. # for more details.
  16. export TERM=xterm
  17. RED=$(tput setaf 1)
  18. GREEN=$(tput setaf 2)
  19. YELLOW=$(tput setaf 3)
  20. NORMAL=$(tput op)
  21. # exit with given error message
  22. # $1 - error message
  23. die() {
  24. echo -e "${RED}$1${NORMAL}"
  25. exit 1
  26. }
  27. # execute command
  28. # $1 - command to execute
  29. # $2 - whether or not to log command exit status
  30. # (0 -> disable exit status logging)
  31. execute()
  32. {
  33. cmd=${1}
  34. echo $cmd >>$LOGFILE 2>&1
  35. $cmd >>$LOGFILE 2>&1
  36. status=$?
  37. [ "$2" != 0 ] && log_status $status
  38. if [ $status != 0 ]; then
  39. echo
  40. echo "! Command $cmd failed, exiting (status $status)"
  41. echo "! Check why here $LOGFILE"
  42. exit 1
  43. fi
  44. }
  45. # execute command in chroot
  46. # $1 - command to execute
  47. execute_chroot()
  48. {
  49. execute "chroot $LOOPDIR env PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin $@"
  50. }
  51. # write green status message to console
  52. # $1 - msg
  53. echo_ok()
  54. {
  55. echo -e "${GREEN}$1${NORMAL}"
  56. }
  57. # write red status message to console
  58. # $1 - msg
  59. echo_failed()
  60. {
  61. echo -e "${RED}$1${NORMAL}"
  62. }
  63. # write yellow status message to console
  64. # $1 - msg
  65. echo_warn()
  66. {
  67. echo -e "${YELLOW}$1${NORMAL}"
  68. }
  69. # log an action
  70. # $1 - current action description
  71. log_action()
  72. {
  73. /bin/echo -n "[....] $1 "
  74. }
  75. # log an action status
  76. # $1 - exit status of action
  77. log_status()
  78. {
  79. tput hpa 0
  80. if [ $1 -eq 0 ]; then
  81. /bin/echo -ne "[${GREEN} ok ${NORMAL}"
  82. else
  83. /bin/echo -ne "[${RED}FAIL${NORMAL}"
  84. fi
  85. echo
  86. }
  87. # the following two functions are stolen from [1]
  88. # [1] - http://www.linuxjournal.com/content/use-bash-trap-statement-cleanup-temporary-files
  89. declare -a on_exit_items
  90. # perform registered actions on exit
  91. on_exit()
  92. {
  93. for ((onex=${#on_exit_items[@]}-1; onex>=0; onex--))
  94. do
  95. echo "On_Exit: ${on_exit_items[$onex]}" >>$LOGFILE
  96. ${on_exit_items[$onex]} >>$LOGFILE 2>&1
  97. done
  98. on_exit_items=""
  99. trap - EXIT
  100. }
  101. # register a command to execute when the calling script terminates. The
  102. # registered commands are called in FILO order.
  103. # $* - command to register
  104. do_on_exit()
  105. {
  106. local n=${#on_exit_items[*]}
  107. on_exit_items[$n]="$*"
  108. if [ $n -eq 0 ]; then
  109. trap on_exit EXIT
  110. fi
  111. }
  112. # wait for a mount to disappear
  113. # $1 - device/image to wait for
  114. # $2 - maximum time to wait in seconds, default is 5 seconds
  115. graceful_umount()
  116. {
  117. secs=$2
  118. [ ! $secs ] && secs=5
  119. let steps=$secs*100
  120. for st in `seq 1 $steps`
  121. do
  122. umount $1 >>$LOGFILE 2>&1
  123. mount | grep $1 >/dev/null 2>&1
  124. [ $? -eq 0 ] || return 0
  125. sleep 0.01
  126. done
  127. return 1
  128. }
  129. # load qemu NBD kernel module, if not already loaded
  130. load_qemu_nbd()
  131. {
  132. lsmod | grep ^nbd[[:space:]]* >/dev/null 2>&1
  133. if [ $? != 0 ]
  134. then
  135. log_action "Loading NBD kernel module"
  136. execute "modprobe nbd max_part=16"
  137. fi
  138. }
  139. # check if given commands exist in $PATH
  140. # $* - commands to check
  141. check_commands()
  142. {
  143. for i in $*
  144. do
  145. command -v $i >/dev/null || { die "Required command $i not found"; exit 1; }
  146. done
  147. }
  148. # check if any of the given virtual guests are running
  149. # $* - names of guests to check
  150. running_any()
  151. {
  152. command -v virsh >/dev/null || return 1
  153. for host in $*
  154. do
  155. virsh list --name 2>/dev/null | grep "^$host$" >/dev/null && return 0
  156. done
  157. return 1
  158. }
  159. #############################################
  160. # search and replace strings throughout a
  161. # whole directory
  162. #
  163. function searchandreplace {
  164. SEARCHSTRING="$1"
  165. REPLACESTRING="$2"
  166. DESTDIR="$3"
  167. [ -d "$DESTDIR" ] || die "$DESTDIR is not a directory!"
  168. ###########################################
  169. # search and replace in each found file the
  170. # given string
  171. #
  172. for eachfoundfile in `find $DESTDIR -type f`
  173. do
  174. sed -i -e "s/$SEARCHSTRING/$REPLACESTRING/g" "$eachfoundfile"
  175. done
  176. }