forked from cartsnitch/cartsnitch
feat: parameterize seed tooling for UAT + document UAT receipt-submission path
- Add scripts/seed-env.sh with --env dev|uat argument, replacing hardcoded namespace - Keep scripts/seed-dev.sh as one-line wrapper calling seed-env.sh dev - Add scripts/seed-env-job.yaml with __ENV__ placeholder for namespace/label - Add scripts/apply-seed-job.sh <env> helper using sed substitution - Keep scripts/seed-dev-job.yaml as unchanged backward-compat copy - Add docs/uat-receipt-submission.md documenting the inbound email receipt path for UAT Refs: CAR-812, CAR-808
This commit is contained in:
Executable
+38
@@ -0,0 +1,38 @@
|
||||
#!/usr/bin/env bash
|
||||
# =============================================================================
|
||||
# apply-seed-job.sh — Apply the seed Job manifest for a given environment.
|
||||
#
|
||||
# Usage:
|
||||
# ./apply-seed-job.sh <env>
|
||||
#
|
||||
# Example:
|
||||
# ./apply-seed-job.sh uat
|
||||
# ./apply-seed-job.sh dev
|
||||
# =============================================================================
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
ENV="${1:-}"
|
||||
HELP_FLAG=""
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
--help) HELP_FLAG="1"; shift ;;
|
||||
*) ENV="$1"; shift ;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [[ -n "$HELP_FLAG" ]] || [[ -z "$ENV" ]]; then
|
||||
echo "Usage: $0 <env>"
|
||||
echo " env dev or uat"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [[ "$ENV" != "dev" && "$ENV" != "uat" ]]; then
|
||||
echo "ERROR: Invalid environment: $ENV (must be 'dev' or 'uat')" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
SCRIPT_DIR="$(dirname "$0")"
|
||||
sed "s/__ENV__/${ENV}/g" "${SCRIPT_DIR}/seed-env-job.yaml" | kubectl apply -f -
|
||||
echo "Seed job applied for environment: $ENV"
|
||||
@@ -58,4 +58,4 @@ spec:
|
||||
memory: 256Mi
|
||||
limits:
|
||||
cpu: 500m
|
||||
memory: 512Mi
|
||||
memory: 512Mi
|
||||
+2
-103
@@ -1,104 +1,3 @@
|
||||
#!/usr/bin/env bash
|
||||
# =============================================================================
|
||||
# seed-dev.sh — Run the CartSnitch seed runner against the dev database.
|
||||
#
|
||||
# Usage:
|
||||
# ./seed-dev.sh Run full seed against dev
|
||||
# ./seed-dev.sh --dry-run Show planned record counts without writing
|
||||
# ./seed-dev.sh --help Show this help
|
||||
#
|
||||
# Prerequisites:
|
||||
# - kubectl configured for the cartsnitch-dev cluster
|
||||
# - Namespace cartsnitch-dev exists (CNPG Postgres must be running)
|
||||
#
|
||||
# What it does:
|
||||
# 1. Starts a background port-forward to cartsnitch-pg-rw:5432
|
||||
# 2. Waits for the tunnel to be ready
|
||||
# 3. Runs python -m cartsnitch_common.seed with --database-url pointing
|
||||
# to localhost:<forwarded-port>/cartsnitch
|
||||
# 4. Cleans up the port-forward on exit (normal, interrupt, or error)
|
||||
# =============================================================================
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# --- Config -------------------------------------------------------------------
|
||||
readonly NAMESPACE="cartsnitch-dev"
|
||||
readonly SVC_NAME="cartsnitch-pg-rw"
|
||||
readonly LOCAL_PORT="5433" # use a non-privileged port to avoid conflicts
|
||||
readonly DB_NAME="cartsnitch"
|
||||
readonly PG_USER="cartsnitch"
|
||||
# Retrieve password from the CNPG credentials secret
|
||||
readonly PG_PASSWORD="$(
|
||||
kubectl get secret cartsnitch-pg-credentials \
|
||||
-n "$NAMESPACE" \
|
||||
-o jsonpath='{.data.password}' \
|
||||
| base64 -d
|
||||
)"
|
||||
readonly DB_URL="postgresql://${PG_USER}:${PG_PASSWORD}@localhost:${LOCAL_PORT}/${DB_NAME}"
|
||||
|
||||
# --- Helpers ------------------------------------------------------------------
|
||||
log() { echo "[seed-dev] $*"; }
|
||||
fail() { log "ERROR: $*" >&2; exit 1; }
|
||||
|
||||
# Cleanup port-forward and exit.
|
||||
cleanup() {
|
||||
if [[ -n "${PF_PID:-}" ]]; then
|
||||
log "Stopping port-forward (PID $PF_PID)..."
|
||||
kill "$PF_PID" 2>/dev/null || true
|
||||
wait "$PF_PID" 2>/dev/null || true
|
||||
fi
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
# --- Args ---------------------------------------------------------------------
|
||||
DRY_RUN=""
|
||||
HELP_FLAG=""
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
--dry-run) DRY_RUN="--dry-run"; shift ;;
|
||||
--help) HELP_FLAG="1"; shift ;;
|
||||
*) fail "Unknown argument: $1";;
|
||||
esac
|
||||
done
|
||||
|
||||
if [[ -n "$HELP_FLAG" ]]; then
|
||||
sed -n '3,/^# ---/p' "$0" | head -n -1 | sed 's/^# //'
|
||||
echo ""
|
||||
echo "Additional arguments are passed through to the seed runner."
|
||||
echo "Common seed-runner options:"
|
||||
echo " --dry-run Show planned record counts without writing"
|
||||
echo " --seed N Set random seed (default: 42)"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# --- Prerequisites ------------------------------------------------------------
|
||||
if ! command -v kubectl &>/dev/null; then
|
||||
fail "kubectl not found — must be installed and configured."
|
||||
fi
|
||||
|
||||
# --- Port-forward -------------------------------------------------------------
|
||||
log "Starting port-forward ${SVC_NAME}:5432 -> localhost:${LOCAL_PORT} ..."
|
||||
kubectl port-forward \
|
||||
-n "$NAMESPACE" \
|
||||
svc/"$SVC_NAME" \
|
||||
"${LOCAL_PORT}:5432" \
|
||||
&>/dev/null &
|
||||
PF_PID=$!
|
||||
|
||||
# Give the tunnel a moment to establish
|
||||
sleep 2
|
||||
|
||||
# Verify the tunnel is up
|
||||
if ! kill -0 "$PF_PID" 2>/dev/null; then
|
||||
fail "Port-forward failed to start."
|
||||
fi
|
||||
log "Port-forward active (PID $PF_PID) on localhost:${LOCAL_PORT}"
|
||||
|
||||
# --- Seed --------------------------------------------------------------------
|
||||
log "Running seed against dev database..."
|
||||
set -x
|
||||
python -m cartsnitch_common.seed --database-url "$DB_URL" $DRY_RUN
|
||||
set +x
|
||||
|
||||
log "Done."
|
||||
# Backward-compat wrapper — delegates to seed-env.sh dev
|
||||
exec "$(dirname "$0")/seed-env.sh" dev "$@"
|
||||
@@ -0,0 +1,58 @@
|
||||
# seed-env-job.yaml
|
||||
# K8s Job to run the CartSnitch seed runner against any CartSnitch database.
|
||||
#
|
||||
# Usage (via apply-seed-job.sh):
|
||||
# bash scripts/apply-seed-job.sh dev
|
||||
# bash scripts/apply-seed-job.sh uat
|
||||
#
|
||||
# To view logs:
|
||||
# kubectl logs -n cartsnitch-<env> job/seed-env -f
|
||||
#
|
||||
# To re-run after fixing issues:
|
||||
# kubectl delete -f - -n cartsnitch-<env> && bash scripts/apply-seed-job.sh <env>
|
||||
#
|
||||
apiVersion: batch/v1
|
||||
kind: Job
|
||||
metadata:
|
||||
name: seed-env
|
||||
namespace: cartsnitch-__ENV__
|
||||
labels:
|
||||
app: cartsnitch
|
||||
component: seed
|
||||
environment: __ENV__
|
||||
annotations:
|
||||
description: "Runs cartsnitch-common seed runner to populate __ENV__ database with realistic test data."
|
||||
spec:
|
||||
backoffLimit: 0
|
||||
concurrencyPolicy: Forbid
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: cartsnitch
|
||||
component: seed
|
||||
environment: __ENV__
|
||||
spec:
|
||||
restartPolicy: Never
|
||||
containers:
|
||||
- name: seed
|
||||
image: python:3.12-slim
|
||||
command:
|
||||
- sh
|
||||
- -c
|
||||
- |
|
||||
pip install --no-cache-dir "cartsnitch-common @ git+https://github.com/cartsnitch/common.git@main" && \
|
||||
python -m cartsnitch_common.seed --database-url "$${DATABASE_URL}"
|
||||
env:
|
||||
- name: DATABASE_URL
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: cartsnitch-secrets
|
||||
key: database-url-pg
|
||||
optional: false
|
||||
resources:
|
||||
requests:
|
||||
cpu: 100m
|
||||
memory: 256Mi
|
||||
limits:
|
||||
cpu: 500m
|
||||
memory: 512Mi
|
||||
Executable
+120
@@ -0,0 +1,120 @@
|
||||
#!/usr/bin/env bash
|
||||
# =============================================================================
|
||||
# seed-env.sh — Run the CartSnitch seed runner against any CartSnitch database.
|
||||
#
|
||||
# Usage:
|
||||
# ./seed-env.sh [--env dev|uat] [--dry-run] [--help]
|
||||
# ./seed-env.sh uat --dry-run Run dry-run against UAT
|
||||
# ./seed-env.sh dev Run full seed against dev (default)
|
||||
#
|
||||
# Prerequisites:
|
||||
# - kubectl configured for the target cluster
|
||||
# - Namespace cartsnitch-<env> exists (CNPG Postgres must be running)
|
||||
#
|
||||
# What it does:
|
||||
# 1. Starts a background port-forward to cartsnitch-pg-rw:5432
|
||||
# 2. Waits for the tunnel to be ready
|
||||
# 3. Runs python -m cartsnitch_common.seed with --database-url pointing
|
||||
# to localhost:<forwarded-port>/cartsnitch
|
||||
# 4. Cleans up the port-forward on exit (normal, interrupt, or error)
|
||||
# =============================================================================
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# --- Config -------------------------------------------------------------------
|
||||
ENV="${1:-dev}"
|
||||
shift || true
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
--env) ENV="$2"; shift 2 ;;
|
||||
--dry-run|--help) break ;;
|
||||
*) break ;;
|
||||
esac
|
||||
done
|
||||
|
||||
NAMESPACE="cartsnitch-${ENV}"
|
||||
SVC_NAME="cartsnitch-pg-rw"
|
||||
LOCAL_PORT="5433"
|
||||
DB_NAME="cartsnitch"
|
||||
PG_USER="cartsnitch"
|
||||
PG_PASSWORD="$(
|
||||
kubectl get secret cartsnitch-pg-credentials \
|
||||
-n "$NAMESPACE" \
|
||||
-o jsonpath='{.data.password}' \
|
||||
| base64 -d
|
||||
)"
|
||||
DB_URL="postgresql://${PG_USER}:${PG_PASSWORD}@localhost:${LOCAL_PORT}/${DB_NAME}"
|
||||
|
||||
# --- Helpers ------------------------------------------------------------------
|
||||
log() { echo "[seed-env] [$ENV] $*"; }
|
||||
fail() { log "ERROR: $*" >&2; exit 1; }
|
||||
|
||||
cleanup() {
|
||||
if [[ -n "${PF_PID:-}" ]]; then
|
||||
log "Stopping port-forward (PID $PF_PID)..."
|
||||
kill "$PF_PID" 2>/dev/null || true
|
||||
wait "$PF_PID" 2>/dev/null || true
|
||||
fi
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
# --- Args ---------------------------------------------------------------------
|
||||
DRY_RUN=""
|
||||
HELP_FLAG=""
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
--dry-run) DRY_RUN="--dry-run"; shift ;;
|
||||
--help) HELP_FLAG="1"; shift ;;
|
||||
*) fail "Unknown argument: $1";;
|
||||
esac
|
||||
done
|
||||
|
||||
if [[ -n "$HELP_FLAG" ]]; then
|
||||
echo "Usage: $0 [--env dev|uat] [--dry-run] [--help]"
|
||||
echo ""
|
||||
echo "Positional / keyword arguments:"
|
||||
echo " --env dev|uat Target environment (default: dev)"
|
||||
echo " --dry-run Show planned record counts without writing"
|
||||
echo " --help Show this help"
|
||||
echo ""
|
||||
echo "Additional arguments are passed through to the seed runner."
|
||||
echo "Common seed-runner options:"
|
||||
echo " --seed N Set random seed (default: 42)"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# --- Validate env --------------------------------------------------------------
|
||||
if [[ "$ENV" != "dev" && "$ENV" != "uat" ]]; then
|
||||
fail "Invalid environment: $ENV (must be 'dev' or 'uat')"
|
||||
fi
|
||||
|
||||
# --- Prerequisites ------------------------------------------------------------
|
||||
if ! command -v kubectl &>/dev/null; then
|
||||
fail "kubectl not found — must be installed and configured."
|
||||
fi
|
||||
|
||||
# --- Port-forward -------------------------------------------------------------
|
||||
log "Starting port-forward ${SVC_NAME}:5432 -> localhost:${LOCAL_PORT} ..."
|
||||
kubectl port-forward \
|
||||
-n "$NAMESPACE" \
|
||||
svc/"$SVC_NAME" \
|
||||
"${LOCAL_PORT}:5432" \
|
||||
&>/dev/null &
|
||||
PF_PID=$!
|
||||
|
||||
sleep 2
|
||||
|
||||
if ! kill -0 "$PF_PID" 2>/dev/null; then
|
||||
fail "Port-forward failed to start."
|
||||
fi
|
||||
log "Port-forward active (PID $PF_PID) on localhost:${LOCAL_PORT}"
|
||||
|
||||
# --- Seed --------------------------------------------------------------------
|
||||
log "Running seed against ${ENV} database..."
|
||||
set -x
|
||||
python -m cartsnitch_common.seed --database-url "$DB_URL" $DRY_RUN
|
||||
set +x
|
||||
|
||||
log "Done."
|
||||
Reference in New Issue
Block a user