live_ast 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #!/bin/sh
  2. # live_ast: run asterisk from a newly-built copy with minimal changes.
  3. # Copyright (C) 2007 Tzafrir Cohen <tzafrir.cohen@xorcom.com>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  18. # USA
  19. # This script allows you to install Asterisk into a subdirectory of
  20. # your source distribution and run it from there.
  21. #
  22. # Example usage:
  23. #
  24. # contrib/scripts/live_ast conf-file # optionally. and now edit live/live.conf
  25. # # edit live/live.conf
  26. # contrib/scripts/live_ast configure
  27. # make
  28. # contrib/scripts/live_ast install
  29. # contrib/scripts/live_ast samples
  30. # contrib/scripts/live_ast run
  31. # contrib/scripts/live_ast run -r
  32. # ./live/asterisk -r # Same as run -r
  33. #
  34. # A standard debugging cycle of a code in a module:
  35. #
  36. # # edit apps/app_skel.c
  37. # contrib/scripts/live_ast install
  38. # contrib/scripts/live_ast run -r
  39. # # and in the CLI:
  40. # module unload app_skel.so
  41. # module load app_skel.so
  42. #
  43. # If you have external scripts that run asterisk, use the script
  44. # live/asterisk that is generated by the 'samples' command. In this case you
  45. # should probably also rem-out the line 'astvarrundir' and maybe also
  46. # 'astetcdir' in live/etc/asterisk.conf .
  47. #
  48. ####################### Begin Samples
  49. # optional environment variables. Set those in live/live.conf or in
  50. # your envirnment.
  51. #
  52. # LIVE_AST_LIBPRI_PATH:
  53. # To use a libpri SVN directory (without running 'make install': make include
  54. # a symlink to the current directory:
  55. # ln -s . /path/to/checkout/of/libpri/include
  56. # Be sure to run there 'make'. Then set in live.conf:
  57. #LIVE_AST_LIBPRI_PATH="/path/to/checkout/of/libpri"
  58. #
  59. # LIVE_AST_ZAPTEL_PATH:
  60. # Likewise, the same trick can be used to build vs. a local copy of zaptel:
  61. # ln -s /path/to/checkout/of/zaptel/include .
  62. # ln -s /path/to/checkout/of/zaptel/zaptel .
  63. #LIVE_AST_ZAPTEL_PATH="/path/to/checkout/of/zaptel"
  64. #
  65. #LIVE_AST_DAHDI_PATH="/path/to/dahdi-linux/dir"
  66. #LIVE_AST_DAHDITOOLS_PATH="/path/to/dahdi-tools/dir"
  67. #
  68. # Another alternative for Zaptel is to use the live_zap script included
  69. # with the Zaptel distribution. The following should work after you used
  70. # './live_zap install' or even with a copy generated with
  71. # './live_zap rsync' . '/apth/to/zaptel' is the directory containing the
  72. # live/ subdirectory:
  73. #
  74. #LIVE_AST_ZAPLIVE_PATH="/path/to/zaptel"
  75. #
  76. # LIVE_AST_LD_PATH_EXTRA:
  77. # space-separated or colon-separated directories to add to
  78. # LD_LIBRARY_PATH. It is added before any existing components of
  79. # LD_LIBRARY_PATH.
  80. #LIVE_AST_LD_PATH_EXTRA="$HOME/lib:$HOME/libtest /opt/testapp"
  81. #
  82. # LIVE_AST_CONFIGURE_PARAMS:
  83. # Extra parameters to pass to ./configure.
  84. #LIVE_AST_CONFIGURE_PARAMS="--enable-dev-mode"
  85. #
  86. # LIVE_AST_FORCE_DEF_CONF:
  87. # If set to a non-empty value, always regenerate menuselect config.
  88. # This avoids emenselect's nag about "configuration has changed" that
  89. # happens occasionally after an svn update.
  90. #LIVE_AST_FORCE_DEF_CONF=yes
  91. #
  92. # LIVE_AST_BRISTUFFED_LIBPRI
  93. # A hack to use the second, "bristuffed" copy of libpri that exists
  94. # e.g. in the Debian libpri-dev package. If set to a non-empty value,
  95. # live_ast will edit makeopts to use that second copy after ./configure
  96. # is run.
  97. #LIVE_AST_BRISTUFFED_LIBPRI=yes
  98. #
  99. # LIVE_AST_VALGRIND_ARGS:
  100. # Parameters to pass to valgrind if using the 'valgrind' command.
  101. #LIVE_AST_VALGRIND_ARGS="-v --leak-check=full --suppressions=contrib/valgrind.supp --log-file=valgrind.log"
  102. #
  103. # LIVE_AST_FOR_SYSTEM
  104. # When generating asterisk.conf, use most components from the installed
  105. # system. Also provide a sane var-run directory for those of us who want
  106. # to do the right thing and run asterisk as non-root.
  107. #LIVE_AST_FOR_SYSTEM=yes
  108. ####################### End Samples
  109. BASE_DIR="${AST_LIVE_BASE_DIR:-$PWD/live}"
  110. AST_CONF_DIR="$BASE_DIR/etc/asterisk"
  111. AST_CONF="$AST_CONF_DIR/asterisk.conf"
  112. AST_BIN="$BASE_DIR/usr/sbin/asterisk"
  113. GDB_INIT="$BASE_DIR/gdbinit"
  114. LIVE_CONF="$BASE_DIR/live.conf"
  115. DISABLED_MODS="chan_h323 pbx_dundi"
  116. DISABLED_MODS_FILE="modules-disabled.conf"
  117. if [ -r "$LIVE_CONF" ]; then . "$LIVE_CONF"; fi
  118. if [ "$LIVE_AST_LIBPRI_PATH" != '' ]; then
  119. LIVE_AST_CONFIGURE_PARAMS="$LIVE_AST_CONFIGURE_PARAMS --with-pri=$LIVE_AST_LIBPRI_PATH"
  120. LIVE_AST_LD_PATH_EXTRA="$LIVE_AST_LD_PATH_EXTRA $LIVE_AST_LIBPRI_PATH"
  121. fi
  122. if [ "$LIVE_AST_ZAPTEL_PATH" != '' ]; then
  123. LIVE_AST_CONFIGURE_PARAMS="$LIVE_AST_CONFIGURE_PARAMS --with-tonezone=$LIVE_AST_ZAPTEL_PATH"
  124. LIVE_AST_CONFIGURE_PARAMS="$LIVE_AST_CONFIGURE_PARAMS --with-zaptel=$LIVE_AST_ZAPTEL_PATH"
  125. LIVE_AST_CONFIGURE_PARAMS="$LIVE_AST_CONFIGURE_PARAMS --with-zaptel_transcode=$LIVE_AST_ZAPTEL_PATH"
  126. LIVE_AST_CONFIGURE_PARAMS="$LIVE_AST_CONFIGURE_PARAMS --with-zaptel_vldtmf=$LIVE_AST_ZAPTEL_PATH"
  127. LIVE_AST_LD_PATH_EXTRA="$LIVE_AST_LD_PATH_EXTRA $LIVE_AST_ZAPTEL_PATH"
  128. fi
  129. if [ "$LIVE_AST_DAHDI_PATH" != '' ]; then
  130. LIVE_AST_CONFIGURE_PARAMS="$LIVE_AST_CONFIGURE_PARAMS --with-dahdi=$LIVE_AST_DAHDI_PATH"
  131. fi
  132. if [ "$LIVE_AST_DAHDITOOLS_PATH" != '' ]; then
  133. LIVE_AST_CONFIGURE_PARAMS="$LIVE_AST_CONFIGURE_PARAMS --with-tonezone=$LIVE_AST_DAHDITOOLS_PATH"
  134. LIVE_AST_LD_PATH_EXTRA="$LIVE_AST_LD_PATH_EXTRA $LIVE_AST_DAHDITOOLS_PATH"
  135. fi
  136. if [ "$LIVE_AST_ZAPLIVE_PATH" != '' ]; then
  137. ZAPLIVE_USR_DIR="$LIVE_AST_ZAPLIVE_PATH/live/usr"
  138. LIVE_AST_CONFIGURE_PARAMS="$LIVE_AST_CONFIGURE_PARAMS --with-tonezone=$ZAPLIVE_USR_DIR"
  139. LIVE_AST_CONFIGURE_PARAMS="$LIVE_AST_CONFIGURE_PARAMS --with-zaptel=$ZAPALIVE_USR_DIR"
  140. LIVE_AST_CONFIGURE_PARAMS="$LIVE_AST_CONFIGURE_PARAMS --with-zaptel_transcode=$ZAPALIVE_USR_DIR"
  141. LIVE_AST_CONFIGURE_PARAMS="$LIVE_AST_CONFIGURE_PARAMS --with-zaptel_vldtmf=$ZAPALIVE_USR_DIR"
  142. LIVE_AST_LD_PATH_EXTRA="$LIVE_AST_LD_PATH_EXTRA $ZAPLIVE_USR_DIR/lib"
  143. fi
  144. # gets rid of excessive spaces. Leves nothing if there were only white spaces:
  145. LIVE_AST_LD_PATH_EXTRA=`echo $LIVE_AST_LD_PATH_EXTRA | tr ' ' :`
  146. LIVE_AST_LD_PATH_EXTRA="$BASE_DIR/usr/lib:$LIVE_AST_LD_PATH_EXTRA"
  147. set_ld_env() {
  148. if [ "$LD_LIBRARY_PATH" = '' ]; then
  149. LD_LIBRARY_PATH="$LIVE_AST_LD_PATH_EXTRA"
  150. else
  151. LD_LIBRARY_PATH=`echo $LD_LIBRARY_PATH | tr ' ' :`
  152. LD_LIBRARY_PATH="$LIVE_AST_LD_PATH_EXTRA:$LD_LIBRARY_PATH"
  153. fi
  154. export LD_LIBRARY_PATH
  155. }
  156. # if live.conf does not exist, generate it from the sample
  157. gen_live_conf() {
  158. if [ -r $LIVE_CONF ]; then return; fi
  159. # TODO: `dirname $LIVE_CONF` in case someone redefines it?
  160. mkdir -p $BASE_DIR
  161. sed -n -e '/^#* Begin Samples/,/^#* End Samples/p' "$0" \
  162. | sed -e '/^#* \(Begin\|End\) Samples/d' >"$LIVE_CONF"
  163. }
  164. # (re?)generate the ./live/asterisk wrapper script
  165. gen_live_asterisk() {
  166. cat <<EOF >"$BASE_DIR/asterisk"
  167. #!/bin/sh
  168. # a wrapper to run asterisk from the "live" copy:
  169. EOF
  170. if realpath $0 >/dev/null 2>&1; then
  171. cat <<EOF >>"$BASE_DIR/asterisk"
  172. export AST_LIVE_BASE_DIR="$PWD/live"
  173. real_me='`realpath $0`'
  174. exec "\$real_me" run "\$@"
  175. EOF
  176. else # No realpath. Fall back to change working directory:
  177. cat <<EOF >>"$BASE_DIR/asterisk"
  178. cd "$PWD"
  179. exec "$0" run "\$@"
  180. EOF
  181. fi
  182. }
  183. command="$1"
  184. if [ $# -gt 0 ]; then
  185. shift
  186. fi
  187. case "$command" in
  188. configure)
  189. ./configure $LIVE_AST_CONFIGURE_PARAMS "$@"
  190. if [ "$LIVE_AST_FORCE_DEF_CONF" != '' ]; then
  191. rm -f menuselect.makeopts
  192. fi
  193. if [ "$LIVE_AST_BRISTUFFED_LIBPRI" != '' ]; then
  194. sed -i \
  195. -e 's|^\(PRI_INCLUDE=\).*|\1-I/usr/include/bristuffed|' \
  196. -e 's|^\(PRI_LIB=\).*|\1-lpri-bristuffed|' \
  197. makeopts
  198. fi
  199. ;;
  200. install)
  201. make install DESTDIR="$BASE_DIR" "$@"
  202. ;;
  203. samples)
  204. make samples DESTDIR="$BASE_DIR" "$@"
  205. sed -r -i \
  206. -e '/^\[directories\]\(!\)/s/\(!\).*//' \
  207. -e "/^\[directories\]/a; rem-out any of the following to use Asterisk's defaults:" \
  208. -e "/^ast(etc|mod|varlib|data|agi|run|spool|log|db|key)dir\>/s| /| $BASE_DIR/|" \
  209. "$AST_CONF"
  210. if [ "$LIVE_AST_FOR_SYSTEM" != '' ]; then
  211. sed -r -i \
  212. -e "/^ast(etc|varlib|data|agi|run|spool|log|db|key)dir\>/s|^|;|" \
  213. -e "/^;astrundir\>/aastrundir => /var/run/asterisk" \
  214. "$AST_CONF"
  215. fi
  216. # disable some modules that bind on a port that is already in use by a
  217. # main Asterisk copy, and would crash asterisk in failing:
  218. rm -f "$AST_CONF_DIR/$DISABLED_MODS_FILE"
  219. for mod in $DISABLED_MODS; do
  220. echo "noload => $mod.so" >> "$AST_CONF_DIR/$DISABLED_MODS_FILE"
  221. done
  222. echo "#include $DISABLED_MODS_FILE" >> "$AST_CONF_DIR/modules.conf"
  223. cat <<EOF >"$GDB_INIT"
  224. set args -C "$AST_CONF" -c
  225. EOF
  226. gen_live_asterisk
  227. chmod +x "$BASE_DIR/asterisk"
  228. # Generate a sample config file for live_ast itself:
  229. gen_live_conf
  230. ;;
  231. conf-file)
  232. # Just regenerate live.conf from the sample if it does not already exist:
  233. gen_live_conf
  234. ;;
  235. run)
  236. set_ld_env
  237. exec $AST_BIN -C $AST_CONF "$@"
  238. ;;
  239. valgrind)
  240. set_ld_env
  241. exec valgrind $LIVE_AST_VALGRIND_ARGS $AST_BIN -C $AST_CONF "$@"
  242. ;;
  243. rsync)
  244. remote_host="$1"
  245. remote_dir="$2"
  246. me=`basename $0`
  247. # FIXME: assumes varrundir is /var/run/asterisk
  248. rsync -ai "$0" \
  249. --exclude '/live/asterisk' \
  250. --exclude '/live/var/run/asterisk/*' --exclude '/live/var/log/asterisk/*' \
  251. live "$remote_host:$remote_dir/"
  252. ssh $remote_host "cd '$remote_dir' && ./$me gen-live-asterisk"
  253. ;;
  254. gen-live-asterisk)
  255. gen_live_asterisk
  256. ;;
  257. gdb)
  258. set_ld_env
  259. exec gdb -x $GDB_INIT $AST_BIN
  260. ;;
  261. *)
  262. echo "$0: Unknown command '$command'. Aborting"
  263. echo
  264. echo "$0: Usage: Equivalent of:"
  265. echo "$0 configure [params] ./configure [params]"
  266. echo "$0 install make install"
  267. echo "$0 samples make samples"
  268. echo "$0 run [params] asterisk [params]"
  269. echo "$0 gdb gdb asterisk"
  270. echo "$0 valgrind valgrind asterisk"
  271. echo "$0 rsync [user@]host dir copy files over to [user@]host:dir"
  272. echo "$0 gen-live-asterisk regenerate the wrapper ./live/asterisk"
  273. echo "$0 conf-file create live.conf if it does not exist"
  274. exit 1
  275. ;;
  276. esac