86 lines
3.7 KiB
Bash
Executable File
86 lines
3.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Native, unprivileged Asterisk installation for the approved rogee user.
|
|
set -euo pipefail
|
|
nonprod=0
|
|
if [[ ${1:-} == --nonprod ]]; then nonprod=1; shift; fi
|
|
[[ $# == 1 ]] || { echo 'usage: install-asterisk-user.sh [--nonprod] <verified-native-package-dir>' >&2; exit 2; }
|
|
[[ $(id -u) != 0 ]] || { echo 'do not run the user service installer as root' >&2; exit 1; }
|
|
package=$(cd "$1" && pwd)
|
|
. /etc/os-release
|
|
source "$(dirname "$0")/native-platform.sh"
|
|
[[ -f $package/build-platform ]] || { echo 'native build platform metadata is missing' >&2; exit 1; }
|
|
require_native_platform "$ID" "$VERSION_ID" "$(uname -m)" "$(<"$package/build-platform")" "$nonprod"
|
|
archive=asterisk-22.10.1-native-stage.tar
|
|
(cd "$package" && sha256sum -c "$archive.sha256")
|
|
if [[ $nonprod == 0 && $(loginctl show-user "$(id -un)" -p Linger --value) != yes ]]; then
|
|
echo 'enable user lingering before a production user-service installation' >&2
|
|
exit 1
|
|
fi
|
|
|
|
prefix="$HOME/.local/opt/go-sip-asterisk/22.10.1"
|
|
config="$HOME/.config/go-sip-asterisk"
|
|
state="$HOME/.local/state/go-sip-asterisk"
|
|
data="$HOME/.local/share/go-sip-asterisk"
|
|
cache="$HOME/.cache/go-sip-asterisk"
|
|
unit="$HOME/.config/systemd/user/go-sip-asterisk.service"
|
|
runtime="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/go-sip-asterisk"
|
|
[[ ! -e $prefix && ! -e $unit && ! -e $config/asterisk.conf && ! -e $config/modules.conf ]] || { echo 'existing Asterisk installation or user configuration; refuse to overwrite' >&2; exit 1; }
|
|
[[ $HOME != *[[:space:]]* ]] || { echo 'home path with whitespace is unsupported by the unit' >&2; exit 1; }
|
|
|
|
mkdir -p "$(dirname "$prefix")"
|
|
staged=$(mktemp -d "$(dirname "$prefix")/.staged.XXXXXXXX")
|
|
trap 'rm -rf "$staged"' EXIT
|
|
tar -xpf "$package/$archive" -C "$staged"
|
|
[[ -x $staged/usr/sbin/asterisk ]] || { echo 'native package has no Asterisk executable' >&2; exit 1; }
|
|
if LD_LIBRARY_PATH="$staged/usr/lib" ldd "$staged/usr/sbin/asterisk" | grep -q 'not found'; then
|
|
echo 'native Asterisk runtime libraries are missing' >&2; exit 1
|
|
fi
|
|
LD_LIBRARY_PATH="$staged/usr/lib" "$staged/usr/sbin/asterisk" -V
|
|
mv "$staged" "$prefix"
|
|
trap - EXIT
|
|
|
|
mkdir -p "$config" "$state/log" "$state/spool" "$data/db" "$data/keys" "$data/agi-bin" "$cache" "$(dirname "$unit")"
|
|
cat > "$config/asterisk.conf" <<EOF
|
|
[directories]
|
|
astcachedir => $cache
|
|
astetcdir => $config
|
|
astmoddir => $prefix/usr/lib/asterisk/modules
|
|
astvarlibdir => $data
|
|
astdbdir => $data/db
|
|
astkeydir => $data/keys
|
|
astdatadir => $prefix/var/lib/asterisk
|
|
astagidir => $data/agi-bin
|
|
astspooldir => $state/spool
|
|
astrundir => $runtime
|
|
astlogdir => $state/log
|
|
EOF
|
|
printf '[modules]\nautoload=yes\n' > "$config/modules.conf"
|
|
cat > "$unit" <<EOF
|
|
[Unit]
|
|
Description=Go SIP Cell native Asterisk (user service)
|
|
After=network-online.target
|
|
Wants=network-online.target
|
|
|
|
[Service]
|
|
Type=exec
|
|
Environment=LD_LIBRARY_PATH=$prefix/usr/lib
|
|
RuntimeDirectory=go-sip-asterisk
|
|
WorkingDirectory=$data
|
|
ExecStart=$prefix/usr/sbin/asterisk -f -C $config/asterisk.conf
|
|
ExecReload=$prefix/usr/sbin/asterisk -C $config/asterisk.conf -rx "module reload res_pjsip.so"
|
|
Restart=on-failure
|
|
RestartSec=3
|
|
|
|
[Install]
|
|
WantedBy=default.target
|
|
EOF
|
|
systemctl --user daemon-reload
|
|
systemctl --user enable --now go-sip-asterisk.service
|
|
systemctl --user is-enabled go-sip-asterisk.service
|
|
sleep 2
|
|
systemctl --user is-active --quiet go-sip-asterisk.service || { echo 'Asterisk exited during startup' >&2; exit 1; }
|
|
LD_LIBRARY_PATH="$prefix/usr/lib" "$prefix/usr/sbin/asterisk" -C "$config/asterisk.conf" -rx 'core show version' | grep -q 'Asterisk 22.10.1' || { echo 'Asterisk CLI did not report the expected live version' >&2; exit 1; }
|
|
if [[ $(loginctl show-user "$(id -un)" -p Linger --value) != yes ]]; then
|
|
echo 'nonproduction only: user lingering disabled; reboot persistence NOT verified' >&2
|
|
fi
|