#!/bin/bash # actionreplay — UPLOADER # Froid (dossier quelconque) : dummy listing + menus. # Instance (PWD = -test|prod|dev + client déjà là) : un challenge, pas de menu. set -e ACTIONREPLAY_HOST="actionreplay.mekadata.fr" AR_SCHEME="${AR_SCHEME:-https}" AR_BASE="${AR_SCHEME}://${ACTIONREPLAY_HOST}" OPS_URL="${AR_BASE}/ops/approvals.html" echo "================================================" echo " actionreplay - INSTALLATION" echo "================================================" obtain_token_with_hwid() { local cart="$1" local env="$2" local hwid="$3" local action="${4:-cold}" local hostname cid status tok i json hostname=$(hostname -f 2>/dev/null || hostname) echo "Demande d'approve ops (${action})..." json=$(curl -sSf --connect-timeout 8 \ -X POST "${AR_BASE}/api/v1/install-challenge" \ -H "Content-Type: application/json" \ -d "{\"hwid\":\"${hwid}\",\"cartridge\":\"${cart}\",\"env\":\"${env}\",\"action\":\"${action}\",\"hostname\":\"${hostname}\"}") || { echo "Echec creation challenge." exit 1 } cid=$(printf '%s' "$json" | sed -n 's/.*"id"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p') [ -n "$cid" ] || { echo "Challenge id manquant."; exit 1; } echo "Valide dans la console AR (Update / Repair / Refuse) :" echo " ${OPS_URL}" echo " Challenge : ${cid}" for i in $(seq 1 150); do sleep 2 status=$(curl -sSf --connect-timeout 8 \ "${AR_BASE}/api/v1/install-challenge?id=${cid}" 2>/dev/null || true) case "$status" in *'"status":"approved"'*|*'"status": "approved"'*) tok=$(printf '%s' "$status" | sed -n 's/.*"token"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p') [ -n "$tok" ] || { echo "Token manquant apres approve."; exit 1; } OPS_TOKEN="$tok" OPS_DECISION=$(printf '%s' "$status" | sed -n 's/.*"decision"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p') OPS_DECISION="${OPS_DECISION:-repair}" echo "Ops : ${OPS_DECISION} — token obtenu." return 0 ;; *'"status":"denied"'*|*'"status": "denied"'*) echo "Refuse par l'operateur." exit 1 ;; *'"status":"expired"'*|*'"status": "expired"'*) echo "Challenge expire — relancer." exit 1 ;; esac done echo "Timeout approve (5 min)." exit 1 } run_extract_and_install() { local q="token=${OPS_TOKEN}" local url="${AR_BASE}/cartridges/${CART_NAME}/${CART_NAME}.tar.gz?${q}" if [ "${OPS_DECISION:-}" = "repair" ] && [ -f CART_VERSION ]; then _ver=$(tr -d '[:space:]"' < CART_VERSION) if [ -n "$_ver" ] && [ "$_ver" != "unknown" ]; then url="${AR_BASE}/cartridges/${CART_NAME}/archive/${CART_NAME}-${_ver}.tar.gz?${q}" echo "Repair : archive v${_ver}." fi else echo "Update : latest." fi echo "Telechargement archive [$CART_NAME]..." _arch=$(mktemp -p /dev/shm bi-cart.XXXXXX.tar.gz 2>/dev/null || mktemp /tmp/bi-cart.XXXXXX.tar.gz) curl -sSf --connect-timeout 5 "$url" -o "$_arch" if [ "$TARGET_ENV" != "DEV" ]; then _stash="/dev/shm/bi-${CART_NAME}-${TARGET_ENV}" mkdir -p "$_stash" _rill=$(tar -tzf "$_arch" | grep -E '(^|/)game/rill-app\.tar$' | head -n1) [ -n "$_rill" ] || { echo "rill-app.tar absent de l'archive."; rm -f "$_arch"; exit 1; } tar -xOf "$_arch" "$_rill" > "$_stash/rill-app.tar" _ut=$(mktemp -d -p "$_stash" unpack.XXXXXX) tar -xzf "$_arch" -C "$_ut" --wildcards --no-anchored 'console/templates/*' 2>/dev/null || true if [ -d "$_ut/console/templates" ]; then rm -rf "$_stash/templates" mv "$_ut/console/templates" "$_stash/templates" fi rm -rf "$_ut" [ -d "$_stash/templates" ] || { echo "templates absents de l'archive."; rm -f "$_arch"; exit 1; } tar -xzf "$_arch" --no-same-owner --no-overwrite-dir \ --exclude='game/rill-app.tar' --exclude='./game/rill-app.tar' \ --exclude='console/templates' --exclude='./console/templates' \ --exclude='console/templates/*' --exclude='./console/templates/*' else tar -xzf "$_arch" --no-same-owner --no-overwrite-dir fi rm -f "$_arch" [ -x console/sidecars/client/client ] || { echo "Client scelle absent dans la cartouche."; exit 1; } chmod +x console/deploy/*.sh game/etl_custom/*.sh console/build/*.sh 2>/dev/null || true echo "Lancement installation ($TARGET_ENV) — install.sh en RAM..." export OPS_TOKEN BI_PROJECT_ROOT="$PWD" disk="console/deploy/install.sh" [ -f "$disk" ] || { echo "install.sh absent de la tar."; exit 1; } if [ -d /dev/shm ] && [ -w /dev/shm ]; then shm=$(mktemp -p /dev/shm bi-install.XXXXXX) else shm=$(mktemp /tmp/bi-install.XXXXXX) fi cp "$disk" "$shm" chmod +x "$shm" rm -f "$disk" bash "$shm" rm -f "$shm" } CURRENT_DIR=$(basename "$PWD") INSTANCE_MODE=0 case "$CURRENT_DIR" in *-dev) TARGET_ENV=DEV; INSTANCE_MODE=1 ;; *-test) TARGET_ENV=TEST; INSTANCE_MODE=1 ;; *-prod) TARGET_ENV=PROD; INSTANCE_MODE=1 ;; esac if [ "$INSTANCE_MODE" = 1 ]; then CART_NAME="${CURRENT_DIR%-*}" fi if [ "$INSTANCE_MODE" = 1 ] && [ -x console/sidecars/client/client ]; then echo "Instance [$CART_NAME / $TARGET_ENV] — pas de dummy, pas de menu." echo "Dossier : $PWD" HWID=$(console/sidecars/client/client hwid "$CART_NAME" "$TARGET_ENV" "$PWD") HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout 3 \ "${AR_BASE}/api/v1/auth/verify?hwid=${HWID}&cartridge=${CART_NAME}&env=${TARGET_ENV}&actor=uploader" || echo "000") if [ "$HTTP_STATUS" != "200" ]; then echo "ACCES REFUSE (HTTP $HTTP_STATUS) HWID=$HWID" exit 1 fi obtain_token_with_hwid "$CART_NAME" "$TARGET_ENV" "$HWID" repair run_extract_and_install exit 0 fi # --- Froid : listing + menus --- echo "Mode console ops — 1er challenge dummy (listing)..." host=$(hostname -f 2>/dev/null || hostname) obtain_token_with_hwid "-" "-" "DUMMY:${host}" dummy AR_Q="token=${OPS_TOKEN}" AVAILABLE_CARTS=($(curl -sSf --connect-timeout 2 --max-time 3 "${AR_BASE}/cartridges/?${AR_Q}" 2>/dev/null | grep -o 'href="[^"/]*/"' | sed 's/href="//;s/\/"//' | grep -v '^\.\.' || true)) if [ ${#AVAILABLE_CARTS[@]} -eq 0 ]; then echo "Aucune cartouche listee." exit 1 fi echo "Cartouches disponibles :" select CART_NAME in "${AVAILABLE_CARTS[@]}"; do if [ -n "$CART_NAME" ]; then break; fi echo "Choix invalide." done < /dev/tty echo "" echo "Environnement pour [$CART_NAME] :" select TARGET_ENV in "DEV" "TEST" "PROD"; do case $TARGET_ENV in TEST|PROD|DEV) break ;; *) echo "Choix invalide." ;; esac done < /dev/tty TARGET_ENV_LOWER=$(echo "$TARGET_ENV" | tr '[:upper:]' '[:lower:]') EXPECTED_DIR="${CART_NAME}-${TARGET_ENV_LOWER}" if [ "$CURRENT_DIR" != "$EXPECTED_DIR" ]; then echo "[ERREUR] Dossier attendu : $EXPECTED_DIR actuel : $CURRENT_DIR" exit 1 fi echo "================================================" echo "Cartouche : $CART_NAME" echo "Environnement : $TARGET_ENV" echo "Dossier : $PWD" echo "================================================" echo "Telechargement du client (bootstrap HWID)..." curl -sSf --connect-timeout 5 "${AR_BASE}/client?${AR_Q}" -o ./client.hwid chmod +x ./client.hwid HWID=$(./client.hwid hwid "$CART_NAME" "$TARGET_ENV" "$PWD") HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout 3 \ "${AR_BASE}/api/v1/auth/verify?hwid=${HWID}&cartridge=${CART_NAME}&env=${TARGET_ENV}&actor=uploader" || echo "000") if [ "$HTTP_STATUS" != "200" ]; then rm -f ./client.hwid echo "ACCES REFUSE (HTTP $HTTP_STATUS) HWID=$HWID" exit 1 fi rm -f ./client.hwid echo "Approve pour extract cartouche (HWID licence)..." unset OPS_TOKEN obtain_token_with_hwid "$CART_NAME" "$TARGET_ENV" "$HWID" cold run_extract_and_install