Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4c01cf6ffe | ||
|
|
66333fb5dd | ||
|
|
06f809b4c8 | ||
|
|
343c889250 | ||
|
|
1b57dcc850 | ||
|
|
77d750992d |
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "dhv2-api",
|
"name": "dhv2-api",
|
||||||
"version": "0.21.0-1",
|
"version": "0.21.0-2",
|
||||||
"private": true,
|
"private": true,
|
||||||
"license": "UNLICENSED",
|
"license": "UNLICENSED",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -0,0 +1,173 @@
|
|||||||
|
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* One-time production reset requested before the first clean Android rollout.
|
||||||
|
*
|
||||||
|
* Keeps only structural product configuration plus the single `admin` account.
|
||||||
|
* Operational/business data is removed. Recovery is intentionally performed
|
||||||
|
* from the deploy PRE backup, not through a synthetic down migration.
|
||||||
|
*/
|
||||||
|
export class ResetProductionOperationalData1788652800000 implements MigrationInterface {
|
||||||
|
name = 'ResetProductionOperationalData1788652800000';
|
||||||
|
|
||||||
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
const adminRows: Array<{ id: string; username: string }> = await queryRunner.query(`
|
||||||
|
SELECT id, username
|
||||||
|
FROM users
|
||||||
|
WHERE lower(trim(username)) = 'admin'
|
||||||
|
ORDER BY id
|
||||||
|
`);
|
||||||
|
|
||||||
|
if (adminRows.length !== 1) {
|
||||||
|
throw new Error(
|
||||||
|
`Production reset aborted: expected exactly one username admin, found ${adminRows.length}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const adminId = adminRows[0].id;
|
||||||
|
|
||||||
|
const structuralTables = [
|
||||||
|
'roles',
|
||||||
|
'permissions',
|
||||||
|
'role_permissions',
|
||||||
|
'asset_types',
|
||||||
|
'asset_attribute_definitions',
|
||||||
|
'asset_type_parent_rules',
|
||||||
|
'finding_categories',
|
||||||
|
'finding_catalog_items',
|
||||||
|
'finding_catalog_item_asset_types',
|
||||||
|
'finding_catalog_asset_type_profiles',
|
||||||
|
];
|
||||||
|
|
||||||
|
// Snapshot structural row counts so TRUNCATE ... CASCADE can never silently
|
||||||
|
// remove product configuration while still leaving operational tables empty.
|
||||||
|
const structuralCounts = new Map<string, string>();
|
||||||
|
for (const table of structuralTables) {
|
||||||
|
const safeTable = `"${table.replace(/"/g, '""')}"`;
|
||||||
|
const rows: Array<{ total: string }> = await queryRunner.query(
|
||||||
|
`SELECT count(*)::text AS total FROM ${safeTable}`,
|
||||||
|
);
|
||||||
|
structuralCounts.set(table, rows[0]?.total ?? '0');
|
||||||
|
}
|
||||||
|
|
||||||
|
const adminRolesBefore: Array<{ total: string }> = await queryRunner.query(
|
||||||
|
`SELECT count(*)::text AS total FROM user_roles WHERE user_id = $1`,
|
||||||
|
[adminId],
|
||||||
|
);
|
||||||
|
const adminRoleCount = adminRolesBefore[0]?.total ?? '0';
|
||||||
|
if (adminRoleCount === '0') {
|
||||||
|
throw new Error('Production reset aborted: admin has no assigned role');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Product configuration that must survive a clean operational start.
|
||||||
|
const preservedTables = new Set([
|
||||||
|
'typeorm_migrations',
|
||||||
|
'users',
|
||||||
|
'user_roles',
|
||||||
|
...structuralTables,
|
||||||
|
]);
|
||||||
|
|
||||||
|
const tableRows: Array<{ table_name: string }> = await queryRunner.query(`
|
||||||
|
SELECT table_name
|
||||||
|
FROM information_schema.tables
|
||||||
|
WHERE table_schema = 'public'
|
||||||
|
AND table_type = 'BASE TABLE'
|
||||||
|
ORDER BY table_name
|
||||||
|
`);
|
||||||
|
|
||||||
|
const operationalTables = tableRows
|
||||||
|
.map((row) => row.table_name)
|
||||||
|
.filter((table) => !preservedTables.has(table));
|
||||||
|
|
||||||
|
if (operationalTables.length > 0) {
|
||||||
|
const quoted = operationalTables
|
||||||
|
.map((table) => `"${table.replace(/"/g, '""')}"`)
|
||||||
|
.join(', ');
|
||||||
|
await queryRunner.query(`TRUNCATE TABLE ${quoted} RESTART IDENTITY CASCADE`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove every user except the explicitly validated administrator.
|
||||||
|
// user_roles for removed users follow their FK cascade.
|
||||||
|
await queryRunner.query(`DELETE FROM users WHERE id <> $1`, [adminId]);
|
||||||
|
|
||||||
|
// A reset must invalidate every prior login token, including admin's.
|
||||||
|
// auth_sessions is operational and was truncated above; admin simply logs in again.
|
||||||
|
await queryRunner.query(
|
||||||
|
`
|
||||||
|
UPDATE users
|
||||||
|
SET failed_login_attempts = 0,
|
||||||
|
locked_until = NULL,
|
||||||
|
last_login_at = NULL,
|
||||||
|
updated_at = CURRENT_TIMESTAMP
|
||||||
|
WHERE id = $1
|
||||||
|
`,
|
||||||
|
[adminId],
|
||||||
|
);
|
||||||
|
|
||||||
|
const finalUsers: Array<{ total: string; admins: string }> = await queryRunner.query(`
|
||||||
|
SELECT
|
||||||
|
count(*)::text AS total,
|
||||||
|
count(*) FILTER (WHERE lower(trim(username)) = 'admin')::text AS admins
|
||||||
|
FROM users
|
||||||
|
`);
|
||||||
|
|
||||||
|
if (finalUsers[0]?.total !== '1' || finalUsers[0]?.admins !== '1') {
|
||||||
|
throw new Error('Production reset verification failed: users table is not admin-only');
|
||||||
|
}
|
||||||
|
|
||||||
|
const finalAdminRoles: Array<{ total: string; foreign_users: string }> = await queryRunner.query(
|
||||||
|
`
|
||||||
|
SELECT
|
||||||
|
count(*) FILTER (WHERE user_id = $1)::text AS total,
|
||||||
|
count(*) FILTER (WHERE user_id <> $1)::text AS foreign_users
|
||||||
|
FROM user_roles
|
||||||
|
`,
|
||||||
|
[adminId],
|
||||||
|
);
|
||||||
|
|
||||||
|
if (
|
||||||
|
finalAdminRoles[0]?.total !== adminRoleCount ||
|
||||||
|
finalAdminRoles[0]?.foreign_users !== '0'
|
||||||
|
) {
|
||||||
|
throw new Error('Production reset verification failed: admin role assignments changed');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assert every structural table kept exactly the same number of rows.
|
||||||
|
for (const table of structuralTables) {
|
||||||
|
const safeTable = `"${table.replace(/"/g, '""')}"`;
|
||||||
|
const rows: Array<{ total: string }> = await queryRunner.query(
|
||||||
|
`SELECT count(*)::text AS total FROM ${safeTable}`,
|
||||||
|
);
|
||||||
|
const before = structuralCounts.get(table) ?? '0';
|
||||||
|
if (rows[0]?.total !== before) {
|
||||||
|
throw new Error(
|
||||||
|
`Production reset verification failed: structural table ${table} changed (${before} -> ${rows[0]?.total ?? 'unknown'})`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assert that every operational table is empty. This makes the migration
|
||||||
|
// fail atomically if a table was repopulated during the reset transaction.
|
||||||
|
for (const table of operationalTables) {
|
||||||
|
const safeTable = `"${table.replace(/"/g, '""')}"`;
|
||||||
|
const rows: Array<{ total: string }> = await queryRunner.query(
|
||||||
|
`SELECT count(*)::text AS total FROM ${safeTable}`,
|
||||||
|
);
|
||||||
|
if (rows[0]?.total !== '0') {
|
||||||
|
throw new Error(`Production reset verification failed: ${table} is not empty`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keep a concise server-side record in the migration log for deploy diagnostics.
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.log(
|
||||||
|
`[production-reset] kept admin=${adminRows[0].username} (${adminId}); preserved ${structuralTables.length} structural tables; cleared ${operationalTables.length} operational tables`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async down(): Promise<void> {
|
||||||
|
throw new Error(
|
||||||
|
'ResetProductionOperationalData is irreversible by migration; restore the deploy PRE database backup instead.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,2 +1,2 @@
|
|||||||
export const API_VERSION = '0.21.0-1';
|
export const API_VERSION = '0.21.0-2';
|
||||||
export const API_PHASE = 'F2.1';
|
export const API_PHASE = 'F2.1';
|
||||||
|
|||||||
+259
-70
@@ -3,112 +3,301 @@ set -Eeuo pipefail
|
|||||||
|
|
||||||
APP="/var/www/dhv2.korexlabs.com"
|
APP="/var/www/dhv2.korexlabs.com"
|
||||||
KEY="/root/.ssh/dhv2_github"
|
KEY="/root/.ssh/dhv2_github"
|
||||||
|
BACKUP_ROOT="/root/DH_V2_BACKUPS"
|
||||||
DEPLOY_REF="${DHV2_DEPLOY_REF:-deploy}"
|
DEPLOY_REF="${DHV2_DEPLOY_REF:-deploy}"
|
||||||
LOG="$(mktemp /tmp/dhv2-android-discovery.XXXXXX.log)"
|
STAMP="$(date +%Y%m%d_%H%M%S)"
|
||||||
STATUS="$(mktemp /tmp/dhv2-android-discovery-status.XXXXXX)"
|
BACKUP="$BACKUP_ROOT/GITHUB_DEPLOY_${STAMP}"
|
||||||
|
STAGE="/root/dhv2-github-stage-${STAMP}"
|
||||||
|
LOG="/tmp/dhv2-github-deploy-${STAMP}.log"
|
||||||
|
API_TEST_IMAGE="dhv2-api:github-${STAMP}"
|
||||||
|
WEB_TEST_IMAGE="dhv2-web:github-${STAMP}"
|
||||||
|
PHASE="bootstrap"
|
||||||
|
PREV_SHA=""
|
||||||
|
TARGET_SHA=""
|
||||||
|
EXPECTED_API_VERSION=""
|
||||||
|
EXPECTED_WEB_VERSION=""
|
||||||
|
APP_TOUCHED=0
|
||||||
|
|
||||||
cd "$APP"
|
cd "$APP"
|
||||||
export GIT_SSH_COMMAND="ssh -i $KEY -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new"
|
export GIT_SSH_COMMAND="ssh -i $KEY -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new"
|
||||||
exec > >(tee -a "$LOG") 2>&1
|
exec > >(tee -a "$LOG") 2>&1
|
||||||
|
|
||||||
|
cleanup() {
|
||||||
|
set +e
|
||||||
|
git worktree remove --force "$STAGE" >/dev/null 2>&1 || true
|
||||||
|
rm -rf "$STAGE"
|
||||||
|
docker image rm "$API_TEST_IMAGE" "$WEB_TEST_IMAGE" >/dev/null 2>&1 || true
|
||||||
|
}
|
||||||
|
|
||||||
publish_status() {
|
publish_status() {
|
||||||
local rc="${1:-1}"
|
local rc="${1:-1}"
|
||||||
set +e
|
set +e
|
||||||
|
|
||||||
local outcome="failure"
|
local outcome="failure"
|
||||||
[ "$rc" -eq 0 ] && outcome="success"
|
[ "$rc" -eq 0 ] && outcome="success"
|
||||||
local current target status_blob log_blob tree commit
|
local current="unknown"
|
||||||
current="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
|
current="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
|
||||||
target="$(git rev-parse origin/$DEPLOY_REF 2>/dev/null || echo unknown)"
|
local status_file log_file status_blob log_blob tree commit
|
||||||
|
|
||||||
|
status_file="$(mktemp /tmp/dhv2-status.XXXXXX)"
|
||||||
|
log_file="$(mktemp /tmp/dhv2-log.XXXXXX)"
|
||||||
|
|
||||||
{
|
{
|
||||||
echo "status=$outcome"
|
echo "status=$outcome"
|
||||||
echo "exit_code=$rc"
|
echo "exit_code=$rc"
|
||||||
echo "phase=android-source-discovery"
|
echo "phase=$PHASE"
|
||||||
echo "timestamp=$(date --iso-8601=seconds)"
|
echo "timestamp=$(date --iso-8601=seconds)"
|
||||||
echo "deploy_ref=$DEPLOY_REF"
|
echo "deploy_ref=$DEPLOY_REF"
|
||||||
echo "target_sha=$target"
|
echo "previous_sha=${PREV_SHA:-unknown}"
|
||||||
|
echo "target_sha=${TARGET_SHA:-unknown}"
|
||||||
echo "current_sha=$current"
|
echo "current_sha=$current"
|
||||||
echo "app_touched=0"
|
echo "api_version=${EXPECTED_API_VERSION:-unknown}"
|
||||||
echo "backup=not-required-read-only"
|
echo "web_version=${EXPECTED_WEB_VERSION:-unknown}"
|
||||||
} > "$STATUS"
|
echo "app_touched=$APP_TOUCHED"
|
||||||
status_blob="$(git hash-object -w "$STATUS" 2>/dev/null || true)"
|
echo "backup=${BACKUP:-unknown}"
|
||||||
log_blob="$(git hash-object -w "$LOG" 2>/dev/null || true)"
|
} > "$status_file"
|
||||||
|
|
||||||
|
tail -n 500 "$LOG" > "$log_file" 2>/dev/null || true
|
||||||
|
status_blob="$(git hash-object -w "$status_file" 2>/dev/null || true)"
|
||||||
|
log_blob="$(git hash-object -w "$log_file" 2>/dev/null || true)"
|
||||||
|
|
||||||
if [ -n "$status_blob" ] && [ -n "$log_blob" ]; then
|
if [ -n "$status_blob" ] && [ -n "$log_blob" ]; then
|
||||||
tree="$(printf '100644 blob %s\tdeploy.log\n100644 blob %s\tstatus.txt\n' "$log_blob" "$status_blob" | git mktree 2>/dev/null || true)"
|
tree="$(printf '100644 blob %s\tdeploy.log\n100644 blob %s\tstatus.txt\n' "$log_blob" "$status_blob" | git mktree 2>/dev/null || true)"
|
||||||
if [ -n "$tree" ]; then
|
if [ -n "$tree" ]; then
|
||||||
commit="$(printf 'deploy-status: %s · android-source-discovery\n' "$outcome" | git -c user.name='DH V2 Deploy Bot' -c user.email='deploy@dhv2.local' commit-tree "$tree" 2>/dev/null || true)"
|
commit="$(printf 'deploy-status: %s · phase %s\n' "$outcome" "$PHASE" | git -c user.name='DH V2 Deploy Bot' -c user.email='deploy@dhv2.local' commit-tree "$tree" 2>/dev/null || true)"
|
||||||
[ -z "$commit" ] || git push --force origin "$commit:refs/heads/deploy-status" >/dev/null 2>&1 || true
|
[ -z "$commit" ] || git push --force origin "$commit:refs/heads/deploy-status" >/dev/null 2>&1 || true
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
rm -f "$STATUS" "$LOG"
|
|
||||||
|
rm -f "$status_file" "$log_file"
|
||||||
}
|
}
|
||||||
trap 'rc=$?; trap - EXIT; publish_status "$rc"; exit "$rc"' EXIT
|
|
||||||
|
|
||||||
|
on_exit() {
|
||||||
|
local rc=$?
|
||||||
|
trap - EXIT ERR
|
||||||
|
cleanup
|
||||||
|
publish_status "$rc"
|
||||||
|
exit "$rc"
|
||||||
|
}
|
||||||
|
trap on_exit EXIT
|
||||||
|
|
||||||
|
rollback() {
|
||||||
|
local rc=$?
|
||||||
|
trap - ERR
|
||||||
|
PHASE="rollback"
|
||||||
|
|
||||||
|
echo
|
||||||
|
echo "============================================================"
|
||||||
|
echo " DH V2 · DEPLOY FALLÓ · ROLLBACK"
|
||||||
|
echo "============================================================"
|
||||||
|
|
||||||
|
cd "$APP"
|
||||||
|
if [ "$APP_TOUCHED" -eq 1 ] && [ -n "${PREV_SHA:-}" ]; then
|
||||||
|
echo "Restaurando aplicación al commit previo: $PREV_SHA"
|
||||||
|
git reset --hard "$PREV_SHA" || true
|
||||||
|
docker compose build api web </dev/null || true
|
||||||
|
docker compose up -d --no-deps --force-recreate api web </dev/null || true
|
||||||
|
else
|
||||||
|
echo "El candidato falló antes de modificar producción; no se reconstruye ni reinicia la aplicación activa."
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo
|
||||||
|
echo "Estado actual:"
|
||||||
|
docker compose ps -a </dev/null || true
|
||||||
|
|
||||||
|
if [ "$APP_TOUCHED" -eq 1 ]; then
|
||||||
|
echo
|
||||||
|
echo "Últimos logs:"
|
||||||
|
docker compose logs --tail=160 api web </dev/null || true
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo
|
||||||
|
if [ -d "$BACKUP" ]; then
|
||||||
|
echo "Backup PRE disponible en: $BACKUP"
|
||||||
|
echo "Las migraciones son forward-only; database-before.dump queda disponible para restauración manual si hiciera falta."
|
||||||
|
else
|
||||||
|
echo "No fue necesario crear backup PRE: el fallo ocurrió durante el preflight del candidato, antes de tocar producción."
|
||||||
|
fi
|
||||||
|
exit "$rc"
|
||||||
|
}
|
||||||
|
trap rollback ERR
|
||||||
|
|
||||||
|
echo
|
||||||
echo "============================================================"
|
echo "============================================================"
|
||||||
echo " DH V2 · ANDROID SOURCE DISCOVERY · SOLO LECTURA"
|
echo " DH V2 · DEPLOY DESDE GITHUB · $DEPLOY_REF"
|
||||||
echo "============================================================"
|
echo "============================================================"
|
||||||
|
|
||||||
|
for cmd in git docker curl tar node; do
|
||||||
|
command -v "$cmd" >/dev/null || { echo "ERROR: falta $cmd"; false; }
|
||||||
|
done
|
||||||
|
[ -f "$KEY" ] || { echo "ERROR: falta deploy key $KEY"; false; }
|
||||||
|
[ -d .git ] || { echo "ERROR: $APP no es repositorio Git"; false; }
|
||||||
|
[ -f .env ] || { echo "ERROR: falta $APP/.env"; false; }
|
||||||
|
|
||||||
|
git config --global --get-all safe.directory 2>/dev/null | grep -Fxq "$APP" || git config --global --add safe.directory "$APP"
|
||||||
|
|
||||||
|
if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
|
||||||
|
echo "ERROR: hay cambios locales versionados en producción."
|
||||||
|
git status --short
|
||||||
|
false
|
||||||
|
fi
|
||||||
|
|
||||||
|
PREV_SHA="$(git rev-parse HEAD)"
|
||||||
|
PHASE="fetch"
|
||||||
git fetch origin "$DEPLOY_REF"
|
git fetch origin "$DEPLOY_REF"
|
||||||
TARGET="$(git rev-parse "origin/$DEPLOY_REF")"
|
TARGET_SHA="$(git rev-parse "origin/$DEPLOY_REF")"
|
||||||
CURRENT="$(git rev-parse HEAD)"
|
|
||||||
echo "Current: $CURRENT"
|
echo "Actual: $PREV_SHA"
|
||||||
echo "Target: $TARGET"
|
echo "Objetivo: $TARGET_SHA"
|
||||||
git merge-base --is-ancestor "$CURRENT" "$TARGET"
|
|
||||||
|
if [ "$TARGET_SHA" = "$PREV_SHA" ]; then
|
||||||
|
echo "Producción ya está en el commit autorizado."
|
||||||
|
PHASE="complete"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if ! git merge-base --is-ancestor "$PREV_SHA" "$TARGET_SHA"; then
|
||||||
|
echo "ERROR: origin/$DEPLOY_REF no es fast-forward desde producción."
|
||||||
|
false
|
||||||
|
fi
|
||||||
|
|
||||||
|
PHASE="candidate-preflight"
|
||||||
|
rm -rf "$STAGE"
|
||||||
|
git worktree add --detach "$STAGE" "$TARGET_SHA" >/dev/null
|
||||||
|
|
||||||
|
EXPECTED_API_VERSION="$(node -p "require('$STAGE/api-v3/package.json').version")"
|
||||||
|
EXPECTED_WEB_VERSION="$(node -p "require('$STAGE/web-v2/package.json').version")"
|
||||||
|
|
||||||
|
echo "API candidata: $EXPECTED_API_VERSION"
|
||||||
|
echo "WEB candidata: $EXPECTED_WEB_VERSION"
|
||||||
|
|
||||||
|
docker compose --env-file "$APP/.env" -f "$STAGE/docker-compose.yml" config >/dev/null
|
||||||
|
|
||||||
|
while IFS= read -r -d '' script; do
|
||||||
|
bash -n "$script"
|
||||||
|
done < <(find "$STAGE/scripts" -type f -name '*.sh' -print0)
|
||||||
|
|
||||||
echo
|
echo
|
||||||
echo "========== PROYECTOS GRADLE / ANDROID =========="
|
echo "========== TEST API CANDIDATA =========="
|
||||||
for root in /root /var/www /home /tmp; do
|
docker build --target builder -t "$API_TEST_IMAGE" "$STAGE/api-v3" </dev/null
|
||||||
[ -d "$root" ] || continue
|
docker run --rm \
|
||||||
find "$root" -maxdepth 9 -type f \
|
-v "$STAGE/api-v3/test:/app/test:ro" \
|
||||||
\( -name gradlew -o -name settings.gradle -o -name settings.gradle.kts -o -name build.gradle -o -name build.gradle.kts \) \
|
-v "$STAGE/api-v3/tsconfig.test.json:/app/tsconfig.test.json:ro" \
|
||||||
-printf '%TY-%Tm-%Td %TH:%TM %10s %p\n' 2>/dev/null || true
|
"$API_TEST_IMAGE" npm test </dev/null
|
||||||
done | sort -r | head -300
|
|
||||||
|
|
||||||
echo
|
echo
|
||||||
echo "========== ZIP / APK / AAB RELACIONADOS =========="
|
echo "========== BUILD WEB CANDIDATA =========="
|
||||||
for root in /root /var/www /home /tmp; do
|
docker build -t "$WEB_TEST_IMAGE" "$STAGE/web-v2" </dev/null
|
||||||
[ -d "$root" ] || continue
|
|
||||||
find "$root" -maxdepth 10 -type f \
|
|
||||||
\( -iname '*android*.zip' -o -iname '*inspeccion*.zip' -o -iname '*dh*.apk' -o -iname '*inspeccion*.apk' -o -iname '*.aab' -o -iname '*E1.1*' -o -iname '*E1_1*' -o -iname '*E1.2*' -o -iname '*E1_2*' -o -iname '*F2.2*' -o -iname '*F2_2*' \) \
|
|
||||||
-printf '%TY-%Tm-%Td %TH:%TM %10s %p\n' 2>/dev/null || true
|
|
||||||
done | sort -r | head -300
|
|
||||||
|
|
||||||
|
PHASE="backup"
|
||||||
echo
|
echo
|
||||||
echo "========== VERSIONES ANDROID =========="
|
echo "========== BACKUP PRE =========="
|
||||||
for root in /root /var/www /home /tmp; do
|
install -d -m 700 "$BACKUP"
|
||||||
[ -d "$root" ] || continue
|
docker compose exec -T db sh -lc 'pg_dump -U "$POSTGRES_USER" -d "$POSTGRES_DB" -Fc' </dev/null > "$BACKUP/database-before.dump"
|
||||||
find "$root" -maxdepth 10 -type f \( -name build.gradle -o -name build.gradle.kts \) -print0 2>/dev/null || true
|
tar \
|
||||||
done | while IFS= read -r -d '' f; do
|
--exclude='./.git' \
|
||||||
if grep -Eq 'applicationId|versionCode|versionName|namespace' "$f" 2>/dev/null; then
|
--exclude='./.env' \
|
||||||
echo "----- $f -----"
|
--exclude='*/node_modules' \
|
||||||
grep -nE 'applicationId|namespace|versionCode|versionName' "$f" 2>/dev/null | head -30 || true
|
--exclude='*/dist' \
|
||||||
fi
|
--exclude='*.zip' \
|
||||||
done
|
--exclude='*.tar.gz' \
|
||||||
|
--exclude='*.tgz' \
|
||||||
|
-czf "$BACKUP/source-before.tar.gz" .
|
||||||
|
install -m 600 .env "$BACKUP/.env"
|
||||||
|
git rev-parse HEAD > "$BACKUP/previous.sha"
|
||||||
|
printf '%s\n' "$TARGET_SHA" > "$BACKUP/target.sha"
|
||||||
|
docker compose ps -a > "$BACKUP/docker-before.txt"
|
||||||
|
(
|
||||||
|
cd "$BACKUP"
|
||||||
|
sha256sum database-before.dump source-before.tar.gz .env previous.sha target.sha docker-before.txt > SHA256SUMS.txt
|
||||||
|
sha256sum -c SHA256SUMS.txt
|
||||||
|
)
|
||||||
|
chmod 600 "$BACKUP"/* "$BACKUP/.env" 2>/dev/null || true
|
||||||
|
|
||||||
|
PHASE="fast-forward"
|
||||||
echo
|
echo
|
||||||
echo "========== GIT REPOS CON GRADLE =========="
|
echo "========== FAST-FORWARD =========="
|
||||||
for root in /root /var/www /home /tmp; do
|
git log --oneline --no-decorate "$PREV_SHA..$TARGET_SHA"
|
||||||
[ -d "$root" ] || continue
|
APP_TOUCHED=1
|
||||||
find "$root" -maxdepth 9 -type d -name .git -print 2>/dev/null || true
|
|
||||||
done | while read -r gitdir; do
|
|
||||||
dir="${gitdir%/.git}"
|
|
||||||
if find "$dir" -maxdepth 3 \( -name gradlew -o -name settings.gradle -o -name settings.gradle.kts \) -print -quit 2>/dev/null | grep -q .; then
|
|
||||||
echo "----- $dir -----"
|
|
||||||
git -C "$dir" status --short --branch 2>/dev/null || true
|
|
||||||
git -C "$dir" log -8 --oneline 2>/dev/null || true
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
echo
|
|
||||||
echo "========== HUELLA KOTLIN / COMPOSE =========="
|
|
||||||
for root in /root /var/www /home /tmp; do
|
|
||||||
[ -d "$root" ] || continue
|
|
||||||
find "$root" -maxdepth 10 -type f -name '*.kt' -printf '%h\n' 2>/dev/null || true
|
|
||||||
done | grep -Ei 'dh|inspe|android|mobile|app' | sort -u | head -300
|
|
||||||
|
|
||||||
echo
|
|
||||||
echo "DISCOVERY_OK"
|
|
||||||
|
|
||||||
echo
|
|
||||||
echo "========== REGISTRAR COMMIT DIAGNÓSTICO =========="
|
|
||||||
git merge --ff-only "origin/$DEPLOY_REF"
|
git merge --ff-only "origin/$DEPLOY_REF"
|
||||||
echo "Diagnostic commit registrado localmente: $(git rev-parse HEAD)"
|
|
||||||
|
PHASE="build"
|
||||||
|
echo
|
||||||
|
echo "========== BUILD PRODUCCIÓN =========="
|
||||||
|
docker compose build api migrate web </dev/null
|
||||||
|
|
||||||
|
PHASE="migrations"
|
||||||
|
echo
|
||||||
|
echo "========== MIGRACIONES =========="
|
||||||
|
docker compose --profile tools run --rm migrate </dev/null
|
||||||
|
docker compose --profile tools run --rm migrate npm run migration:show </dev/null | tee "$BACKUP/migrations.txt"
|
||||||
|
grep -Fq 'Pending migrations: no' "$BACKUP/migrations.txt"
|
||||||
|
|
||||||
|
PHASE="recreate"
|
||||||
|
echo
|
||||||
|
echo "========== RECREATE API + WEB =========="
|
||||||
|
docker compose up -d --no-deps --force-recreate api web </dev/null
|
||||||
|
|
||||||
|
PHASE="health"
|
||||||
|
echo
|
||||||
|
echo "========== HEALTH =========="
|
||||||
|
HEALTH_OK=0
|
||||||
|
for _ in $(seq 1 60); do
|
||||||
|
if curl -fsS --max-time 5 http://127.0.0.1:3101/api/v3/health > "$BACKUP/health.json" 2>/dev/null; then
|
||||||
|
if grep -F '"status":"ok"' "$BACKUP/health.json" >/dev/null; then
|
||||||
|
HEALTH_OK=1
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
sleep 2
|
||||||
|
done
|
||||||
|
|
||||||
|
if [ "$HEALTH_OK" -ne 1 ]; then
|
||||||
|
echo "ERROR: API no pasó healthcheck."
|
||||||
|
docker compose logs --tail=180 api
|
||||||
|
false
|
||||||
|
fi
|
||||||
|
|
||||||
|
cat "$BACKUP/health.json"
|
||||||
|
echo
|
||||||
|
|
||||||
|
grep -Fq "\"version\":\"$EXPECTED_API_VERSION\"" "$BACKUP/health.json"
|
||||||
|
grep -Fq '"database":"ok"' "$BACKUP/health.json"
|
||||||
|
|
||||||
|
WEB_CODE="$(curl -sS -o /dev/null -w '%{http_code}' --max-time 10 http://127.0.0.1:8182/)"
|
||||||
|
[ "$WEB_CODE" = "200" ] || { echo "ERROR: WEB HTTP $WEB_CODE"; false; }
|
||||||
|
|
||||||
|
PHASE="verify"
|
||||||
|
echo
|
||||||
|
echo "========== VERIFICACIÓN FINAL =========="
|
||||||
|
docker compose ps -a | tee "$BACKUP/docker-after.txt"
|
||||||
|
if docker compose ps --status running --services | grep -Fxq api && docker compose ps --status running --services | grep -Fxq web && docker compose ps --status running --services | grep -Fxq db; then
|
||||||
|
echo "Servicios críticos: OK"
|
||||||
|
else
|
||||||
|
echo "ERROR: falta un servicio crítico en ejecución."
|
||||||
|
false
|
||||||
|
fi
|
||||||
|
|
||||||
|
PHASE="post-backup"
|
||||||
|
docker compose exec -T db sh -lc 'pg_dump -U "$POSTGRES_USER" -d "$POSTGRES_DB" -Fc' </dev/null > "$BACKUP/database-after.dump"
|
||||||
|
git rev-parse HEAD > "$BACKUP/deployed.sha"
|
||||||
|
printf 'API=%s\nWEB=%s\n' "$EXPECTED_API_VERSION" "$EXPECTED_WEB_VERSION" > "$BACKUP/deployed-versions.txt"
|
||||||
|
(
|
||||||
|
cd "$BACKUP"
|
||||||
|
sha256sum database-after.dump deployed.sha deployed-versions.txt health.json migrations.txt docker-after.txt >> SHA256SUMS.txt
|
||||||
|
sha256sum -c SHA256SUMS.txt
|
||||||
|
)
|
||||||
|
chmod 600 "$BACKUP"/* "$BACKUP/.env" 2>/dev/null || true
|
||||||
|
|
||||||
|
PHASE="complete"
|
||||||
|
trap - ERR
|
||||||
|
|
||||||
|
echo
|
||||||
|
echo "============================================================"
|
||||||
|
echo " DH V2 · DEPLOY OK"
|
||||||
|
echo "============================================================"
|
||||||
|
echo "Commit: $TARGET_SHA"
|
||||||
|
echo "API: $EXPECTED_API_VERSION"
|
||||||
|
echo "WEB: $EXPECTED_WEB_VERSION"
|
||||||
|
echo "Backup: $BACKUP"
|
||||||
|
echo "============================================================"
|
||||||
|
|||||||
Reference in New Issue
Block a user