#!/usr/bin/env bash
set -uo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$HERE/hosts.env"
NAME="auditor-build"
SRCBIN=""
VERIFY_ONLY=0
while [ $# -gt 0 ]; do
case "$1" in
--name) shift; NAME="${1:-}" ;;
--verify) VERIFY_ONLY=1 ;;
-h|--help) sed -n '2,30p' "$0"; exit 0 ;;
*) SRCBIN="$1" ;;
esac
shift
done
DEST="/opt/pgrust/bin/postgres.$NAME"
SSH="ssh -o StrictHostKeyChecking=no ${AUDIT_USER}@${OLTP_SERVER_PRIVATE}"
echo "=============================================================================="
echo "deploy-binary — install a locally built binary on the OLTP server"
echo "=============================================================================="
echo " target host : $OLTP_SERVER_PRIVATE (private network, SG-internal only)"
echo " install as : $DEST"
echo
if [ "$VERIFY_ONLY" = 1 ]; then
echo " Binaries currently installed on the OLTP server:"
$SSH 'for f in /opt/pgrust/bin/postgres*; do
case "$f" in *.sha256) continue;; esac
printf " %-46s %s\n" "$f" "$(sha256sum "$f" | cut -d" " -f1)"
done' || { echo " !! could not reach the server"; exit 1; }
exit 0
fi
[ -n "$SRCBIN" ] || { echo "!! need a path to a binary. See --help."; exit 2; }
[ -x "$SRCBIN" ] || { echo "!! not an executable file: $SRCBIN"; exit 2; }
LOCAL_SHA=$(sha256sum "$SRCBIN" | cut -d' ' -f1)
SIZE=$(stat -c %s "$SRCBIN")
echo " source : $SRCBIN"
echo " size : $SIZE bytes"
echo " sha256 : $LOCAL_SHA"
MANIFEST="$(dirname "$SRCBIN")/build-manifest.txt"
echo
echo " copying ..."
scp -q -o StrictHostKeyChecking=no "$SRCBIN" "${AUDIT_USER}@${OLTP_SERVER_PRIVATE}:/tmp/postgres.$NAME" \
|| { echo " !! copy failed"; exit 1; }
if [ -r "$MANIFEST" ]; then
scp -q -o StrictHostKeyChecking=no "$MANIFEST" \
"${AUDIT_USER}@${OLTP_SERVER_PRIVATE}:/tmp/build-manifest.$NAME.txt" 2>/dev/null \
&& echo " build manifest copied alongside"
fi
echo " installing ..."
REMOTE_SHA=$($SSH "sudo install -m 755 /tmp/postgres.$NAME $DEST \
&& rm -f /tmp/postgres.$NAME \
&& if [ -f /tmp/build-manifest.$NAME.txt ]; then sudo install -m 644 /tmp/build-manifest.$NAME.txt $DEST.manifest.txt; rm -f /tmp/build-manifest.$NAME.txt; fi \
&& sudo sha256sum $DEST | cut -d' ' -f1")
echo " installed sha256: ${REMOTE_SHA:-<none>}"
echo
if [ "$REMOTE_SHA" != "$LOCAL_SHA" ]; then
echo "!! SHA MISMATCH AFTER COPY"
echo "!! local : $LOCAL_SHA"
echo "!! remote: ${REMOTE_SHA:-<none>}"
echo "!! Refusing to declare this deployed. Do not benchmark it — any number"
echo "!! it produced would be attributed to a binary that never ran."
exit 1
fi
$SSH "sudo sh -c 'echo $LOCAL_SHA > $DEST.sha256'"
echo " VERIFIED — local and installed sha256 match."
echo
echo " Run the OLTP benchmarks against it:"
echo " ./run-oltp-ro.sh --binary $NAME"
echo " ./run-oltp-rw.sh --binary $NAME"
echo " (run those on the CLIENT box: ${OLTP_CLIENT_HOST})"
echo
echo " The C PostgreSQL arm is untouched — it is the same 18.3 build in both"
echo " modes, so a shipped-vs-your-build comparison changes only the pgrust side."
echo "=============================================================================="