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