forked from Anantys-oss/koan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-docker.sh
More file actions
executable file
·284 lines (241 loc) · 10.1 KB
/
Copy pathsetup-docker.sh
File metadata and controls
executable file
·284 lines (241 loc) · 10.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
#!/bin/bash
set -euo pipefail
# =========================================================================
# Kōan Docker Setup — Auto-detect host auth and generate mounts
# =========================================================================
# This script inspects the host system to find auth directories (gh, copilot)
# and workspace layout, then generates a docker-compose.override.yml with
# the correct volume mounts.
#
# Claude CLI is installed in the Docker image via npm — no host mount needed.
# Auth: ANTHROPIC_API_KEY in .env (API billing) or interactive login (subscription).
#
# Usage:
# ./setup-docker.sh # Auto-detect and generate
# ./setup-docker.sh --dry-run # Show what would be mounted without writing
# =========================================================================
DRY_RUN=false
if [ "${1:-}" = "--dry-run" ]; then
DRY_RUN=true
fi
OVERRIDE_FILE="docker-compose.override.yml"
# --- ANSI Colors (disabled when stdout is not a TTY) ---
if [ -n "${KOAN_FORCE_COLOR:-}" ] || [ -t 1 ]; then
BOLD='\033[1m' DIM='\033[2m'
RED='\033[31m' GREEN='\033[32m' YELLOW='\033[33m' CYAN='\033[36m'
RESET='\033[0m'
else
BOLD='' DIM='' RED='' GREEN='' YELLOW='' CYAN='' RESET=''
fi
log() { printf "${DIM}[setup-docker]${RESET} %s\n" "$*"; }
warn() { printf "${YELLOW}[setup-docker] ⚠ %s${RESET}\n" "$*" >&2; }
error() { printf "${RED}[setup-docker] ✗ %s${RESET}\n" "$*" >&2; }
success() { printf "${GREEN}[setup-docker] ✓ %s${RESET}\n" "$*"; }
section() { printf "\n${BOLD}${CYAN}--- %s ---${RESET}\n" "$*"; }
# -------------------------------------------------------------------------
# Detect binaries and their real paths (resolve symlinks)
# -------------------------------------------------------------------------
declare -a VOLUME_MOUNTS=()
declare -a FOUND_BINS=()
detect_binary() {
local name="$1"
local target="/host-bin/$name"
local path
path=$(command -v "$name" 2>/dev/null || true)
if [ -z "$path" ]; then
return 1
fi
# Resolve symlinks to get the real binary
local real_path
real_path=$(realpath "$path" 2>/dev/null || readlink -f "$path" 2>/dev/null || echo "$path")
success "Found $name: $real_path"
VOLUME_MOUNTS+=(" - ${real_path}:${target}:ro")
FOUND_BINS+=("$name")
return 0
}
# Detect a directory if it exists
detect_dir() {
local host_dir="$1"
local container_dir="$2"
local mode="${3:-ro}"
local label="${4:-$host_dir}"
# Expand ~ to $HOME
local expanded="${host_dir/#\~/$HOME}"
if [ -d "$expanded" ]; then
success "Found $label: $expanded"
VOLUME_MOUNTS+=(" - ${expanded}:${container_dir}:${mode}")
return 0
fi
return 1
}
# Node.js and gh are installed in the Docker image — no host mounts needed.
# -------------------------------------------------------------------------
# Detect host UID/GID
# -------------------------------------------------------------------------
detect_uid_gid() {
local uid gid tz
uid=$(id -u)
gid=$(id -g)
if [ -n "${TZ:-}" ]; then
tz="$TZ"
elif [ -L /etc/localtime ]; then
tz=$(readlink /etc/localtime | sed 's|.*/zoneinfo/||')
elif [ -f /etc/timezone ]; then
tz=$(cat /etc/timezone)
else
tz="UTC"
fi
log "Host user: UID=$uid GID=$gid TZ=$tz"
{ echo "HOST_UID=$uid"; echo "HOST_GID=$gid"; echo "TZ=$tz"; } > .env.docker
}
# -------------------------------------------------------------------------
# Resolve workspace entries for Docker bind mounts (per-project, no symlinks)
# -------------------------------------------------------------------------
resolve_workspace() {
if [ ! -d "workspace" ]; then
log "No workspace/ directory — skipping"
return
fi
local count=0
local entry name real_path
for entry in workspace/*/; do
[ -d "$entry" ] || continue
name=$(basename "$entry")
real_path=$(realpath "workspace/$name" 2>/dev/null || echo "$(pwd)/workspace/$name")
VOLUME_MOUNTS+=(" - ${real_path}:/app/workspace/${name}")
count=$((count + 1))
log "Workspace: $name → $real_path"
done
if [ $count -gt 0 ]; then
success "Resolved $count workspace project(s)"
fi
}
# -------------------------------------------------------------------------
# Generate docker-compose.override.yml
# -------------------------------------------------------------------------
generate_override() {
if [ ${#VOLUME_MOUNTS[@]} -eq 0 ]; then
warn "No volume mounts detected — override file will be minimal"
fi
local content
content="# Auto-generated by setup-docker.sh — $(date -Iseconds)
# Re-run ./setup-docker.sh if you change your workspace layout.
# This file is gitignored.
services:
koan:
build:
args:
HOST_UID: \${HOST_UID:-$(id -u)}
HOST_GID: \${HOST_GID:-$(id -g)}
volumes:"
for mount in "${VOLUME_MOUNTS[@]}"; do
content="$content
$mount"
done
if [ "$DRY_RUN" = true ]; then
printf "\n${BOLD}${CYAN}=== Would write to %s ===${RESET}\n" "$OVERRIDE_FILE"
echo "$content"
printf "${BOLD}${CYAN}=== End ===${RESET}\n"
else
echo "$content" > "$OVERRIDE_FILE"
success "Written: $OVERRIDE_FILE"
fi
}
# =========================================================================
# Main
# =========================================================================
log "Detecting host environment..."
# 1. Auth directories (Claude CLI is installed in the image — no host mount needed)
section "Auth Directories"
log "Claude CLI: installed in Docker image via npm"
log " Auth option 1: ANTHROPIC_API_KEY in .env (API billing)"
log " Auth option 2: make docker-auth (subscription — generates OAuth token from host CLI)"
# Create claude-auth/ for interactive login persistence
if [ ! -d "claude-auth" ]; then
mkdir -p "claude-auth"
success "Created claude-auth/ (persistent auth state for Claude CLI)"
else
success "Found claude-auth/"
fi
VOLUME_MOUNTS+=(" - ./claude-auth:/home/koan/.claude:rw")
detect_dir "~/.copilot" "/home/koan/.copilot" "rw" "Copilot auth" || \
log "~/.copilot not found (ok if not using Copilot)"
detect_dir "~/.config/gh" "/home/koan/.config/gh" "ro" "GitHub CLI auth" || \
warn "~/.config/gh not found — gh CLI won't be authenticated"
# SSH agent socket forwarding (for git push/fetch over SSH)
if [ -n "${SSH_AUTH_SOCK:-}" ] && [ -S "$SSH_AUTH_SOCK" ]; then
success "Found SSH agent socket: $SSH_AUTH_SOCK"
VOLUME_MOUNTS+=(" - ${SSH_AUTH_SOCK}:/run/ssh-agent.sock:ro")
else
log "No SSH agent socket — see docs/ssh-setup.md for options"
fi
# SSH keys (fallback when agent is unavailable)
detect_dir "~/.ssh" "/home/koan/.ssh" "ro" "SSH keys" || \
log "~/.ssh not found (SSH key fallback not available)"
detect_dir "~/.gitconfig" "/home/koan/.gitconfig" "ro" "Git config" || true
# 2. Workspace symlink resolution + projects.yaml
section "Workspace"
resolve_workspace
# 2b. Ensure missions.docker.md exists (isolated mission queue for container)
if [ ! -f "instance/missions.docker.md" ]; then
touch "instance/missions.docker.md"
success "Created instance/missions.docker.md (empty mission queue for container)"
else
success "Found instance/missions.docker.md"
fi
# 2c. Generate projects.docker.yaml with workspace entries (smart merge)
generate_projects_yaml() {
local yaml_file="projects.docker.yaml"
# Create skeleton if file doesn't exist
if [ ! -f "$yaml_file" ]; then
cat > "$yaml_file" << 'EOF'
defaults:
git_auto_merge:
enabled: false
base_branch: "main"
strategy: "squash"
max_open_prs: 10
projects:
EOF
log "Created $yaml_file"
fi
# Ensure projects: section exists (handles files generated before this fix)
if ! grep -q '^projects:' "$yaml_file"; then
printf '\nprojects:\n' >> "$yaml_file"
log "Added projects: section to $yaml_file"
fi
# Append workspace entries not already present
local added=0
local entry name
for entry in workspace/*/; do
[ -d "$entry" ] || continue
name=$(basename "$entry")
if ! grep -qE "^ ${name}:" "$yaml_file"; then
printf ' %s:\n path: /app/workspace/%s\n' "$name" "$name" >> "$yaml_file"
added=$((added + 1))
log "Added project '$name'"
fi
done
if [ $added -gt 0 ]; then
success "Added $added new workspace project(s) to $yaml_file"
else
success "$yaml_file up to date"
fi
}
generate_projects_yaml
VOLUME_MOUNTS+=(" - ./projects.docker.yaml:/app/projects.docker.yaml:rw")
# 3. Generate files
section "Generating"
detect_uid_gid
generate_override
if [ "$DRY_RUN" = false ]; then
printf "\n${BOLD}${CYAN}╭──────────────────────────────────────────────────────────╮${RESET}\n"
printf "${BOLD}${CYAN}│${RESET} ${GREEN}✓ Setup complete!${RESET} ${BOLD}${CYAN}│${RESET}\n"
printf "${BOLD}${CYAN}├──────────────────────────────────────────────────────────┤${RESET}\n"
printf "${BOLD}${CYAN}│${RESET} 1. Review %-43s ${BOLD}${CYAN}│${RESET}\n" "$OVERRIDE_FILE"
printf "${BOLD}${CYAN}│${RESET} 2. Auth (pick one): ${BOLD}${CYAN}│${RESET}\n"
printf "${BOLD}${CYAN}│${RESET} a. Set ANTHROPIC_API_KEY in .env (API billing) ${BOLD}${CYAN}│${RESET}\n"
printf "${BOLD}${CYAN}│${RESET} b. make docker-auth (subscription — host token) ${BOLD}${CYAN}│${RESET}\n"
printf "${BOLD}${CYAN}│${RESET} 3. docker compose up --build ${BOLD}${CYAN}│${RESET}\n"
printf "${BOLD}${CYAN}╰──────────────────────────────────────────────────────────╯${RESET}\n"
fi