Spaces:
Sleeping
Sleeping
File size: 1,737 Bytes
f580254 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | #!/usr/bin/env sh
set -eu
# Write SSH keys from env vars into /root/.ssh with correct permissions.
# Handles both real newlines and literal \n sequences in SSH_PRIVATE_KEY.
HOME_DIR="${HOME:-/root}"
SSH_DIR="$HOME_DIR/.ssh"
mkdir -p "$SSH_DIR"
chmod 700 "$SSH_DIR"
# Public key (optional)
if [ -n "${SSH_PUBLIC_KEY:-}" ]; then
printf "%s\n" "$SSH_PUBLIC_KEY" > "$SSH_DIR/id_rsa.pub"
chmod 644 "$SSH_DIR/id_rsa.pub"
fi
# Private key (optional)
if [ -n "${SSH_PRIVATE_KEY:-}" ]; then
# If the key contains literal \n sequences, convert them to newlines.
if printf "%s" "$SSH_PRIVATE_KEY" | grep -q '\\n'; then
printf "%s" "$SSH_PRIVATE_KEY" | sed 's/\\n/\n/g' > "$SSH_DIR/id_rsa"
else
printf "%s" "$SSH_PRIVATE_KEY" > "$SSH_DIR/id_rsa"
fi
chmod 600 "$SSH_DIR/id_rsa"
fi
# Prime known_hosts to avoid host key prompts for common git hosts
if command -v ssh-keyscan >/dev/null 2>&1; then
for host in github.com gitlab.com; do
ssh-keyscan -H "$host" 2>/dev/null >> "$SSH_DIR/known_hosts" || true
done
# Optionally pre-seed remote backup host if provided via REMOTE_HOST (user@host)
if [ -n "${REMOTE_HOST:-}" ]; then
remote_host_domain="$(printf "%s" "$REMOTE_HOST" | awk -F'@' '{print $NF}')"
if [ -n "$remote_host_domain" ]; then
ssh-keyscan -H "$remote_host_domain" 2>/dev/null >> "$SSH_DIR/known_hosts" || true
fi
fi
chmod 644 "$SSH_DIR/known_hosts" || true
fi
#Optionally run Claude config restore after SSH is ready
if command -v download_claude_backup.sh >/dev/null 2>&1; then
echo "[ssh-setup] Running download_claude_backup.sh to restore config..." >&2
download_claude_backup.sh || true
fi
# rm /app/scripts/*.sh
# rm ~/.ssh/id_rsa
# rm ~/.ssh/id_rsa.pub
exec "$@"
|