mnemo_cards/tools/scripts/fix_ssh_key.sh

82 lines
2.2 KiB
Bash
Raw Normal View History

#!/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"
2025-11-19 22:41:54 +00:00
print_info "Generate it first with: ssh-keygen -t ed25519 -C 'forgejo-deploy@mnemo-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"