mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 21:36:40 -10:00
Every file devvm keeps for a project VM is in one folder, vms/<hash>/, where <hash> is the 14-hex hash of the project folder's path that the VM's name ends with: its state, its own rules (policy), its backend's files (backend/: record.json, key, known_hosts, proxy, ports and the port forwarder's ports.*), what its runs leave (run/: proxy.*, sandbox.sb, started, lifeline.pid, secrets.*, links.*) and its logs (logs/: network.jsonl, proxy.log, links.log). Build VMs keep the same layout in run/builds/<name>/. The top-level run/ holds only what isn't one VM's or must be short: the hashed control sockets, links' link-<hash>.sock/.ctl, the QEMU wrapper and the ports lock. A folder's VM is found by its path's hash (state.ReadHash), with the name and backend its state records, so devvm --backend docker then devvm in the same folder opens the one VM, and a backend's name limit applies to new VMs only. The state records the VM's name and its ssh alias, chosen once at creation (sshconfig.ChooseAlias) and never recomputed. Links' targets match recorded names, aliases and folders (project.Targets). One teardown (vmFolders.teardown) replaces Lifecycle.forget, undoCreate's own list, Leftovers.Forget's, Proxy.Forget and space's leftoverFiles: it stops the proxy and link session, then the pieces outside the folder (clone remote or .git locks, other VMs' links and the group, host keys, the backend's leftovers), removes the folder, and writes the ssh config. Each backend's Delete and Forget remove the VM's backend/ folder (vmssh.Forwards.Forget). A new VM made where an earlier one's folder was left clears it first. devvm space counts an orphan's whole folder as leftover state and the other VMs' logs as network logs. Path builders move into paths: Dirs.VMDir, Dirs.VM and paths.VM's files (secrets' BindingsFile/PolicyFile, netpolicy's run, EventLog, ProxyLog, LinkList, VMPolicyFile, Lima's profileFile, startedFile, forwards.file, vmssh.Forwards.file, links' LinksLog/linksPID, Docker's vmFile and lifeline), ToolLockConfig, ToolLockChecked and ToolsCheckLog; the user's cache folder for Lima's and smolvm's caches is Dirs.UserCache, from HOME, not os.UserCacheDir. Dirs.VMState, LimaVMs, SmolvmVM and DockerVM go. vmname.HashOf and IsHash, project.Hash. The proxy takes the links' sockets' folder (-link-sockets, devvm's run folder) apart from the links' list, which is in the VM's run folder. Tests: a VM made and removed (rm, gc --orphans) leaves nothing under vms/, and with network policy has no file named for it outside its folder (TestTeardownLeavesNothing); a new VM over an old folder starts clean; lookup finds a VM made on another backend with a long folder name and makes no second one (TestLookupOtherBackend, and in docker.txtar); the alias stays put when a second VM shares the folder name (TestChooseAlias, ssh.txtar); every socket fits under a 35-character home with the longest VM name (TestSocketsFit); space counts an orphan's whole folder (TestLeftoverFolders); paths.VM's files; state by hash and name; the proxy refuses a socket in the list's own folder. Co-Authored-By: Claude Opus 5.5 <[email protected]>
235 lines
9.7 KiB
Plaintext
235 lines
9.7 KiB
Plaintext
# devvm group: VMs that reach each other's published ports by name,
|
|
# through links the group makes in each of them, kept in their state.
|
|
# A VM is in one group at a time, and a group's VMs all have network
|
|
# policy or none do.
|
|
|
|
exec devvm --help
|
|
stdout '^Links between VMs:\n group .*\n link '
|
|
|
|
# No VM yet.
|
|
project app
|
|
! exec devvm group
|
|
stderr '^devvm: no_vm_group dir=\S*/app$'
|
|
|
|
# Usage errors, before devvm looks for the VM.
|
|
! exec devvm group nope
|
|
stderr '^devvm: no_subcommand command=devvm group commands=join, leave given=nope$'
|
|
! exec devvm group join
|
|
stderr '^devvm: arg_missing args=NAME command=devvm group join$'
|
|
stderr '^usage_help command=devvm group join$'
|
|
! exec devvm group join Web
|
|
stderr '^devvm: group_name_invalid name="Web"$'
|
|
! exec devvm group join 42
|
|
stderr '^devvm: group_name_invalid name="42"$'
|
|
! exec devvm group join web --as Bad_Name
|
|
stderr '^devvm: link_name_invalid name="Bad_Name"$'
|
|
! exec devvm group leave web
|
|
stderr '^devvm: args_none command=devvm group leave given=web$'
|
|
! exec devvm group join --json web
|
|
stderr '^devvm: flag_unknown command=devvm group join flag=--json$'
|
|
! stderr no_vm_group
|
|
|
|
# The VMs: app, db and two folders called api; cache with network policy.
|
|
exec devvm --no-network-policy --no-git-protect
|
|
project db
|
|
exec devvm --no-network-policy --no-git-protect
|
|
exec devvm link publish 5432,8000-8001
|
|
mkdir $WORK/x/api
|
|
cd $WORK/x/api
|
|
exec devvm --no-network-policy --no-git-protect
|
|
exec devvm link publish 80
|
|
mkdir $WORK/y/api
|
|
cd $WORK/y/api
|
|
exec devvm --no-network-policy --no-git-protect
|
|
exec devvm link publish 3000
|
|
project cache
|
|
exec devvm --network-policy --no-git-protect
|
|
vm_name APP app
|
|
vm_name DB db
|
|
vm_name CACHE cache
|
|
|
|
# Not in a group: said, and an empty list in JSON.
|
|
cd $WORK/app
|
|
exec devvm group
|
|
stdout '^==> group_none vm=devvm-app-[0-9a-f]{14}$'
|
|
exec devvm group --json
|
|
stdout '\A{\n "version": 1,\n "group": "",\n "members": \[\]\n}\n\z'
|
|
! exec devvm group leave
|
|
stderr '^devvm: group_none vm=devvm-app-[0-9a-f]{14}$'
|
|
|
|
# The first to join makes the group; the trust is said on every join.
|
|
exec devvm group join web
|
|
stdout '^==> group_made group=web vm=devvm-app-[0-9a-f]{14}$'
|
|
stderr '^group_trust group=web$'
|
|
exec cat $APP_dir/state
|
|
stdout '^group=web$'
|
|
exec devvm group join web
|
|
stdout '^==> group_already group=web vm=devvm-app-[0-9a-f]{14}$'
|
|
! exec devvm group join web --as other
|
|
stderr '^devvm: group_named group=web name=app vm=devvm-app-[0-9a-f]{14}$'
|
|
! exec devvm group join other
|
|
stderr '^devvm: group_other group=web vm=devvm-app-[0-9a-f]{14}$'
|
|
|
|
# db joins: app gets a link to each port db publishes, db none to app,
|
|
# which publishes none.
|
|
cd $WORK/db
|
|
exec devvm group join web
|
|
stdout '^==> group_joined group=web vm=devvm-db-[0-9a-f]{14}$'
|
|
stdout '^==> group_links_added group=web links=db:5432,8000-8001 vm=devvm-app-[0-9a-f]{14}$'
|
|
! stdout 'group_links_added .* vm=devvm-db-'
|
|
exec sh -c 'id=$(sed -n "s/^id=//p" "$DB_dir/state"); grep -x "links=db,5432,$DB,$id,127.77.0.1,group db,8000,$DB,$id,127.77.0.1,group db,8001,$DB,$id,127.77.0.1,group" "$APP_dir/state"'
|
|
|
|
# All or none: cache, with network policy, can't join a group whose VMs
|
|
# have none, and a group of its own refuses those without.
|
|
cd $WORK/cache
|
|
! exec devvm group join web
|
|
stderr '^devvm: group_policy_off group=web vm=devvm-cache-[0-9a-f]{14}$'
|
|
exec devvm group join safe
|
|
stdout '^==> group_made group=safe '
|
|
cd $WORK/y/api
|
|
! exec devvm group join safe
|
|
stderr '^devvm: group_policy_on group=safe vm=devvm-api-[0-9a-f]{14}$'
|
|
# Network policy can't change for a member.
|
|
cd $WORK/cache
|
|
! exec devvm network off
|
|
stderr '^devvm: group_policy_mixed group=safe vm=devvm-cache-[0-9a-f]{14}$'
|
|
exec cat $CACHE_dir/state
|
|
stdout '^net=on$'
|
|
cd $WORK/db
|
|
! exec devvm network on
|
|
stderr '^devvm: group_policy_mixed group=web vm=devvm-db-[0-9a-f]{14}$'
|
|
cd $WORK/cache
|
|
exec devvm group leave
|
|
stdout '^==> group_left group=safe vm=devvm-cache-[0-9a-f]{14}$'
|
|
stdout '^==> group_gone group=safe$'
|
|
|
|
# Two members with the same folder part: the first's name would be the
|
|
# second's own hostname, so the second is refused whatever it joins as,
|
|
# until the first joins again with another name.
|
|
cd $WORK/x/api
|
|
exec devvm group join web
|
|
stdout '^==> group_links_added group=web links=api:80 vm=devvm-app-'
|
|
stdout '^==> group_links_added group=web links=api:80 vm=devvm-db-'
|
|
stdout '^==> group_links_added group=web links=db:5432,8000-8001 vm=devvm-api-'
|
|
cd $WORK/y/api
|
|
! exec devvm group join web
|
|
stderr '^devvm: group_name_hostname group=web member=devvm-api-[0-9a-f]{14} name=api vm=devvm-api-[0-9a-f]{14}$'
|
|
! exec devvm group join web --as api2
|
|
stderr '^devvm: group_name_hostname group=web member=devvm-api-[0-9a-f]{14} name=api vm=devvm-api-[0-9a-f]{14}$'
|
|
exec sh -c 'grep -l "^group=web$" $(grep -l "^name=devvm-api-" "$VMS"/*/state) | wc -l'
|
|
stdout '^ *1$'
|
|
cd $WORK/x/api
|
|
exec devvm group leave
|
|
exec devvm group join web --as api1
|
|
stdout '^==> group_links_added group=web links=api1:80 vm=devvm-app-'
|
|
cd $WORK/y/api
|
|
! exec devvm group join web
|
|
stderr '^devvm: group_name_taken group=web member=devvm-api-[0-9a-f]{14} name=api$'
|
|
! exec devvm group join web --as db
|
|
stderr '^devvm: group_name_taken group=web member=devvm-db-[0-9a-f]{14} name=db$'
|
|
! exec devvm group join web --as localhost
|
|
stderr '^devvm: group_name_host vm=devvm-api-[0-9a-f]{14}$'
|
|
exec devvm group join web --as api2
|
|
stdout '^==> group_joined group=web '
|
|
! stderr group_link_name
|
|
stdout '^==> group_links_added group=web links=api2:3000 vm=devvm-app-'
|
|
stdout '^==> group_links_added group=web links=api1:80, db:5432,8000-8001 vm=devvm-api-'
|
|
# A folder whose name is the name a member joined with: its own hostname
|
|
# would name that member.
|
|
project api1
|
|
exec devvm --no-network-policy --no-git-protect
|
|
! exec devvm group join web --as other
|
|
stderr '^devvm: group_name_hostname group=web member=devvm-api-[0-9a-f]{14} name=api1 vm=devvm-api1-[0-9a-f]{14}$'
|
|
|
|
# The group: each VM, its name, folder and published ports.
|
|
cd $WORK/app
|
|
exec devvm group
|
|
stdout '\ANAME +VM +FOLDER +PUBLISHED\n'
|
|
stdout '^api1 +devvm-api-[0-9a-f]{14} +\S*/x/api +80$'
|
|
stdout '^api2 +devvm-api-[0-9a-f]{14} +\S*/y/api +3000$'
|
|
stdout '^app +devvm-app-[0-9a-f]{14} +\S*/app +none$'
|
|
stdout '^db +devvm-db-[0-9a-f]{14} +\S*/db +5432,8000-8001$'
|
|
exec devvm group --json
|
|
stdout '\A{\n "version": 1,\n "group": "web",\n "members": \[\n {\n "name": "api1",\n "vm": "devvm-api-[0-9a-f]{14}",\n "folder": "\S*/x/api",\n "published": "80"\n },\n'
|
|
exec devvm link
|
|
stdout '^api2 +3000 +devvm-api-[0-9a-f]{14} +\S*/y/api +Running +yes$'
|
|
stdout '^db +8001 +devvm-db-[0-9a-f]{14} +\S*/db +Running +yes$'
|
|
|
|
# A group's link isn't devvm link rm's; a link of the user's to a
|
|
# member's port takes its place, and its name names the member; removed,
|
|
# the group's comes back.
|
|
! exec devvm link rm db 8000
|
|
stderr '^devvm: link_from_group group=web name=db port=8000 vm=devvm-app-[0-9a-f]{14}$'
|
|
exec devvm link add db 8000 --as pg
|
|
stdout '^==> link_added name=pg port=8000 '
|
|
stdout '^==> group_links_removed group=web links=db:5432,8000-8001 vm=devvm-app-'
|
|
stdout '^==> group_links_added group=web links=pg:5432,8001 vm=devvm-app-'
|
|
exec sh -c 'grep -o "pg,8000,[^ ]*" "$APP_dir/state"'
|
|
stdout '^pg,8000,devvm-db-[0-9a-f]{14},[0-9a-f]{32},127\.77\.0\.1$'
|
|
exec devvm link rm pg 8000
|
|
stdout '^==> link_removed name=pg port=8000 '
|
|
stdout '^==> group_links_added group=web links=db:5432,8000-8001 vm=devvm-app-'
|
|
# A link of the user's whose name is another member's takes it: the
|
|
# group's links to that member go, said; removed, they come back.
|
|
exec devvm link add ../y/api 3000 --as api1
|
|
stdout '^==> link_added name=api1 port=3000 '
|
|
stderr '^devvm: group_link_name_clash name=api1 target=devvm-api-[0-9a-f]{14} vm=devvm-app-[0-9a-f]{14}$'
|
|
stdout '^==> group_links_removed group=web links=api1:80, api2:3000 vm=devvm-app-'
|
|
exec devvm link rm api1 3000
|
|
stdout '^==> group_links_added group=web links=api1:80, api2:3000 vm=devvm-app-'
|
|
|
|
# Published ports reach every member's links at once.
|
|
cd $WORK/db
|
|
exec devvm link publish 6379
|
|
stdout '^==> group_links_added group=web links=db:6379 vm=devvm-app-'
|
|
stdout '^==> group_links_added group=web links=db:6379 vm=devvm-api-'
|
|
exec devvm link unpublish 6379
|
|
stdout '^==> group_links_removed group=web links=db:6379 vm=devvm-app-'
|
|
! stderr unpublished_linked
|
|
|
|
# A stopped member gets the change at its next start.
|
|
cd $WORK/x/api
|
|
exec devvm stop
|
|
cd $WORK/db
|
|
exec devvm link publish 7000
|
|
stdout '^==> group_links_added group=web links=db:7000 vm=devvm-api-[0-9a-f]{14}$'
|
|
stdout '^==> links_next_start vm=devvm-api-[0-9a-f]{14}$'
|
|
|
|
# leave: only the group's links go, each named; the user's stay.
|
|
cd $WORK/app
|
|
exec devvm link add db 7000
|
|
exec devvm group leave
|
|
stdout '^==> group_left group=web vm=devvm-app-[0-9a-f]{14}$'
|
|
stdout '^==> group_links_removed group=web links=api1:80, api2:3000, db:5432,8000-8001 vm=devvm-app-[0-9a-f]{14}$'
|
|
exec cat $APP_dir/state
|
|
stdout '^links=db,7000,devvm-db-[0-9a-f]{14},[0-9a-f]{32},127\.77\.0\.1$'
|
|
stdout '^group=$'
|
|
exec devvm group
|
|
stdout '^==> group_none '
|
|
cd $WORK/db
|
|
exec devvm group
|
|
! stdout '^app '
|
|
|
|
# devvm rm of a member takes it out, and the last one's leaving removes
|
|
# the group.
|
|
cd $WORK/y/api
|
|
exec devvm rm --force --no-export
|
|
stdout '^==> group_left group=web vm=devvm-api-[0-9a-f]{14}$'
|
|
cd $WORK/x/api
|
|
exec devvm group leave
|
|
stdout '^==> group_left group=web '
|
|
cd $WORK/db
|
|
exec devvm group leave
|
|
stdout '^==> group_gone group=web$'
|
|
exec sh -c 'grep -l "^group=web$" "$VMS"/*/state || true'
|
|
! stdout .
|
|
|
|
# Two VMs joining one group at once, app without network policy and
|
|
# cache with it: they take turns, so only one gets in.
|
|
exec sh -c '(cd "$WORK/app" && devvm group join race) & (cd "$WORK/cache" && devvm group join race) & wait; true'
|
|
stderr 'devvm: group_policy_o(n|ff) group=race '
|
|
exec sh -c 'grep -l "^group=race$" "$VMS"/*/state | wc -l'
|
|
stdout '^ *1$'
|
|
exec sh -c 'for d in app cache; do (cd "$WORK/$d" && devvm group leave) || true; done'
|
|
stdout '^==> group_gone group=race$'
|