← Retour
#!/bin/bash
# ═══════════════════════════════════════════════════════════════════════════
# TARGETS.SH - Résolution des targets de déploiement
# Usage: resolve_target "prod-portainer"
# Définit: TARGET_TYPE, TARGET_HOST, TARGET_USER, TARGET_PATH, TARGET_LOCAL
# ═══════════════════════════════════════════════════════════════════════════
# Utiliser STOCK_BASE défini par smart-deploy.sh, ou le détecter
if [[ -z "${STOCK_BASE:-}" ]]; then
if [[ -d "/stock_8to/33800-stack" ]]; then
STOCK_BASE="/stock_8to/33800-stack"
elif [[ -d "/mnt/stock_8to/33800-stack" ]]; then
STOCK_BASE="/mnt/stock_8to/33800-stack"
fi
fi
MACHINES_REGISTRY="${STOCK_BASE}/scripts/deploy/machines-registry.json"
# ───────────────────────────────────────────────────────────────────────────
# Résoudre un target vers ses propriétés
# ───────────────────────────────────────────────────────────────────────────
resolve_target() {
local target="$1"
if [[ ! -f "$MACHINES_REGISTRY" ]]; then
log_error "Registre des machines non trouvé: $MACHINES_REGISTRY"
exit 1
fi
# Vérifier que le target existe
local exists=$(jq -r --arg t "$target" '.machines[$t] // empty' "$MACHINES_REGISTRY")
if [[ -z "$exists" ]]; then
log_error "Target inconnu: $target"
log_info "Targets disponibles:"
jq -r '.machines | keys[]' "$MACHINES_REGISTRY" | sed 's/^/ - /'
exit 1
fi
# Extraire les propriétés
export TARGET_NAME="$target"
export TARGET_TYPE=$(jq -r --arg t "$target" '.machines[$t].type' "$MACHINES_REGISTRY")
export TARGET_HOST=$(jq -r --arg t "$target" '.machines[$t].ip // .machines[$t].host' "$MACHINES_REGISTRY")
export TARGET_USER=$(jq -r --arg t "$target" '.machines[$t].ssh_user' "$MACHINES_REGISTRY")
export TARGET_PATH=$(jq -r --arg t "$target" '.machines[$t].services_path' "$MACHINES_REGISTRY")
export TARGET_LOCAL=$(jq -r --arg t "$target" '.machines[$t].local // false' "$MACHINES_REGISTRY")
export TARGET_SSH_ALIAS=$(jq -r --arg t "$target" '.machines[$t].ssh_alias // empty' "$MACHINES_REGISTRY")
# Docker spécifique
if [[ "$TARGET_TYPE" == "docker" ]]; then
export TARGET_DOCKER_ROOT=$(jq -r --arg t "$target" '.machines[$t].docker_data_root // empty' "$MACHINES_REGISTRY")
fi
# Remote spécifique (o2switch)
if [[ "$TARGET_TYPE" == "remote" ]]; then
export TARGET_METHODS=$(jq -r --arg t "$target" '.machines[$t].methods | join(",")' "$MACHINES_REGISTRY")
fi
log_info "Target résolu: $target"
log_info " Type: $TARGET_TYPE"
log_info " Host: $TARGET_HOST"
log_info " User: $TARGET_USER"
log_info " Path: $TARGET_PATH"
log_info " Local: $TARGET_LOCAL"
}
# ───────────────────────────────────────────────────────────────────────────
# Vérifier la connectivité vers un target
# ───────────────────────────────────────────────────────────────────────────
check_target_connectivity() {
local target="$1"
if [[ "$TARGET_LOCAL" == "true" ]]; then
log_info "Target local, pas de vérification SSH"
return 0
fi
# Utiliser l'alias SSH si disponible
local ssh_dest
if [[ -n "${TARGET_SSH_ALIAS:-}" ]]; then
ssh_dest="${TARGET_SSH_ALIAS}"
log_step "Vérification connectivité vers ${ssh_dest} (alias)..."
else
ssh_dest="${TARGET_USER}@${TARGET_HOST}"
log_step "Vérification connectivité vers $TARGET_HOST..."
fi
if ssh -o StrictHostKeyChecking=no -o BatchMode=yes -o ConnectTimeout=5 \
"$ssh_dest" "echo 'OK'" &>/dev/null; then
log_success "Connectivité OK"
return 0
else
log_error "Impossible de se connecter à ${ssh_dest}"
return 1
fi
}
# ───────────────────────────────────────────────────────────────────────────
# Liste des targets disponibles
# ───────────────────────────────────────────────────────────────────────────
list_targets() {
if [[ ! -f "$MACHINES_REGISTRY" ]]; then
log_error "Registre des machines non trouvé"
return 1
fi
echo "Targets disponibles:"
jq -r '.machines | to_entries[] | " \(.key): \(.value.type) (\(.value.ip // .value.host))"' "$MACHINES_REGISTRY"
}