sdk_relay/scripts/init-ssh-env.sh

154 lines
4.7 KiB
Bash
Executable File

#!/bin/bash
# Script d'initialisation de l'environnement SSH pour ihm_client
# Configure automatiquement SSH pour tous les push
set -e
echo "🚀 Initialisation de l'environnement SSH pour ihm_client..."
# Couleurs pour les messages
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Fonction pour afficher les messages colorés
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Vérifier si on est dans le bon répertoire
if [[ ! -f "package.json" ]] || [[ ! -d ".git" ]]; then
print_error "Ce script doit être exécuté depuis le répertoire racine de ihm_client"
exit 1
fi
print_status "Configuration de l'environnement SSH..."
# 1. Configuration Git pour SSH
print_status "Configuration Git pour utiliser SSH..."
git config --global url."git@git.4nkweb.com:".insteadOf "https://git.4nkweb.com/"
# 2. Vérifier si une clé SSH existe
print_status "Vérification des clés SSH existantes..."
if [[ -f ~/.ssh/id_rsa ]] || [[ -f ~/.ssh/id_ed25519 ]]; then
print_success "Clé SSH trouvée"
SSH_KEY_EXISTS=true
else
print_warning "Aucune clé SSH trouvée"
SSH_KEY_EXISTS=false
fi
# 3. Tester la connexion SSH
print_status "Test de la connexion SSH vers git.4nkweb.com..."
if ssh -T git@git.4nkweb.com 2>&1 | grep -q "successfully authenticated"; then
print_success "Authentification SSH réussie"
SSH_WORKING=true
else
print_error "Échec de l'authentification SSH"
SSH_WORKING=false
fi
# 4. Configuration des alias Git
print_status "Configuration des alias Git..."
git config --global alias.ssh-push '!f() { git add . && git commit -m "${1:-Auto-commit $(date)}" && git push origin $(git branch --show-current); }; f'
git config --global alias.quick-push '!f() { git add . && git commit -m "Update $(date)" && git push origin $(git branch --show-current); }; f'
print_success "Alias Git configurés"
# 5. Vérifier les remotes
print_status "Vérification des remotes Git..."
if git remote -v | grep -q "git@git.4nkweb.com"; then
print_success "Remotes configurés pour SSH"
else
print_warning "Remotes non configurés pour SSH"
print_status "Mise à jour des remotes..."
git remote set-url origin git@git.4nkweb.com:4nk/ihm_client.git
print_success "Remotes mis à jour"
fi
# 6. Rendre les scripts exécutables
print_status "Configuration des permissions des scripts..."
chmod +x scripts/auto-ssh-push.sh 2>/dev/null || true
chmod +x scripts/setup-ssh-ci.sh 2>/dev/null || true
print_success "Scripts rendus exécutables"
# 7. Créer un fichier de configuration local
print_status "Création du fichier de configuration local..."
cat > .ssh-config << EOF
# Configuration SSH automatique pour ihm_client
# Généré le $(date)
# Configuration Git
git config --global url."git@git.4nkweb.com:".insteadOf "https://git.4nkweb.com/"
# Alias Git
git config --global alias.ssh-push '!f() { git add . && git commit -m "\${1:-Auto-commit \$(date)}" && git push origin \$(git branch --show-current); }; f'
git config --global alias.quick-push '!f() { git add . && git commit -m "Update \$(date)" && git push origin \$(git branch --show-current); }; f'
# Test SSH
ssh -T git@git.4nkweb.com
# Scripts disponibles
./scripts/auto-ssh-push.sh quick
./scripts/auto-ssh-push.sh message "Mon message"
git ssh-push "Mon message"
git quick-push
EOF
print_success "Fichier de configuration créé: .ssh-config"
# 8. Résumé de la configuration
echo ""
print_success "=== Configuration SSH terminée ==="
echo ""
echo "✅ Configuration Git pour SSH"
echo "✅ Alias Git configurés"
echo "✅ Remotes vérifiés"
echo "✅ Scripts configurés"
echo ""
if [[ "$SSH_WORKING" == "true" ]]; then
print_success "SSH fonctionne correctement"
echo ""
echo "🚀 Vous pouvez maintenant utiliser :"
echo " ./scripts/auto-ssh-push.sh quick"
echo " ./scripts/auto-ssh-push.sh message \"Mon message\""
echo " git ssh-push \"Mon message\""
echo " git quick-push"
echo ""
else
print_warning "SSH ne fonctionne pas encore"
echo ""
echo "🔧 Pour configurer SSH :"
echo " 1. Générer une clé SSH : ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_4nk"
echo " 2. Ajouter à l'agent SSH : ssh-add ~/.ssh/id_ed25519_4nk"
echo " 3. Ajouter la clé publique à votre compte Gitea"
echo " 4. Relancer ce script : ./scripts/init-ssh-env.sh"
echo ""
fi
# 9. Test final
if [[ "$SSH_WORKING" == "true" ]]; then
print_status "Test final de push SSH..."
echo "💡 Pour tester, utilisez : ./scripts/auto-ssh-push.sh status"
fi
print_success "Initialisation SSH terminée !"