stacks: remember where a container's bind mounts pointed
Every stack on core and grid is labelled with a path inside Dockhand's filesystem — /app/data/stacks/… — because Dockhand ran compose from inside its own container. Ham Lab resolves those by walking the bind mounts of running containers, which works right up until the container doing the mounting is removed. Retiring Dockhand would, in one step, make seventeen stacks on core and five on grid unfindable. So the mounts are written down as they are seen, and a path no live container explains is looked for in what was seen before. A remembered mount resolves with no container to run in, which is the right answer: compose runs on the host, against the host's own copy of the file. The same problem bites a scheduled update, which captures how to invoke compose when it is made so the cron job never has to rediscover the world at 3am. repoint() checks the named container is still running and falls back the same way if it is not — and pointedly leaves the spec alone if nothing explains the path, so it fails loudly rather than deploying a guess. Out in cron the record is read and never added to: the daemon owns state.json and would clobber anything written from there. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
192504ead3
commit
f17a365ea1
+103
-2
@@ -12,6 +12,12 @@ package api
|
||||
// relative paths, the .env beside them and the labels all stay exactly as they were
|
||||
// deployed — else on the host.
|
||||
//
|
||||
// Those mounts are also written down. An orchestrator can be removed, and when it is, the
|
||||
// containers it left behind still carry labels naming paths inside a filesystem that no
|
||||
// longer exists; without a record of where that filesystem was mounted, every stack on the
|
||||
// machine would become unfindable at once. A remembered mount resolves with no container to
|
||||
// run in, which is right: compose runs on the host against the host's copy of the file.
|
||||
//
|
||||
// A stack can also be deployed *from another machine*: Dockhand on the hub drives a node's
|
||||
// Docker over tcp, so the node runs the containers and the hub holds the files. The node
|
||||
// alone can only list such a stack read-only. The hub, asked about one of its nodes, fills
|
||||
@@ -41,6 +47,7 @@ import (
|
||||
|
||||
"hamlab/internal/compose"
|
||||
"hamlab/internal/priv"
|
||||
"hamlab/internal/store"
|
||||
)
|
||||
|
||||
type stack struct {
|
||||
@@ -90,6 +97,100 @@ func stackMounts(ctx context.Context) []mountMap {
|
||||
return maps
|
||||
}
|
||||
|
||||
// mountsFor is stackMounts plus memory. An orchestrator that deployed everything here and
|
||||
// has since been removed takes the only record of where its files really are with it: the
|
||||
// containers it left behind are labelled with paths inside *its* filesystem. So every map
|
||||
// seen is written down, and a path that no live container explains is looked for in what
|
||||
// was seen before. A remembered map resolves with no `via`, which is correct — the
|
||||
// container is gone, so compose runs on the host, against the host's copy of the file.
|
||||
func mountsFor(ctx context.Context, db *store.Store) []mountMap {
|
||||
live := stackMounts(ctx)
|
||||
rememberMounts(db, live)
|
||||
return append(live, recallMounts(db, live)...)
|
||||
}
|
||||
|
||||
// mountsSeen is the same without writing anything down. The daemon owns state.json; a cron
|
||||
// job that wrote to it would be clobbered by the next thing the daemon saved, so out there
|
||||
// the record is read and never added to.
|
||||
func mountsSeen(ctx context.Context, db *store.Store) []mountMap {
|
||||
live := stackMounts(ctx)
|
||||
return append(live, recallMounts(db, live)...)
|
||||
}
|
||||
|
||||
// rememberMounts folds what is mounted now into what was mounted before, and forgets a map
|
||||
// whose source has gone from disk — that one is not coming back, and keeping it would make
|
||||
// a stack look editable when its file no longer exists.
|
||||
//
|
||||
// This is on the path of every stack listing, which the Docker tab polls, so it reads first
|
||||
// and only writes when the set actually changed.
|
||||
func rememberMounts(db *store.Store, live []mountMap) {
|
||||
if len(live) == 0 {
|
||||
return // docker is down or answered nothing; that is not evidence of anything
|
||||
}
|
||||
key := func(c, d string) string { return c + "\x00" + d }
|
||||
var have []store.RememberedMount
|
||||
db.View(func(st *store.State) { have = append([]store.RememberedMount(nil), st.Mounts...) })
|
||||
at := map[string]int{}
|
||||
for i, m := range have {
|
||||
at[key(m.Container, m.Dest)] = i
|
||||
}
|
||||
now, add, moved := time.Now(), 0, false
|
||||
for _, m := range live {
|
||||
i, ok := at[key(m.container, m.dest)]
|
||||
if !ok {
|
||||
add++
|
||||
continue
|
||||
}
|
||||
if have[i].Src != m.src {
|
||||
moved = true
|
||||
}
|
||||
}
|
||||
// Pruning is the expensive half — a stat, or a sudo test(1), per remembered map — so it
|
||||
// only happens when something else has already made a write necessary.
|
||||
if add == 0 && !moved {
|
||||
return
|
||||
}
|
||||
_ = db.Update(func(st *store.State) {
|
||||
at := map[string]int{}
|
||||
for i, m := range st.Mounts {
|
||||
at[key(m.Container, m.Dest)] = i
|
||||
}
|
||||
for _, m := range live {
|
||||
if i, ok := at[key(m.container, m.dest)]; ok {
|
||||
st.Mounts[i].Src, st.Mounts[i].Seen = m.src, now
|
||||
continue
|
||||
}
|
||||
st.Mounts = append(st.Mounts, store.RememberedMount{Container: m.container, Dest: m.dest, Src: m.src, Seen: now})
|
||||
}
|
||||
kept := st.Mounts[:0]
|
||||
for _, m := range st.Mounts {
|
||||
if readable(m.Src) {
|
||||
kept = append(kept, m)
|
||||
}
|
||||
}
|
||||
st.Mounts = kept
|
||||
})
|
||||
}
|
||||
|
||||
// recallMounts is what was seen before and is not running now.
|
||||
func recallMounts(db *store.Store, live []mountMap) []mountMap {
|
||||
var remembered []store.RememberedMount
|
||||
db.View(func(st *store.State) { remembered = append([]store.RememberedMount(nil), st.Mounts...) })
|
||||
now := map[string]bool{}
|
||||
for _, m := range live {
|
||||
now[m.container+"\x00"+m.dest] = true
|
||||
}
|
||||
var out []mountMap
|
||||
for _, m := range remembered {
|
||||
if now[m.Container+"\x00"+m.Dest] {
|
||||
continue
|
||||
}
|
||||
out = append(out, mountMap{"", filepath.Clean(m.Dest), filepath.Clean(m.Src)}) // no container: run compose here
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool { return len(out[i].dest) > len(out[j].dest) })
|
||||
return out
|
||||
}
|
||||
|
||||
func readable(p string) bool {
|
||||
if _, err := os.Stat(p); err == nil {
|
||||
return true
|
||||
@@ -173,7 +274,7 @@ func (s *Server) findStacks(ctx context.Context) ([]stack, error) {
|
||||
}
|
||||
}
|
||||
}
|
||||
maps := stackMounts(ctx)
|
||||
maps := mountsFor(ctx, s.store)
|
||||
stacks := []stack{}
|
||||
for _, st := range by {
|
||||
sort.Strings(st.Containers)
|
||||
@@ -354,7 +455,7 @@ func (s *Server) hubStacks(ctx context.Context, node string) ([]stack, error) {
|
||||
continue
|
||||
}
|
||||
if remote == "" {
|
||||
remote, maps = s.nodeDocker(ctx, node), stackMounts(ctx)
|
||||
remote, maps = s.nodeDocker(ctx, node), mountsFor(ctx, s.store)
|
||||
}
|
||||
if remote == "-" {
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user