diff --git a/availableVersions.json b/availableVersions.json index 93a2344..50a3e48 100644 --- a/availableVersions.json +++ b/availableVersions.json @@ -1,6 +1,6 @@ { - "production": "1.6.21", - "test": "1.6.21", - "local": "1.6.21", - "all": "1.6.21" + "production": "1.6.22", + "test": "1.6.22", + "local": "1.6.22", + "all": "1.6.22" } diff --git a/src/agent.go b/src/agent.go index 53f136e..d46f9e1 100644 --- a/src/agent.go +++ b/src/agent.go @@ -7,7 +7,9 @@ import ( "os" "reagent/api" "reagent/apps" + "reagent/appstorage" "reagent/benchmark" + "reagent/common" "reagent/config" "reagent/container" "reagent/crashrecovery" @@ -460,6 +462,32 @@ func NewAgent(generalConfig *config.Config) (agent *Agent) { safe.Go(func() { diskGuard.Run(context.Background()) }) } + // What each installed app takes on this device's disk (its volumes, its + // folders, its containers' writable layers), measured every 30 minutes + // and carried in the device stats for the app's storage view. + safe.Go(func() { + appstorage.Run(context.Background(), container, appstorage.Config{ + AppsDirectory: cliArgs.AppsDirectory, + AppsComposeDir: cliArgs.AppsComposeDir, + AppsSharedDir: cliArgs.AppsSharedDir, + AgentDir: cliArgs.AgentDir, + Apps: func() ([]appstorage.App, error) { + payloads, err := appStore.GetRequestedStates() + if err != nil { + return nil, err + } + prod := make([]appstorage.App, 0, len(payloads)) + for _, p := range payloads { + if p.Stage == common.PROD { + prod = append(prod, appstorage.App{AppKey: p.AppKey, AppName: p.AppName}) + } + } + return prod, nil + }, + Paused: diskguard.IsEmergency, + }) + }) + // Reconcile local app state against the Docker daemon. fatal=true keeps // the historical behavior (die and let the supervisor restart us); the // late-daemon path must not kill an otherwise healthy agent, so it logs diff --git a/src/appstorage/appstorage.go b/src/appstorage/appstorage.go new file mode 100644 index 0000000..9758277 --- /dev/null +++ b/src/appstorage/appstorage.go @@ -0,0 +1,328 @@ +// Package appstorage measures how much of the device's disk each installed +// app takes, for the app's storage view in the platform. It runs as a slow +// background loop in the agent; the last measurement rides in the device +// stats of every heartbeat (StatsFields), which the backend keeps on the +// device row. +// +// Per app (the PROD install; a development run is not the installed app) it +// counts: +// +// - volumes: the Docker volumes its containers mount, and those labeled with +// its compose project (kept by `compose down`, so a stopped app still has +// them), each with its size; +// - folder: the app's own folders the agent creates — the /data folder of a +// single-container app (/prod/) and the folder of a +// compose app (/), which holds the data of its +// relative bind mounts (./data:/data); +// - container: the writable layers of its containers, the data written +// outside any mount (an app that forgot to declare a volume fills it); +// - host_paths: the other host folders it binds (/mnt/usb:/data). They are +// listed, not counted: such a folder can be shared with other apps or the +// host, so its size is not the app's. +// +// The Docker part comes from the daemon's disk-usage report, which walks +// every volume and writable layer: on an SD card that takes minutes and keeps +// the daemon busy. So it runs every 30 minutes, with a deadline, and not +// while the device is in a disk emergency (when the daemon is often wedged). +// A failed pass keeps the previous measurement. +package appstorage + +import ( + "context" + "io/fs" + "os" + "path/filepath" + "reagent/common" + "reagent/container" + "slices" + "strings" + "sync/atomic" + "time" + + "github.com/rs/zerolog/log" +) + +const ( + initialDelay = 3 * time.Minute // not while the agent starts the apps + interval = 30 * time.Minute + passTimeout = 10 * time.Minute + + // The heartbeat carries this on every beat: keep it small. + maxVolumesPerApp = 20 + maxHostPathsPerApp = 10 + + composeProjectLabel = "com.docker.compose.project" +) + +// Docker is the subset of the container layer the measurement needs. +// *container.Docker satisfies it. +type Docker interface { + DiskUsage(ctx context.Context) ([]container.DiskUsageVolume, []container.DiskUsageContainer, error) +} + +// App is one locally known app. +type App struct { + AppKey uint64 + AppName string +} + +type Config struct { + AppsDirectory string // holds /, the /data folders + AppsComposeDir string // holds , the PROD compose folders + AppsSharedDir string // the /shared folder of all apps: nobody's alone + AgentDir string + // Apps returns the locally known PROD apps, stopped ones included. + Apps func() ([]App, error) + // Paused reports whether a pass must be skipped now (disk emergency). + Paused func() bool +} + +// Volume is one volume of an app with its size in bytes. +type Volume struct { + Name string + Bytes int64 +} + +// Usage is what one app takes on this device. +type Usage struct { + AppKey uint64 + VolumesBytes int64 + Volumes []Volume // largest first, at most maxVolumesPerApp + FolderBytes int64 + ContainerBytes int64 + HostPaths []string // at most maxHostPathsPerApp +} + +type Report struct { + MeasuredAt time.Time + Apps []Usage +} + +// dict is the report as the heartbeat carries it: plain maps, the time as +// RFC 3339, so every WAMP serializer encodes it the same way. +func (r *Report) dict() common.Dict { + apps := make([]common.Dict, 0, len(r.Apps)) + for _, u := range r.Apps { + app := common.Dict{ + "app_key": u.AppKey, + "volumes_bytes": u.VolumesBytes, + "folder_bytes": u.FolderBytes, + "container_bytes": u.ContainerBytes, + } + if len(u.Volumes) > 0 { + volumes := make([]common.Dict, 0, len(u.Volumes)) + for _, v := range u.Volumes { + volumes = append(volumes, common.Dict{"name": v.Name, "bytes": v.Bytes}) + } + app["volumes"] = volumes + } + if len(u.HostPaths) > 0 { + app["host_paths"] = u.HostPaths + } + apps = append(apps, app) + } + return common.Dict{"measured_at": r.MeasuredAt.Format(time.RFC3339), "apps": apps} +} + +var last atomic.Pointer[Report] + +// StatsFields is the last measurement for the heartbeat's stats: +// "app_storage" with measured_at and apps, one entry per locally known PROD +// app (app_key, volumes_bytes, volumes [name, bytes], folder_bytes, +// container_bytes, host_paths). Until the first pass has finished, apps is +// empty and measured_at missing: the platform then knows the agent measures, +// unlike one too old to, whose stats have no app_storage at all. +func StatsFields() common.Dict { + r := last.Load() + if r == nil { + return common.Dict{"app_storage": common.Dict{"apps": []common.Dict{}}} + } + return common.Dict{"app_storage": r.dict()} +} + +// Run measures after initialDelay and then every interval, until ctx ends. +func Run(ctx context.Context, docker Docker, cfg Config) { + timer := time.NewTimer(initialDelay) + defer timer.Stop() + for { + select { + case <-ctx.Done(): + return + case <-timer.C: + } + if cfg.Paused == nil || !cfg.Paused() { + passCtx, cancel := context.WithTimeout(ctx, passTimeout) + report, err := Measure(passCtx, docker, cfg) + cancel() + if err != nil { + log.Warn().Err(err).Msg("appstorage: measuring the apps' disk usage failed; keeping the last measurement") + } else { + last.Store(report) + } + } + timer.Reset(interval) + } +} + +// Measure takes one measurement of every locally known PROD app. +func Measure(ctx context.Context, docker Docker, cfg Config) (*Report, error) { + apps, err := cfg.Apps() + if err != nil { + return nil, err + } + volumes, containers, err := docker.DiskUsage(ctx) + if err != nil { + return nil, err + } + + byProject := make(map[string]int, len(apps)) + byContainer := make(map[string]int, len(apps)) + usages := make([]Usage, len(apps)) + folders := make([][]string, len(apps)) + for i, app := range apps { + usages[i].AppKey = app.AppKey + byProject[common.NormalizeComposeProjectName( + common.BuildComposeContainerName(common.PROD, app.AppKey, app.AppName))] = i + byContainer[common.BuildContainerName(common.PROD, app.AppKey, app.AppName)] = i + folders[i] = appFolders(cfg, app.AppName) + } + + ownerOf := func(labels map[string]string, names []string) (int, bool) { + if project := labels[composeProjectLabel]; project != "" { + i, ok := byProject[common.NormalizeComposeProjectName(project)] + return i, ok + } + for _, n := range names { + if i, ok := byContainer[strings.TrimPrefix(n, "/")]; ok { + return i, true + } + } + return 0, false + } + + volumeOwner := make(map[string]int) + for _, c := range containers { + i, ok := ownerOf(c.Labels, c.Names) + if !ok { + continue + } + if c.SizeRw > 0 { + usages[i].ContainerBytes += c.SizeRw + } + for _, m := range c.Mounts { + switch m.Type { + case "volume": + if _, taken := volumeOwner[m.Name]; !taken && m.Name != "" { + volumeOwner[m.Name] = i + } + case "bind": + if isHostPath(cfg, folders[i], m.Source) && !slices.Contains(usages[i].HostPaths, m.Source) { + usages[i].HostPaths = append(usages[i].HostPaths, m.Source) + } + } + } + } + + for _, v := range volumes { + i, ok := volumeOwner[v.Name] + if !ok { + i, ok = ownerOf(v.Labels, nil) + } + if !ok || v.Size < 0 { + continue + } + usages[i].VolumesBytes += v.Size + usages[i].Volumes = append(usages[i].Volumes, Volume{Name: v.Name, Bytes: v.Size}) + } + + for i := range usages { + for _, dir := range folders[i] { + size, err := dirSize(ctx, dir) + if err != nil { + return nil, err + } + usages[i].FolderBytes += size + } + // Largest first, so a cut list still shows what matters. + slices.SortFunc(usages[i].Volumes, func(a, b Volume) int { + switch { + case a.Bytes > b.Bytes: + return -1 + case a.Bytes < b.Bytes: + return 1 + } + return strings.Compare(a.Name, b.Name) + }) + if len(usages[i].Volumes) > maxVolumesPerApp { + usages[i].Volumes = usages[i].Volumes[:maxVolumesPerApp] + } + slices.Sort(usages[i].HostPaths) + if len(usages[i].HostPaths) > maxHostPathsPerApp { + usages[i].HostPaths = usages[i].HostPaths[:maxHostPathsPerApp] + } + } + + return &Report{MeasuredAt: time.Now().UTC().Truncate(time.Second), Apps: usages}, nil +} + +// appFolders are the app's own folders, the ones the agent creates for it. +func appFolders(cfg Config, appName string) []string { + var dirs []string + if cfg.AppsDirectory != "" { + dirs = append(dirs, filepath.Join(cfg.AppsDirectory, strings.ToLower(string(common.PROD)), strings.ToLower(appName))) + } + if cfg.AppsComposeDir != "" { + dirs = append(dirs, filepath.Join(cfg.AppsComposeDir, appName)) + } + return dirs +} + +// systemPrefixes are host folders an app binds to reach the host (devices, +// sockets, the D-Bus, configuration), not to keep data there. +var systemPrefixes = []string{"/dev", "/sys", "/proc", "/run", "/var/run", "/etc", "/usr", "/lib", "/bin", "/sbin", "/boot", "/tmp"} + +// isHostPath reports whether a bind source is a host folder outside the app's +// own folders and the agent's, i.e. one worth listing as "not counted". +func isHostPath(cfg Config, appFolders []string, source string) bool { + if source == "" || !filepath.IsAbs(source) { + return false + } + clean := filepath.Clean(source) + own := append([]string{cfg.AppsDirectory, cfg.AppsSharedDir, cfg.AgentDir}, appFolders...) + for _, root := range append(own, systemPrefixes...) { + if root != "" && within(clean, root) { + return false + } + } + info, err := os.Stat(clean) + return err == nil && info.IsDir() +} + +func within(path, root string) bool { + root = filepath.Clean(root) + return path == root || strings.HasPrefix(path, root+string(filepath.Separator)) +} + +// dirSize sums the sizes of the regular files below dir. A missing dir is 0; +// files that vanish or cannot be read during the walk are skipped. +func dirSize(ctx context.Context, dir string) (int64, error) { + var total int64 + err := filepath.WalkDir(dir, func(_ string, d fs.DirEntry, err error) error { + if ctxErr := ctx.Err(); ctxErr != nil { + return ctxErr + } + if err != nil { + return nil + } + if d.Type().IsRegular() { + if info, err := d.Info(); err == nil { + total += info.Size() + } + } + return nil + }) + if err != nil && ctx.Err() != nil { + return 0, err + } + return total, nil +} diff --git a/src/appstorage/appstorage_test.go b/src/appstorage/appstorage_test.go new file mode 100644 index 0000000..92a4b91 --- /dev/null +++ b/src/appstorage/appstorage_test.go @@ -0,0 +1,236 @@ +package appstorage + +import ( + "context" + "errors" + "fmt" + "os" + "path/filepath" + "reagent/common" + "reagent/container" + "testing" +) + +type fakeDocker struct { + volumes []container.DiskUsageVolume + containers []container.DiskUsageContainer + err error +} + +func (f *fakeDocker) DiskUsage(context.Context) ([]container.DiskUsageVolume, []container.DiskUsageContainer, error) { + return f.volumes, f.containers, f.err +} + +func writeFile(t *testing.T, path string, size int) { + t.Helper() + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, make([]byte, size), 0o644); err != nil { + t.Fatal(err) + } +} + +// testConfig lays out an agent folder like a device's: apps/prod/ for +// /data folders, apps/compose/ for compose folders, apps/shared. +func testConfig(t *testing.T, apps ...App) Config { + t.Helper() + root := t.TempDir() + // t.TempDir lives below /tmp on Linux, which counts as a system folder. + saved := systemPrefixes + systemPrefixes = []string{"/dev", "/sys", "/proc"} + t.Cleanup(func() { systemPrefixes = saved }) + return Config{ + AppsDirectory: filepath.Join(root, "apps"), + AppsComposeDir: filepath.Join(root, "apps", "compose"), + AppsSharedDir: filepath.Join(root, "apps", "shared"), + AgentDir: filepath.Join(root, "agent"), + Apps: func() ([]App, error) { return apps, nil }, + } +} + +func usageOf(t *testing.T, r *Report, appKey uint64) Usage { + t.Helper() + for _, u := range r.Apps { + if u.AppKey == appKey { + return u + } + } + t.Fatalf("no usage for app %d in %+v", appKey, r.Apps) + return Usage{} +} + +func TestMeasureComposeApp(t *testing.T) { + cfg := testConfig(t, App{AppKey: 6, AppName: "Flow_Meter"}) + project := common.NormalizeComposeProjectName(common.BuildComposeContainerName(common.PROD, 6, "Flow_Meter")) + composeDir := filepath.Join(cfg.AppsComposeDir, "Flow_Meter") + writeFile(t, filepath.Join(composeDir, "docker-compose.yml"), 100) + writeFile(t, filepath.Join(composeDir, "data", "readings.db"), 4000) // ./data:/data + hostDir := filepath.Join(filepath.Dir(cfg.AppsDirectory), "mnt-usb") + if err := os.MkdirAll(hostDir, 0o755); err != nil { + t.Fatal(err) + } + + docker := &fakeDocker{ + volumes: []container.DiskUsageVolume{ + {Name: project + "_db", Labels: map[string]string{composeProjectLabel: project}, Size: 5000}, + // Kept by compose down: labeled, mounted by no container. + {Name: project + "_cache", Labels: map[string]string{composeProjectLabel: project}, Size: 700}, + }, + containers: []container.DiskUsageContainer{ + { + Names: []string{"/" + project + "-db-1"}, + Labels: map[string]string{composeProjectLabel: project}, + SizeRw: 300, + Mounts: []container.DiskUsageMount{ + {Type: "volume", Name: project + "_db"}, + {Type: "bind", Source: filepath.Join(composeDir, "data")}, + {Type: "bind", Source: hostDir}, + {Type: "bind", Source: cfg.AppsSharedDir}, + {Type: "bind", Source: "/dev/ttyUSB0"}, + }, + }, + {Names: []string{"/" + project + "-web-1"}, Labels: map[string]string{composeProjectLabel: project}, SizeRw: 20}, + }, + } + + r, err := Measure(context.Background(), docker, cfg) + if err != nil { + t.Fatal(err) + } + u := usageOf(t, r, 6) + if u.VolumesBytes != 5700 || len(u.Volumes) != 2 || u.Volumes[0].Name != project+"_db" { + t.Errorf("volumes: got %d %+v, want 5700 with %s_db first", u.VolumesBytes, u.Volumes, project) + } + if u.FolderBytes != 4100 { + t.Errorf("folder bytes: got %d, want 4100", u.FolderBytes) + } + if u.ContainerBytes != 320 { + t.Errorf("container bytes: got %d, want 320", u.ContainerBytes) + } + if len(u.HostPaths) != 1 || u.HostPaths[0] != hostDir { + t.Errorf("host paths: got %v, want only %s", u.HostPaths, hostDir) + } +} + +func TestMeasureSingleContainerApp(t *testing.T) { + cfg := testConfig(t, App{AppKey: 7, AppName: "Sensor"}) + writeFile(t, filepath.Join(cfg.AppsDirectory, "prod", "sensor", "state.json"), 1234) + writeFile(t, filepath.Join(cfg.AppsDirectory, "dev", "sensor", "dev.json"), 99999) // the development run + + docker := &fakeDocker{ + volumes: []container.DiskUsageVolume{ + {Name: "3f2a9c", Size: 800}, // anonymous volume of the image's VOLUME + {Name: "someone-elses", Size: 1 << 30}, + }, + containers: []container.DiskUsageContainer{ + { + Names: []string{"/prod_7_sensor"}, + SizeRw: 50, + Mounts: []container.DiskUsageMount{ + {Type: "bind", Source: filepath.Join(cfg.AppsDirectory, "prod", "sensor")}, + {Type: "volume", Name: "3f2a9c"}, + }, + }, + {Names: []string{"/dev_7_sensor"}, SizeRw: 77777, Mounts: []container.DiskUsageMount{{Type: "volume", Name: "someone-elses"}}}, + {Names: []string{"/portainer"}, SizeRw: 1 << 20}, + }, + } + + r, err := Measure(context.Background(), docker, cfg) + if err != nil { + t.Fatal(err) + } + u := usageOf(t, r, 7) + if u.VolumesBytes != 800 || u.FolderBytes != 1234 || u.ContainerBytes != 50 || len(u.HostPaths) != 0 { + t.Errorf("got %+v, want volumes 800, folder 1234, container 50, no host paths", u) + } +} + +func TestMeasureStoppedAppWithoutFolders(t *testing.T) { + cfg := testConfig(t, App{AppKey: 8, AppName: "idle"}) + r, err := Measure(context.Background(), &fakeDocker{}, cfg) + if err != nil { + t.Fatal(err) + } + if u := usageOf(t, r, 8); u.VolumesBytes != 0 || u.FolderBytes != 0 || u.ContainerBytes != 0 || len(u.Volumes) != 0 || len(u.HostPaths) != 0 { + t.Errorf("got %+v, want an all-zero entry", u) + } +} + +func TestMeasureSkipsUnknownVolumeSize(t *testing.T) { + cfg := testConfig(t, App{AppKey: 9, AppName: "nfs"}) + project := common.NormalizeComposeProjectName(common.BuildComposeContainerName(common.PROD, 9, "nfs")) + docker := &fakeDocker{volumes: []container.DiskUsageVolume{ + {Name: "remote", Labels: map[string]string{composeProjectLabel: project}, Size: -1}, + }} + r, err := Measure(context.Background(), docker, cfg) + if err != nil { + t.Fatal(err) + } + if u := usageOf(t, r, 9); u.VolumesBytes != 0 || len(u.Volumes) != 0 { + t.Errorf("got %+v, want the volume of unknown size left out", u) + } +} + +func TestMeasureKeepsLargestVolumes(t *testing.T) { + cfg := testConfig(t, App{AppKey: 10, AppName: "many"}) + project := common.NormalizeComposeProjectName(common.BuildComposeContainerName(common.PROD, 10, "many")) + docker := &fakeDocker{} + var total int64 + for i := range maxVolumesPerApp + 5 { + size := int64(i + 1) + total += size + docker.volumes = append(docker.volumes, container.DiskUsageVolume{ + Name: fmt.Sprintf("v%02d", i), Labels: map[string]string{composeProjectLabel: project}, Size: size, + }) + } + r, err := Measure(context.Background(), docker, cfg) + if err != nil { + t.Fatal(err) + } + u := usageOf(t, r, 10) + if len(u.Volumes) != maxVolumesPerApp || u.Volumes[0].Bytes != int64(maxVolumesPerApp+5) || u.VolumesBytes != total { + t.Errorf("got %d volumes, first %+v, total %d; want %d largest first and the full total %d", + len(u.Volumes), u.Volumes[0], u.VolumesBytes, maxVolumesPerApp, total) + } +} + +func TestMeasureFailsWhenDockerFails(t *testing.T) { + cfg := testConfig(t, App{AppKey: 6, AppName: "x"}) + if _, err := Measure(context.Background(), &fakeDocker{err: errors.New("daemon down")}, cfg); err == nil { + t.Error("want the daemon's error, so the last measurement is kept") + } +} + +func TestStatsFields(t *testing.T) { + saved := last.Load() + t.Cleanup(func() { last.Store(saved) }) + + last.Store(nil) + pending, _ := StatsFields()["app_storage"].(common.Dict) + if apps, ok := pending["apps"].([]common.Dict); !ok || len(apps) != 0 || pending["measured_at"] != nil { + t.Errorf("before the first pass: got %v, want app_storage with no apps and no measured_at", pending) + } + + cfg := testConfig(t, App{AppKey: 6, AppName: "x"}) + r, err := Measure(context.Background(), &fakeDocker{volumes: []container.DiskUsageVolume{}}, cfg) + if err != nil { + t.Fatal(err) + } + last.Store(r) + stats, ok := StatsFields()["app_storage"].(common.Dict) + if !ok { + t.Fatalf("app_storage: got %T, want common.Dict", StatsFields()["app_storage"]) + } + if _, ok := stats["measured_at"].(string); !ok { + t.Errorf("measured_at: got %T, want an RFC 3339 string", stats["measured_at"]) + } + apps, _ := stats["apps"].([]common.Dict) + if len(apps) != 1 || apps[0]["app_key"] != uint64(6) { + t.Errorf("apps: got %v", stats["apps"]) + } + if _, has := apps[0]["volumes"]; has { + t.Error("an app without volumes must not carry an empty volumes list") + } +} diff --git a/src/container/docker.go b/src/container/docker.go index dfc9bec..0c74bd5 100644 --- a/src/container/docker.go +++ b/src/container/docker.go @@ -10,6 +10,7 @@ import ( cerrdefs "github.com/containerd/errdefs" + "github.com/docker/docker/api/types" dockerbuild "github.com/docker/docker/api/types/build" "github.com/docker/docker/api/types/image" @@ -420,6 +421,69 @@ func (docker *Docker) PruneDanglingImages(ctx context.Context) (string, error) { return string(output), nil } +// DiskUsageVolume is one volume of the daemon's disk-usage report. Size is +// -1 when the daemon does not know it (a volume driver other than local). +type DiskUsageVolume struct { + Name string + Labels map[string]string + Size int64 +} + +// DiskUsageMount is one mount of a container: Type "volume" carries the +// volume's Name, Type "bind" the host Source. +type DiskUsageMount struct { + Type string + Name string + Source string +} + +// DiskUsageContainer is one container of the daemon's disk-usage report. +// SizeRw is the size of its writable layer: what it wrote outside its mounts. +type DiskUsageContainer struct { + Names []string + Labels map[string]string + SizeRw int64 + Mounts []DiskUsageMount +} + +// DiskUsage is the daemon's disk-usage report (docker system df -v) for +// volumes and containers. The daemon computes it by walking every volume and +// every writable layer, which takes long on slow disks: call it rarely and +// with a deadline. +func (docker *Docker) DiskUsage(ctx context.Context) ([]DiskUsageVolume, []DiskUsageContainer, error) { + report, err := docker.client.DiskUsage(ctx, types.DiskUsageOptions{ + Types: []types.DiskUsageObject{types.VolumeObject, types.ContainerObject}, + }) + if err != nil { + return nil, nil, err + } + + volumes := make([]DiskUsageVolume, 0, len(report.Volumes)) + for _, v := range report.Volumes { + if v == nil { + continue + } + size := int64(-1) + if v.UsageData != nil { + size = v.UsageData.Size + } + volumes = append(volumes, DiskUsageVolume{Name: v.Name, Labels: v.Labels, Size: size}) + } + + containers := make([]DiskUsageContainer, 0, len(report.Containers)) + for _, c := range report.Containers { + if c == nil { + continue + } + mounts := make([]DiskUsageMount, 0, len(c.Mounts)) + for _, m := range c.Mounts { + mounts = append(mounts, DiskUsageMount{Type: string(m.Type), Name: m.Name, Source: m.Source}) + } + containers = append(containers, DiskUsageContainer{Names: c.Names, Labels: c.Labels, SizeRw: c.SizeRw, Mounts: mounts}) + } + return volumes, containers, nil +} + // VolumeResult is the subset of a Docker volume the callers need to decide // whether it is safe to remove. type VolumeResult struct { diff --git a/src/messenger/wamp.go b/src/messenger/wamp.go index 62e769f..f2e20d3 100644 --- a/src/messenger/wamp.go +++ b/src/messenger/wamp.go @@ -14,6 +14,7 @@ import ( "sync/atomic" "time" + "reagent/appstorage" "reagent/common" "reagent/config" "reagent/container" @@ -1077,6 +1078,10 @@ func (s *WampSession) UpdateRemoteDeviceStatus(status DeviceStatus) error { // host restarts by itself, so nobody would notice otherwise. maps.Copy(statsDict, crashrecovery.StatsFields()) + // What each installed app takes on this disk (app_storage; see + // appstorage.StatsFields), for the app's storage view. + maps.Copy(statsDict, appstorage.StatsFields()) + // Docker daemon health, probed live at send time so the payload is // truthful even for outages nothing else has noticed yet (a hung daemon, // a stopped Docker Desktop). It rides INSIDE stats deliberately: the diff --git a/src/release/version.txt b/src/release/version.txt index d003414..cdacd06 100644 --- a/src/release/version.txt +++ b/src/release/version.txt @@ -1 +1 @@ -1.6.21 \ No newline at end of file +1.6.22 \ No newline at end of file