-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathclod
More file actions
executable file
·478 lines (452 loc) · 13.8 KB
/
clod
File metadata and controls
executable file
·478 lines (452 loc) · 13.8 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#!/usr/bin/env bash
# Build a MacOS virtual machine that runs AI agents and developer tools
set -Eeuo pipefail
trap 'echo "${BASH_SOURCE[0]}: line $LINENO: $BASH_COMMAND: exitcode $?"' ERR
# Resolve symlinks (macOS bash 3.2 compatible, no readlink -f)
SOURCE="${BASH_SOURCE[0]}"
while [[ -L "$SOURCE" ]]; do
SOURCE_DIR="$(cd -P "$(dirname "$SOURCE")" && pwd -P)"
SOURCE="$(readlink "$SOURCE")"
[[ "$SOURCE" = /* ]] || SOURCE="$SOURCE_DIR/$SOURCE"
done
WORKSPACE="$(cd -P "$(dirname "$SOURCE")" && pwd -P)"
# Homebrew Cellar: use stable opt/ symlink instead of versioned path
if [[ "$WORKSPACE" =~ ^(.*)/homebrew/Cellar/([^/]+)/[^/]+$ ]]; then
WORKSPACE="${BASH_REMATCH[1]}/homebrew/opt/${BASH_REMATCH[2]}"
fi
###############################################################################
# Overrides
###############################################################################
MACOS_VERSION="${MACOS_VERSION:-tahoe}"
MACOS_FLAVOR="${MACOS_FLAVOR:-xcode}"
###############################################################################
# Libraries
###############################################################################
source "$WORKSPACE/lib/common.sh"
source "$WORKSPACE/lib/config.sh"
source "$WORKSPACE/lib/db.sh"
source "$WORKSPACE/lib/vm.sh"
source "$WORKSPACE/lib/ssh.sh"
source "$WORKSPACE/lib/project.sh"
source "$WORKSPACE/lib/instance.sh"
source "$WORKSPACE/lib/commands.sh"
source "$WORKSPACE/lib/build.sh"
validate_platform
validate_config
init_config
install_tools
init_db
migrate_vm_names
# Pre-parse global options so they take effect even on early-dispatched subcommands.
# Unknown flags and positional args are preserved in $@ for subcommand parsers.
NO_GRAPHICS="${NO_GRAPHICS:-}"
SHOULD_SELECT_PROJECT=true
ALLOW_SUDO=
_pre_args=()
while [[ $# -gt 0 ]]; do
case "$1" in
-v|--verbose)
((VERBOSE++)) || true
shift
;;
-vv)
((VERBOSE+=2)) || true
shift
;;
-vvv)
((VERBOSE+=3)) || true
shift
;;
--graphics)
NO_GRAPHICS=
shift
;;
--no-graphics)
NO_GRAPHICS=true
shift
;;
--rebuild-base)
REBUILD_BASE=true
shift
;;
--rebuild-oci)
REBUILD_OCI=true
shift
;;
--rebuild-dst)
REBUILD_DST=true
shift
;;
--allow-sudo)
ALLOW_SUDO=true
shift
;;
--no-allow-sudo)
ALLOW_SUDO=false
shift
;;
-n|--no-select)
SHOULD_SELECT_PROJECT=false
shift
;;
--)
_pre_args+=("$@")
break
;;
*)
_pre_args+=("$1")
shift
;;
esac
done
set -- ${_pre_args[@]+"${_pre_args[@]}"}
# Intercept commands that need their own option parsing before the global loop.
# But let -h/--help/--version pass through to the normal handler.
for arg in "$@"; do
case "$arg" in
-h|--help|--version|-V)
break # let global parser handle these
;;
-*)
continue
;;
build-base)
while [[ "${1:-}" != "$arg" ]]; do shift; done
shift
for _a in "$@"; do
case "$_a" in
--)
break
;;
-h|--help)
show_help_build_base
exit 0
;;
*)
;;
esac
done
cmd_build_base "$@"
exit 0
;;
create|cr)
while [[ "${1:-}" != "$arg" ]]; do shift; done
shift
for _a in "$@"; do
case "$_a" in
--)
break
;;
-h|--help)
show_help_create
exit 0
;;
*)
;;
esac
done
vm_create "$@"
exit 0
;;
destroy)
while [[ "${1:-}" != "$arg" ]]; do shift; done
shift
for _a in "$@"; do
case "$_a" in
--)
break
;;
-h|--help)
show_help_destroy
exit 0
;;
*)
;;
esac
done
vm_destroy "$@"
exit 0
;;
set)
while [[ "${1:-}" != "$arg" ]]; do shift; done
shift
for _a in "$@"; do
case "$_a" in
--)
break
;;
-h|--help)
show_help_set
exit 0
;;
*)
;;
esac
done
vm_set "$@"
exit 0
;;
shell|sh|s)
while [[ "${1:-}" != "$arg" ]]; do shift; done
shift
# Check --help before shell's own parser (stop at --)
for _a in "$@"; do
case "$_a" in
--)
break
;;
-h|--help)
show_help_shell
exit 0
;;
*)
;;
esac
done
# Parse shell args: [--ram SIZE] [target] [-- cmd...]
COMMAND_ARGS=()
_shell_ram=""
_shell_target=""
while [[ $# -gt 0 ]]; do
case "$1" in
--ram)
[[ $# -ge 2 ]] || abort "Error: --ram requires a size value (e.g. 8G)"
_shell_ram="$(parse_ram_size "$2")"
shift 2
;;
--)
shift
while [[ $# -gt 0 ]]; do
COMMAND_ARGS+=("$1")
shift
done
break
;;
*)
if [[ -z "$_shell_target" ]]; then
_shell_target="$1"
fi
shift
;;
esac
done
# Dispatch: named instance, auto-select, or fall through to legacy
if [[ -n "$_shell_target" ]] && vm_instance_exists "$_shell_target"; then
SHELL_RAM_OVERRIDE="$_shell_ram" vm_shell "$_shell_target"
exit 0
fi
if [[ -z "$_shell_target" ]]; then
_instance_count="$(vm_get_instance_count 2>/dev/null || echo 0)"
if [[ "${_instance_count:-0}" -eq 1 ]]; then
_only_name="$(vm_get_only_instance_name)"
SHELL_RAM_OVERRIDE="$_shell_ram" vm_shell "$_only_name"
exit 0
elif [[ "${_instance_count:-0}" -gt 1 ]]; then
vm_list
abort "Multiple instances exist. Specify name: clod shell <name>"
fi
fi
# --ram not supported on legacy shell
if [[ -n "$_shell_ram" ]]; then
abort "Error: --ram is only supported with named VM shell"
fi
# Fall through to legacy: put args back for global parser
set -- shell ${_shell_target:+"$_shell_target"}
break
;;
*)
break
;;
esac
done
# Parse remaining arguments (global options already consumed by pre-parse above)
NEW_ARGS=()
COMMAND_ARGS=()
while [[ $# -gt 0 ]]; do
case "$1" in
--)
shift
while [[ $# -gt 0 ]]; do
COMMAND_ARGS+=("$1")
shift
done
break
;;
-h|--help)
show_help
exit 0
;;
--version)
show_version
;;
-*)
echo "Unknown option: $1" >&2
exit 1
;;
*)
NEW_ARGS+=("$1")
shift
;;
esac
done
# bash 3.2 treats empty arrays as unbound even when initialized.
# With set -u enabled, "${NEW_ARGS[@]}" fails if the array is empty.
# The fix is to use ${array[@]+"${array[@]}"} which only expands if the array has elements.
set -- ${NEW_ARGS[@]+"${NEW_ARGS[@]}"}
# Parse fixed arguments
case "${1:-}" in
claude|cl|c)
COMMAND=claude
PROJECT_DIR="${2:-}"
PROJECT_NAME="${3:-}"
;;
codex|co)
COMMAND=codex
PROJECT_DIR="${2:-}"
PROJECT_NAME="${3:-}"
;;
cursor|cu|ca)
COMMAND=cursor
PROJECT_DIR="${2:-}"
PROJECT_NAME="${3:-}"
;;
gemini|gem|g)
COMMAND=gemini
PROJECT_DIR="${2:-}"
PROJECT_NAME="${3:-}"
;;
shell|sh|s)
unset COMMAND
IS_SHELL_COMMAND=true
PROJECT_DIR="${2:-}"
PROJECT_NAME="${3:-}"
;;
start)
COMMAND=start
;;
stop)
if [[ -n "${2:-}" ]]; then
vm_stop_instance "$2"
else
stop_all_vms
fi
exit 0
;;
add|a)
add_project "${2:-}" "${3:-}"
exit 0
;;
remove|rm)
remove_project "${2:-}"
exit 0
;;
list|ls|l)
list_all
exit 0
;;
migrate)
migrate_db
exit 0
;;
help|h)
dispatch_help "${2:-}"
exit 0
;;
status|st)
COMMAND=status
;;
*)
# Handle the special case that user asked for rebuild but
# did not specify a command. We'd like to do something
# nicer than just exit
if [[ -z "${REBUILD_BASE:-}" ]] && [[ -z "${REBUILD_DST:-}" ]] && [[ -z "${REBUILD_OCI:-}" ]]; then
show_help
exit 0
fi
COMMAND=start
;;
esac
# Named instance shell dispatch is handled in early-dispatch above.
# Legacy shell (path/project) falls through to here.
# Decide whether sudo-setting changes require rebuild
STORED_ALLOW_SUDO="$(get_setting "allow_sudo" "false")"
if [[ "${COMMAND:-}" == "status" ]]; then
check_oci_provenance true
show_status "$STORED_ALLOW_SUDO"
exit 0
fi
ALLOW_SUDO="${ALLOW_SUDO:-"$STORED_ALLOW_SUDO"}"
if [[ "$ALLOW_SUDO" != "$STORED_ALLOW_SUDO" ]]; then
info "Sudo setting changed ($STORED_ALLOW_SUDO -> $ALLOW_SUDO); forcing base rebuild"
REBUILD_BASE=true
fi
# Resolve explicit command target argument
if [[ -n "${PROJECT_DIR:-}" ]]; then
if [[ "${IS_SHELL_COMMAND:-}" == "true" ]] && project_path="$(get_project_path_by_name "$PROJECT_DIR")" && [[ -n "$project_path" ]]; then
# NAME-only form
PROJECT_NAME="$PROJECT_DIR"
PROJECT_DIR="$project_path"
SHOULD_SELECT_PROJECT=false
set_active_project "$PROJECT_NAME" "$PROJECT_DIR"
else
resolved_path="$(resolve_physical_path "$PROJECT_DIR" 2>/dev/null || true)"
if [[ -d "$resolved_path" ]]; then
# PATH [NAME] form
add_project "$PROJECT_DIR" "$PROJECT_NAME"
SHOULD_SELECT_PROJECT=false
elif project_path="$(get_project_path_by_name "$PROJECT_DIR")" && [[ -n "$project_path" ]]; then
# NAME-only form
PROJECT_NAME="$PROJECT_DIR"
PROJECT_DIR="$project_path"
SHOULD_SELECT_PROJECT=false
set_active_project "$PROJECT_NAME" "$PROJECT_DIR"
else
if [[ "${IS_SHELL_COMMAND:-}" == "true" ]]; then
abort "Error: project path or instance not found ($PROJECT_DIR)"
fi
abort "Error: Project not found ($PROJECT_DIR)"
fi
fi
fi
# Get most recent project. If none exists then add the current directory as a project.
IFS='|' read -r PROJECT_NAME PROJECT_DIR < <(sqlite3 "$DB_FILE" "SELECT name, path FROM
projects ORDER BY date_added DESC LIMIT 1;" 2>/dev/null) || true
if [[ -z "${PROJECT_DIR:-}" ]]; then
# Try to set the project directory to the top-level directory of the git repository
# to minimize the number of directories that get mapped into the virtual machine.
PROJECT_DIR="$(git rev-parse --show-toplevel 2>/dev/null)" || PROJECT_DIR="$PWD"
PROJECT_DIR="$(resolve_physical_path "$PROJECT_DIR" 2>/dev/null || echo "$PROJECT_DIR")"
PROJECT_NAME="$(basename "$PROJECT_DIR")"
add_project "$PROJECT_DIR" "$PROJECT_NAME"
SHOULD_SELECT_PROJECT=false
fi
if [[ "$SHOULD_SELECT_PROJECT" == "true" ]]; then
if [[ ! -t 0 ]] || [[ ! -t 1 ]]; then
warn "Interactive project selection disabled (no TTY); using active project"
elif selected_project=$(select_project); then
IFS='|' read -r PROJECT_NAME PROJECT_DIR <<< "$selected_project"
set_active_project "$PROJECT_NAME" "$PROJECT_DIR"
else
selection_rc=$?
if [[ "$selection_rc" -eq 1 ]]; then
info "Cancelled"
exit 1
else
warn "Interactive project selection unavailable; using active project"
fi
fi
fi
info "Active project: $PROJECT_NAME ($PROJECT_DIR)"
check_oci_provenance
ensure_ssh_key
ensure_guest_home
determine_rebuilds
prepare_rebuilds
if [[ "${REBUILD_DST:-}" != "" ]]; then
refresh_guest_home
fi
configure_dhcp_lease
TMP_VM_NAME="clodpod-tmp-$(openssl rand -hex 8)"
build_base_vm
build_dst_vm
legacy_start_and_connect