Some checks are pending
build-images / api (push) Waiting to run
build-images / worker-gpu (push) Waiting to run
build-images / web (push) Waiting to run
codeql / Analyze (javascript-typescript) (push) Waiting to run
codeql / Analyze (python) (push) Waiting to run
perf / API hot-path micro-smoke (budgeted) (push) Waiting to run
perf / Desktop engine crates (release build check) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Waiting to run
vision2026-ci / API phase modules (ruff + pytest) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Waiting to run
vision2026-ci / Python SDK resource tests (push) Waiting to run
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Waiting to run
vision2026-ci / Alembic upgrade + drift check (push) Waiting to run
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Waiting to run
Paid, hosted product at menivor.com, now retired as a company project. Fresh squashed history of the cleaned working tree (secret-scanned, no .env, no customer data, no outreach logs). Built on MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
221 lines
10 KiB
Bash
221 lines
10 KiB
Bash
# shellcheck shell=bash
|
|
# lib.sh: shared functions for the Menivor money database move to Neon (MG9,
|
|
# owner decision MD21). Sourced by the scripts next to it; does nothing alone.
|
|
#
|
|
# Runbook: infra-box/RUNBOOK-MENIVOR-NEON.md.
|
|
#
|
|
# Endpoints. Every database the scripts touch is named by one string:
|
|
# container:<name>/<db>[?user=<role>] a Postgres running in a local container
|
|
# (box 1: container:menivor-db-1/reelforge).
|
|
# Tools run inside it with docker exec, so
|
|
# the database never needs a published port.
|
|
# env:<VAR> a connection URL held in the variable VAR
|
|
# (Neon: env:MENIVOR_NEON_DIRECT_URL). Tools
|
|
# run in a throwaway postgres:<major>-alpine
|
|
# container; the URL reaches it through the
|
|
# environment, never argv, so `ps` on the
|
|
# host never shows the password.
|
|
#
|
|
# Secrets are read from the env file key by key (menivor_env_get); the file is
|
|
# never sourced, so a value with shell characters cannot run anything.
|
|
# shellcheck disable=SC2034 # variables here are read by the sourcing scripts
|
|
|
|
NCUT_DIR="${NCUT_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)}"
|
|
MENIVOR_ENV_FILE="${MENIVOR_ENV_FILE:-/etc/products/menivor.env}"
|
|
MENIVOR_STACK="${MENIVOR_STACK:-/opt/infra-box/stacks/menivor}"
|
|
MENIVOR_DB_CONTAINER="${MENIVOR_DB_CONTAINER:-menivor-db-1}"
|
|
MENIVOR_DB_NAME="${MENIVOR_DB_NAME:-reelforge}"
|
|
MENIVOR_DB_USER="${MENIVOR_DB_USER:-reelforge}"
|
|
MENIVOR_HEALTH_URL="${MENIVOR_HEALTH_URL:-http://127.0.0.1:8080/healthz}"
|
|
# Where reports, dumps and env backups go. Dumps hold customer data: 0700.
|
|
NCUT_STATE_DIR="${NCUT_STATE_DIR:-/var/lib/infra-box/menivor-neon}"
|
|
# One line per origin flip or cutover (ISO time, then a name). The 48 h rule in
|
|
# WAVE-C-DISPATCH section 0 rule 5 is checked against it; cutover.sh appends.
|
|
NCUT_FLIPS_LOG="${NCUT_FLIPS_LOG:-/etc/products/origin-flips.log}"
|
|
NCUT_MIN_FLIP_GAP_S="${NCUT_MIN_FLIP_GAP_S:-172800}"
|
|
# Docker network for the throwaway tool container ("host" on the box).
|
|
NCUT_DOCKER_NETWORK="${NCUT_DOCKER_NETWORK:-host}"
|
|
# Writers: everything in the stack that opens a database session.
|
|
NCUT_WRITERS="${NCUT_WRITERS:-api worker housekeeping beat}"
|
|
# The compose command. Tests replace it with a fake that records calls.
|
|
NCUT_COMPOSE="${NCUT_COMPOSE:-docker compose -p menivor --env-file $MENIVOR_ENV_FILE}"
|
|
# Tables whose full contents are fingerprinted (md5 over every row) on top of the
|
|
# row count every table gets. Money first; users because it carries balances.
|
|
NCUT_MONEY_TABLES="${NCUT_MONEY_TABLES:-credit_ledger premium_credit_ledger wallet_accounts wallet_account_owners wallet_ledger_entries wallet_transfers payment_intents subscriptions creator_subscriptions dodo_usage_events usage_records metered_usage users alembic_version}"
|
|
# Any other table up to this many rows is fingerprinted too.
|
|
NCUT_FINGERPRINT_MAX_ROWS="${NCUT_FINGERPRINT_MAX_ROWS:-200000}"
|
|
|
|
NCUT_ISO_RE='^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$'
|
|
NCUT_IDENT_RE='^[a-z_][a-z0-9_]*$'
|
|
|
|
ncut_log() { printf '[neon-cutover %s] %s\n' "$(date -u +%FT%TZ)" "$*" >&2; }
|
|
# A script can define ncut_on_die to report before exiting (cutover.sh does).
|
|
ncut_die() {
|
|
ncut_log "error: $*"
|
|
if declare -F ncut_on_die >/dev/null; then ncut_on_die "$*"; fi
|
|
exit 1
|
|
}
|
|
|
|
ncut_now() { date -u +%FT%TZ; }
|
|
ncut_epoch() { date -u +%s; }
|
|
|
|
# ISO 8601 UTC (2026-09-28T20:00:00Z) to epoch seconds, GNU or BSD date.
|
|
ncut_iso_to_epoch() {
|
|
local iso="$1"
|
|
[[ "$iso" =~ $NCUT_ISO_RE ]] || return 1
|
|
date -u -d "$iso" +%s 2>/dev/null || date -u -j -f '%Y-%m-%dT%H:%M:%SZ' "$iso" +%s
|
|
}
|
|
|
|
# ------------------------------------------------------------------ env file ---
|
|
# menivor_env_get KEY: the last KEY=value line of the env file, quotes removed.
|
|
menivor_env_get() {
|
|
local key="$1" line
|
|
[[ -r "$MENIVOR_ENV_FILE" ]] || return 1
|
|
line="$(grep -E "^[[:space:]]*${key}=" "$MENIVOR_ENV_FILE" | tail -n 1)" || return 1
|
|
line="${line#*=}"
|
|
line="${line%\"}"; line="${line#\"}"
|
|
line="${line%\'}"; line="${line#\'}"
|
|
printf '%s' "$line"
|
|
}
|
|
|
|
# menivor_env_set KEY VALUE: replace (or append) one line. The previous file is
|
|
# kept next to it as <file>.bak-<time>-<reason>; mode and owner are preserved.
|
|
menivor_env_set() {
|
|
local key="$1" value="$2" reason="${3:-neon-cutover}" stamp tmp
|
|
[[ "$key" =~ ^[A-Z_][A-Z0-9_]*$ ]] || ncut_die "bad env key: $key"
|
|
[[ "$value" != *$'\n'* ]] || ncut_die "env value for $key has a newline"
|
|
[[ -w "$MENIVOR_ENV_FILE" ]] || ncut_die "cannot write $MENIVOR_ENV_FILE"
|
|
stamp="$(date -u +%Y%m%dT%H%M%SZ)"
|
|
[[ -e "$MENIVOR_ENV_FILE.bak-$stamp-$reason" ]] \
|
|
|| cp -p "$MENIVOR_ENV_FILE" "$MENIVOR_ENV_FILE.bak-$stamp-$reason"
|
|
tmp="$(mktemp "$MENIVOR_ENV_FILE.tmp.XXXXXX")"
|
|
awk -v k="$key" -v v="$value" '
|
|
BEGIN { done = 0 }
|
|
$0 ~ "^[[:space:]]*" k "=" { if (!done) { print k "=" v; done = 1 } ; next }
|
|
{ print }
|
|
END { if (!done) print k "=" v }
|
|
' "$MENIVOR_ENV_FILE" >"$tmp"
|
|
# Keep the original inode's mode and owner: write through it.
|
|
cat "$tmp" >"$MENIVOR_ENV_FILE"
|
|
rm -f "$tmp"
|
|
}
|
|
|
|
# The compose file list for each database target. Paths are relative to the
|
|
# stack directory (build/build-menivor.sh runs compose from there too, so its
|
|
# `up` follows COMPOSE_FILE from the env file without any change to it).
|
|
NCUT_COMPOSE_FILES_BOX="docker-compose.yml"
|
|
NCUT_COMPOSE_FILES_NEON="docker-compose.yml:docker-compose.neon.yml"
|
|
|
|
ncut_db_target() { # prints neon or box (from the env file)
|
|
local files
|
|
files="$(menivor_env_get COMPOSE_FILE || true)"
|
|
if [[ "$files" == *docker-compose.neon.yml* ]]; then echo neon; else echo box; fi
|
|
}
|
|
|
|
ncut_compose() { # ncut_compose <compose args...>; runs in the stack directory
|
|
# shellcheck disable=SC2086 # NCUT_COMPOSE is a command line on purpose
|
|
(cd "$MENIVOR_STACK" && $NCUT_COMPOSE "$@")
|
|
}
|
|
|
|
# ----------------------------------------------------------------- endpoints ---
|
|
# ncut_endpoint_check <endpoint>: dies unless the endpoint string is well formed.
|
|
ncut_endpoint_check() {
|
|
local ep="$1" rest name db var
|
|
case "$ep" in
|
|
container:*)
|
|
rest="${ep#container:}"; rest="${rest%%\?*}"
|
|
name="${rest%%/*}"; db="${rest#*/}"
|
|
[[ "$name" =~ ^[A-Za-z0-9][A-Za-z0-9_.-]*$ && "$db" =~ $NCUT_IDENT_RE && "$rest" == */* ]] \
|
|
|| ncut_die "bad container endpoint: $ep (want container:<name>/<db>)"
|
|
;;
|
|
env:*)
|
|
var="${ep#env:}"
|
|
[[ "$var" =~ ^[A-Z_][A-Z0-9_]*$ ]] || ncut_die "bad env endpoint: $ep"
|
|
[[ -n "${!var:-}" ]] || ncut_die "$var is empty (endpoint $ep)"
|
|
[[ "${!var}" =~ ^postgres(ql)?(\+[a-z0-9]+)?:// ]] || ncut_die "$var is not a postgres URL"
|
|
;;
|
|
*) ncut_die "unknown endpoint: $ep (container:<name>/<db> or env:<VAR>)" ;;
|
|
esac
|
|
}
|
|
|
|
# A libpq URL from a SQLAlchemy one: drop "+psycopg" and friends.
|
|
ncut_libpq_url() { sed -E 's#^postgres(ql)?\+[a-z0-9]+://#postgresql://#; s#^postgres://#postgresql://#' <<<"$1"; }
|
|
|
|
# ncut_pg <endpoint> <tool> [args...]: runs psql, pg_dump or pg_restore against
|
|
# the endpoint. stdin is passed through (pg_restore reads the dump from it).
|
|
# For an env: endpoint, NCUT_MOUNT (a host directory) is mounted read-only at
|
|
# /work in the tool container.
|
|
# Session settings that shape text output are pinned so both sides of a
|
|
# comparison render rows identically.
|
|
NCUT_PGOPTIONS='-c TimeZone=UTC -c DateStyle=ISO,MDY -c IntervalStyle=postgres -c extra_float_digits=1 -c bytea_output=hex -c statement_timeout=0'
|
|
ncut_pg() {
|
|
local ep="$1" tool="$2"; shift 2
|
|
local rest name db user url major var
|
|
case "$tool" in psql | pg_dump | pg_restore) ;; *) ncut_die "tool not allowed: $tool" ;; esac
|
|
case "$ep" in
|
|
container:*)
|
|
rest="${ep#container:}"
|
|
user="$MENIVOR_DB_USER"
|
|
if [[ "$rest" == *\?user=* ]]; then user="${rest##*\?user=}"; rest="${rest%%\?*}"; fi
|
|
name="${rest%%/*}"; db="${rest#*/}"
|
|
docker exec -i -e PGOPTIONS="$NCUT_PGOPTIONS" "$name" "$tool" -U "$user" -d "$db" "$@"
|
|
;;
|
|
env:*)
|
|
var="${ep#env:}"
|
|
url="$(ncut_libpq_url "${!var}")"
|
|
major="${NCUT_PG_MAJOR:-16}"
|
|
local mount=()
|
|
[[ -n "${NCUT_MOUNT:-}" ]] && mount=(-v "$NCUT_MOUNT:/work:ro")
|
|
NCUT_URL="$url" docker run --rm -i --network "$NCUT_DOCKER_NETWORK" \
|
|
-e NCUT_URL -e PGOPTIONS="$NCUT_PGOPTIONS" -e PGCONNECT_TIMEOUT=15 \
|
|
"${mount[@]+"${mount[@]}"}" "postgres:${major}-alpine" sh -c 'tool="$1"; shift; exec "$tool" -d "$NCUT_URL" "$@"' sh "$tool" "$@"
|
|
;;
|
|
esac
|
|
}
|
|
|
|
# ncut_sql <endpoint> <sql>: one value per line, unaligned, no headers.
|
|
ncut_sql() { ncut_pg "$1" psql -X -v ON_ERROR_STOP=1 -At -F $'\t' -c "$2"; }
|
|
|
|
ncut_server_major() { ncut_sql "$1" "SELECT current_setting('server_version_num')::int / 10000"; }
|
|
|
|
# Tables in user schemas (not pg_catalog, information_schema, pg_toast or the
|
|
# restore drill's scratch schema), one schema.table per line, sorted.
|
|
NCUT_USER_TABLES_SQL="SELECT n.nspname || '.' || c.relname FROM pg_class c
|
|
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
WHERE c.relkind IN ('r', 'p')
|
|
AND n.nspname NOT IN ('pg_catalog', 'information_schema', 'restore_drill')
|
|
AND n.nspname NOT LIKE 'pg\\_%'
|
|
ORDER BY 1"
|
|
|
|
ncut_user_tables() { ncut_sql "$1" "$NCUT_USER_TABLES_SQL"; }
|
|
|
|
# ------------------------------------------------------------------ measures ---
|
|
# Memory in MiB: "<used> <total>" from /proc/meminfo, or "n/a n/a".
|
|
ncut_host_mem() {
|
|
if [[ -r /proc/meminfo ]]; then
|
|
awk '/^MemTotal:/ {t=$2} /^MemAvailable:/ {a=$2} END {printf "%d %d\n", (t-a)/1024, t/1024}' /proc/meminfo
|
|
else
|
|
echo "n/a n/a"
|
|
fi
|
|
}
|
|
|
|
# Memory of one container ("n/a" when it is not running).
|
|
ncut_container_mem() {
|
|
docker stats --no-stream --format '{{.MemUsage}}' "$1" 2>/dev/null | awk '{print $1}' | grep . || echo n/a
|
|
}
|
|
|
|
ncut_wait_healthy() { # ncut_wait_healthy <seconds>
|
|
local deadline=$((SECONDS + $1))
|
|
while ((SECONDS < deadline)); do
|
|
if curl -fs -o /dev/null --max-time 5 "$MENIVOR_HEALTH_URL"; then return 0; fi
|
|
sleep 2
|
|
done
|
|
return 1
|
|
}
|
|
|
|
ncut_prepare_state_dir() {
|
|
install -d -m 700 "$NCUT_STATE_DIR"
|
|
NCUT_WORK="$(mktemp -d "$NCUT_STATE_DIR/run.XXXXXX")"
|
|
chmod 700 "$NCUT_WORK"
|
|
}
|