| 1 | package activator |
| 2 | |
| 3 | import ( |
| 4 | "path/filepath" |
| 5 | |
| 6 | "github.com/carlosframework/platform/internal/provision" |
| 7 | "github.com/carlosframework/platform/internal/registry" |
| 8 | ) |
| 9 | |
| 10 | // instancePaths is every per-host path the exec backend touches, resolved |
| 11 | // once per call by layout so that no method builds a path from DataDir and |
| 12 | // the host on its own. Flat and foreign rows keep today's flat names; a |
| 13 | // nested row's files live in its instance directory with the same names |
| 14 | // (design §3.1). |
| 15 | type instancePaths struct { |
| 16 | layout Layout |
| 17 | reason string |
| 18 | dataDir string // the directory that holds the four files |
| 19 | runDir string // the directory that holds app.sock and the control socket |
| 20 | dbPath string |
| 21 | envFile string |
| 22 | sidecarEnv string |
| 23 | configEnv string |
| 24 | } |
| 25 | |
| 26 | // Layout is ExecBackend's Classifier implementation: the same |
| 27 | // classification pathsFor already computes, exposed so doWake can check it |
| 28 | // before Acquire or Restore ever run. |
| 29 | func (b *ExecBackend) Layout(rt registry.Route) (Layout, string) { |
| 30 | return Classify(rt, b.layoutInputs()) |
| 31 | } |
| 32 | |
| 33 | func (b *ExecBackend) pathsFor(rt registry.Route) instancePaths { |
| 34 | l, reason := Classify(rt, b.layoutInputs()) |
| 35 | p := instancePaths{layout: l, reason: reason, dataDir: b.DataDir, runDir: provision.SocketDir} |
| 36 | if l == LayoutNested { |
| 37 | p.dataDir = provision.InstanceDir(b.DataDir, rt.Host) |
| 38 | if b.RunDirOverride != "" { |
| 39 | // Test-only: a test points the nested run dir at a temp dir |
| 40 | // rather than /run/carlos, exactly as it points DataDir at one. |
| 41 | p.runDir = filepath.Join(b.RunDirOverride, "instances", provision.InstanceID(rt.Host)) |
| 42 | } else { |
| 43 | p.runDir = provision.InstanceRunDir(rt.Host) |
| 44 | } |
| 45 | } |
| 46 | p.dbPath = rt.DBPath |
| 47 | if p.dbPath == "" { |
| 48 | p.dbPath = provision.DefaultDBPath(b.DataDir, rt.Host) |
| 49 | } |
| 50 | p.envFile = filepath.Join(p.dataDir, rt.Host+".env") |
| 51 | p.sidecarEnv = filepath.Join(p.dataDir, rt.Host+"-sidecar.env") |
| 52 | p.configEnv = filepath.Join(p.dataDir, rt.Host+".config.env") |
| 53 | return p |
| 54 | } |
| 55 | |
| 56 | // layoutInputs is the backend's LayoutInputs with DataDir filled in. A |
| 57 | // non-empty RunDirOverride also steers the socket dir Classify tests |
| 58 | // against, so a route built against the override dir still classifies as |
| 59 | // nested through Task 2's sockets() helper. |
| 60 | func (b *ExecBackend) layoutInputs() LayoutInputs { |
| 61 | in := b.LayoutFacts |
| 62 | in.DataDir = b.DataDir |
| 63 | if b.RunDirOverride != "" { |
| 64 | in.SocketDir = b.RunDirOverride |
| 65 | } |
| 66 | return in |
| 67 | } |
| 68 | |