| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
# live_ast: run asterisk from a newly-built copy with minimal changes. |
| 4 |
|
| 5 |
# Copyright (C) 2007 Tzafrir Cohen <[email protected]> |
| 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="${AST_LIVE_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 a unit file that could be installed on the system to use |
| 179 |
# this copy of Asterisk. |
| 180 |
gen_ast_live_service() { |
| 181 |
local service_file="$BASE_DIR/asterisk-live.service" |
| 182 |
local included_dir='lib' |
| 183 |
local included_dir_remmed='etc' |
| 184 |
if [ -f /etc/systemd/system/asterisk.service ]; then |
| 185 |
included_dir='etc' |
| 186 |
included_dir_remmed='lib' |
| 187 |
fi |
| 188 |
local varlibdir=`awk '/astvarlibdir/ {print $3}' "$AST_CONF" | head -n1` |
| 189 |
cat <<EOF >"$service_file" |
| 190 |
# This file is based on your existing asterisk systemd service unit, and |
| 191 |
# overrides some fields. If you have an overriding unit in /etc, you |
| 192 |
# need to include it, rather than the one in /lib: |
| 193 |
.include /$included_dir/systemd/system/asterisk.service |
| 194 |
#.include /$included_dir_remmed/systemd/system/asterisk.service |
| 195 |
|
| 196 |
# This file is not installed. If you want to use it, you need to copy it |
| 197 |
# to /etc/systemd/system and run 'systemctl daemon-reload'. You'll then |
| 198 |
# need to be able to start this manually (note that it will not be |
| 199 |
# enabled and thus the standard bash completion will not complete it). |
| 200 |
# If you have multiple live asterisk instances you want to run on the |
| 201 |
# same system, be sure to give each one a unique name. |
| 202 |
|
| 203 |
[Unit] |
| 204 |
Description = Asterisk PBX (Live Asterisk) |
| 205 |
|
| 206 |
# Note: if you changed the parameters passed in the command-line (ExecStart) |
| 207 |
# be sure to copy them over here as well. |
| 208 |
[Service] |
| 209 |
ExecStart = $BASE_DIR/asterisk g -f -p -U asterisk |
| 210 |
ExecReload = $BASE_DIR/asterisk -rx 'core reload' |
| 211 |
WorkingDirectory = $varlibdir |
| 212 |
EOF |
| 213 |
} |
| 214 |
|
| 215 |
# (re?)generate the ./live/asterisk wrapper script |
| 216 |
gen_live_asterisk() { |
| 217 |
cat <<EOF >"$BASE_DIR/asterisk" |
| 218 |
#!/bin/sh |
| 219 |
# a wrapper to run asterisk from the "live" copy: |
| 220 |
EOF |
| 221 |
if realpath $0 >/dev/null 2>&1; then |
| 222 |
cat <<EOF >>"$BASE_DIR/asterisk" |
| 223 |
export AST_LIVE_BASE_DIR="$PWD/live" |
| 224 |
real_me='`realpath $0`' |
| 225 |
exec "\$real_me" run "\$@" |
| 226 |
EOF |
| 227 |
else # No realpath. Fall back to change working directory: |
| 228 |
cat <<EOF >>"$BASE_DIR/asterisk" |
| 229 |
cd "$PWD" |
| 230 |
exec "$0" run "\$@" |
| 231 |
EOF |
| 232 |
fi |
| 233 |
} |
| 234 |
|
| 235 |
command="$1" |
| 236 |
if [ $# -gt 0 ]; then |
| 237 |
shift |
| 238 |
fi |
| 239 |
|
| 240 |
case "$command" in |
| 241 |
configure) |
| 242 |
./configure $LIVE_AST_CONFIGURE_PARAMS "$@" |
| 243 |
if [ "$LIVE_AST_FORCE_DEF_CONF" != '' ]; then |
| 244 |
rm -f menuselect.makeopts |
| 245 |
fi |
| 246 |
if [ "$LIVE_AST_BRISTUFFED_LIBPRI" != '' ]; then |
| 247 |
sed -i \ |
| 248 |
-e 's|^\(PRI_INCLUDE=\).*|\1-I/usr/include/bristuffed|' \ |
| 249 |
-e 's|^\(PRI_LIB=\).*|\1-lpri-bristuffed|' \ |
| 250 |
makeopts |
| 251 |
fi |
| 252 |
;; |
| 253 |
install) |
| 254 |
make install DESTDIR="$BASE_DIR" "$@" |
| 255 |
;; |
| 256 |
samples) |
| 257 |
make samples DESTDIR="$BASE_DIR" "$@" |
| 258 |
sed -r -i \ |
| 259 |
-e '/^\[directories\]\(!\)/s/\(!\).*//' \ |
| 260 |
-e "/^\[directories\]/a; rem-out any of the following to use Asterisk's defaults:" \ |
| 261 |
-e "/^ast(cache|etc|mod|varlib|data|agi|run|spool|log|db|key)dir\>/s| /| $BASE_DIR/|" \ |
| 262 |
"$AST_CONF" |
| 263 |
if [ "$LIVE_AST_FOR_SYSTEM" != '' ]; then |
| 264 |
sed -r -i \ |
| 265 |
-e "/^ast(etc|varlib|data|agi|run|spool|log|db|key)dir\>/s|^|;|" \ |
| 266 |
-e "/^;astrundir\>/aastrundir => /var/run/asterisk" \ |
| 267 |
"$AST_CONF" |
| 268 |
fi |
| 269 |
# disable some modules that bind on a port that is already in use by a |
| 270 |
# main Asterisk copy, and would crash asterisk in failing: |
| 271 |
rm -f "$AST_CONF_DIR/$DISABLED_MODS_FILE" |
| 272 |
for mod in $DISABLED_MODS; do |
| 273 |
echo "noload => $mod.so" >> "$AST_CONF_DIR/$DISABLED_MODS_FILE" |
| 274 |
done |
| 275 |
echo "#include $DISABLED_MODS_FILE" >> "$AST_CONF_DIR/modules.conf" |
| 276 |
|
| 277 |
cat <<EOF >"$GDB_INIT" |
| 278 |
set args -C "$AST_CONF" -c |
| 279 |
EOF |
| 280 |
gen_live_asterisk |
| 281 |
chmod +x "$BASE_DIR/asterisk" |
| 282 |
# Generate a sample config file for live_ast itself: |
| 283 |
gen_live_conf |
| •284 |
gen_ast_live_service |
| 285 |
;; |
| 286 |
conf-file) |
| 287 |
# Just regenerate live.conf from the sample if it does not already exist: |
| 288 |
gen_live_conf |
| 289 |
;; |
| 290 |
run) |
| 291 |
set_ld_env |
| 292 |
exec $AST_BIN -C $AST_CONF "$@" |
| 293 |
;; |
| 294 |
valgrind) |
| 295 |
set_ld_env |
| 296 |
exec valgrind $LIVE_AST_VALGRIND_ARGS $AST_BIN -C $AST_CONF "$@" |
| 297 |
;; |
| 298 |
rsync) |
| 299 |
remote_host="$1" |
| 300 |
remote_dir="$2" |
| 301 |
me=`basename $0` |
| 302 |
# FIXME: assumes varrundir is /var/run/asterisk |
| 303 |
rsync -ai "$0" \ |
| 304 |
--exclude '/live/asterisk' \ |
| 305 |
--exclude '/live/var/run/asterisk/*' --exclude '/live/var/log/asterisk/*' \ |
| 306 |
live "$remote_host:$remote_dir/" |
| 307 |
ssh $remote_host "cd '$remote_dir' && ./$me gen-live-asterisk" |
| 308 |
;; |
| 309 |
gen-live-asterisk) |
| 310 |
gen_live_asterisk |
| 311 |
;; |
| 312 |
gdb) |
| 313 |
set_ld_env |
| 314 |
exec gdb -x $GDB_INIT $AST_BIN |
| 315 |
;; |
| 316 |
*) |
| 317 |
echo "$0: Unknown command '$command'. Aborting" |
| 318 |
echo |
| 319 |
echo "$0: Usage: Equivalent of:" |
| 320 |
echo "$0 configure [params] ./configure [params]" |
| 321 |
echo "$0 install make install" |
| 322 |
echo "$0 samples make samples" |
| 323 |
echo "$0 run [params] asterisk [params]" |
| 324 |
echo "$0 gdb gdb asterisk" |
| 325 |
echo "$0 valgrind valgrind asterisk" |
| 326 |
echo "$0 rsync [user@]host dir copy files over to [user@]host:dir" |
| 327 |
echo "$0 gen-live-asterisk regenerate the wrapper ./live/asterisk" |
| 328 |
echo "$0 conf-file create live.conf if it does not exist" |
| 329 |
exit 1 |
| 330 |
;; |
| 331 |
esac |