#!/usr/bin/env bash set -e echo "=== Coder.ai Environment Setup ===" echo "" # Configuration - use current directory as persistent storage PERSISTENT_DIR="$PWD" echo "This script will use the following directory for persistent storage:" echo " $PERSISTENT_DIR" echo "" read -p "Is this correct? (y/n): " -n 1 -r echo "" if [[ ! $REPLY =~ ^[Yy]$ ]]; then echo "Setup cancelled. Please cd to your persistent storage directory and run again." exit 1 fi SSH_DIR="$PERSISTENT_DIR/ssh" SSH_KEY="$SSH_DIR/id_ed25519" # ----------------------------- # Create SSH directory structure # ----------------------------- echo "Setting up SSH directory in persistent storage..." mkdir -p "$SSH_DIR" chmod 700 "$SSH_DIR" # ----------------------------- # Generate SSH key if needed # ----------------------------- if [[ -f "$SSH_KEY" && -f "$SSH_KEY.pub" ]]; then echo "SSH key already exists at $SSH_KEY" echo "" else echo "Generating new ed25519 SSH key..." ssh-keygen -t ed25519 -f "$SSH_KEY" -N "" -C "coder-workspace" chmod 600 "$SSH_KEY" chmod 644 "$SSH_KEY.pub" echo "SSH key generated successfully" echo "" fi # ----------------------------- # Test GitHub connectivity # ----------------------------- echo "Testing GitHub connectivity..." echo "" GITHUB_PORT=22 GITHUB_HOST="github.com" # Test port 22 with a short timeout echo "Testing GitHub on port 22..." if timeout 3 ssh -T -p 22 -o ConnectTimeout=3 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i "$SSH_KEY" git@github.com 2>&1 | grep -q "successfully authenticated"; then echo "Port 22 is working" GITHUB_PORT=22 GITHUB_HOST="github.com" else # Port 22 failed, try port 443 echo "Port 22 failed or timed out, testing port 443..." if timeout 3 ssh -T -p 443 -o ConnectTimeout=3 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i "$SSH_KEY" git@ssh.github.com 2>&1 | grep -q "successfully authenticated"; then echo "Port 443 is working, using ssh.github.com:443" GITHUB_PORT=443 GITHUB_HOST="ssh.github.com" else echo "WARNING: Neither port 22 nor 443 worked. Using port 443 as default." echo "You may need to add your SSH key to GitHub before this works." GITHUB_PORT=443 GITHUB_HOST="ssh.github.com" fi fi # ----------------------------- # Create SSH config # ----------------------------- echo "Creating SSH configuration..." cat > "$SSH_DIR/config" <