| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/ecdh" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | |
| 9 | "github.com/carlosframework/platform/internal/activator" |
| 10 | "github.com/carlosframework/platform/internal/appconfig" |
| 11 | "github.com/carlosframework/platform/internal/edge" |
| 12 | "github.com/carlosframework/platform/internal/provision" |
| 13 | "github.com/carlosframework/platform/internal/registry" |
| 14 | "github.com/carlosframework/platform/internal/release" |
| 15 | ) |
| 16 | |
| 17 | // T3's production composition of a flat held row (moves design §7.3, |
| 18 | // ruling F19). armPhase1 wires it as moveCfg.ComposeHeldEnv. |
| 19 | |
| 20 | // composeHeldEnv is T3's composition for a flat held row (ruling F19): a |
| 21 | // scratch directory holding the configuration rendered from the merged |
| 22 | // bundle (appconfig.LoadMergedHostVersions, rendered as RenderHeld renders |
| 23 | // it) and the carried <host>.env, an ExecBackend over it with this box's |
| 24 | // region and cache, and a synthetic flat route; LaunchEnvironment composes |
| 25 | // what the next spawn would get. |
| 26 | func composeHeldEnv(priv *ecdh.PrivateKey, region string, cache bool) func(context.Context, release.Store, provision.Record, []byte) (map[string]string, map[string]bool, []string, provision.MoveInputs, error) { |
| 27 | return func(ctx context.Context, s release.Store, rec provision.Record, carried []byte) (map[string]string, map[string]bool, []string, provision.MoveInputs, error) { |
| 28 | m, v, err := appconfig.LoadMergedHostVersions(ctx, s, rec.Account, rec.App, rec.Environment, rec.Host) |
| 29 | if err != nil { |
| 30 | return nil, nil, nil, provision.MoveInputs{}, err |
| 31 | } |
| 32 | var rendered []byte |
| 33 | if m != nil { |
| 34 | if rendered, err = appconfig.RenderEnv(m, priv, appconfig.Binding{Account: rec.Account, App: rec.App}); err != nil { |
| 35 | return nil, nil, nil, provision.MoveInputs{}, err |
| 36 | } |
| 37 | } |
| 38 | tmp, err := os.MkdirTemp("", "carlos-move-t3-") |
| 39 | if err != nil { |
| 40 | return nil, nil, nil, provision.MoveInputs{}, err |
| 41 | } |
| 42 | defer os.RemoveAll(tmp) |
| 43 | if err := os.WriteFile(filepath.Join(tmp, rec.Host+".config.env"), rendered, 0o600); err != nil { |
| 44 | return nil, nil, nil, provision.MoveInputs{}, err |
| 45 | } |
| 46 | if carried != nil { |
| 47 | if err := os.WriteFile(filepath.Join(tmp, rec.Host+".env"), carried, 0o600); err != nil { |
| 48 | return nil, nil, nil, provision.MoveInputs{}, err |
| 49 | } |
| 50 | } |
| 51 | eb := &activator.ExecBackend{DataDir: tmp, Region: region} |
| 52 | if cache { |
| 53 | eb.Cache = heldEnvCache |
| 54 | } |
| 55 | env, instance, shadowed, err := eb.LaunchLayers(registry.Route{Host: rec.Host, Socket: provision.DefaultSocket(rec.Host)}) |
| 56 | if err != nil { |
| 57 | return nil, nil, nil, provision.MoveInputs{}, err |
| 58 | } |
| 59 | in := provision.MoveInputs{BundleETags: map[string]string{"default": v.Default, "environment": v.Environment}, OverlayETag: v.Overlay} |
| 60 | return env, instance, shadowed, in, nil |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // heldEnvCache is the non-nil cache T3's synthetic backend carries when the |
| 65 | // live box has a response cache: LaunchEnvironment reads only whether one |
| 66 | // is set (CARLOS_PURGE). |
| 67 | var heldEnvCache = edge.NewCache(1, 1) |
| 68 | |