mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 21:36:40 -10:00
feat(gc): gc --exports [--older-than AGE] deletes saved exports
Exports (exports/<vm>-<date>-<time>.tar.gz, from devvm export and devvm rm) were never deleted, and each can be the only copy of a deleted VM's agent history. --exports deletes them, never as part of gc's default run; --older-than AGE (state.ParseAge: a whole number of days, weeks or hours, 30d, 2w, 12h, which retention will read too) keeps those newer, by the date in their name. Only plain files named as devvm names them: an export being written (.devvm-export-…), a folder or a file of the user's in the folder stays. The list names each export's VM and date (gc_exports, gc_export); it asks in a terminal, goes ahead without one, never deletes a VM. --older-than without --exports, or an age devvm doesn't take, is refused. devvm space's exports row says gc --exports frees them, and counts the exports as gc does. Messages: gc_exports, gc_export, deleted_exports, gc_older_than_needs_exports, gc_bad_age. Docs: coding-agents' History, using/images.md's Cleaning up, the What devvm keeps page, the CLI reference. Tests: TestParseAge; TestExports (names, order, the age boundary); gc_exports.txtar (the default run keeps them, the flags refused, 30d deletes the old one only, then every one, nothing else in the folder). Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
15 files changed
+318
-13
No files matched your search
@@ -7,7 +7,7 @@ Delete stale images and unused tool images
|
||||
|
||||
## Synopsis
|
||||
|
||||
Delete what devvm images lists as stale or unfinished, on every backend, with their records, the shared tool images it lists as unused, the copies of devvm's own files older versions wrote out, and the older versions of the mise devvm runs on this machine. With --backend, only that backend's images, its current ones too, and its unused tool images; without it, every backend's, whatever the default backend is. With --tools, also the current tool images no VM has, on every backend and C library, or only those of the TOOLs named (node, claude). It lists them first, and in a terminal asks. VMs are never deleted; on Linux hosts, an image whose disk VMs use stays, and so does a smolvm or Docker image a VM was made from.
|
||||
Delete what devvm images lists as stale or unfinished, on every backend, with their records, the shared tool images it lists as unused, the copies of devvm's own files older versions wrote out, and the older versions of the mise devvm runs on this machine. With --backend, only that backend's images, its current ones too, and its unused tool images; without it, every backend's, whatever the default backend is. With --tools, also the current tool images no VM has, on every backend and C library, or only those of the TOOLs named (node, claude). With --exports, also the agents' history devvm export and devvm rm saved, the only copy of a deleted VM's, or only those older than --older-than (30d, 2w, 12h). It lists them first, and in a terminal asks. VMs are never deleted; on Linux hosts, an image whose disk VMs use stays, and so does a smolvm or Docker image a VM was made from.
|
||||
|
||||
```text
|
||||
devvm gc [--tools [TOOL...]] [flags]
|
||||
@@ -16,8 +16,10 @@ devvm gc [--tools [TOOL...]] [flags]
|
||||
## Options
|
||||
|
||||
```text
|
||||
-h, --help help for gc
|
||||
--tools also delete the current shared tool images no VM has; with TOOL names, only theirs
|
||||
--exports also delete the saved exports of agents' history
|
||||
-h, --help help for gc
|
||||
--older-than AGE with --exports, only those older than AGE: 30d, 2w or 12h
|
||||
--tools also delete the current shared tool images no VM has; with TOOL names, only theirs
|
||||
```
|
||||
|
||||
## Options inherited from parent commands
|
||||
|
||||
@@ -78,7 +78,7 @@ With network policy, the VM's `auth.json` holds placeholders, and its proxy hold
|
||||
|
||||
## History
|
||||
|
||||
Each VM's transcripts and sessions stay in that VM. `devvm export [FILE]` saves them, and `devvm rm` saves them before it deletes a VM whose image has an agent. The export is a `.tar.gz` of `~/.claude`, `~/.claude.json`, `~/.codex`, `~/.local/share/opencode` and `~/.local/state/opencode` in the VM, written to FILE or to `~/.local/share/devvm/exports/<vm>-<date>-<time>.tar.gz`. devvm never deletes an export, so the exports stay until you delete them, and after `devvm rm` an export is the only copy of that VM's history.
|
||||
Each VM's transcripts and sessions stay in that VM. `devvm export [FILE]` saves them, and `devvm rm` saves them before it deletes a VM whose image has an agent. The export is a `.tar.gz` of `~/.claude`, `~/.claude.json`, `~/.codex`, `~/.local/share/opencode` and `~/.local/state/opencode` in the VM, written to FILE or to `~/.local/share/devvm/exports/<vm>-<date>-<time>.tar.gz`. After `devvm rm`, an export is the only copy of that VM's history, so devvm keeps the exports until you ask it to delete them. `devvm gc --exports` deletes the exports in that folder, and `devvm gc --exports --older-than 30d` deletes only those written 30 days ago or more. gc lists each one by its VM and date first, and in a terminal it asks. Exports you wrote to a FILE of your own stay where they are.
|
||||
|
||||
The agents' logins aren't in the export, which leaves out `~/.claude/.credentials.json`, `~/.codex/auth.json`, and opencode's `auth.json`, `account.json` and `bin`. devvm starts a stopped VM for the export, and the VM stays running. devvm writes the file only once the export is complete, so a failed export leaves no file and leaves an existing FILE as it was. Only you can read the file, and devvm makes the exports folder yours alone each time it writes one there, whatever its mode was before. When FILE is in a folder of your own, that folder keeps its mode. If the export fails, `devvm rm` keeps the VM, and `devvm rm -f` deletes it without exporting.
|
||||
|
||||
|
||||
@@ -158,6 +158,7 @@ After the tables, devvm says on stderr how many images are stale or unfinished a
|
||||
`devvm gc` deletes four things. The first is what `devvm images` lists as stale or unfinished, on every backend. The second is the shared tools it lists as `unused`. The third is the copies of devvm's own files that older versions [wrote out](../internals/install.md#devvms-own-files), except those a VM's proxy was started with. The fourth is the older versions of the mise that devvm runs on this machine to look up the tools' versions, which an upgrade of devvm leaves behind, except one that changed in the last hour, because another devvm may be downloading it. gc lists them all first and, in a terminal, asks. Without a terminal, it goes ahead.
|
||||
|
||||
- `devvm gc --tools` also deletes the current shared tools that no VM has, such as a toolchain you tried once, on every backend and C library. `devvm gc --tools node claude` deletes only those tools' builds, by the names images use, so `node` also covers pnpm and bun. A tool that a VM's state lists stays, including a stopped VM's, and so does one built in the last hour, because the VM it was built for may not list it yet. A VM whose image names a deleted tool builds it again when it needs it.
|
||||
- `devvm gc --exports` also deletes the [exports](coding-agents.md#history) of agents' history that `devvm export` and `devvm rm` saved, which are the only copy of a deleted VM's history, and `--older-than AGE` keeps those newer than `AGE`, such as `30d`, `2w` or `12h`. gc never deletes exports without `--exports`.
|
||||
- `devvm gc --backend NAME` acts on that backend alone, for example once you stop using smolvm. It deletes all of that backend's images, the current ones too, and its shared tools that are `unused`, and it leaves every other backend's images and devvm's own files as they are. Without `--backend`, gc acts on every backend, whichever backend new VMs get by default. An image that a smolvm or Docker VM was made from stays, and devvm names the VMs that use it.
|
||||
- gc never deletes VMs. Without `--backend` it keeps current images, and without `--tools` the current shared tools. It always keeps every shared tool that any VM has, including a stopped VM's and that of a VM deleted outside devvm whose state is left.
|
||||
- On Linux hosts, a stale image whose disk VMs are overlays of stays, and devvm says which VMs use it.
|
||||
|
||||
@@ -35,7 +35,7 @@ devvm keeps its state in `~/.local/share/devvm`, or in `$XDG_DATA_HOME/devvm` wh
|
||||
| `tools/` | the [shared tools](images.md#shared-tools), one read-only image per tool version, each with its checksum beside it in a `.sha256` file and what it's built for in a `.target` file | `devvm gc` deletes the versions that no VM has and that aren't the current ones, and `devvm gc --tools` also the current ones that no VM has |
|
||||
| `mise.lock`, `mise.lock.toml`, `mise.lock.checked` | the tools lock, which holds the tools' versions and their checksums, the config it was resolved from, and when devvm last checked upstream for newer versions | devvm replaces them when it checks again |
|
||||
| `mise/<version>` | the mise that devvm runs on this machine to resolve the tools lock, one folder per mise version | `devvm gc` deletes the folders of older versions, which an upgrade of devvm leaves |
|
||||
| `exports/` | the agents' [history](coding-agents.md#history) that `devvm export` and `devvm rm` save, one `.tar.gz` per export | nothing deletes them, so you delete the ones you don't need |
|
||||
| `exports/` | the agents' [history](coding-agents.md#history) that `devvm export` and `devvm rm` save, one `.tar.gz` per export | `devvm gc --exports` deletes them, or with `--older-than 30d` those that old, and nothing else does |
|
||||
| `logs/runs/` | the [run logs](troubleshooting.md#a-command-fails), one per devvm run that runs a command | devvm keeps the newest 20 |
|
||||
| `logs/<vm>.jsonl`, `logs/<vm>.proxy.log` | a VM's network log, which holds its proxy's decisions, and its proxy's own output | `devvm rm` deletes them with the VM |
|
||||
| `ca/` | the certificate authority that VMs trust for their proxy | devvm makes a new one when the old one has less than a week of its 30 days left |
|
||||
@@ -56,7 +56,7 @@ Each tool version is built once for each kind of VM that uses it, so `tools/` ca
|
||||
|
||||
### Exports
|
||||
|
||||
`devvm rm` saves the history of a VM whose image has an agent before it deletes the VM, so each export in `exports/` can be the only copy of that VM's transcripts and sessions. devvm never deletes an export. To free the space, delete the files you don't need, which are named `<vm>-<date>-<time>.tar.gz`.
|
||||
`devvm rm` saves the history of a VM whose image has an agent before it deletes the VM, so each export in `exports/` can be the only copy of that VM's transcripts and sessions. devvm deletes an export only when you ask it to. `devvm gc --exports` lists the exports, which are named `<vm>-<date>-<time>.tar.gz`, by their VM and date, then deletes them after asking in a terminal, and `devvm gc --exports --older-than 30d` deletes only those written 30 days ago or more. gc leaves anything else in the folder alone, such as an export that `devvm export` is still writing.
|
||||
|
||||
### Leftovers of a VM deleted outside devvm
|
||||
|
||||
|
||||
+44
-4
@@ -6,6 +6,7 @@ import (
|
||||
"os"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
@@ -28,6 +29,11 @@ type gcOpts struct {
|
||||
// toolNames when there are any.
|
||||
tools bool
|
||||
toolNames []string
|
||||
// exports is --exports: also the exports, only those older than
|
||||
// --older-than when it's given.
|
||||
exports bool
|
||||
olderThan string
|
||||
age time.Duration
|
||||
}
|
||||
|
||||
// newGCCmd is devvm gc.
|
||||
@@ -42,7 +48,9 @@ func newGCCmd(u *ui.UI, g *globals) *cobra.Command {
|
||||
"With --backend, only that backend's images, its current ones too, and its unused tool " +
|
||||
"images; without it, every backend's, whatever the default backend is. With --tools, also " +
|
||||
"the current tool images no VM has, on every backend and C library, or only those of the " +
|
||||
"TOOLs named (node, claude). It lists them first, and in a terminal asks. VMs are never " +
|
||||
"TOOLs named (node, claude). With --exports, also the agents' history devvm export and " +
|
||||
"devvm rm saved, the only copy of a deleted VM's, or only those older than --older-than " +
|
||||
"(30d, 2w, 12h). It lists them first, and in a terminal asks. VMs are never " +
|
||||
"deleted; on Linux hosts, an image whose disk VMs use stays, and so does a smolvm or " +
|
||||
"Docker image a VM was made from.",
|
||||
Args: func(_ *cobra.Command, args []string) error { return o.checkArgs(args) },
|
||||
@@ -53,12 +61,24 @@ func newGCCmd(u *ui.UI, g *globals) *cobra.Command {
|
||||
},
|
||||
}
|
||||
c.Flags().BoolVar(&o.tools, "tools", false, "also delete the current shared tool images no VM has; with TOOL names, only theirs")
|
||||
c.Flags().BoolVar(&o.exports, "exports", false, "also delete the saved exports of agents' history")
|
||||
c.Flags().StringVar(&o.olderThan, "older-than", "", "with --exports, only those older than `AGE`: 30d, 2w or 12h")
|
||||
return c
|
||||
}
|
||||
|
||||
// checkArgs takes tool names, by the names images have them by, only
|
||||
// with --tools.
|
||||
func (o gcOpts) checkArgs(args []string) error {
|
||||
// with --tools, and an age only with --exports.
|
||||
func (o *gcOpts) checkArgs(args []string) error {
|
||||
if o.olderThan != "" {
|
||||
if !o.exports {
|
||||
return msg.GCOlderThanNeedsExports()
|
||||
}
|
||||
age, err := state.ParseAge(o.olderThan)
|
||||
if err != nil {
|
||||
return msg.GCBadAge(o.olderThan)
|
||||
}
|
||||
o.age = age
|
||||
}
|
||||
if len(args) > 0 && !o.tools {
|
||||
return msg.GCNamesNeedTools(args[0])
|
||||
}
|
||||
@@ -75,6 +95,7 @@ type gcPlan struct {
|
||||
images []listedImage // stale and unfinished images, each with its backend's
|
||||
tools []tools.Listed // unused tool images
|
||||
current []tools.Listed // with --tools, current tool images no VM has
|
||||
exports []savedExport // with --exports, the exports old enough
|
||||
assets []string // old copies of devvm's files no VM's proxy was started with
|
||||
mise []string // older versions of devvm's own mise
|
||||
share tools.Share // where the tool images are
|
||||
@@ -124,6 +145,13 @@ func planGC(ctx context.Context, u *ui.UI, b *build.Env, o gcOpts) (gcPlan, erro
|
||||
if o.tools {
|
||||
p.current = p.share.NoVM(ts, o.toolNames)
|
||||
}
|
||||
if o.exports {
|
||||
es, err := exports(b.Dirs)
|
||||
if err != nil {
|
||||
return p, err
|
||||
}
|
||||
p.exports = olderThan(es, o.age, time.Now())
|
||||
}
|
||||
if o.only != "" {
|
||||
target, err := toolTargets(u, b)
|
||||
if err != nil {
|
||||
@@ -168,7 +196,7 @@ func backendTools(ts []tools.Listed, be string, target func(tools.Listed) (strin
|
||||
}
|
||||
|
||||
func (p gcPlan) empty() bool {
|
||||
return len(p.images)+len(p.tools)+len(p.current)+len(p.assets)+len(p.mise) == 0
|
||||
return len(p.images)+len(p.tools)+len(p.current)+len(p.exports)+len(p.assets)+len(p.mise) == 0
|
||||
}
|
||||
|
||||
// show lists what gc deletes.
|
||||
@@ -195,6 +223,12 @@ func (p gcPlan) show(u *ui.UI) {
|
||||
if len(p.mise) > 0 {
|
||||
u.Say(msg.GCMise(len(p.mise), humanSize(dirsSize(p.mise))))
|
||||
}
|
||||
if len(p.exports) > 0 {
|
||||
u.Say(msg.GCExports(len(p.exports), humanSize(exportsSize(p.exports))))
|
||||
for _, e := range p.exports {
|
||||
u.Say(msg.GCExport(exportDate(e.At), humanSize(e.Size), e.VM))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// run deletes what's planned.
|
||||
@@ -214,6 +248,12 @@ func (p gcPlan) run(ctx context.Context, u *ui.UI) error {
|
||||
if len(p.mise) > 0 {
|
||||
u.Info(msg.DeletedMise(len(p.mise)))
|
||||
}
|
||||
if err := removeAll(exportPaths(p.exports)); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(p.exports) > 0 {
|
||||
u.Info(msg.DeletedExports(len(p.exports)))
|
||||
}
|
||||
if len(p.tools) > 0 {
|
||||
if err := p.share.Delete(toolFiles(p.tools)); err != nil {
|
||||
return err
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/w3cj/devvm/internal/paths"
|
||||
)
|
||||
|
||||
// exportName is an export's name in the exports folder, as devvm export
|
||||
// and devvm rm write it: <vm>-<date>-<time>.tar.gz. Nothing else there
|
||||
// is one: an export being written (.devvm-export-…) or the user's own.
|
||||
var exportName = regexp.MustCompile(`^(devvm-.+-[0-9a-f]{14})-(\d{8}-\d{6})\.tar\.gz$`)
|
||||
|
||||
// savedExport is one of the exports in devvm's exports folder.
|
||||
type savedExport struct {
|
||||
Path string
|
||||
VM string // the VM it's of
|
||||
At time.Time // when it was written, from its name
|
||||
Size int64
|
||||
}
|
||||
|
||||
// exports lists the exports in devvm's exports folder, oldest first: only
|
||||
// plain files named as devvm names them.
|
||||
func exports(d paths.Dirs) ([]savedExport, error) {
|
||||
entries, err := os.ReadDir(d.Exports())
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
return nil, nil
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var out []savedExport
|
||||
for _, e := range entries {
|
||||
m := exportName.FindStringSubmatch(e.Name())
|
||||
if m == nil || !e.Type().IsRegular() {
|
||||
continue
|
||||
}
|
||||
at, err := time.ParseInLocation("20060102-150405", m[2], time.Local)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
p := filepath.Join(d.Exports(), e.Name())
|
||||
out = append(out, savedExport{Path: p, VM: m[1], At: at, Size: du(p)})
|
||||
}
|
||||
slices.SortFunc(out, func(a, b savedExport) int { return a.At.Compare(b.At) })
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// olderThan returns the exports written at least age before now; all of
|
||||
// them when age is 0.
|
||||
func olderThan(es []savedExport, age time.Duration, now time.Time) []savedExport {
|
||||
if age == 0 {
|
||||
return es
|
||||
}
|
||||
var out []savedExport
|
||||
for _, e := range es {
|
||||
if now.Sub(e.At) >= age {
|
||||
out = append(out, e)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// exportPaths are these exports' files.
|
||||
func exportPaths(es []savedExport) []string {
|
||||
out := make([]string, 0, len(es))
|
||||
for _, e := range es {
|
||||
out = append(out, e.Path)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// exportsSize is the disk these exports take.
|
||||
func exportsSize(es []savedExport) int64 {
|
||||
var n int64
|
||||
for _, e := range es {
|
||||
n += e.Size
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// exportDate is how an export's list shows its date.
|
||||
func exportDate(t time.Time) string { return strings.TrimSpace(t.Format("2006-01-02 15:04")) }
|
||||
@@ -337,7 +337,7 @@ func formatBackend(reg *backend.Registry, format string) string {
|
||||
// backends, and its other state. Each file is counted once.
|
||||
func (e spaceEnv) dataRows() ([]spaceRow, error) {
|
||||
d := e.b.Dirs
|
||||
exports, err := filesIn(d.Exports(), func(n string) bool { return strings.HasSuffix(n, ".tar.gz") })
|
||||
es, err := exports(d)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -371,7 +371,7 @@ func (e spaceEnv) dataRows() ([]spaceRow, error) {
|
||||
}
|
||||
other := otherState(d) - left.Size
|
||||
return nonEmpty(
|
||||
spaceRow{What: "exports", Count: exports.count, Size: exports.size, Sized: true, Kept: keptOnlyCopy, Rest: "delete them"},
|
||||
spaceRow{What: "exports", Count: len(es), Size: exportsSize(es), Sized: true, Kept: keptOnlyCopy, Rest: "gc --exports"},
|
||||
spaceRow{What: "network logs", Count: logs.count, Size: logs.size, Sized: true, Kept: keptLog, Rest: "devvm rm"},
|
||||
spaceRow{What: "run logs", Count: runs.count, Size: runs.size, Sized: true, Kept: keptLog, Rest: "the newest 20 stay"},
|
||||
spaceRow{What: "mise", Count: mise.count, Size: mise.size, Sized: true, Kept: keptRemade, GC: dirsSize(oldMise), Rest: "-"},
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/w3cj/devvm/internal/backend"
|
||||
"github.com/w3cj/devvm/internal/backend/backendtest"
|
||||
@@ -113,6 +114,40 @@ func TestLeftoverFiles(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Exports: only plain files named as devvm names them, oldest first; the
|
||||
// age filter keeps those at least that old.
|
||||
func TestExports(t *testing.T) {
|
||||
d := paths.Dirs{Data: t.TempDir()}
|
||||
if err := os.MkdirAll(d.Exports(), 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, f := range []string{
|
||||
"devvm-b-12345612345612-20260301-000000.tar.gz", "devvm-a-12345612345612-20260101-120000.tar.gz",
|
||||
".devvm-export-1", "notes.tar.gz", "devvm-c-1234-20260101-000000.tar.gz",
|
||||
} {
|
||||
if err := os.WriteFile(filepath.Join(d.Exports(), f), []byte("x"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
es, err := exports(d)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(es) != 2 || es[0].VM != "devvm-a-12345612345612" || es[1].VM != "devvm-b-12345612345612" {
|
||||
t.Fatalf("exports = %+v", es)
|
||||
}
|
||||
now := time.Date(2026, 3, 31, 0, 0, 0, 0, time.Local)
|
||||
if got := olderThan(es, 30*24*time.Hour, now); len(got) != 1 || got[0].VM != "devvm-a-12345612345612" {
|
||||
t.Errorf("olderThan 30d = %+v", got)
|
||||
}
|
||||
if got := olderThan(es, 0, now); len(got) != 2 {
|
||||
t.Errorf("olderThan 0 = %+v", got)
|
||||
}
|
||||
if got := olderThan(es, 30*24*time.Hour, time.Date(2026, 3, 31, 0, 0, 0, 0, time.Local).Add(-time.Second)); len(got) != 1 {
|
||||
t.Errorf("olderThan, a second short = %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// The table: rows, a total, and the caches apart; what a row lacks is -.
|
||||
func TestWriteSpace(t *testing.T) {
|
||||
var b strings.Builder
|
||||
|
||||
@@ -106,6 +106,8 @@ var docs = map[string]string{ //nolint:gosec // message IDs and pages, not crede
|
||||
"layer_exists": "using/customizing#your-own-layers",
|
||||
"images_stale_note": "using/images#cleaning-up",
|
||||
"images_unused": "using/images#cleaning-up",
|
||||
"gc_older_than_needs_exports": "using/images#cleaning-up",
|
||||
"gc_bad_age": "using/images#cleaning-up",
|
||||
"gc_names_need_tools": "using/images#cleaning-up",
|
||||
"gc_unknown_tool": "using/images#cleaning-up",
|
||||
"images_total": "using/space#devvm-space",
|
||||
|
||||
@@ -504,6 +504,11 @@ func DeletedAssets(count int) M {
|
||||
return M{id: "deleted_assets", plural: true, count: count, vals: []val{{"count", strconv.Itoa(count)}}}
|
||||
}
|
||||
|
||||
// DeletedExports is [deleted_exports]: "deleted {{.count}} exports"
|
||||
func DeletedExports(count int) M {
|
||||
return M{id: "deleted_exports", plural: true, count: count, vals: []val{{"count", strconv.Itoa(count)}}}
|
||||
}
|
||||
|
||||
// DeletedImage is [deleted_image]: "deleted image {{.image}} ({{.key}})"
|
||||
func DeletedImage(image string, key string) M {
|
||||
return M{id: "deleted_image", vals: []val{{"image", image}, {"key", key}}}
|
||||
@@ -1034,11 +1039,26 @@ func GCAssets(count int) M {
|
||||
return M{id: "gc_assets", plural: true, count: count, vals: []val{{"count", strconv.Itoa(count)}}}
|
||||
}
|
||||
|
||||
// GCBadAge is [gc_bad_age]: "{{.age}} isn't an age devvm takes. Give a whole number of days, weeks or hours, such as 30d, 2w or 12h."
|
||||
func GCBadAge(age string) M {
|
||||
return M{id: "gc_bad_age", vals: []val{{"age", age}}}
|
||||
}
|
||||
|
||||
// GCConfirm is [gc_confirm]: "Your VMs stay as they are. Delete these?"
|
||||
func GCConfirm() M {
|
||||
return M{id: "gc_confirm"}
|
||||
}
|
||||
|
||||
// GCExport is [gc_export]: " {{.vm}}, {{.date}} ({{.size}})"
|
||||
func GCExport(date string, size string, vm string) M {
|
||||
return M{id: "gc_export", vals: []val{{"date", date}, {"size", size}, {"vm", vm}}}
|
||||
}
|
||||
|
||||
// GCExports is [gc_exports]: "{{.count}} exports of agents' history ({{.size}}), the only copy of a deleted VM's:"
|
||||
func GCExports(count int, size string) M {
|
||||
return M{id: "gc_exports", plural: true, count: count, vals: []val{{"count", strconv.Itoa(count)}, {"size", size}}}
|
||||
}
|
||||
|
||||
// GCFailed is [gc_failed]: "Cleaning up failed: {{.reason}}."
|
||||
func GCFailed(reason string) M {
|
||||
return M{id: "gc_failed", vals: []val{{"reason", reason}}}
|
||||
@@ -1069,6 +1089,11 @@ func GCOldHint() M {
|
||||
return M{id: "gc_old_hint"}
|
||||
}
|
||||
|
||||
// GCOlderThanNeedsExports is [gc_older_than_needs_exports]: "--older-than goes with --exports. Run devvm gc --exports --older-than AGE."
|
||||
func GCOlderThanNeedsExports() M {
|
||||
return M{id: "gc_older_than_needs_exports"}
|
||||
}
|
||||
|
||||
// GCTools is [gc_tools]: "{{.count}} shared tool images are unused ({{.size}})."
|
||||
func GCTools(count int, size string) M {
|
||||
return M{id: "gc_tools", plural: true, count: count, vals: []val{{"count", strconv.Itoa(count)}, {"size", size}}}
|
||||
@@ -2696,6 +2721,7 @@ var specs = map[string]spec{
|
||||
"delete_parent_vm": {fields: []string{"dir", "vm"}},
|
||||
"deleted": {fields: []string{"vm"}},
|
||||
"deleted_assets": {plural: true, fields: []string{"count"}},
|
||||
"deleted_exports": {plural: true, fields: []string{"count"}},
|
||||
"deleted_image": {fields: []string{"image", "key"}},
|
||||
"deleted_mise": {plural: true, fields: []string{"count"}},
|
||||
"deleted_old_image": {fields: []string{"image", "key"}},
|
||||
@@ -2802,13 +2828,17 @@ var specs = map[string]spec{
|
||||
"from_not_found": {fields: []string{"from", "host"}},
|
||||
"from_unreachable": {fields: []string{"from", "host"}},
|
||||
"gc_assets": {plural: true, fields: []string{"count"}},
|
||||
"gc_bad_age": {fields: []string{"age"}},
|
||||
"gc_confirm": {},
|
||||
"gc_export": {fields: []string{"date", "size", "vm"}},
|
||||
"gc_exports": {plural: true, fields: []string{"count", "size"}},
|
||||
"gc_failed": {fields: []string{"reason"}},
|
||||
"gc_image": {fields: []string{"image", "key"}},
|
||||
"gc_images": {},
|
||||
"gc_mise": {plural: true, fields: []string{"count", "size"}},
|
||||
"gc_names_need_tools": {fields: []string{"tool"}},
|
||||
"gc_old_hint": {},
|
||||
"gc_older_than_needs_exports": {},
|
||||
"gc_tools": {plural: true, fields: []string{"count", "size"}},
|
||||
"gc_tools_no_vm": {plural: true, fields: []string{"count", "size", "tools"}},
|
||||
"gc_unfinished": {fields: []string{"key"}},
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// errAge is an age that isn't a whole number of days, weeks or hours.
|
||||
var errAge = errors.New("an age is a whole number of days, weeks or hours, such as 30d, 2w or 12h")
|
||||
|
||||
// ParseAge reads an age as devvm's settings and flags take one: a whole
|
||||
// number of days (30d), weeks (2w) or hours (12h), more than zero.
|
||||
func ParseAge(s string) (time.Duration, error) {
|
||||
units := map[string]time.Duration{"d": 24 * time.Hour, "w": 7 * 24 * time.Hour, "h": time.Hour}
|
||||
if len(s) < 2 {
|
||||
return 0, errAge
|
||||
}
|
||||
unit, ok := units[s[len(s)-1:]]
|
||||
digits := s[:len(s)-1]
|
||||
if !ok || strings.TrimLeft(digits, "0123456789") != "" {
|
||||
return 0, errAge
|
||||
}
|
||||
n, err := strconv.Atoi(digits)
|
||||
if err != nil || n <= 0 || n > 100000 {
|
||||
return 0, errAge
|
||||
}
|
||||
return time.Duration(n) * unit, nil
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestParseAge(t *testing.T) {
|
||||
day := 24 * time.Hour
|
||||
for in, want := range map[string]time.Duration{"30d": 30 * day, "2w": 14 * day, "12h": 12 * time.Hour, "1d": day} {
|
||||
if got, err := ParseAge(in); err != nil || got != want {
|
||||
t.Errorf("ParseAge(%q) = %v, %v; want %v", in, got, err, want)
|
||||
}
|
||||
}
|
||||
for _, in := range []string{"", "d", "0d", "-1d", "30", "30m", "1.5d", "30 d", "+3d", "999999d", "30D"} {
|
||||
if _, err := ParseAge(in); err == nil {
|
||||
t.Errorf("ParseAge(%q): want an error", in)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -718,6 +718,24 @@ other = "Tool names such as {{.tool}} go with --tools. Run devvm gc --tools {{.t
|
||||
[gc_unknown_tool]
|
||||
other = "There's no shared tool named {{.tool}}. The names are {{.tools}}."
|
||||
|
||||
[gc_exports]
|
||||
one = "{{.count}} export of agents' history ({{.size}}), the only copy of a deleted VM's:"
|
||||
other = "{{.count}} exports of agents' history ({{.size}}), the only copy of a deleted VM's:"
|
||||
|
||||
[gc_export]
|
||||
description = "date is when devvm wrote it, as 2026-10-27 23:23"
|
||||
other = " {{.vm}}, {{.date}} ({{.size}})"
|
||||
|
||||
[deleted_exports]
|
||||
one = "deleted {{.count}} export"
|
||||
other = "deleted {{.count}} exports"
|
||||
|
||||
[gc_older_than_needs_exports]
|
||||
other = "--older-than goes with --exports. Run devvm gc --exports --older-than AGE."
|
||||
|
||||
[gc_bad_age]
|
||||
other = "{{.age}} isn't an age devvm takes. Give a whole number of days, weeks or hours, such as 30d, 2w or 12h."
|
||||
|
||||
[gc_mise]
|
||||
one = "{{.count}} older version of the mise devvm runs on this machine is left ({{.size}})."
|
||||
other = "{{.count}} older versions of the mise devvm runs on this machine are left ({{.size}})."
|
||||
|
||||
Vendored
+38
@@ -0,0 +1,38 @@
|
||||
# gc --exports: the exports devvm export and devvm rm saved, never part of
|
||||
# gc's default run; with --older-than, only those that old. Only files
|
||||
# named as devvm names them: never an export being written, nor anything
|
||||
# else in the folder. Never a VM.
|
||||
env DEVVM_MESSAGES=names
|
||||
env EXPORTS=$XDG_DATA_HOME/devvm/exports
|
||||
|
||||
mkdir $EXPORTS
|
||||
exec sh -c 'cd "$EXPORTS" && for f in devvm-old-12345612345612-20200101-000000 devvm-new-abcdefabcdefab-20991231-235959; do echo x > $f.tar.gz; done'
|
||||
exec sh -c 'cd "$EXPORTS" && echo x > .devvm-export-123 && echo x > notes.tar.gz && mkdir devvm-dir-12345612345612-20200101-000000.tar.gz'
|
||||
|
||||
# The default run keeps them.
|
||||
exec devvm gc
|
||||
stdout '^==> nothing_to_clean$'
|
||||
exists $EXPORTS/devvm-old-12345612345612-20200101-000000.tar.gz
|
||||
|
||||
# An age only with --exports, and only one devvm takes.
|
||||
! exec devvm gc --older-than 30d
|
||||
stderr '^devvm: gc_older_than_needs_exports$'
|
||||
! exec devvm gc --exports --older-than 30
|
||||
stderr '^devvm: gc_bad_age age=30$'
|
||||
|
||||
# --older-than 30d: the old one, by its VM and date.
|
||||
exec devvm gc --exports --older-than 30d
|
||||
stdout '^gc_exports count=1 size=\S+$'
|
||||
stdout '^gc_export date=2020-01-01 00:00 size=\S+ vm=devvm-old-12345612345612$'
|
||||
stdout '^==> deleted_exports count=1$'
|
||||
! exists $EXPORTS/devvm-old-12345612345612-20200101-000000.tar.gz
|
||||
exists $EXPORTS/devvm-new-abcdefabcdefab-20991231-235959.tar.gz
|
||||
|
||||
# --exports: every one left, and nothing that isn't one.
|
||||
exec devvm gc --exports
|
||||
stdout '^gc_export .* vm=devvm-new-abcdefabcdefab$'
|
||||
stdout '^==> deleted_exports count=1$'
|
||||
! exists $EXPORTS/devvm-new-abcdefabcdefab-20991231-235959.tar.gz
|
||||
exists $EXPORTS/.devvm-export-123
|
||||
exists $EXPORTS/notes.tar.gz
|
||||
exists $EXPORTS/devvm-dir-12345612345612-20200101-000000.tar.gz
|
||||
Vendored
+1
-1
@@ -37,7 +37,7 @@ stdout '^VMs +lima +1 +\S+ +only copy +- +devvm rm$'
|
||||
stdout '^images +lima +2 +\S+ +remade +\S+ +gc --backend lima$'
|
||||
stdout '^tools gnu +lima +\d+ +\S+ +remade +- +gc --tools$'
|
||||
stdout '^tools - +- +1 +\S+ +remade +\S+ +gc --tools$'
|
||||
stdout '^exports +- +1 +\S+ +only copy +- +delete them$'
|
||||
stdout '^exports +- +1 +\S+ +only copy +- +gc --exports$'
|
||||
stdout '^network logs +- +2 +\S+ +log +- +devvm rm$'
|
||||
stdout '^run logs +- +\d+ +\S+ +log +- +the newest 20 stay$'
|
||||
stdout '^mise +- +1 +'
|
||||
|
||||
Reference in new issue
Block a user