diff --git a/tools/scripts/README.md b/tools/scripts/README.md index 4bfd5a1..7045838 100644 --- a/tools/scripts/README.md +++ b/tools/scripts/README.md @@ -32,11 +32,27 @@ ./test_ci_locally.sh ``` +### `fix_ssh_key.sh` + +Исправляет проблемы с SSH ключом для деплоя: + +- Удаляет passphrase с SSH ключа +- Устанавливает правильные права доступа +- Копирует публичный ключ на сервер +- Тестирует SSH подключение + +```bash +./fix_ssh_key.sh +``` + ## Использование ### Перед деплоем ```bash +# Если проблемы с SSH ключом +./fix_ssh_key.sh + # Всегда проверяйте готовность перед деплоем ./check_deploy_ready.sh diff --git a/tools/scripts/check_deploy_ready.sh b/tools/scripts/check_deploy_ready.sh index 4d66c94..0b05d21 100755 --- a/tools/scripts/check_deploy_ready.sh +++ b/tools/scripts/check_deploy_ready.sh @@ -6,7 +6,7 @@ set -e # Change to project root -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)" cd "$PROJECT_ROOT" @@ -54,8 +54,11 @@ if ssh -o ConnectTimeout=10 -o BatchMode=yes -i "$SSH_KEY" root@$SERVER_IP "echo print_status "SSH connection to server successful" else print_error "Cannot connect to server via SSH" - echo " Make sure the public key is added to root@$SERVER_IP:~/.ssh/authorized_keys" - echo " Run: ssh-copy-id -i $SSH_KEY.pub root@$SERVER_IP" + echo " Possible causes:" + echo " 1. SSH key has a passphrase - remove it with: ssh-keygen -p -f $SSH_KEY" + echo " 2. Public key not added to server: ssh-copy-id -i $SSH_KEY.pub root@$SERVER_IP" + echo " 3. Wrong permissions on ~/.ssh directory: chmod 700 ~/.ssh && chmod 600 ~/.ssh/*" + echo " 4. Server firewall blocks SSH: check ufw status on server" exit 1 fi diff --git a/tools/scripts/fix_ssh_key.sh b/tools/scripts/fix_ssh_key.sh new file mode 100755 index 0000000..0906970 --- /dev/null +++ b/tools/scripts/fix_ssh_key.sh @@ -0,0 +1,81 @@ +#!/bin/bash + +# Script to fix SSH key issues for deployment +# Run this if check_deploy_ready.sh fails on SSH connection + +set -e + +SSH_KEY="${SSH_DEPLOY_KEY:-$HOME/.ssh/forgejo_deploy}" +SERVER_IP="147.45.152.129" +SERVER_USER="root" + +echo "🔧 Fixing SSH key for deployment..." + +# Colors +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' + +print_status() { + echo -e "${GREEN}[INFO]${NC} $1" +} + +print_warning() { + echo -e "${YELLOW}[WARNING]${NC} $1" +} + +print_error() { + echo -e "${RED}[ERROR]${NC} $1" +} + +print_success() { + echo -e "${GREEN}[SUCCESS]${NC} $1" +} + +# Check if key exists +if [ ! -f "$SSH_KEY" ]; then + print_error "SSH key not found at $SSH_KEY" + print_info "Generate it first with: ssh-keygen -t ed25519 -C 'forgejo-deploy@memo-cards.online' -f $SSH_KEY" + exit 1 +fi + +# Remove passphrase from key +print_status "Removing passphrase from SSH key..." +if ssh-keygen -p -f "$SSH_KEY" -N ""; then + print_success "Passphrase removed from SSH key" +else + print_warning "Could not remove passphrase (key might not have one)" +fi + +# Set correct permissions +print_status "Setting correct permissions..." +chmod 700 ~/.ssh +chmod 600 "$SSH_KEY" +chmod 644 "${SSH_KEY}.pub" +print_success "Permissions set" + +# Copy public key to server +print_status "Copying public key to server..." +if ssh-copy-id -i "${SSH_KEY}.pub" "$SERVER_USER@$SERVER_IP" 2>/dev/null; then + print_success "Public key copied to server" +else + print_warning "ssh-copy-id failed, trying manual method..." + ssh "$SERVER_USER@$SERVER_IP" "mkdir -p ~/.ssh && chmod 700 ~/.ssh" + cat "${SSH_KEY}.pub" | ssh "$SERVER_USER@$SERVER_IP" "cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys" + print_success "Public key added manually" +fi + +# Test connection +print_status "Testing SSH connection..." +if ssh -o ConnectTimeout=10 -o BatchMode=yes -i "$SSH_KEY" "$SERVER_USER@$SERVER_IP" "echo 'SSH test successful'" 2>/dev/null; then + print_success "SSH connection works! ✅" +else + print_error "SSH connection still fails" + print_info "Try manual connection: ssh -i $SSH_KEY $SERVER_USER@$SERVER_IP" + exit 1 +fi + +print_success "SSH setup completed! 🎉" +print_info "You can now run: ./check_deploy_ready.sh"