Repository Public
1#!/bin/bash2# =============================================================================3# SP BOUCHER | INFRASTRUCTURE & AUTOMATION4# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# Project : KA Maps Engine8# File : server/pipeline/run-refresh.sh9# Module : server10# Repository : https://git.spboucher.ai/ka-maps-engine11# Created : 2026-12-0112# Last Modified : 2026-12-0113#14# Author : Simon-Pierre Boucher15# Position : Professor of Finance16# Institution : Université du Québec en Outaouais (UQO)17# Organization : SP Boucher18# Website : https://spboucher.ai19# Git : https://git.spboucher.ai20#21# Preferred Contact: support@spboucher.ai22# Technical Contact: dev@spboucher.ai23# Security/Admin : admin@spboucher.ai24#25# Copyright (c) 202626# Simon-Pierre Boucher27# All rights reserved where applicable. This file is governed by the28# applicable project license, copyright notices and relevant law.29# Existing third-party rights and notices remain unaffected.30#31# License : None specified (no license file in this repository)32#33# Do not embed credentials, private keys, access tokens,34# certificates or confidential personal information.35#36# =============================================================================37# END OF SP BOUCHER SOURCE HEADER38# =============================================================================3940# Cycle complet de rafraîchissement OSM (minuterie hebdomadaire kamaps-osm-refresh.timer) :41# téléchargement → vérification → préparation → import dans osm_next → généralisation → validation →42# pré-génération du cache de la nouvelle version → contrôle des tuiles → bascule → archives.43# L'ancienne version reste servie jusqu'à la bascule ; tout échec laisse le service intact.44. "$(dirname "$0")/env.sh"45mkdir -p "$KM_LOG"46LOGF="$KM_LOG/refresh-$(date -u +%Y%m%dT%H%M%SZ).log"47exec > >(tee -a "$LOGF") 2>&148exec 9>"$KM_ROOT/refresh.lock"49flock -n 9 || { log "un rafraîchissement est déjà en cours"; exit 0; }50TILER=${KM_TILER:-/usr/local/bin/km-tiler}51step() { log "=== $*"; }52fail() { log "ÉCHEC : $* — la version en service n'est pas modifiée"; exit 1; }5354# KM_RESUME=seed : reprend à la pré-génération une version déjà importée et validée (osm_next intact).55if [ "${KM_RESUME:-}" = seed ]; then56 VERSION=$(psql -X -At "$KM_DB_URL" -c "SELECT version FROM meta.version WHERE status = 'validated' ORDER BY created_at DESC LIMIT 1")57 [ -n "$VERSION" ] || fail "aucune version validée à reprendre"58 log "reprise de la version $VERSION à la pré-génération"59else60step "téléchargement"; [ "${KM_SKIP_DOWNLOAD:-0}" = 1 ] || "$KM_PIPE/01-download.sh" || fail "téléchargement"61step "préparation du fichier"; "$KM_PIPE/03-osm-prepare.sh" || fail "préparation"62VERSION=$(cat "$KM_SRC/osm/work/import.version")63[ -n "${KM_VERSION_SUFFIX:-}" ] && VERSION="$VERSION$KM_VERSION_SUFFIX"64if [ "$(psql -X -At "$KM_DB_URL" -c "SELECT count(*) FROM meta.version WHERE version = '$VERSION' AND status = 'active'" 2>/dev/null)" = "1" ]; then65 if [ "${KM_FORCE:-0}" = 1 ]; then66 # Même extrait, règles ou sources complémentaires changées : nouvelle révision AAAAMMJJrN.67 N=$(psql -X -At "$KM_DB_URL" -c "SELECT count(*) + 1 FROM meta.version WHERE version LIKE '${VERSION}%' AND version !~ '-'")68 VERSION="${VERSION}r${N}"69 log "extrait déjà en service, reconstruction forcée : révision $VERSION"70 else71 log "l'extrait $VERSION est déjà en service : terminé (KM_FORCE=1 pour reconstruire)"; exit 072 fi73fi74step "import $VERSION"; "$KM_PIPE/04-osm-import.sh" "$VERSION" || fail "import"75step "validation des données"; "$KM_PIPE/05-validate.py" "$VERSION" > "$KM_LOG/validation-$VERSION.json" || { cat "$KM_LOG/validation-$VERSION.json"; fail "validation (seuils)"; }76fi77step "pré-génération du cache $VERSION (depuis osm_next)"78( set -a; . /etc/kamaps/db-ro.env; set +a79 "$TILER" --schema osm_next --data-version "$VERSION" seed --concurrency "$KM_THREADS" \80 --context-bbox=-84,43,-56,63 > "$KM_LOG/seed-$VERSION.json" ) || fail "pré-génération"81step "contrôle des tuiles du cache"82python3 "$KM_PIPE/07-check-tiles.py" --cache "$KM_ROOT/cache/$VERSION" --tiler "$TILER" > "$KM_LOG/tiles-$VERSION.json" || { cat "$KM_LOG/tiles-$VERSION.json"; fail "contrôle des tuiles"; }83step "bascule"; "$KM_PIPE/06-switch.sh" "$VERSION" || fail "bascule"84sleep 1585step "archives PMTiles"; "$KM_PIPE/08-packs.sh" || log "AVERTISSEMENT : archives non régénérées"86find "$KM_LOG" -name '*.log' -mtime +90 -delete; find "$KM_LOG" -name '*.json' -mtime +90 -delete87step "terminé : version $VERSION en service"88