96 lines
5.1 KiB
Bash
96 lines
5.1 KiB
Bash
#!/usr/bin/env bash
|
|
set -Eeuo pipefail
|
|
commit=${1:?commit required}
|
|
archive=${2:?archive required}
|
|
expected=${3:?sha256 required}
|
|
[[ "$commit" =~ ^[0-9a-f]{40}$ ]]
|
|
test "$(sha256sum "$archive" | awk '{print $1}')" = "$expected"
|
|
stamp=$(date -u +%Y%m%dT%H%M%SZ)
|
|
old=$(readlink -f /opt/lisglosips/current)
|
|
new=/opt/lisglosips/releases/caller-analytics-$stamp
|
|
backup=/var/backups/lisglosips-caller-analytics/$stamp
|
|
test -d "$old"
|
|
test ! -e "$new"
|
|
# Refuse to interrupt calls, including unrelated test calls.
|
|
curl -fsS -H 'Content-Type: application/json' --data '{"jsonrpc":"2.0","id":1,"method":"dlg_list","params":[]}' http://127.0.0.1:8888/mi | python3 -c 'import sys,json; assert json.load(sys.stdin)["result"]["Dialogs"]==[], "Active calls: retry after calls end"'
|
|
systemctl start lisglosips-backup.service
|
|
install -d -m 0700 "$backup"
|
|
cp -a /etc/opensips/opensips.cfg "$backup/opensips.cfg"
|
|
printf '%s\n' "$old" > "$backup/previous-release"
|
|
for file in /etc/lisglosips/caller-analytics.env /etc/rsyslog.d/35-caller-analytics.conf; do
|
|
if test -f "$file"; then cp -a "$file" "$backup/$(basename "$file")"; fi
|
|
done
|
|
changed=0
|
|
was_running=$(systemctl is-active lisglosips@caller-analytics || true)
|
|
rollback() {
|
|
rc=$?
|
|
if test "$changed" = 1; then
|
|
systemctl stop lisglosips@caller-analytics || true
|
|
cp -a "$backup/opensips.cfg" /etc/opensips/opensips.cfg
|
|
for file in /etc/lisglosips/caller-analytics.env /etc/rsyslog.d/35-caller-analytics.conf; do
|
|
if test -f "$backup/$(basename "$file")"; then cp -a "$backup/$(basename "$file")" "$file"; else rm -f "$file"; fi
|
|
done
|
|
ln -sfn "$old" /opt/lisglosips/current
|
|
systemctl restart rsyslog || true
|
|
systemctl restart opensips lisglosips@api || true
|
|
if test "$was_running" = active; then systemctl start lisglosips@caller-analytics || true; fi
|
|
fi
|
|
echo "FAILED rc=$rc backup=$backup (additive analytics tables retained)"
|
|
exit "$rc"
|
|
}
|
|
trap rollback ERR
|
|
cp -a --reflink=auto "$old" "$new"
|
|
tar -xzf "$archive" -C "$new"
|
|
printf '%s\n' "$commit" > "$new/.deployed-commit"
|
|
printf '%s\n' "$old" > "$new/.delta-base-release"
|
|
source_cfg="$backup/opensips.cfg"
|
|
if grep -q 'CRA1|' "$source_cfg"; then
|
|
# A code-only redeploy keeps previously reviewed instrumentation unchanged.
|
|
cp "$source_cfg" "$new/opensips-candidate.cfg"
|
|
else
|
|
/usr/bin/node "$new/scripts/instrument-caller-analytics.mjs" "$source_cfg" "$new/opensips-candidate.cfg"
|
|
fi
|
|
python3 "$new/infra/server-b/caller-analytics/verify-hotpath.py" "$new/opensips-candidate.cfg"
|
|
systemctl start lisglosips-redis-hotpath-load.service
|
|
opensips -C -f "$new/opensips-candidate.cfg"
|
|
set -a
|
|
source /etc/lisglosips/secrets/mysql-migrate.env
|
|
set +a
|
|
export DATABASE_URL
|
|
DATABASE_URL=$(/usr/bin/node -e 'console.log(`mysql://${encodeURIComponent(process.env.MYSQL_USER)}:${encodeURIComponent(process.env.MYSQL_PASSWORD)}@${process.env.MYSQL_HOST}:${process.env.MYSQL_PORT}/${process.env.MYSQL_DATABASE}`)')
|
|
# Previous delta releases stripped executable bits from bundled native engines.
|
|
find "$new/node_modules/.pnpm" -path '*/@prisma/engines/schema-engine-debian-openssl-3.0.x' -type f -exec chmod 0750 {} \;
|
|
/usr/bin/node "$new/node_modules/prisma/build/index.js" migrate deploy --schema "$new/prisma/schema.prisma"
|
|
unset DATABASE_URL MYSQL_PASSWORD
|
|
install -d -o syslog -g lisglosips -m 0750 /var/log/lisglosips
|
|
touch /var/log/lisglosips/caller-analytics.log
|
|
chown syslog:lisglosips /var/log/lisglosips/caller-analytics.log
|
|
chmod 0640 /var/log/lisglosips/caller-analytics.log
|
|
install -m 0644 "$new/infra/server-b/caller-analytics/rsyslog.conf" /etc/rsyslog.d/35-caller-analytics.conf
|
|
rsyslogd -N1
|
|
if ! test -f /etc/lisglosips/caller-analytics.env; then
|
|
grep -E '^(DATABASE_URL|REDIS_URL)=' /etc/lisglosips/cdr-worker.env > /etc/lisglosips/caller-analytics.env
|
|
printf '%s\n' 'LISGLOSIPS_ENTRYPOINT=apps/worker-cdr/dist/analytics-main.js' 'LISGLOSIPS_SERVICE_NAME=caller-analytics' "ANALYTICS_CAPTURE_SINCE=$(date -u +%Y-%m-%dT%H:%M:%SZ)" >> /etc/lisglosips/caller-analytics.env
|
|
chown root:lisglosips /etc/lisglosips/caller-analytics.env
|
|
chmod 0640 /etc/lisglosips/caller-analytics.env
|
|
fi
|
|
chown -R root:lisglosips "$new/apps/api/dist" "$new/apps/worker-cdr/dist" "$new/packages/database/dist" "$new/packages/auth/dist" "$new/public" "$new/scripts" "$new/infra/server-b/caller-analytics"
|
|
chmod -R u=rwX,g=rX,o= "$new/apps/api/dist" "$new/apps/worker-cdr/dist" "$new/packages/database/dist" "$new/packages/auth/dist" "$new/public" "$new/scripts" "$new/infra/server-b/caller-analytics"
|
|
changed=1
|
|
systemctl restart rsyslog
|
|
install -o root -g root -m 0644 "$new/opensips-candidate.cfg" /etc/opensips/opensips.cfg
|
|
ln -sfn "$new" /opt/lisglosips/current
|
|
systemctl restart opensips lisglosips@api
|
|
systemctl enable --now lisglosips@caller-analytics
|
|
systemctl restart lisglosips@caller-analytics
|
|
for i in $(seq 1 15); do
|
|
if curl -fsS http://127.0.0.1:3000/api/v2/health/ready >/dev/null; then break; fi
|
|
sleep 1
|
|
done
|
|
curl -fsS http://127.0.0.1:3000/api/v2/health/ready
|
|
systemctl is-active opensips lisglosips@api lisglosips@cdr-worker lisglosips@caller-analytics
|
|
test "$(cat /opt/lisglosips/current/.deployed-commit)" = "$commit"
|
|
changed=0
|
|
trap - ERR
|
|
printf '\nRELEASE=%s\nBACKUP=%s\nCOMMIT=%s\n' "$new" "$backup" "$commit"
|