#!/bin/bash # ============================================================================== # actionreplay - CLIENT DOWNLOADER (In-Folder Edition) # ============================================================================== 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 "================================================" # Token : DOWNLOADER_OPS_BYPASS_FOR_DEV (lab) OU challenge console /ops obtain_token_bypass() { local pass="${DOWNLOADER_OPS_BYPASS_FOR_DEV:-}" local json tok [ -n "$pass" ] || return 1 echo "Demande de token (DOWNLOADER_OPS_BYPASS_FOR_DEV)..." json=$(curl -sSf --connect-timeout 8 \ -X POST "${AR_BASE}/api/v1/downloader-ops-bypass-for-dev" \ -H "Content-Type: application/json" \ -d "{\"password\":\"${pass}\"}") || { echo "DOWNLOADER_OPS_BYPASS_FOR_DEV refuse ou actionreplay injoignable." exit 1 } tok=$(printf '%s' "$json" | sed -n 's/.*"token"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p') [ -n "$tok" ] || { echo "Reponse downloader-ops-bypass-for-dev invalide."; exit 1; } OPS_TOKEN="$tok" } obtain_token_with_hwid() { local cart="$1" local env="$2" local hwid="$3" local action="${4:-cold}" local hostname cid status tok i json if [ -n "${DOWNLOADER_OPS_BYPASS_FOR_DEV:-}" ]; then obtain_token_bypass return 0 fi 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 :" 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" echo "Approve OK — 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 } # --- Auth initiale --- if [ -n "${DOWNLOADER_OPS_BYPASS_FOR_DEV:-}" ]; then obtain_token_bypass else echo "Mode console ops — 1er challenge dummy (listing)..." host=$(hostname -f 2>/dev/null || hostname) obtain_token_with_hwid "-" "-" "DUMMY:${host}" dummy fi 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 "TEST" "PROD" "DEV"; 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}" CURRENT_DIR=$(basename "$PWD") if [ "$CURRENT_DIR" != "$EXPECTED_DIR" ]; then echo "" echo "[ERREUR] Emplacement invalide." echo "Cartouche : $CART_NAME" echo "Environnement : $TARGET_ENV" echo "Dossier attendu : $EXPECTED_DIR" echo "Dossier 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") echo "Verification licence Control Plane..." AUTH_URL="${AR_BASE}/api/v1/auth/verify?hwid=${HWID}&cartridge=${CART_NAME}&env=${TARGET_ENV}&actor=downloader" HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout 3 "$AUTH_URL" || echo "000") if [ "$HTTP_STATUS" != "200" ]; then rm -f ./client.hwid echo "" echo "ACCES REFUSE (HTTP $HTTP_STATUS) — ajoute la licence puis relance." echo "HWID calcule : $HWID" echo "Fichier : actionreplay/config/licenses.jsonl" echo "" exit 1 fi echo "Autorisation OK." rm -f ./client.hwid # 2e challenge cold : HWID licence + extract tar (sauf bypass lab). if [ -z "${DOWNLOADER_OPS_BYPASS_FOR_DEV:-}" ]; then echo "Approve pour extract cartouche (HWID licence)..." unset OPS_TOKEN obtain_token_with_hwid "$CART_NAME" "$TARGET_ENV" "$HWID" cold AR_Q="token=${OPS_TOKEN}" fi echo "Telechargement archive [$CART_NAME]..." curl -sSf --connect-timeout 5 "${AR_BASE}/cartridges/${CART_NAME}/${CART_NAME}.tar.gz?${AR_Q}" | tar -xz --no-same-owner if [ ! -x console/sidecars/client/client ]; then echo "Client scelle absent dans la cartouche." exit 1 fi chmod +x console/deploy/*.sh game/etl_custom/*.sh console/build/*.sh 2>/dev/null || true echo "Lancement installation ($TARGET_ENV)..." export OPS_TOKEN export DOWNLOADER_OPS_BYPASS_FOR_DEV="${DOWNLOADER_OPS_BYPASS_FOR_DEV:-}" ./console/deploy/install.sh