forked from 1rgs/claude-code-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_env.sh
More file actions
executable file
·236 lines (217 loc) · 8.78 KB
/
setup_env.sh
File metadata and controls
executable file
·236 lines (217 loc) · 8.78 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/env bash
#
# Claude Code Proxy — create Python virtual environment and check prerequisites.
# If venv already exists, it is removed and recreated from scratch.
# Prerequisites: curl, git, Homebrew (macOS), Python 3.10+, uv, Google Cloud SDK (gcloud).
# Run from repo root.
#
set -e
VENV_DIR=".venv"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR" && pwd)"
# Detect OS
case "$(uname -s)" in
Darwin) OS="macos" ;;
Linux) OS="linux" ;;
*) OS="other" ;;
esac
echo "=== Claude Code Proxy — env setup (prerequisites + venv) ==="
cd "$REPO_ROOT"
# -----------------------------------------------------------------------------
# curl — required for downloading installers (Homebrew, uv, gcloud)
# -----------------------------------------------------------------------------
if ! command -v curl &>/dev/null; then
if [[ "$OS" == "macos" ]] && command -v xcode-select &>/dev/null; then
echo "Installing Xcode Command Line Tools (provides curl)..."
xcode-select --install 2>/dev/null || true
echo "After the installer finishes, re-run this script."
exit 1
elif [[ "$OS" == "linux" ]]; then
if command -v apt-get &>/dev/null; then
echo "Installing curl..."
sudo apt-get update -qq && sudo apt-get install -y curl
elif command -v dnf &>/dev/null; then
echo "Installing curl..."
sudo dnf install -y curl
else
echo "Error: curl is required. Install curl and re-run."
exit 1
fi
else
echo "Error: curl is required for installs. Install curl and re-run."
exit 1
fi
fi
echo "curl: $(curl --version 2>/dev/null | head -1 || true)"
# -----------------------------------------------------------------------------
# Homebrew (macOS) — used to install Python, git, and gcloud if missing
# -----------------------------------------------------------------------------
if [[ "$OS" == "macos" ]]; then
if ! command -v brew &>/dev/null; then
echo "Installing Homebrew..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Add brew to PATH for this session (common post-install path on Apple Silicon)
if [[ -x /opt/homebrew/bin/brew ]]; then
eval "$(/opt/homebrew/bin/brew shellenv)"
elif [[ -x /usr/local/bin/brew ]]; then
eval "$(/usr/local/bin/brew shellenv)"
fi
fi
echo "Homebrew: $(brew --version 2>/dev/null | head -1 || true)"
fi
# -----------------------------------------------------------------------------
# git — optional; used for cloning and version control
# -----------------------------------------------------------------------------
if ! command -v git &>/dev/null; then
if [[ "$OS" == "macos" ]] && command -v brew &>/dev/null; then
echo "Installing git via Homebrew..."
brew install git
elif [[ "$OS" == "linux" ]]; then
if command -v apt-get &>/dev/null; then
echo "Installing git..."
sudo apt-get install -y git
elif command -v dnf &>/dev/null; then
echo "Installing git..."
sudo dnf install -y git
fi
fi
fi
if command -v git &>/dev/null; then
echo "git: $(git --version 2>/dev/null || true)"
# Check if git credentials look configured (for cloning private repos)
GIT_CRED_OK=false
if git config --global credential.helper &>/dev/null; then
GIT_CRED_OK=true
fi
if [[ -f "${HOME}/.ssh/id_ed25519" ]] || [[ -f "${HOME}/.ssh/id_rsa" ]]; then
GIT_CRED_OK=true
fi
if [[ "$GIT_CRED_OK" != true ]]; then
echo " Note: No git credential helper or SSH key found. For private repos, set up: git config credential.helper store (or cache), or add an SSH key to your Git host."
fi
fi
# -----------------------------------------------------------------------------
# Python 3.10+ (required)
# -----------------------------------------------------------------------------
PYTHON_BIN=""
PIP_BIN=""
if command -v pyenv &>/dev/null; then
eval "$(pyenv init -)" 2>/dev/null || true
if ! pyenv versions --bare 2>/dev/null | grep -qE '^3\.(10|11|12)'; then
echo "Installing Python 3.11.11 via pyenv..."
pyenv install 3.11.11
fi
pyenv local 3.11.11 2>/dev/null || true
PYTHON_BIN="python"
PIP_BIN="pip"
fi
if [[ -z "$PYTHON_BIN" ]]; then
for v in 3.11 3.10 3.12; do
if command -v "python${v}" &>/dev/null; then
PYTHON_BIN="python${v}"
PIP_BIN="pip${v}"
break
fi
done
if [[ -z "$PYTHON_BIN" ]] && command -v python3 &>/dev/null; then
ver=$(python3 -c 'import sys; print(sys.version_info.minor)' 2>/dev/null || echo "0")
if [[ -n "$ver" ]] && [[ "$ver" -ge 10 ]]; then
PYTHON_BIN="python3"
PIP_BIN="pip3"
fi
fi
fi
if [[ -z "$PYTHON_BIN" ]]; then
if [[ "$OS" == "macos" ]] && command -v brew &>/dev/null; then
echo "Installing Python 3.11 via Homebrew..."
brew install python@3.11
PYTHON_BIN="python3.11"
PIP_BIN="pip3.11"
else
echo "Error: Python 3.10+ not found. Install it (e.g. pyenv, brew install python@3.11, or system package)."
exit 1
fi
fi
if [[ -z "$PYTHON_BIN" ]]; then
echo "Error: Could not resolve Python. Install Python 3.10+ and re-run."
exit 1
fi
echo "Python: $($PYTHON_BIN --version)"
# -----------------------------------------------------------------------------
# Google Cloud SDK (gcloud) — optional for running proxy (needed for Vertex scripts)
# -----------------------------------------------------------------------------
if ! command -v gcloud &>/dev/null; then
if [[ "$OS" == "macos" ]] && command -v brew &>/dev/null; then
echo "Installing Google Cloud SDK (gcloud) via Homebrew..."
brew install --cask google-cloud-sdk
for prefix in /opt/homebrew /usr/local; do
gcp="$prefix/Caskroom/google-cloud-sdk/latest/google-cloud-sdk"
if [[ -d "$gcp" ]]; then
export PATH="$gcp/bin:$PATH"
break
fi
done
elif [[ "$OS" == "linux" ]]; then
echo "Installing Google Cloud SDK (gcloud)..."
GCLOUD_ROOT="${HOME}/google-cloud-sdk"
curl -fsSL https://sdk.cloud.google.com | bash -s -- --install-dir="${HOME}" --disable-prompts
if [[ -f "${GCLOUD_ROOT}/path.bash.inc" ]]; then
# shellcheck source=/dev/null
source "${GCLOUD_ROOT}/path.bash.inc"
fi
export PATH="${GCLOUD_ROOT}/bin:${PATH}"
else
echo "Warning: gcloud not found. Install from https://cloud.google.com/sdk/docs/install for Vertex scripts. Proxy can still run with a service account key file."
fi
fi
if command -v gcloud &>/dev/null; then
echo "gcloud: $(gcloud --version 2>/dev/null | head -1 || true)"
fi
# -----------------------------------------------------------------------------
# uv — Python package manager/runner (used in README/Dockerfile: uv run ...)
# -----------------------------------------------------------------------------
if ! command -v uv &>/dev/null; then
echo "Installing uv..."
curl -LsSf https://astral.sh/uv/install.sh | sh
export PATH="${HOME}/.local/bin:${PATH}"
if ! command -v uv &>/dev/null; then
export PATH="${HOME}/.cargo/bin:${PATH}"
fi
fi
if command -v uv &>/dev/null; then
echo "uv: $(uv --version 2>/dev/null || true)"
fi
# -----------------------------------------------------------------------------
# Virtual environment and project install (using uv)
# -----------------------------------------------------------------------------
if [[ -d "$VENV_DIR" ]]; then
echo "Removing existing $VENV_DIR (starting over)..."
rm -rf "$VENV_DIR"
fi
if command -v uv &>/dev/null; then
echo "Creating virtual environment in $VENV_DIR with uv..."
uv venv "$VENV_DIR" --python "$PYTHON_BIN"
echo "Installing project and dependencies with uv (from lockfile)..."
if [[ -f "$REPO_ROOT/uv.lock" ]]; then
uv sync --locked
else
uv sync
fi
else
echo "Creating virtual environment in $VENV_DIR..."
"$PYTHON_BIN" -m venv "$VENV_DIR"
echo "Installing project and dependencies with pip..."
# shellcheck source=/dev/null
source "$VENV_DIR/bin/activate"
pip install --upgrade pip
pip install -e .
fi
echo ""
echo "=== Done ==="
echo " Activate: source $VENV_DIR/bin/activate"
echo " Run proxy: uv run uvicorn server:app --host 127.0.0.1 --port 8082"
echo " (or with env active: uvicorn server:app --host 127.0.0.1 --port 8082)"
echo " .env: cp .env.example .env then edit .env"
if ! command -v gcloud &>/dev/null; then
echo " (Install gcloud for Vertex: https://cloud.google.com/sdk/docs/install)"
fi