File size: 6,324 Bytes
6f5bb62
 
 
804f82a
 
6f5bb62
 
 
804f82a
6f5bb62
 
804f82a
6f5bb62
 
804f82a
6f5bb62
 
 
804f82a
6f5bb62
 
804f82a
 
6f5bb62
804f82a
 
 
 
 
 
 
 
 
6f5bb62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
804f82a
6f5bb62
 
804f82a
6f5bb62
 
 
 
 
 
804f82a
 
6f5bb62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
804f82a
 
 
 
 
6f5bb62
 
804f82a
6f5bb62
 
 
 
 
804f82a
 
6f5bb62
804f82a
 
 
6f5bb62
 
804f82a
6f5bb62
 
 
804f82a
 
6f5bb62
 
 
 
804f82a
6f5bb62
804f82a
6f5bb62
804f82a
6f5bb62
804f82a
6f5bb62
 
 
 
804f82a
 
 
 
 
 
 
6f5bb62
 
 
 
 
 
 
804f82a
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
#!/bin/bash
# ─────────────────────────────────────────────────────────────────
#  Code Server Β· Startup Script
#  Runs as: root  |  Port: 7860  |  Extensions: image-local
#  Persisted to /data: workspace + User settings only
# ─────────────────────────────────────────────────────────────────
set -euo pipefail

# ── Storage paths ────────────────────────────────────────────────
if [ -d "/data" ]; then
  DATA_DIR="/data"
  echo "βœ… /data storage bucket mounted"
else
  DATA_DIR="/root/.local/codeserver-data"
  echo "⚠️  /data not found β€” using local storage (not persistent)"
fi

WORKSPACE="${DATA_DIR}/workspace"
CS_USER_DATA="${DATA_DIR}/vscode-user-data"
CS_LOGS="${DATA_DIR}/logs"

# Extensions stay LOCAL β€” never on the bucket (S3 FUSE can't atomic-rename)
CS_EXTENSIONS_DIR="/root/.local/share/code-server/extensions"

# ── Create directories safely ────────────────────────────────────
mkdir -p \
  "$WORKSPACE" \
  "$CS_USER_DATA/User" \
  "$CS_LOGS" \
  "$CS_EXTENSIONS_DIR"

# ── VS Code settings (written once; user can edit inside editor) ─
SETTINGS_FILE="$CS_USER_DATA/User/settings.json"
if [ ! -f "$SETTINGS_FILE" ]; then
  cat > "$SETTINGS_FILE" << 'SETTINGS'
{
  "workbench.colorTheme": "Default Dark+",
  "workbench.iconTheme": "vs-seti",
  "editor.fontSize": 14,
  "editor.fontFamily": "monospace",
  "editor.tabSize": 2,
  "editor.formatOnSave": true,
  "editor.wordWrap": "on",
  "editor.minimap.enabled": true,
  "editor.bracketPairColorization.enabled": true,
  "editor.guides.bracketPairs": true,
  "editor.smoothScrolling": true,
  "editor.cursorBlinking": "smooth",
  "terminal.integrated.fontSize": 13,
  "terminal.integrated.scrollback": 5000,
  "terminal.integrated.defaultProfile.linux": "bash",
  "files.autoSave": "afterDelay",
  "files.autoSaveDelay": 1000,
  "files.trimTrailingWhitespace": true,
  "workbench.startupEditor": "none",
  "git.autofetch": true,
  "git.confirmSync": false,
  "extensions.autoUpdate": false,
  "telemetry.telemetryLevel": "off",
  "update.mode": "none"
}
SETTINGS
  echo "βœ… VS Code settings written"
fi

# ── code-server config ───────────────────────────────────────────
mkdir -p /root/.config/code-server
cat > /root/.config/code-server/config.yaml << CSCONFIG
bind-addr: 0.0.0.0:7860
auth: password
password: ${PASSWORD:-codeserver123}
cert: false
user-data-dir: ${CS_USER_DATA}
extensions-dir: ${CS_EXTENSIONS_DIR}
CSCONFIG

# ── Git config ───────────────────────────────────────────────────
git config --global init.defaultBranch main
git config --global pull.rebase false
git config --global core.editor "code --wait"
[ -n "${GIT_USER_NAME:-}"  ] && git config --global user.name  "$GIT_USER_NAME"
[ -n "${GIT_USER_EMAIL:-}" ] && git config --global user.email "$GIT_USER_EMAIL"

if [ -n "${GITHUB_TOKEN:-}" ]; then
  git config --global \
    url."https://${GITHUB_TOKEN}@github.com/".insteadOf "https://github.com/"
  echo "βœ… GitHub token configured"
fi

# ── HuggingFace login ────────────────────────────────────────────
if [ -n "${HF_TOKEN:-}" ]; then
  huggingface-cli login --token "$HF_TOKEN" --add-to-git-credential 2>/dev/null || true
  echo "βœ… HuggingFace token configured"
fi

# ── Welcome README (only on first boot) ─────────────────────────
README_FILE="$WORKSPACE/README.md"
if [ ! -f "$README_FILE" ]; then
  # Write via tee to avoid heredoc redirect issues on FUSE mounts
  tee "$README_FILE" > /dev/null << 'WSREADME'
# πŸš€ Code Server β€” Cloud Dev Environment

Your persistent workspace is backed by HF Storage Bucket at `/data`.

## πŸ“ Paths
| Path | Purpose |
|------|---------|
| `/data/workspace` | Your code (this folder) |
| `/data/vscode-user-data` | Settings & keybindings |
| `/root/.local/share/code-server/extensions` | Extensions (image-local) |

## πŸ”Œ Port Proxy
Access any running server:
https://-.hf.space/proxy/PORT/
Example: app on port 3000 β†’ `/proxy/3000/`

## πŸ” Secrets (Space Settings β†’ Repository secrets)
| Secret | Purpose |
|--------|---------|
| `PASSWORD` | code-server login password |
| `GITHUB_TOKEN` | Private GitHub repo auth |
| `HF_TOKEN` | HuggingFace CLI auth |
| `GIT_USER_NAME` | Git commit name |
| `GIT_USER_EMAIL` | Git commit email |

## πŸ› οΈ Pre-installed
- Python 3, pip, venv, numpy, pandas, black
- Node.js 20, npm, yarn, pnpm
- Git, GitHub CLI (gh)
- build-essential, cmake, curl, wget, jq, ripgrep
- Extensions: Python, Jupyter, Prettier, GitLens, Material Icons
WSREADME
  echo "βœ… Workspace README created"
fi

# ── Launch ───────────────────────────────────────────────────────
echo ""
echo "╔══════════════════════════════════════════════════╗"
echo "β•‘  πŸ–₯️  Code Server starting...                      β•‘"
echo "╠══════════════════════════════════════════════════╣"
echo "β•‘  Workspace  : $WORKSPACE"
echo "β•‘  Extensions : $CS_EXTENSIONS_DIR"
echo "β•‘  Port       : 7860"
echo "β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•"
echo ""

exec code-server \
  --config /root/.config/code-server/config.yaml \
  --disable-telemetry \
  --disable-update-check \
  "$WORKSPACE" \
  2>&1 | tee -a "$CS_LOGS/code-server.log"