forked from sphinxcode/claude-code-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint.sh
More file actions
352 lines (288 loc) · 13.1 KB
/
entrypoint.sh
File metadata and controls
352 lines (288 loc) · 13.1 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/bin/bash
set -e
# ============================================================================
# Claude Code Server - Entrypoint
# Handles permission fix, optional user switching, and service startup
# ============================================================================
echo "╔══════════════════════════════════════════════════════════════════════╗"
echo "║ Claude Code Server - VS Code + AI in the Browser ║"
echo "╚══════════════════════════════════════════════════════════════════════╝"
echo ""
# ============================================================================
# CONFIGURABLE PATHS AND USER
# ============================================================================
CLAUDER_HOME="${CLAUDER_HOME:-/home/clauder}"
CLAUDER_UID="${CLAUDER_UID:-1000}"
CLAUDER_GID="${CLAUDER_GID:-1000}"
# RUN_AS_USER: Defaults to "clauder" for non-root. Set to "root" if needed.
RUN_AS_USER="${RUN_AS_USER:-clauder}"
export HOME="$CLAUDER_HOME"
export XDG_DATA_HOME="$CLAUDER_HOME/.local/share"
export XDG_CONFIG_HOME="$CLAUDER_HOME/.config"
export XDG_CACHE_HOME="$CLAUDER_HOME/.cache"
export XDG_STATE_HOME="$CLAUDER_HOME/.local/state"
# PATH: Include all possible locations for installed tools
# - ~/.local/bin: pip user installs, pipx, local scripts
# - ~/.npm-global/bin: npm global installs (non-root)
# - /usr/local/bin: system-wide installs
# - /usr/lib/node_modules/.bin: npm global installs (root/sudo)
export PATH="$CLAUDER_HOME/.local/bin:$CLAUDER_HOME/.npm-global/bin:$CLAUDER_HOME/.local/node/bin:$CLAUDER_HOME/.claude/local:$CLAUDER_HOME/node_modules/.bin:/usr/local/bin:/usr/bin:/usr/lib/node_modules/.bin:/usr/lib/code-server/lib/vscode/bin/remote-cli:$PATH"
echo "→ Initial user: $(whoami) (UID: $(id -u))"
echo "→ RUN_AS_USER: $RUN_AS_USER"
echo "→ HOME: $HOME"
# ============================================================================
# DIRECTORY CREATION AND PERMISSION FIX
# ============================================================================
if [ "$(id -u)" = "0" ]; then
echo ""
echo "→ Running setup as root..."
# Create directories if they don't exist
mkdir -p "$XDG_DATA_HOME" \
"$XDG_CONFIG_HOME" \
"$XDG_CACHE_HOME" \
"$XDG_STATE_HOME" \
"$HOME/.local/bin" \
"$HOME/.local/node" \
"$HOME/.claude" \
"$HOME/entrypoint.d" \
"$HOME/workspace" \
"$XDG_DATA_HOME/code-server/extensions" \
"$XDG_CONFIG_HOME/code-server" 2>/dev/null || true
# ========================================================================
# SHELL PROFILE SETUP
# ========================================================================
PROFILE_FILE="$HOME/.bashrc"
if [ ! -f "$PROFILE_FILE" ] || ! grep -q '.npm-global' "$PROFILE_FILE" 2>/dev/null; then
echo "→ Setting up shell profile..."
cat >> "$PROFILE_FILE" << 'PROFILE'
# ============================================================================
# Claude Code Server - PATH Configuration
# ============================================================================
export PATH="$HOME/.local/bin:$HOME/.npm-global/bin:$HOME/.local/node/bin:$HOME/.claude/local:$PATH"
# npm global prefix for non-root installs
export NPM_CONFIG_PREFIX="$HOME/.npm-global"
# Claude Code alias with --dangerously-skip-permissions
alias claude-auto='claude --dangerously-skip-permissions'
PROFILE
# Create npm global directory
mkdir -p "$HOME/.npm-global/bin" 2>/dev/null || true
echo " ✓ Shell profile configured"
fi
# Also set up .profile for login shells
if [ ! -f "$HOME/.profile" ] || ! grep -q '.local/bin' "$HOME/.profile" 2>/dev/null; then
cat >> "$HOME/.profile" << 'PROFILE'
# Load .bashrc for interactive shells
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
PROFILE
fi
# ========================================================================
# USER SWITCHING (if RUN_AS_USER=clauder)
# ========================================================================
if [ "$RUN_AS_USER" = "clauder" ]; then
echo "→ Fixing permissions for clauder user (UID: $CLAUDER_UID)..."
chown -R "$CLAUDER_UID:$CLAUDER_GID" "$CLAUDER_HOME" 2>/dev/null || true
echo " ✓ Permissions fixed"
# Check if gosu is available
if command -v gosu &>/dev/null; then
echo "→ Switching to clauder user via gosu..."
exec gosu "$CLAUDER_UID:$CLAUDER_GID" "$0" "$@"
else
echo " ⚠ gosu not found, staying as root"
fi
else
echo "→ Staying as root (set RUN_AS_USER=clauder to switch)"
# Create symlinks from /root to volume for persistence
mkdir -p /root/.local 2>/dev/null || true
for dir in ".local/share" ".local/bin" ".local/node" ".config" ".cache" ".claude"; do
target="$CLAUDER_HOME/$dir"
link="/root/$dir"
if [ -d "$target" ] && [ ! -L "$link" ]; then
rm -rf "$link" 2>/dev/null || true
mkdir -p "$(dirname "$link")" 2>/dev/null || true
ln -sf "$target" "$link" 2>/dev/null || true
fi
done
echo " ✓ Root directories symlinked to $CLAUDER_HOME"
fi
fi
# ============================================================================
# RUNNING AS FINAL USER
# ============================================================================
echo ""
echo "→ Running as: $(whoami) (UID: $(id -u))"
# ============================================================================
# FIRST RUN SETUP
# ============================================================================
FIRST_RUN_MARKER="$XDG_DATA_HOME/.vscode-cloud-initialized"
if [ ! -f "$FIRST_RUN_MARKER" ]; then
echo "→ First run detected - initializing..."
if [ ! -f "$HOME/workspace/README.md" ]; then
cat > "$HOME/workspace/README.md" << 'WELCOME'
# Welcome to Claude Code Server
Your cloud development environment is ready!
## Features
- **Claude Code CLI** - Pre-installed and ready to use
- **TOTP 2FA** - Secured with authenticator app
- **Node.js 22** - Pre-installed and ready to use
- **Persistent Extensions** - Install once, keep forever
- **Full Terminal** - npm, git, and more
## Quick Start
```bash
# Start Claude Code (with auto-accept for automation)
claude --dangerously-skip-permissions
# Or use the alias
claude-auto
# Interactive mode
claude
```
You'll need to authenticate with your Anthropic API key on first use.
WELCOME
fi
# Copy pre-bundled extensions from image to volume
if [ -d /opt/default-extensions ] && [ "$(ls -A /opt/default-extensions 2>/dev/null)" ]; then
echo "→ Installing pre-bundled extensions..."
mkdir -p "$XDG_DATA_HOME/code-server/extensions" 2>/dev/null || true
cp -rn /opt/default-extensions/* "$XDG_DATA_HOME/code-server/extensions/" 2>/dev/null || true
echo " ✓ Extensions installed"
fi
# Default VS Code settings (Claude Code as chat provider)
SETTINGS_DIR="$XDG_DATA_HOME/code-server/User"
SETTINGS_FILE="$SETTINGS_DIR/settings.json"
if [ ! -f "$SETTINGS_FILE" ]; then
mkdir -p "$SETTINGS_DIR" 2>/dev/null || true
cat > "$SETTINGS_FILE" << 'SETTINGS'
{
"workbench.colorTheme": "Default Dark+",
"chat.agent.enabled": true,
"workbench.secondarySideBar.defaultVisibility": "visible",
"claudeCode.preferredLocation": "sidebar",
"claudeCode.useTerminal": false
}
SETTINGS
echo " ✓ Default settings configured"
fi
# Default Claude Code model to Opus
CLAUDE_SETTINGS_DIR="$HOME/.claude"
CLAUDE_SETTINGS_FILE="$CLAUDE_SETTINGS_DIR/settings.json"
if [ ! -f "$CLAUDE_SETTINGS_FILE" ]; then
mkdir -p "$CLAUDE_SETTINGS_DIR" 2>/dev/null || true
cat > "$CLAUDE_SETTINGS_FILE" << 'SETTINGS'
{
"model": "opus"
}
SETTINGS
echo " ✓ Claude Code default model set to Opus"
fi
touch "$FIRST_RUN_MARKER" 2>/dev/null || true
echo " ✓ Initialization complete"
fi
# ============================================================================
# ENVIRONMENT VERIFICATION
# ============================================================================
echo ""
echo "Environment:"
# Node.js - show source
if [ -x "$CLAUDER_HOME/.local/node/bin/node" ]; then
echo " → Node.js: $(node --version 2>/dev/null) [volume]"
else
echo " → Node.js: $(node --version 2>/dev/null || echo 'not found') [image]"
fi
# npm
echo " → npm: $(npm --version 2>/dev/null || echo 'not found')"
# git
echo " → git: $(git --version 2>/dev/null | cut -d' ' -f3 || echo 'not found')"
# GitHub CLI
echo " → gh: $(gh --version 2>/dev/null | head -1 | cut -d' ' -f3 || echo 'not found')"
# Claude Code - show source
if [ -x "$CLAUDER_HOME/.local/bin/claude" ]; then
echo " → claude: $(claude --version 2>/dev/null || echo 'installed') [volume ~/.local/bin]"
elif [ -x "$CLAUDER_HOME/.claude/local/claude" ]; then
echo " → claude: $(claude --version 2>/dev/null || echo 'installed') [volume ~/.claude/local]"
elif command -v claude &>/dev/null; then
echo " → claude: $(claude --version 2>/dev/null || echo 'installed') [image]"
else
echo " → claude: not installed"
fi
# Extensions count
if [ -d "$XDG_DATA_HOME/code-server/extensions" ]; then
EXT_COUNT=$(find "$XDG_DATA_HOME/code-server/extensions" -maxdepth 1 -type d 2>/dev/null | wc -l)
EXT_COUNT=$((EXT_COUNT - 1))
if [ $EXT_COUNT -gt 0 ]; then
echo " → Extensions: $EXT_COUNT installed"
fi
fi
# ============================================================================
# CUSTOM STARTUP SCRIPTS
# ============================================================================
if [ -d "$HOME/entrypoint.d" ]; then
for script in "$HOME/entrypoint.d"/*.sh; do
if [ -f "$script" ] && [ -x "$script" ]; then
echo ""
echo "Running: $(basename "$script")"
"$script" || echo " ⚠ Script exited with code $?"
fi
done
fi
# ============================================================================
# SESSION SECRET (auto-generate and persist if not set)
# ============================================================================
if [ -z "${SESSION_SECRET:-}" ]; then
SESSION_SECRET_FILE="$XDG_CONFIG_HOME/claude-2fa/session-secret"
mkdir -p "$(dirname "$SESSION_SECRET_FILE")" 2>/dev/null || true
if [ ! -f "$SESSION_SECRET_FILE" ]; then
openssl rand -hex 32 > "$SESSION_SECRET_FILE"
chmod 600 "$SESSION_SECRET_FILE"
echo "→ Generated new session secret"
fi
export SESSION_SECRET=$(cat "$SESSION_SECRET_FILE")
fi
# ============================================================================
# START SERVICES
# ============================================================================
APP_NAME="${APP_NAME:-Claude Code Server}"
WELCOME_TEXT="${WELCOME_TEXT:-Welcome to Claude Code Server}"
echo ""
echo "════════════════════════════════════════════════════════════════════════"
echo "Starting $APP_NAME as $(whoami)..."
echo "════════════════════════════════════════════════════════════════════════"
echo ""
# Force code-server config to use port 8080 on localhost
# (without this, code-server picks up the PORT env var set by platforms like Coolify)
mkdir -p "$XDG_CONFIG_HOME/code-server" 2>/dev/null || true
cat > "$XDG_CONFIG_HOME/code-server/config.yaml" << EOF
bind-addr: 127.0.0.1:8080
auth: none
cert: false
EOF
# Clear PORT env var - PaaS platforms (Coolify, etc.) set this and code-server reads it,
# overriding both config file and CLI flags
unset PORT
# Start code-server in background on internal port
echo "→ Starting code-server on 127.0.0.1:8080 (internal)..."
dumb-init /usr/bin/code-server \
--bind-addr 127.0.0.1:8080 \
--auth none \
--app-name "$APP_NAME" \
--welcome-text "$WELCOME_TEXT" \
"$CLAUDER_HOME/workspace" &
# Wait for code-server to be truly ready (not just accepting connections)
echo "→ Waiting for code-server to be ready..."
for i in $(seq 1 30); do
if curl -sf http://127.0.0.1:8080/healthz > /dev/null 2>&1; then
echo " ✓ code-server ready (${i}s)"
break
fi
if [ "$i" = "30" ]; then
echo " ⚠ code-server not ready after 30s, starting proxy anyway"
fi
sleep 1
done
# Start 2FA auth proxy in foreground on external port
echo "→ Starting 2FA auth proxy on 0.0.0.0:3000..."
export AUTH_PROXY_PORT=3000
export CODE_SERVER_PORT=8080
export CODE_SERVER_HOST=127.0.0.1
export APP_NAME
exec node /usr/lib/auth-proxy/server.js