Files
cti/installer/build.sh
2026-10-14 23:05:02 +03:30

262 lines
14 KiB
Bash
Executable File

#!/usr/bin/env bash
set -Eeuo pipefail
umask 022
PROJECT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
INSTALLER_DIR="$PROJECT_DIR/installer/online-installer"
APP_VERSION="$(awk '
/<artifactId>raymandcti<\/artifactId>/ { nextline = 1; next }
nextline && /<version>/ {
sub(/^.*<version>/, "")
sub(/<\/version>.*/, "")
gsub(/[[:space:]\r]/, "")
print
exit
}
' "$PROJECT_DIR/p-line/pom.xml")"
VERSION="${CTI_PACKAGE_VERSION:-$APP_VERSION}"
BUILD_APP=1
usage() {
cat <<'USAGE'
Usage: ./build-installer.sh [--version VERSION] [--package-only]
Builds the React production files and Java JAR, then packages Raymand CTI into
installer/online-installer/. Use --package-only to reuse the existing JAR and
React build without compiling Java.
USAGE
}
while (($#)); do
case "$1" in
--package-only) BUILD_APP=0 ;;
--version)
(($# >= 2)) || { usage >&2; exit 2; }
VERSION="$2"
shift
;;
-h|--help) usage; exit 0 ;;
*) usage >&2; printf 'Unknown option: %s\n' "$1" >&2; exit 2 ;;
esac
shift
done
[[ "$VERSION" =~ ^[0-9][A-Za-z0-9.+~_-]*$ ]] \
|| { printf 'Invalid Debian package version: %s\n' "$VERSION" >&2; exit 2; }
for tool in dpkg-deb sha256sum tar cmp node jar; do
command -v "$tool" >/dev/null 2>&1 || { printf '%s is required to build the installer\n' "$tool" >&2; exit 1; }
done
if ((BUILD_APP)); then
for tool in mvn java npm; do
command -v "$tool" >/dev/null 2>&1 || { printf '%s is required to build the installer\n' "$tool" >&2; exit 1; }
done
BUILD_JAVA_MAJOR="$(java -version 2>&1 | sed -nE '1s/.*version \"([0-9]+).*/\1/p')"
[[ "$BUILD_JAVA_MAJOR" =~ ^[0-9]+$ ]] && ((BUILD_JAVA_MAJOR >= 25)) \
|| { printf 'Java 25 or newer is required to build the Java application (found %s)\n' "${BUILD_JAVA_MAJOR:-unknown}" >&2; exit 1; }
fi
for required in p-line/pom.xml cti_app/package.json cti_app/package-lock.json \
p-line/src/main/resources/application.properties p-line/features.json \
p-line/SipParameters.json p-line/pjsip_parameters.json \
installer/install.sh installer/README.md installer/debian/raymnad-cti.service \
installer/debian/setup-https installer/debian/nginx-manager \
installer/debian/raymand-cti-nginx.sudoers installer/debian/raymand-cti-serial.sudoers \
installer/debian/ensure-nginx-proxy-include installer/debian/check-nginx-ports; do
[[ -f "$PROJECT_DIR/$required" ]] \
|| { printf 'Required project file not found: %s\n' "$required" >&2; exit 1; }
done
awk '/^APT_PACKAGES=\(/ { collecting = 1; next } collecting && /^\)/ { exit } collecting { print }' \
"$PROJECT_DIR/installer/install.sh" | grep -qE '(^|[[:space:]])sudo([[:space:]]|$)' \
|| { printf 'Installer runtime package list must install sudo before the CTI package.\n' >&2; exit 1; }
grep -qE '^Depends:.*(^|, )[[:space:]]*sudo([,[:space:]]|$)' \
"$PROJECT_DIR/installer/debian/control.in" \
|| { printf 'Debian package dependencies must declare sudo.\n' >&2; exit 1; }
bash -n "$PROJECT_DIR/installer/install.sh"
shopt -s nullglob
ASTERISK_IMAGE_ARCHIVES=("$PROJECT_DIR"/offline/images/asterisk-22-*.tar.gz)
((${#ASTERISK_IMAGE_ARCHIVES[@]} > 0)) \
|| { printf 'Asterisk image archive is missing. Run ./save-asterisk-image.sh first.\n' >&2; exit 1; }
HAS_AMD64_IMAGE=0
for image_archive in "${ASTERISK_IMAGE_ARCHIVES[@]}"; do
image_name="$(basename "$image_archive")"
[[ "$image_name" =~ ^asterisk-22-amd64\.tar\.gz$ ]] \
|| { printf 'Unsupported Asterisk archive name: %s\n' "$image_name" >&2; exit 1; }
[[ "$image_name" == *-amd64.tar.gz ]] && HAS_AMD64_IMAGE=1
[[ -s "$image_archive.sha256" ]] \
|| { printf 'Asterisk image checksum not found: %s.sha256\n' "$image_archive" >&2; exit 1; }
(cd "$(dirname "$image_archive")" && sha256sum -c "$image_name.sha256") \
|| { printf 'Asterisk image checksum validation failed: %s\n' "$image_name" >&2; exit 1; }
done
((HAS_AMD64_IMAGE)) \
|| { printf 'The amd64 Asterisk image archive is required for raymand-cti-amd64.deb.\n' >&2; exit 1; }
PACKAGE_ARCH=amd64
if ((BUILD_APP)); then
printf '[Raymand CTI-build] Building the application with Java 25 (tests skipped)\n'
(cd "$PROJECT_DIR/p-line" && mvn clean -DskipTests package)
if [[ ! -x "$PROJECT_DIR/cti_app/node_modules/.bin/vite" ]]; then
printf '[Raymand CTI-build] Installing locked React build dependencies\n'
(cd "$PROJECT_DIR/cti_app" && npm ci)
fi
printf '[Raymand CTI-build] Building React production assets\n'
(cd "$PROJECT_DIR/cti_app" && npm run build)
else
printf '[Raymand CTI-build] Reusing existing Java and React build artifacts\n'
fi
JAR_FILE="$PROJECT_DIR/p-line/target/raymandcti-${APP_VERSION}.jar"
[[ -s "$JAR_FILE" ]] || { printf 'Compiled application JAR not found: %s\n' "$JAR_FILE" >&2; exit 1; }
jar tf "$JAR_FILE" | grep -qx 'BOOT-INF/classes/ir/peeco/pline/PlineApplication.class' \
|| { printf 'Compiled Java archive is missing the Raymand CTI application entry point.\n' >&2; exit 1; }
if ((!BUILD_APP)) && { [[ "$PROJECT_DIR/p-line/src/main/java/ir/peeco/pline/configuration/jwtAuthorizationCheck.java" -nt "$JAR_FILE" ]] \
|| [[ "$PROJECT_DIR/p-line/src/main/java/ir/peeco/pline/plineServer/tools/GlobalTools.java" -nt "$JAR_FILE" ]] \
|| [[ "$PROJECT_DIR/p-line/src/main/java/ir/peeco/pline/plineServer/PlineTools.java" -nt "$JAR_FILE" ]] \
|| [[ "$PROJECT_DIR/p-line/src/main/java/ir/peeco/pline/init/Initializer.java" -nt "$JAR_FILE" ]]; }; then
printf 'The existing JAR predates required application fixes. Run the full installer build instead of --package-only.\n' >&2
exit 1
fi
[[ -s "$PROJECT_DIR/cti_app/build/index.html" ]] \
|| { printf 'React build output not found: %s\n' "$PROJECT_DIR/cti_app/build" >&2; exit 1; }
REACT_REQUIRED_ASSETS="$(node --input-type=module - "$PROJECT_DIR/cti_app/build" <<'NODE'
import fs from 'node:fs';
import path from 'node:path';
const root = process.argv[2];
const html = fs.readFileSync(path.join(root, 'index.html'), 'utf8');
const assets = [...html.matchAll(/(?:src|href)=["']([^"']+)["']/g)]
.map((match) => match[1])
.filter((value) => !/^(?:[a-z]+:|\/\/|#|data:)/i.test(value));
if (assets.length === 0 || assets.some((asset) => !fs.existsSync(path.join(root, asset.replace(/^\//, ''))))) {
throw new Error('React build index.html references missing production assets');
}
process.stdout.write(assets.map((asset) => asset.replace(/^\//, '')).join('\n'));
NODE
)"
mkdir -p "$INSTALLER_DIR/repo"
BUILD_DIR="$(mktemp -d "${TMPDIR:-/tmp}/raymand-cti-deb.XXXXXX")"
trap 'rm -rf -- "$BUILD_DIR"' EXIT
PACKAGE_ROOT="$BUILD_DIR/package"
mkdir -p "$PACKAGE_ROOT/DEBIAN" "$PACKAGE_ROOT/opt/cti" \
"$PACKAGE_ROOT/opt/cti/installer/images" \
"$PACKAGE_ROOT/usr/lib/raymand-cti" "$PACKAGE_ROOT/etc/sudoers.d" \
"$PACKAGE_ROOT/etc/nginx/raymand-cti" \
"$PACKAGE_ROOT/etc/systemd/system" "$PACKAGE_ROOT/etc/nginx/sites-available" \
"$PACKAGE_ROOT/var/www/cti"
install -m 0644 "$JAR_FILE" "$PACKAGE_ROOT/opt/cti/raymandcti.jar"
install -m 0644 "$PROJECT_DIR/p-line/src/main/resources/application.properties" \
"$PACKAGE_ROOT/opt/cti/application.properties"
install -m 0644 "$PROJECT_DIR/p-line/features.json" "$PROJECT_DIR/p-line/SipParameters.json" \
"$PROJECT_DIR/p-line/pjsip_parameters.json" "$PACKAGE_ROOT/opt/cti/"
[[ ! -f "$PROJECT_DIR/p-line/ha-config.conf" ]] \
|| install -m 0644 "$PROJECT_DIR/p-line/ha-config.conf" "$PACKAGE_ROOT/opt/cti/ha-config.conf"
cp -a "$PROJECT_DIR/p-line/resource" "$PACKAGE_ROOT/opt/cti/resource"
cp -a "$PROJECT_DIR/p-line/tools" "$PACKAGE_ROOT/opt/cti/tools"
[[ ! -d "$PROJECT_DIR/p-line/scripts" ]] \
|| cp -a "$PROJECT_DIR/p-line/scripts" "$PACKAGE_ROOT/opt/cti/scripts"
cp -a "$PROJECT_DIR/cti_app/build/." "$PACKAGE_ROOT/var/www/cti/"
for image_archive in "${ASTERISK_IMAGE_ARCHIVES[@]}"; do
install -m 0644 "$image_archive" "$image_archive.sha256" \
"$PACKAGE_ROOT/opt/cti/installer/images/"
done
sed -i -E "s/^pline\.version=.*/pline.version=$VERSION/" \
"$PACKAGE_ROOT/opt/cti/application.properties"
if grep -qE '^[[:space:]]*pline\.license\.enforce=' "$PACKAGE_ROOT/opt/cti/application.properties"; then
sed -i -E 's/^[[:space:]]*pline\.license\.enforce=.*/pline.license.enforce=true/' \
"$PACKAGE_ROOT/opt/cti/application.properties"
else
printf '\npline.license.enforce=true\n' >>"$PACKAGE_ROOT/opt/cti/application.properties"
fi
grep -qE '^pline\.license\.enforce=true$' "$PACKAGE_ROOT/opt/cti/application.properties" \
|| { printf 'The installer package must enforce license validation.\n' >&2; exit 1; }
install -m 0644 "$PROJECT_DIR/installer/debian/raymnad-cti.service" \
"$PACKAGE_ROOT/etc/systemd/system/raymnad-cti.service"
install -m 0644 "$PROJECT_DIR/installer/debian/nginx-site.conf" \
"$PACKAGE_ROOT/etc/nginx/sites-available/raymand-cti"
install -m 0644 "$PROJECT_DIR/installer/debian/control.in" "$PACKAGE_ROOT/DEBIAN/control"
sed -i "s/@VERSION@/$VERSION/g" "$PACKAGE_ROOT/DEBIAN/control"
sed -i "s/@ARCH@/$PACKAGE_ARCH/g" "$PACKAGE_ROOT/DEBIAN/control"
install -m 0755 "$PROJECT_DIR/installer/debian/postinst" "$PACKAGE_ROOT/DEBIAN/postinst"
install -m 0755 "$PROJECT_DIR/installer/debian/setup-https" "$PACKAGE_ROOT/usr/lib/raymand-cti/setup-https"
install -m 0755 "$PROJECT_DIR/installer/debian/nginx-manager" "$PACKAGE_ROOT/usr/lib/raymand-cti/nginx-manager"
install -m 0755 "$PROJECT_DIR/installer/debian/check-nginx-ports" "$PACKAGE_ROOT/usr/lib/raymand-cti/check-nginx-ports"
install -m 0755 "$PROJECT_DIR/installer/debian/ensure-nginx-proxy-include" \
"$PACKAGE_ROOT/usr/lib/raymand-cti/ensure-nginx-proxy-include"
install -m 0440 "$PROJECT_DIR/installer/debian/raymand-cti-nginx.sudoers" \
"$PACKAGE_ROOT/etc/sudoers.d/raymand-cti-nginx"
install -m 0440 "$PROJECT_DIR/installer/debian/raymand-cti-serial.sudoers" \
"$PACKAGE_ROOT/etc/sudoers.d/raymand-cti-serial"
install -m 0755 "$PROJECT_DIR/installer/debian/prerm" "$PACKAGE_ROOT/DEBIAN/prerm"
install -m 0755 "$PROJECT_DIR/installer/debian/postrm" "$PACKAGE_ROOT/DEBIAN/postrm"
printf '%s\n' /opt/cti/application.properties /etc/nginx/sites-available/raymand-cti \
>"$PACKAGE_ROOT/DEBIAN/conffiles"
OUTPUT_FILE="$INSTALLER_DIR/repo/raymand-cti-amd64.deb"
TEMP_DEB="$INSTALLER_DIR/repo/.raymand-cti-amd64.deb"
STAGED_INSTALLER="$INSTALLER_DIR/.install.sh.$$"
STAGED_README="$INSTALLER_DIR/.README.md.$$"
trap 'rm -rf -- "$BUILD_DIR"; rm -f -- "${TEMP_DEB:-}" "${STAGED_INSTALLER:-}" "${STAGED_README:-}"' EXIT
dpkg-deb --build --root-owner-group -Zxz -z6 "$PACKAGE_ROOT" "$TEMP_DEB" >/dev/null
DEB_PACKAGE="$(dpkg-deb --field "$TEMP_DEB" Package)"
DEB_VERSION="$(dpkg-deb --field "$TEMP_DEB" Version)"
DEB_ARCH="$(dpkg-deb --field "$TEMP_DEB" Architecture)"
DEB_DEPENDS="$(dpkg-deb --field "$TEMP_DEB" Depends)"
[[ "$DEB_PACKAGE" == raymand-cti && "$DEB_VERSION" == "$VERSION" && "$DEB_ARCH" == "$PACKAGE_ARCH" ]] \
|| { printf 'Built Debian package metadata does not match the requested package identity.\n' >&2; exit 1; }
[[ "$DEB_DEPENDS" == *sudo* ]] \
|| { printf 'Built Debian package is missing its sudo dependency.\n' >&2; exit 1; }
PACKAGE_FILES="$(dpkg-deb --fsys-tarfile "$TEMP_DEB" | tar -tf -)"
for required_entry in ./opt/cti/raymandcti.jar ./var/www/cti/index.html \
./usr/lib/raymand-cti/nginx-manager ./etc/systemd/system/raymnad-cti.service \
./etc/sudoers.d/raymand-cti-serial ./usr/lib/raymand-cti/check-nginx-ports; do
grep -Fxq "$required_entry" <<<"$PACKAGE_FILES" \
|| { printf 'Built Debian package is missing %s\n' "$required_entry" >&2; exit 1; }
done
while IFS= read -r required_asset; do
[[ -n "$required_asset" ]] || continue
grep -Fxq "./var/www/cti/$required_asset" <<<"$PACKAGE_FILES" \
|| { printf 'Built Debian package is missing React asset %s\n' "$required_asset" >&2; exit 1; }
done <<<"$REACT_REQUIRED_ASSETS"
cmp -s "$JAR_FILE" <(dpkg-deb --fsys-tarfile "$TEMP_DEB" | tar -xOf - ./opt/cti/raymandcti.jar) \
|| { printf 'Built Debian package does not contain the just-compiled Java archive.\n' >&2; exit 1; }
cmp -s "$PROJECT_DIR/cti_app/build/index.html" \
<(dpkg-deb --fsys-tarfile "$TEMP_DEB" | tar -xOf - ./var/www/cti/index.html) \
|| { printf 'Built Debian package does not contain the just-compiled React interface.\n' >&2; exit 1; }
for image_archive in "${ASTERISK_IMAGE_ARCHIVES[@]}"; do
image_name="$(basename "$image_archive")"
for image_entry in "./opt/cti/installer/images/$image_name" \
"./opt/cti/installer/images/$image_name.sha256"; do
grep -Fxq "$image_entry" <<<"$PACKAGE_FILES" \
|| { printf 'Built Debian package is missing %s\n' "$image_entry" >&2; exit 1; }
done
cmp -s "$image_archive" \
<(dpkg-deb --fsys-tarfile "$TEMP_DEB" | tar -xOf - "./opt/cti/installer/images/$image_name") \
|| { printf 'Built Debian package does not contain the verified Asterisk image %s.\n' "$image_name" >&2; exit 1; }
done
install -m 0755 "$PROJECT_DIR/installer/install.sh" "$STAGED_INSTALLER"
install -m 0644 "$PROJECT_DIR/installer/README.md" "$STAGED_README"
cmp -s "$PROJECT_DIR/installer/install.sh" "$STAGED_INSTALLER" \
|| { printf 'Transferable installer script differs from the source script.\n' >&2; exit 1; }
awk '/^APT_PACKAGES=\(/ { collecting = 1; next } collecting && /^\)/ { exit } collecting { print }' \
"$STAGED_INSTALLER" | grep -qE '(^|[[:space:]])sudo([[:space:]]|$)' \
|| { printf 'Transferable installer script does not install sudo before the CTI package.\n' >&2; exit 1; }
mv -f -- "$TEMP_DEB" "$OUTPUT_FILE"
(cd "$INSTALLER_DIR/repo" && sha256sum raymand-cti-amd64.deb) >"$OUTPUT_FILE.sha256"
rm -f -- "$INSTALLER_DIR/repo/pline-amd64.deb" "$INSTALLER_DIR/repo/pline-amd64.deb.sha256" \
"$INSTALLER_DIR/repo/raymandcti-amd64.deb" "$INSTALLER_DIR/repo/raymandcti-amd64.deb.sha256"
mv -f -- "$STAGED_INSTALLER" "$INSTALLER_DIR/install.sh"
mv -f -- "$STAGED_README" "$INSTALLER_DIR/README.md"
(cd "$INSTALLER_DIR/repo" && sha256sum -c raymand-cti-amd64.deb.sha256)
printf '[Raymand CTI-build] Installer script SHA256: '
sha256sum "$INSTALLER_DIR/install.sh" | awk '{print $1}'
printf '[Raymand CTI-build] Debian package: %s\n' "$OUTPUT_FILE"
printf '[Raymand CTI-build] Transferable installer: %s/\n' "$INSTALLER_DIR"
printf '[Raymand CTI-build] Entry point: %s/install.sh\n' "$INSTALLER_DIR"
printf '[Raymand CTI-build] Size (includes Asterisk image): '
du -h "$OUTPUT_FILE" | awk '{print $1}'
dpkg-deb --field "$OUTPUT_FILE" Package Version Architecture Depends