Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·209 lines (180 loc) · 9.14 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·209 lines (180 loc) · 9.14 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
#!/usr/bin/env bash
# gpu-optimizer install script
# Sets up the vLLM prompt optimizer pipeline for Claude Code.
# Run from the repo root: ./install.sh
set -euo pipefail
REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
HOOK_DEST="$HOME/.claude/hooks"
ASSET_DEST="$HOME/.claude/gpu-optimizer"
CONF_FILE="$HOME/.claude/gpu-optimizer.conf"
SETTINGS_FILE="$HOME/.claude/settings.json"
GREEN='\033[0;32m'; YELLOW='\033[1;33m'; CYAN='\033[0;36m'; NC='\033[0m'
info() { echo -e "${CYAN}[gpu-optimizer]${NC} $*"; }
success() { echo -e "${GREEN}✓${NC} $*"; }
warn() { echo -e "${YELLOW}⚠${NC} $*"; }
# Fix 🔴: single-quote-escape a value for safe embedding in a sourced bash file.
# Replaces every ' with '\'' so the value is always treated as a literal string.
safe_val() { printf "'%s'" "${1//\'/\'\\\'\'}"; }
# Fix 🟡: validate a Kubernetes resource name — [a-z0-9][a-z0-9-]*, max 63 chars.
validate_k8s_name() {
local name="$1" label="$2"
if [[ ! "$name" =~ ^[a-z0-9][a-z0-9-]{0,62}$ ]]; then
echo "Error: '$name' is not a valid Kubernetes $label name." \
"Use lowercase letters, digits, and hyphens only." >&2
exit 1
fi
}
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " gpu-optimizer — Claude Code vLLM pipeline installer"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
# ── check dependencies ────────────────────────────────────────────────────────
for dep in kubectl jq curl shasum; do
if ! command -v "$dep" >/dev/null 2>&1; then
echo "Error: '$dep' is required but not installed." >&2
exit 1
fi
done
success "Dependencies: kubectl, jq, curl, shasum — all found"
# ── collect config ────────────────────────────────────────────────────────────
echo ""
info "Configuration (press Enter to accept defaults)"
echo ""
read -rp " vLLM model name [qwen2.5-14b]: " VLLM_MODEL
VLLM_MODEL="${VLLM_MODEL:-qwen2.5-14b}"
read -rp " Kubernetes namespace [default]: " VLLM_NAMESPACE
VLLM_NAMESPACE="${VLLM_NAMESPACE:-default}"
read -rp " Deployment name [llm-inference]: " VLLM_DEPLOYMENT
VLLM_DEPLOYMENT="${VLLM_DEPLOYMENT:-llm-inference}"
read -rp " Service name [llm-inference]: " VLLM_SERVICE
VLLM_SERVICE="${VLLM_SERVICE:-llm-inference}"
read -rp " PVC name [model-cache]: " VLLM_PVC
VLLM_PVC="${VLLM_PVC:-model-cache}"
read -rp " Min prompt words to optimize [15]: " VLLM_MIN_WORDS
VLLM_MIN_WORDS="${VLLM_MIN_WORDS:-15}"
VLLM_ENDPOINT="http://localhost:8000"
VLLM_MANIFEST="$ASSET_DEST/llm-inference.yaml"
# Fix 🟡 + 🔴: validate k8s names before they reach the config file or any command string.
# This also closes the STOP_CMD single-quote injection: validated names can only be [a-z0-9-].
validate_k8s_name "$VLLM_NAMESPACE" "namespace"
validate_k8s_name "$VLLM_DEPLOYMENT" "deployment"
validate_k8s_name "$VLLM_SERVICE" "service"
validate_k8s_name "$VLLM_PVC" "PVC"
# ── write config file ─────────────────────────────────────────────────────────
echo ""
info "Writing config to $CONF_FILE..."
# Fix 🔴: use safe_val() so user-supplied values are single-quote-escaped.
# A value like `x" && rm -rf ~` becomes `'x" && rm -rf ~'` — a literal string when sourced.
cat > "$CONF_FILE" <<EOF
# gpu-optimizer configuration — generated by install.sh
VLLM_ENDPOINT=$(safe_val "$VLLM_ENDPOINT")
VLLM_MODEL=$(safe_val "$VLLM_MODEL")
VLLM_NAMESPACE=$(safe_val "$VLLM_NAMESPACE")
VLLM_SERVICE=$(safe_val "$VLLM_SERVICE")
VLLM_DEPLOYMENT=$(safe_val "$VLLM_DEPLOYMENT")
VLLM_PVC=$(safe_val "$VLLM_PVC")
VLLM_MANIFEST=$(safe_val "$VLLM_MANIFEST")
VLLM_MIN_WORDS=$(safe_val "$VLLM_MIN_WORDS")
EOF
# Fix 🟠: restrict to owner read/write — conf contains infrastructure names.
chmod 600 "$CONF_FILE"
success "Config written (permissions: 600)"
# ── install hooks ─────────────────────────────────────────────────────────────
info "Installing hooks to $HOOK_DEST..."
mkdir -p "$HOOK_DEST"
for script in prompt-optimizer.sh fetch-compressor.sh gpu-scheduler.sh optimizer-status.sh llm-autoscale-down.sh; do
cp "$REPO_DIR/hooks/$script" "$HOOK_DEST/$script"
chmod +x "$HOOK_DEST/$script"
done
success "5 hooks installed"
# ── install k8s manifest ──────────────────────────────────────────────────────
info "Installing k8s manifest to $ASSET_DEST..."
mkdir -p "$ASSET_DEST"
cp "$REPO_DIR/k8s/llm-inference.yaml" "$ASSET_DEST/llm-inference.yaml"
success "Manifest installed"
# ── patch settings.json ───────────────────────────────────────────────────────
info "Patching $SETTINGS_FILE..."
# Ensure settings.json exists
[ -f "$SETTINGS_FILE" ] || echo '{}' > "$SETTINGS_FILE"
# Fix 🟠: back up before any mutation — settings.json controls all Claude Code hooks.
cp "$SETTINGS_FILE" "${SETTINGS_FILE}.bak"
info "Backup saved to ${SETTINGS_FILE}.bak"
HOOK_DIR="$HOOK_DEST"
# Build the hooks patch with jq — merges into existing hooks without clobbering other keys
STATS_RESET='{"session_prompts":0,"session_tokens_saved":0,"last_orig_tokens":0,"last_opt_tokens":0,"last_mode":"-"}'
STATS_FILE_PATH="$HOME/.claude/gpu-optimizer-stats.json"
SESSION_CMD="${HOOK_DIR}/gpu-scheduler.sh && printf '%s' '${STATS_RESET}' > ${STATS_FILE_PATH}"
STOP_CMD="pkill -f 'kubectl port-forward svc/${VLLM_SERVICE}' 2>/dev/null || true"
# Fix 🟠: write to a temp file then mv — mv on the same filesystem is atomic,
# so a crash or interrupt can never leave settings.json truncated/empty.
SETTINGS_TMP=$(mktemp "${SETTINGS_FILE}.XXXXXX")
PATCHED=$(jq \
--arg opt "$HOOK_DIR/prompt-optimizer.sh" \
--arg comp "$HOOK_DIR/fetch-compressor.sh" \
--arg stat "$HOOK_DIR/optimizer-status.sh" \
--arg session "$SESSION_CMD" \
--arg stop "$STOP_CMD" \
'
.hooks.UserPromptSubmit = [
{
hooks: [{
type: "command",
command: $opt,
timeout: 20,
statusMessage: "Optimizing prompt..."
}]
}
] |
.hooks.PostToolUse = (
(.hooks.PostToolUse // []) |
map(select(.matcher != "WebFetch")) +
[{
matcher: "WebFetch",
hooks: [{
type: "command",
command: $comp,
timeout: 20,
statusMessage: "Compressing fetch..."
}]
}]
) |
.hooks.SessionStart = [
{hooks: [{type: "command", command: $session, async: true}]}
] |
.hooks.Stop = [
{hooks: [{type: "command", command: $stop, async: true}]}
] |
.statusLine = {
type: "command",
command: $stat,
refreshInterval: 10
}
' "$SETTINGS_FILE") && { echo "$PATCHED" > "$SETTINGS_TMP" && mv "$SETTINGS_TMP" "$SETTINGS_FILE"; }
success "settings.json patched"
# ── set up cron ───────────────────────────────────────────────────────────────
info "Setting up auto-scale-down cron (every 15 min)..."
CRON_CMD="*/15 * * * * $HOOK_DIR/llm-autoscale-down.sh >> $HOME/.claude/gpu-optimizer-autoscale.log 2>&1"
# Remove any existing gpu-optimizer cron line, add the new one.
# `crontab -l` exits non-zero when no crontab exists; `|| true` prevents that
# from killing the subshell under set -euo pipefail before echo runs.
EXISTING_CRON=$(crontab -l 2>/dev/null || true)
( echo "$EXISTING_CRON" | grep -v "llm-autoscale-down\|gpu-optimizer-autoscale" ; echo "$CRON_CMD" ) | crontab -
success "Cron job installed"
# ── done ──────────────────────────────────────────────────────────────────────
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo -e "${GREEN} Installation complete!${NC}"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo " Next steps:"
echo " 1. Apply the k8s manifest:"
echo " kubectl apply -f $ASSET_DEST/llm-inference.yaml"
echo ""
echo " 2. Restart Claude Code — hooks are active on next session."
echo ""
echo " 3. Optionally edit $CONF_FILE to tune settings."
echo ""
warn " The k8s manifest targets Qwen2.5-14B-AWQ on nvidia-l4 Spot."
warn " Edit $ASSET_DEST/llm-inference.yaml to change the model or GPU type."
echo ""