feat: add AI SoulMate local admin deployment
This commit is contained in:
1 parent
bd3d409836
commit
1b5e10bbd0
15 files changed
+665
-24
No files matched your search
+31
-2
@@ -28,6 +28,10 @@ env_value() {
|
||||
printf '%s' "$value"
|
||||
}
|
||||
|
||||
env_optional_value() {
|
||||
awk -F= -v key="$1" '$1 == key {sub(/^[^=]*=/, ""); print; exit}' "$env_file"
|
||||
}
|
||||
|
||||
compose_command=(docker compose --env-file "$env_file" -f "$compose_file")
|
||||
"${compose_command[@]}" config --quiet
|
||||
|
||||
@@ -56,6 +60,26 @@ mkdir -p "$staging_directory/minio"
|
||||
'exec pg_dump --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" --format=custom --compress=6 --no-owner --no-acl' \
|
||||
> "$staging_directory/postgres.dump"
|
||||
|
||||
postgres_user="$(env_value POSTGRES_USER)"
|
||||
admin_database="$(env_optional_value ADMIN_POSTGRES_DB)"
|
||||
admin_artifact="null"
|
||||
if [[ -n "$admin_database" ]]; then
|
||||
if [[ ! "$admin_database" =~ ^[A-Za-z][A-Za-z0-9_]{0,62}$ ]]; then
|
||||
printf 'ADMIN_POSTGRES_DB is not a safe PostgreSQL identifier.\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
admin_database_exists="$("${compose_command[@]}" exec -T postgres psql \
|
||||
--username "$postgres_user" --dbname postgres --tuples-only --no-align \
|
||||
--command "SELECT 1 FROM pg_database WHERE datname = '$admin_database'")"
|
||||
if [[ "$admin_database_exists" == "1" ]]; then
|
||||
"${compose_command[@]}" exec -T postgres pg_dump \
|
||||
--username "$postgres_user" --dbname "$admin_database" \
|
||||
--format=custom --compress=6 --no-owner --no-acl \
|
||||
> "$staging_directory/admin-postgres.dump"
|
||||
admin_artifact='"admin-postgres.dump"'
|
||||
fi
|
||||
fi
|
||||
|
||||
"${compose_command[@]}" exec -T redis sh -ec \
|
||||
'redis-cli --no-auth-warning -a "$REDIS_PASSWORD" SAVE >/dev/null'
|
||||
redis_container_id="$("${compose_command[@]}" ps -q redis)"
|
||||
@@ -84,12 +108,13 @@ fi
|
||||
minio_object_count="$(find "$staging_directory/minio" -type f | wc -l | tr -d '[:space:]')"
|
||||
|
||||
cat > "$staging_directory/manifest.yml" <<EOF
|
||||
schema_version: 1
|
||||
schema_version: 2
|
||||
created_at: "$(date --iso-8601=seconds)"
|
||||
deployment_repository_commit: "$deployment_repository_commit"
|
||||
compose_file: "$compose_file"
|
||||
artifacts:
|
||||
postgres: "postgres.dump"
|
||||
admin_postgres: $admin_artifact
|
||||
redis: "redis.rdb"
|
||||
minio: "minio/"
|
||||
minio:
|
||||
@@ -102,7 +127,11 @@ EOF
|
||||
|
||||
(
|
||||
cd "$staging_directory"
|
||||
find manifest.yml postgres.dump redis.rdb minio -type f -print0 \
|
||||
checksum_paths=(manifest.yml postgres.dump redis.rdb minio)
|
||||
if [[ -f admin-postgres.dump ]]; then
|
||||
checksum_paths+=(admin-postgres.dump)
|
||||
fi
|
||||
find "${checksum_paths[@]}" -type f -print0 \
|
||||
| LC_ALL=C sort -z \
|
||||
| xargs -0 sha256sum
|
||||
) > "$staging_directory/checksums.sha256"
|
||||
|
||||
Executable
+59
@@ -0,0 +1,59 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
env_file="${1:-compose/.env}"
|
||||
source_directory="${2:-/srv/ai-soulmate/sources/mlkj-admin}"
|
||||
|
||||
for required_command in awk docker git; do
|
||||
if ! command -v "$required_command" >/dev/null 2>&1; then
|
||||
printf 'Required command is unavailable: %s\n' "$required_command" >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
if [[ ! -r "$env_file" || ! -r "$source_directory/backend-java/Dockerfile" || ! -r "$source_directory/frontend/Dockerfile" ]]; then
|
||||
printf 'Admin environment or source Dockerfiles are unavailable.\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
env_value() {
|
||||
awk -F= -v key="$1" '$1 == key {sub(/^[^=]*=/, ""); print; exit}' "$env_file"
|
||||
}
|
||||
|
||||
source_branch="$(git -C "$source_directory" branch --show-current)"
|
||||
source_commit="$(git -C "$source_directory" rev-parse HEAD)"
|
||||
source_short_commit="${source_commit:0:12}"
|
||||
backend_image="$(env_value ADMIN_BACKEND_IMAGE)"
|
||||
frontend_image="$(env_value ADMIN_FRONTEND_IMAGE)"
|
||||
if [[ "$source_branch" != "local-deployment" ]]; then
|
||||
printf 'Expected mlkj-admin branch local-deployment, found: %s\n' "$source_branch" >&2
|
||||
exit 1
|
||||
fi
|
||||
if [[ -n "$(git -C "$source_directory" status --short --untracked-files=no)" ]]; then
|
||||
printf 'Tracked mlkj-admin source changes are present; refusing an uncommitted build.\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
if [[ "$backend_image" != "ai-soulmate-admin-backend:${source_short_commit}" || "$frontend_image" != "ai-soulmate-admin-frontend:${source_short_commit}" ]]; then
|
||||
printf 'Admin image tags do not match source commit; rerun configure-admin-env.sh.\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
docker build \
|
||||
--build-context "maven-local-repository=$source_directory/.empty-maven-repository" \
|
||||
--build-arg "MAVEN_IMAGE=$(env_value ADMIN_MAVEN_IMAGE)" \
|
||||
--build-arg "JRE_IMAGE=$(env_value ADMIN_JRE_IMAGE)" \
|
||||
--target runtime \
|
||||
--label "org.opencontainers.image.revision=${source_commit}" \
|
||||
--file "$source_directory/backend-java/Dockerfile" \
|
||||
--tag "$backend_image" \
|
||||
"$source_directory"
|
||||
|
||||
docker build \
|
||||
--build-arg "NODE_IMAGE=$(env_value ADMIN_NODE_IMAGE)" \
|
||||
--build-arg ADMIN_MODULE_CONFIG=module-config.local.json \
|
||||
--target runtime \
|
||||
--label "org.opencontainers.image.revision=${source_commit}" \
|
||||
--tag "$frontend_image" \
|
||||
"$source_directory/frontend"
|
||||
|
||||
docker image inspect "$backend_image" "$frontend_image" \
|
||||
--format 'Admin image built: {{index .RepoTags 0}} id={{.Id}} size={{.Size}} revision={{index .Config.Labels "org.opencontainers.image.revision"}}'
|
||||
Executable
+44
@@ -0,0 +1,44 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
env_file="${1:-compose/.env}"
|
||||
admin_compose_file="${ADMIN_COMPOSE_FILE:-compose/docker-compose.admin.yml}"
|
||||
infra_compose_file="${COMPOSE_FILE:-compose/docker-compose.infra.yml}"
|
||||
|
||||
if [[ ! -r "$env_file" ]]; then
|
||||
printf 'Environment file not found: %s\n' "$env_file" >&2
|
||||
exit 1
|
||||
fi
|
||||
env_value() {
|
||||
awk -F= -v key="$1" '$1 == key {sub(/^[^=]*=/, ""); print; exit}' "$env_file"
|
||||
}
|
||||
|
||||
infra_compose=(docker compose --env-file "$env_file" -f "$infra_compose_file")
|
||||
super_admin_count="$("${infra_compose[@]}" exec -T postgres psql \
|
||||
--username "$(env_value POSTGRES_USER)" --dbname "$(env_value ADMIN_POSTGRES_DB)" \
|
||||
--tuples-only --no-align \
|
||||
--command "SELECT count(*) FROM poly_admin_users WHERE role_type = 'SUPER_ADMIN'")"
|
||||
if [[ "$super_admin_count" != "1" ]]; then
|
||||
printf 'Refusing to clear bootstrap password; expected one SUPER_ADMIN, found: %s\n' "$super_admin_count" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
temporary_file="$(mktemp "${env_file}.tmp.XXXXXX")"
|
||||
awk '
|
||||
BEGIN { found = 0 }
|
||||
index($0, "ADMIN_INITIAL_PASSWORD=") == 1 {
|
||||
if (!found) print "ADMIN_INITIAL_PASSWORD="
|
||||
found = 1
|
||||
next
|
||||
}
|
||||
{ print }
|
||||
END { if (!found) print "ADMIN_INITIAL_PASSWORD=" }
|
||||
' "$env_file" > "$temporary_file"
|
||||
chmod 600 "$temporary_file"
|
||||
mv -f "$temporary_file" "$env_file"
|
||||
|
||||
project_name="$(env_value ADMIN_COMPOSE_PROJECT_NAME)"
|
||||
admin_compose=(docker compose --project-name "$project_name" --env-file "$env_file" -f "$admin_compose_file")
|
||||
"${admin_compose[@]}" config --quiet
|
||||
"${admin_compose[@]}" up -d --force-recreate admin-backend
|
||||
printf 'Admin bootstrap password cleared from compose/.env and backend container environment.\n'
|
||||
Executable
+97
@@ -0,0 +1,97 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
env_file="${1:-compose/.env}"
|
||||
source_directory="${2:-/srv/ai-soulmate/sources/mlkj-admin}"
|
||||
public_host="${3:-}"
|
||||
|
||||
for required_command in awk git mktemp openssl; do
|
||||
if ! command -v "$required_command" >/dev/null 2>&1; then
|
||||
printf 'Required command is unavailable: %s\n' "$required_command" >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
if [[ ! -r "$env_file" ]]; then
|
||||
printf 'Environment file not found: %s\n' "$env_file" >&2
|
||||
exit 1
|
||||
fi
|
||||
if [[ ! -d "$source_directory/.git" ]]; then
|
||||
printf 'mlkj-admin source checkout not found: %s\n' "$source_directory" >&2
|
||||
exit 1
|
||||
fi
|
||||
if [[ -z "$public_host" || ! "$public_host" =~ ^[A-Za-z0-9.-]+$ ]]; then
|
||||
printf 'Provide the approved LAN IPv4 address or DNS name as argument 3.\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
source_branch="$(git -C "$source_directory" branch --show-current)"
|
||||
source_commit="$(git -C "$source_directory" rev-parse HEAD)"
|
||||
source_short_commit="${source_commit:0:12}"
|
||||
if [[ "$source_branch" != "local-deployment" ]]; then
|
||||
printf 'Expected mlkj-admin branch local-deployment, found: %s\n' "$source_branch" >&2
|
||||
exit 1
|
||||
fi
|
||||
if [[ -n "$(git -C "$source_directory" status --short --untracked-files=no)" ]]; then
|
||||
printf 'Tracked mlkj-admin source changes are present.\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
env_value() {
|
||||
awk -F= -v key="$1" '$1 == key {sub(/^[^=]*=/, ""); print; exit}' "$env_file"
|
||||
}
|
||||
|
||||
set_value() {
|
||||
local key="$1"
|
||||
local value="$2"
|
||||
local temporary_file
|
||||
temporary_file="$(mktemp "${env_file}.tmp.XXXXXX")"
|
||||
awk -v key="$key" -v value="$value" '
|
||||
BEGIN { prefix = key "="; found = 0 }
|
||||
index($0, prefix) == 1 {
|
||||
if (!found) print prefix value
|
||||
found = 1
|
||||
next
|
||||
}
|
||||
{ print }
|
||||
END { if (!found) print prefix value }
|
||||
' "$env_file" > "$temporary_file"
|
||||
chmod 600 "$temporary_file"
|
||||
mv -f "$temporary_file" "$env_file"
|
||||
}
|
||||
|
||||
ensure_secret() {
|
||||
if [[ -z "$(env_value "$1")" ]]; then
|
||||
set_value "$1" "$(openssl rand -hex 32)"
|
||||
fi
|
||||
}
|
||||
|
||||
set_value ADMIN_COMPOSE_PROJECT_NAME ai-soulmate-admin
|
||||
set_value ADMIN_BACKEND_IMAGE "ai-soulmate-admin-backend:${source_short_commit}"
|
||||
set_value ADMIN_FRONTEND_IMAGE "ai-soulmate-admin-frontend:${source_short_commit}"
|
||||
set_value ADMIN_POSTGRES_DB mlkj_admin
|
||||
set_value ADMIN_BACKEND_BIND_ADDRESS 127.0.0.1
|
||||
set_value ADMIN_BACKEND_HOST_PORT 8088
|
||||
set_value ADMIN_FRONTEND_BIND_ADDRESS "$public_host"
|
||||
set_value ADMIN_FRONTEND_HOST_PORT 8081
|
||||
set_value ADMIN_BACKEND_MEMORY_LIMIT 2g
|
||||
set_value ADMIN_BACKEND_CPU_LIMIT 1.5
|
||||
set_value ADMIN_FRONTEND_MEMORY_LIMIT 512m
|
||||
set_value ADMIN_FRONTEND_CPU_LIMIT 0.5
|
||||
if [[ -z "$(env_value ADMIN_INITIAL_USERNAME)" ]]; then
|
||||
set_value ADMIN_INITIAL_USERNAME localadmin
|
||||
fi
|
||||
if ! grep -q '^ADMIN_INITIAL_PASSWORD=' "$env_file"; then
|
||||
set_value ADMIN_INITIAL_PASSWORD ""
|
||||
fi
|
||||
ensure_secret ADMIN_JWT_SECRET
|
||||
set_value ADMIN_MAVEN_IMAGE maven:3.9-eclipse-temurin-21
|
||||
set_value ADMIN_JRE_IMAGE eclipse-temurin:21-jre
|
||||
set_value ADMIN_NODE_IMAGE node:22-bookworm-slim
|
||||
|
||||
chmod 600 "$env_file"
|
||||
printf 'Admin environment configured without displaying secrets.\n'
|
||||
printf 'Source commit: %s\n' "$source_commit"
|
||||
printf 'Admin backend image: ai-soulmate-admin-backend:%s\n' "$source_short_commit"
|
||||
printf 'Admin frontend image: ai-soulmate-admin-frontend:%s\n' "$source_short_commit"
|
||||
printf 'Admin frontend bind: %s:8081\n' "$public_host"
|
||||
printf 'Admin backend remains bound to 127.0.0.1:8088.\n'
|
||||
@@ -66,6 +66,25 @@ FRONTEND_HOST_PORT=8080
|
||||
FRONTEND_MEMORY_LIMIT=1g
|
||||
FRONTEND_CPU_LIMIT=1.0
|
||||
|
||||
ADMIN_COMPOSE_PROJECT_NAME=ai-soulmate-admin
|
||||
ADMIN_BACKEND_IMAGE=ai-soulmate-admin-backend:local-deployment
|
||||
ADMIN_FRONTEND_IMAGE=ai-soulmate-admin-frontend:local-deployment
|
||||
ADMIN_POSTGRES_DB=mlkj_admin
|
||||
ADMIN_BACKEND_BIND_ADDRESS=127.0.0.1
|
||||
ADMIN_BACKEND_HOST_PORT=8088
|
||||
ADMIN_FRONTEND_BIND_ADDRESS=127.0.0.1
|
||||
ADMIN_FRONTEND_HOST_PORT=8081
|
||||
ADMIN_BACKEND_MEMORY_LIMIT=2g
|
||||
ADMIN_BACKEND_CPU_LIMIT=1.5
|
||||
ADMIN_FRONTEND_MEMORY_LIMIT=512m
|
||||
ADMIN_FRONTEND_CPU_LIMIT=0.5
|
||||
ADMIN_INITIAL_USERNAME=localadmin
|
||||
ADMIN_INITIAL_PASSWORD=
|
||||
ADMIN_JWT_SECRET=$(random_secret)
|
||||
ADMIN_MAVEN_IMAGE=maven:3.9-eclipse-temurin-21
|
||||
ADMIN_JRE_IMAGE=eclipse-temurin:21-jre
|
||||
ADMIN_NODE_IMAGE=node:22-bookworm-slim
|
||||
|
||||
APP_ENV=production
|
||||
SECRET_KEY=$(random_secret)
|
||||
ENCRYPTION_KEY=$(random_secret)
|
||||
|
||||
Executable
+86
@@ -0,0 +1,86 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
env_file="${1:-compose/.env}"
|
||||
source_directory="${2:-/srv/ai-soulmate/sources/mlkj-admin}"
|
||||
compose_file="${COMPOSE_FILE:-compose/docker-compose.infra.yml}"
|
||||
|
||||
for required_command in awk docker git; do
|
||||
if ! command -v "$required_command" >/dev/null 2>&1; then
|
||||
printf 'Required command is unavailable: %s\n' "$required_command" >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
if [[ ! -r "$env_file" ]]; then
|
||||
printf 'Environment file not found: %s\n' "$env_file" >&2
|
||||
exit 1
|
||||
fi
|
||||
if [[ ! -d "$source_directory/.git" ]]; then
|
||||
printf 'mlkj-admin source checkout not found: %s\n' "$source_directory" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
env_value() {
|
||||
awk -F= -v key="$1" '$1 == key {sub(/^[^=]*=/, ""); print; exit}' "$env_file"
|
||||
}
|
||||
|
||||
postgres_user="$(env_value POSTGRES_USER)"
|
||||
admin_database="$(env_value ADMIN_POSTGRES_DB)"
|
||||
source_commit="$(git -C "$source_directory" rev-parse HEAD)"
|
||||
if [[ ! "$admin_database" =~ ^[A-Za-z][A-Za-z0-9_]{0,62}$ ]]; then
|
||||
printf 'ADMIN_POSTGRES_DB is not a safe PostgreSQL identifier.\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
if [[ "$(git -C "$source_directory" branch --show-current)" != "local-deployment" ]]; then
|
||||
printf 'Expected mlkj-admin branch local-deployment.\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
schema_files=(
|
||||
"$source_directory/db/cloud/02_create_poly_admin_schema.sql"
|
||||
"$source_directory/db/cloud/04_add_single_login_session.sql"
|
||||
"$source_directory/db/cloud/05_create_poly_traffic_analytics.sql"
|
||||
"$source_directory/db/cloud/06_backfill_poly_traffic_request_uri.sql"
|
||||
)
|
||||
for schema_file in "${schema_files[@]}"; do
|
||||
if [[ ! -r "$schema_file" ]]; then
|
||||
printf 'Admin schema file is missing: %s\n' "$schema_file" >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
compose_command=(docker compose --env-file "$env_file" -f "$compose_file")
|
||||
"${compose_command[@]}" config --quiet
|
||||
if [[ -z "$("${compose_command[@]}" ps -q postgres)" ]]; then
|
||||
printf 'PostgreSQL is not running.\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
database_exists="$("${compose_command[@]}" exec -T postgres psql \
|
||||
--username "$postgres_user" --dbname postgres --tuples-only --no-align \
|
||||
--command "SELECT 1 FROM pg_database WHERE datname = '$admin_database'")"
|
||||
if [[ "$database_exists" != "1" ]]; then
|
||||
"${compose_command[@]}" exec -T postgres createdb \
|
||||
--username "$postgres_user" --owner "$postgres_user" --encoding UTF8 "$admin_database"
|
||||
fi
|
||||
|
||||
for schema_file in "${schema_files[@]}"; do
|
||||
printf 'Applying admin schema: %s\n' "$(basename "$schema_file")"
|
||||
"${compose_command[@]}" exec -T postgres psql \
|
||||
--username "$postgres_user" --dbname "$admin_database" \
|
||||
--set ON_ERROR_STOP=1 < "$schema_file"
|
||||
done
|
||||
|
||||
"${compose_command[@]}" exec -T postgres psql \
|
||||
--username "$postgres_user" --dbname "$admin_database" \
|
||||
--set ON_ERROR_STOP=1 --set source_commit="$source_commit" <<'SQL'
|
||||
CREATE TABLE IF NOT EXISTS local_deployment_schema_history (
|
||||
source_commit text PRIMARY KEY,
|
||||
applied_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
INSERT INTO local_deployment_schema_history(source_commit)
|
||||
VALUES (:'source_commit')
|
||||
ON CONFLICT (source_commit) DO NOTHING;
|
||||
SQL
|
||||
|
||||
printf 'Local admin database initialized: %s at source %s\n' "$admin_database" "$source_commit"
|
||||
Executable
+58
@@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
env_file="${1:-compose/.env}"
|
||||
if [[ ! -r "$env_file" ]]; then
|
||||
printf 'Environment file not found: %s\n' "$env_file" >&2
|
||||
exit 1
|
||||
fi
|
||||
if [[ ! -t 0 ]]; then
|
||||
printf 'Run this script from an interactive terminal.\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
current_username="$(awk -F= '$1 == "ADMIN_INITIAL_USERNAME" {sub(/^[^=]*=/, ""); print; exit}' "$env_file")"
|
||||
read -r -p "Initial admin username [${current_username:-localadmin}]: " username
|
||||
username="${username:-${current_username:-localadmin}}"
|
||||
if [[ ! "$username" =~ ^[A-Za-z0-9_.-]{3,80}$ ]]; then
|
||||
printf 'Username must be 3-80 characters using letters, numbers, dot, underscore, or hyphen.\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
read -r -s -p 'Initial admin password (at least 16 letters/numbers/_/-): ' password
|
||||
printf '\n'
|
||||
read -r -s -p 'Confirm initial admin password: ' password_confirmation
|
||||
printf '\n'
|
||||
if [[ "$password" != "$password_confirmation" ]]; then
|
||||
printf 'Passwords do not match.\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
if [[ ${#password} -lt 16 || ! "$password" =~ ^[A-Za-z0-9_-]+$ ]]; then
|
||||
printf 'Password must be at least 16 characters using only letters, numbers, underscore, or hyphen.\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
set_value() {
|
||||
local key="$1"
|
||||
local value="$2"
|
||||
local temporary_file
|
||||
temporary_file="$(mktemp "${env_file}.tmp.XXXXXX")"
|
||||
awk -v key="$key" -v value="$value" '
|
||||
BEGIN { prefix = key "="; found = 0 }
|
||||
index($0, prefix) == 1 {
|
||||
if (!found) print prefix value
|
||||
found = 1
|
||||
next
|
||||
}
|
||||
{ print }
|
||||
END { if (!found) print prefix value }
|
||||
' "$env_file" > "$temporary_file"
|
||||
chmod 600 "$temporary_file"
|
||||
mv -f "$temporary_file" "$env_file"
|
||||
}
|
||||
|
||||
set_value ADMIN_INITIAL_USERNAME "$username"
|
||||
set_value ADMIN_INITIAL_PASSWORD "$password"
|
||||
unset password password_confirmation
|
||||
chmod 600 "$env_file"
|
||||
printf 'Initial admin credentials stored without displaying the password.\n'
|
||||
Executable
+40
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
env_file="${1:-compose/.env}"
|
||||
compose_file="${ADMIN_COMPOSE_FILE:-compose/docker-compose.admin.yml}"
|
||||
infra_compose_file="${COMPOSE_FILE:-compose/docker-compose.infra.yml}"
|
||||
|
||||
if [[ ! -r "$env_file" ]]; then
|
||||
printf 'Environment file not found: %s\n' "$env_file" >&2
|
||||
exit 1
|
||||
fi
|
||||
env_value() {
|
||||
awk -F= -v key="$1" '$1 == key {sub(/^[^=]*=/, ""); print; exit}' "$env_file"
|
||||
}
|
||||
|
||||
project_name="$(env_value ADMIN_COMPOSE_PROJECT_NAME)"
|
||||
backend_image="$(env_value ADMIN_BACKEND_IMAGE)"
|
||||
frontend_image="$(env_value ADMIN_FRONTEND_IMAGE)"
|
||||
if [[ -z "$(env_value ADMIN_INITIAL_PASSWORD)" ]]; then
|
||||
infra_compose=(docker compose --env-file "$env_file" -f "$infra_compose_file")
|
||||
super_admin_count="$("${infra_compose[@]}" exec -T postgres psql \
|
||||
--username "$(env_value POSTGRES_USER)" --dbname "$(env_value ADMIN_POSTGRES_DB)" \
|
||||
--tuples-only --no-align \
|
||||
--command "SELECT count(*) FROM poly_admin_users WHERE role_type = 'SUPER_ADMIN'" 2>/dev/null || true)"
|
||||
if [[ "$super_admin_count" != "1" ]]; then
|
||||
printf 'Initial admin password is unset and no SUPER_ADMIN exists; run scripts/set-admin-credentials.sh first.\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
for image in "$backend_image" "$frontend_image"; do
|
||||
if [[ -z "$image" ]] || ! docker image inspect "$image" >/dev/null 2>&1; then
|
||||
printf 'Configured admin image is unavailable: %s\n' "$image" >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
compose_command=(docker compose --project-name "$project_name" --env-file "$env_file" -f "$compose_file")
|
||||
"${compose_command[@]}" config --quiet
|
||||
"${compose_command[@]}" up -d
|
||||
"${compose_command[@]}" ps --all
|
||||
Executable
+70
@@ -0,0 +1,70 @@
|
||||
#!/usr/bin/env bash
|
||||
set -Eeuo pipefail
|
||||
|
||||
env_file="${1:-compose/.env}"
|
||||
admin_compose_file="${ADMIN_COMPOSE_FILE:-compose/docker-compose.admin.yml}"
|
||||
infra_compose_file="${COMPOSE_FILE:-compose/docker-compose.infra.yml}"
|
||||
|
||||
for required_command in awk curl docker jq; do
|
||||
if ! command -v "$required_command" >/dev/null 2>&1; then
|
||||
printf 'Required command is unavailable: %s\n' "$required_command" >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
env_value() {
|
||||
awk -F= -v key="$1" '$1 == key {sub(/^[^=]*=/, ""); print; exit}' "$env_file"
|
||||
}
|
||||
|
||||
project_name="$(env_value ADMIN_COMPOSE_PROJECT_NAME)"
|
||||
backend_url="http://$(env_value ADMIN_BACKEND_BIND_ADDRESS):$(env_value ADMIN_BACKEND_HOST_PORT)"
|
||||
frontend_url="http://$(env_value ADMIN_FRONTEND_BIND_ADDRESS):$(env_value ADMIN_FRONTEND_HOST_PORT)"
|
||||
admin_database="$(env_value ADMIN_POSTGRES_DB)"
|
||||
postgres_user="$(env_value POSTGRES_USER)"
|
||||
admin_compose=(docker compose --project-name "$project_name" --env-file "$env_file" -f "$admin_compose_file")
|
||||
infra_compose=(docker compose --env-file "$env_file" -f "$infra_compose_file")
|
||||
"${admin_compose[@]}" config --quiet
|
||||
|
||||
backend_health=""
|
||||
frontend_health=""
|
||||
for _attempt in $(seq 1 24); do
|
||||
backend_health="$(curl --fail --silent --show-error --max-time 5 "$backend_url/actuator/health" 2>/dev/null || true)"
|
||||
frontend_health="$(curl --fail --silent --show-error --max-time 5 "$frontend_url/actuator/health" 2>/dev/null || true)"
|
||||
if printf '%s' "$backend_health" | jq -e '.status == "UP"' >/dev/null 2>&1 \
|
||||
&& printf '%s' "$frontend_health" | jq -e '.status == "UP"' >/dev/null 2>&1; then
|
||||
break
|
||||
fi
|
||||
backend_health=""
|
||||
frontend_health=""
|
||||
sleep 5
|
||||
done
|
||||
if [[ -z "$backend_health" || -z "$frontend_health" ]]; then
|
||||
printf 'Admin health checks did not become ready.\n' >&2
|
||||
"${admin_compose[@]}" ps >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
index_html="$(curl --fail --silent --show-error --max-time 5 "$frontend_url/")"
|
||||
module_config="$(curl --fail --silent --show-error --max-time 5 "$frontend_url/assets/module-config.json")"
|
||||
if ! printf '%s' "$index_html" | grep -q 'id="app"'; then
|
||||
printf 'Admin frontend index is invalid.\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
if [[ "$(printf '%s' "$module_config" | jq -r '[.modules[].code] | join(",")')" != "AI_SOULMATE,CONSOLE" ]]; then
|
||||
printf 'Admin frontend is not using the AI SoulMate local module configuration.\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
super_admin_count="$("${infra_compose[@]}" exec -T postgres psql \
|
||||
--username "$postgres_user" --dbname "$admin_database" --tuples-only --no-align \
|
||||
--command "SELECT count(*) FROM poly_admin_users WHERE role_type = 'SUPER_ADMIN'")"
|
||||
if [[ "$super_admin_count" != "1" ]]; then
|
||||
printf 'Expected exactly one local SUPER_ADMIN, found: %s\n' "$super_admin_count" >&2
|
||||
exit 1
|
||||
fi
|
||||
"${infra_compose[@]}" exec -T postgres psql \
|
||||
--username "$postgres_user" --dbname "$(env_value POSTGRES_DB)" --tuples-only --no-align \
|
||||
--command "SELECT count(*) FROM pg_tables WHERE schemaname = 'public'" | grep -Eq '^[1-9][0-9]*$'
|
||||
|
||||
printf '%s\n' "$frontend_health" | jq .
|
||||
"${admin_compose[@]}" ps
|
||||
printf 'AI SoulMate local admin, local PostgreSQL, and MinIO configuration checks passed.\n'
|
||||
@@ -48,29 +48,43 @@ postgres_user="$(env_value POSTGRES_USER)"
|
||||
sha256sum --check checksums.sha256
|
||||
)
|
||||
|
||||
restore_database="restore_check_$(date +%s)_$$"
|
||||
restore_databases=()
|
||||
cleanup_postgres() {
|
||||
"${compose_command[@]}" exec -T postgres dropdb \
|
||||
--if-exists --username "$postgres_user" "$restore_database" >/dev/null 2>&1 || true
|
||||
local restore_database
|
||||
for restore_database in "${restore_databases[@]}"; do
|
||||
"${compose_command[@]}" exec -T postgres dropdb \
|
||||
--if-exists --username "$postgres_user" "$restore_database" >/dev/null 2>&1 || true
|
||||
done
|
||||
}
|
||||
trap cleanup_postgres EXIT
|
||||
|
||||
"${compose_command[@]}" exec -T postgres createdb \
|
||||
--username "$postgres_user" "$restore_database"
|
||||
"${compose_command[@]}" exec -T postgres pg_restore \
|
||||
--username "$postgres_user" \
|
||||
--dbname "$restore_database" \
|
||||
--no-owner \
|
||||
--no-acl \
|
||||
--exit-on-error \
|
||||
< "$backup_directory/postgres.dump"
|
||||
"${compose_command[@]}" exec -T postgres psql \
|
||||
--username "$postgres_user" \
|
||||
--dbname "$restore_database" \
|
||||
--tuples-only \
|
||||
--no-align \
|
||||
--command 'SELECT 1' \
|
||||
| grep -qx '1'
|
||||
verify_postgres_dump() {
|
||||
local dump_path="$1"
|
||||
local suffix="$2"
|
||||
local restore_database="restore_check_${suffix}_$(date +%s)_$$"
|
||||
restore_databases+=("$restore_database")
|
||||
"${compose_command[@]}" exec -T postgres createdb \
|
||||
--username "$postgres_user" "$restore_database"
|
||||
"${compose_command[@]}" exec -T postgres pg_restore \
|
||||
--username "$postgres_user" \
|
||||
--dbname "$restore_database" \
|
||||
--no-owner \
|
||||
--no-acl \
|
||||
--exit-on-error \
|
||||
< "$dump_path"
|
||||
"${compose_command[@]}" exec -T postgres psql \
|
||||
--username "$postgres_user" \
|
||||
--dbname "$restore_database" \
|
||||
--tuples-only \
|
||||
--no-align \
|
||||
--command 'SELECT 1' \
|
||||
| grep -qx '1'
|
||||
}
|
||||
|
||||
verify_postgres_dump "$backup_directory/postgres.dump" primary
|
||||
if [[ -f "$backup_directory/admin-postgres.dump" ]]; then
|
||||
verify_postgres_dump "$backup_directory/admin-postgres.dump" admin
|
||||
fi
|
||||
cleanup_postgres
|
||||
trap - EXIT
|
||||
|
||||
|
||||
Reference in new issue
Block a user