Files
opantoantro/cc-remote.sh
T

140 lines
4.5 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
set -euo pipefail
# ──────────────────────────────────────────────
# optoant Remote Gateway → Claude Code Launcher
# ──────────────────────────────────────────────
# Tüm yapılandırma .env dosyasından okunur.
# GATEWAY_URL tanımlandıysa parametresiz çalışır.
#
# .env'de olması gerekenler:
# GATEWAY_URL=http://sunucu-ip:8000 (zorunlu)
# OPENAI_KEY=... (API anahtarı)
#
# Opsiyonel override:
# ./cc-remote.sh http://10.0.0.5:8000 (URL override)
# ./cc-remote.sh --print (sadece env'leri göster)
# ──────────────────────────────────────────────
# Çağrıldığı dizini kaydet
ORIG_DIR="$PWD"
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
# ── .env'yi yükle (API key için) ──
if [[ -f "$SCRIPT_DIR/.env" ]]; then
set -a
source "$SCRIPT_DIR/.env"
set +a
fi
# ── Gateway URL: ilk argüman (-- ile başlamıyorsa) ya da GATEWAY_URL env ──
GATEWAY_URL="${GATEWAY_URL:-}"
if [[ "${1:-}" != "" && "$1" != -* ]]; then
GATEWAY_URL="$1"
shift
fi
if [[ -z "$GATEWAY_URL" ]]; then
echo "❌ GATEWAY_URL tanımlı değil."
echo " .env dosyasına ekleyin: GATEWAY_URL=http://sunucu-ip:8000"
echo " Veya parametreyle: ./cc-remote.sh http://sunucu-ip:8000"
exit 1
fi
ANTHROPIC_ENDPOINT="${ANTHROPIC_BASE_URL:-${GATEWAY_URL}/anthropic}"
# ── Print mode ──
print_env() {
echo "┌─ optoant Remote → Claude Code Environment"
echo "│ ANTHROPIC_BASE_URL=${ANTHROPIC_ENDPOINT}"
echo "│ ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY:-${OPENAI_KEY:-}:0:20}... (masked)"
echo "│ Gateway: ${GATEWAY_URL}/health"
echo "└─"
}
case "${1:-}" in
--print|-p)
print_env
exit 0
;;
--help|-h)
sed -n '2,17p' "$0"
exit 0
;;
esac
# ── Gateway health check ──
echo "🔍 Gateway kontrol ediliyor: ${GATEWAY_URL}/health"
if curl -sf "${GATEWAY_URL}/health" > /dev/null 2>&1; then
echo "✅ Gateway erişilebilir"
else
echo "⚠️ Gateway'e ulaşılamadı, yine de bağlanmayı deneyeceğim"
fi
print_env
# ── Claude Code'u başlat ──
echo "┌─ Claude Code başlatılıyor (remote gateway)..."
echo "│ Gateway: ${ANTHROPIC_ENDPOINT}"
echo "│ Not: Streaming desteklenmiyor. ~/.claude/settings.json'a"
echo '│ "stream": false eklendi.'
echo "│ Çıkmak için: exit veya Ctrl+C"
echo "└─"
echo ""
if [[ $EUID -eq 0 ]]; then
# ── Root: non-root kullanıcı oluştur, su ile başlat ──
CLAUD_USER="optoant-claude"
if ! id "$CLAUD_USER" &>/dev/null; then
echo "👤 Non-root kullanıcı oluşturuluyor: $CLAUD_USER"
useradd -m -s /bin/bash "$CLAUD_USER"
if [[ -d /root/.claude ]]; then
cp -r /root/.claude "/home/${CLAUD_USER}/"
chown -R "${CLAUD_USER}:${CLAUD_USER}" "/home/${CLAUD_USER}/.claude"
fi
fi
CLAUD_HOME="/home/${CLAUD_USER}"
CLAUD_PROJECT="${CLAUD_HOME}/opantoantro"
if [[ ! -d "${CLAUD_PROJECT}" ]]; then
echo "📁 Proje kopyalanıyor: ${CLAUD_PROJECT}"
cp -r /root/opantoantro "${CLAUD_PROJECT}"
chown -R "${CLAUD_USER}:${CLAUD_USER}" "${CLAUD_PROJECT}"
fi
CLAUD_BIN="/usr/local/bin/claude"
if [[ ! -x "$CLAUD_BIN" ]]; then
SRC=$(find /root/.nvm -name "claude" -o -name "claude.exe" -type f 2>/dev/null | head -1)
if [[ -z "$SRC" ]]; then
echo "❌ claude binary bulunamadı. Önce: npm install -g @anthropic-ai/claude-code"
exit 1
fi
cp "$SRC" "$CLAUD_BIN"
chmod 755 "$CLAUD_BIN"
echo "📦 Claude Code kopyalandı: ${CLAUD_BIN}"
fi
CLAUD_RUNNER="/tmp/cc-remote-run-${CLAUD_USER}.sh"
cat > "$CLAUD_RUNNER" <<- WRAPPER
#!/usr/bin/env bash
cd "${ORIG_DIR}" 2>/dev/null || cd "${CLAUD_PROJECT}"
export ANTHROPIC_BASE_URL="${ANTHROPIC_ENDPOINT}"
export ANTHROPIC_API_KEY="${ANTHROPIC_API_KEY:-${OPENAI_KEY:-}}"
exec "${CLAUD_BIN}" --settings '{"stream": false}' "\$@"
WRAPPER
chmod +x "$CLAUD_RUNNER"
SAFE_ARGS=""
for arg in "$@"; do
SAFE_ARGS="${SAFE_ARGS} $(printf '%q' "$arg")"
done
su - "${CLAUD_USER}" -c "${CLAUD_RUNNER}${SAFE_ARGS}"
rm -f "$CLAUD_RUNNER"
else
# ── Non-root: doğrudan başlat ──
cd "$ORIG_DIR"
ANTHROPIC_BASE_URL="${ANTHROPIC_ENDPOINT}" \
ANTHROPIC_API_KEY="${ANTHROPIC_API_KEY:-${OPENAI_KEY:-}}" \
claude --settings '{"stream": false}' "$@"
fi