Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/effect-daemon-microvm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ writes to stdout when it is ready for work; a workload that names none is ready
starts.

```ts
import * as NodeCrypto from '@effect/platform-node/NodeCrypto'
import { layer as nodeServicesLayer } from '@effect/platform-node/NodeServices'
import { MicroVMMedium } from '@systemfsoftware/effect-daemon-microvm'
import { Supervisor } from '@systemfsoftware/effect-daemon-spec'
Expand All @@ -46,7 +47,12 @@ const tree = Supervisor.make('worker').pipe(

const supervised = Effect.scoped(tree.scoped).pipe(
Effect.provide(
Layer.mergeAll(MicroVMMedium.layer(), nodeServicesLayer, Readiness.NodeHostProber.layer),
Layer.mergeAll(
MicroVMMedium.layer(),
nodeServicesLayer,
NodeCrypto.layerSecureRandom,
Readiness.NodeHostProber.layer,
),
),
)
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Conformance } from '@systemfsoftware/effect-daemon-conformance'
import type { MicroVM } from '@systemfsoftware/effect-microsandbox'
import type { Readiness } from '@systemfsoftware/effect-readiness'
import { Effect, HashMap, Option, Queue, Ref, Stream } from 'effect'
import type * as Crypto from 'effect/Crypto'
import type * as SecureRandom from 'effect/crypto/SecureRandom'
import type * as FileSystem from 'effect/FileSystem'
import { declaration, port } from './micro-vm.medium.js'
import type { MicroVMProgram } from './MicroVMProgram.js'
Expand Down Expand Up @@ -82,7 +82,7 @@ export const conformanceDriver = (
): Conformance.ConformanceDriver<
MicroVMProgram,
MicroVM.MicroVMError,
Crypto.Crypto | FileSystem.FileSystem | Readiness.HostProber
SecureRandom.SecureRandom | FileSystem.FileSystem | Readiness.HostProber
> => ({
name: 'microvm',
declaration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Supervisor } from '@systemfsoftware/effect-daemon-spec'
import { MicroVM } from '@systemfsoftware/effect-microsandbox'
import type { Readiness } from '@systemfsoftware/effect-readiness'
import { Deferred, Effect, Layer, Match, Option, Ref, Scope, Stream } from 'effect'
import type * as Crypto from 'effect/Crypto'
import type * as SecureRandom from 'effect/crypto/SecureRandom'
import type * as FileSystem from 'effect/FileSystem'
import { absurd } from 'effect/Function'
import * as Result from 'effect/Result'
Expand All @@ -23,7 +23,7 @@ export interface MicroVMMediumOptions {
readonly memoryMb?: number
}

type Requirements = Scope.Scope | Crypto.Crypto | FileSystem.FileSystem | Readiness.HostProber
type Requirements = Scope.Scope | SecureRandom.SecureRandom | FileSystem.FileSystem | Readiness.HostProber

type PortShape = Supervisor.Medium.MediumPortShape<MicroVMProgram, MicroVM.MicroVMError, Requirements>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as NodeCrypto from '@effect/platform-node/NodeCrypto'
import { layer as nodeServicesLayer } from '@effect/platform-node/NodeServices'
import { Conformance } from '@systemfsoftware/effect-daemon-conformance'
import { MicroVMMedium } from '@systemfsoftware/effect-daemon-microvm'
Expand All @@ -24,6 +25,7 @@ const MicroVMLayer = Layer.mergeAll(
MicroVMMedium.layer(),
Conformance.FiberReferenceLayer,
nodeServicesLayer,
NodeCrypto.layerSecureRandom,
Readiness.NodeHostProber.layer,
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import * as NodeCrypto from '@effect/platform-node/NodeCrypto'
import { layer as nodeServicesLayer } from '@effect/platform-node/NodeServices'
import type { Conformance } from '@systemfsoftware/effect-daemon-conformance'
import { MicroVMMedium } from '@systemfsoftware/effect-daemon-microvm'
import { Gherkin, Given, it, makeFeature, Then } from '@systemfsoftware/effect-gherkin-spec'
import { MicroVM } from '@systemfsoftware/effect-microsandbox'
import { Readiness } from '@systemfsoftware/effect-readiness'
import { Context, Effect, Layer } from 'effect'
import * as Crypto from 'effect/Crypto'
import * as SecureRandom from 'effect/crypto/SecureRandom'
import { Sandbox } from 'microsandbox'
import { ABNORMAL_EXIT_CODE, childScriptWorkload } from './__fixtures__/child-script.js'
import { featureNameOf, kvmGate } from './__fixtures__/kvm-gate.js'
Expand All @@ -19,10 +20,10 @@ class RunSandboxes extends Context.Service<RunSandboxes, { readonly prefix: stri
const RunSandboxesLayer = Layer.effect(
RunSandboxes,
Effect.map(
Effect.flatMap(Crypto.Crypto, (crypto) => Effect.orDie(crypto.randomUUIDv4())),
Effect.flatMap(SecureRandom.SecureRandom, (random) => random.uuidV4()),
(id) => ({ prefix: `run-${id.slice(0, 8)}-` }),
),
).pipe(Layer.provide(nodeServicesLayer))
).pipe(Layer.provide(NodeCrypto.layerSecureRandom))

const hostSandboxRuntime = MicroVM.SandboxRuntime.defaultValue()

Expand All @@ -37,6 +38,7 @@ const RunScopedSandboxRuntimeLayer = Layer.effect(
const MicroVMLayer = Layer.mergeAll(
MicroVMMedium.layer(),
nodeServicesLayer,
NodeCrypto.layerSecureRandom,
Readiness.NodeHostProber.layer,
RunScopedSandboxRuntimeLayer,
).pipe(Layer.provideMerge(RunSandboxesLayer))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { MicroVMMedium } from '@systemfsoftware/effect-daemon-microvm'
import { Supervisor } from '@systemfsoftware/effect-daemon-spec'
import { Gherkin, Given, it, makeFeature, Then, When } from '@systemfsoftware/effect-gherkin-spec'
import { Readiness } from '@systemfsoftware/effect-readiness'
import { ConfigProvider, Crypto, Effect, Layer, Match, Stream } from 'effect'
import { ConfigProvider, Effect, Layer, Match, Stream } from 'effect'
import * as SecureRandom from 'effect/crypto/SecureRandom'
import { FIXTURE_IMAGE, READY_TOKEN } from './__fixtures__/child-script.js'
import {
ExceptionalTermination,
Expand Down Expand Up @@ -41,11 +42,8 @@ const normalTermination = (reason: Supervisor.Medium.TerminationReason): Effect.
const fakePlatform = (behaviour: SandboxBehaviour) =>
Layer.mergeAll(
Layer.succeed(
Crypto.Crypto,
Crypto.make({
randomBytes: (size) => new Uint8Array(size),
digest: () => Effect.succeed(new Uint8Array()),
}),
SecureRandom.SecureRandom,
SecureRandom.make((size) => new Uint8Array(size)),
),
Layer.succeed(Readiness.HostProber, {
dial: () => Effect.succeed({ _tag: 'Connected' as const }),
Expand Down
18 changes: 15 additions & 3 deletions packages/effect-microsandbox/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pnpm add @systemfsoftware/effect-microsandbox

```ts
import { NodeRuntime } from '@effect/platform-node'
import * as NodeCrypto from '@effect/platform-node/NodeCrypto'
import { layer as nodeServicesLayer } from '@effect/platform-node/NodeServices'
import { MicroVM } from '@systemfsoftware/effect-microsandbox'
import { Readiness } from '@systemfsoftware/effect-readiness'
Expand All @@ -30,7 +31,12 @@ const program = Effect.scoped(
}),
)

NodeRuntime.runMain(Effect.provide(program, Layer.merge(nodeServicesLayer, Readiness.NodeHostProber.layer)))
NodeRuntime.runMain(
Effect.provide(
program,
Layer.mergeAll(nodeServicesLayer, NodeCrypto.layerSecureRandom, Readiness.NodeHostProber.layer),
),
)
```

When the scope closes — normally or through interruption — the sandbox is stopped, destroyed, and its record removed. There is nothing to clean up by hand.
Expand All @@ -43,7 +49,7 @@ Shared-kernel containers leak state between tests and require a local Docker soc

### Layer & Scoped Execution

A configured container specification (`MicroVM.spec(...)`) directly exposes `.scoped` (to acquire inside an `Effect.scoped` block) and `.layer` (to provide as a testcontainer `Layer`). In accordance with `compound-packs/cell-architecture`, platform dependencies (`Crypto`, `FileSystem`, and the readiness `HostProber`) propagate to `R` — `.scoped` and `.run` carry them in the effect's requirements, and `.layer` carries them in the layer's input requirements — and are satisfied once at your application or test composition root (such as `@effect/platform-node/NodeServices` plus `Readiness.NodeHostProber.layer`).
A configured container specification (`MicroVM.spec(...)`) directly exposes `.scoped` (to acquire inside an `Effect.scoped` block) and `.layer` (to provide as a testcontainer `Layer`). In accordance with `compound-packs/cell-architecture`, platform dependencies (`SecureRandom`, `FileSystem`, and the readiness `HostProber`) propagate to `R` — `.scoped` and `.run` carry them in the effect's requirements, and `.layer` carries them in the layer's input requirements — and are satisfied once at your application or test composition root (such as `@effect/platform-node/NodeServices`, `@effect/platform-node/NodeCrypto`'s `layerSecureRandom`, plus `Readiness.NodeHostProber.layer`).

## Specifying Containers

Expand Down Expand Up @@ -90,6 +96,7 @@ VM startup does not complete until the specified wait strategy passes (30-second

```ts
import { NodeRuntime } from '@effect/platform-node'
import * as NodeCrypto from '@effect/platform-node/NodeCrypto'
import { layer as nodeServicesLayer } from '@effect/platform-node/NodeServices'
import { MicroVM } from '@systemfsoftware/effect-microsandbox'
import { Readiness } from '@systemfsoftware/effect-readiness'
Expand All @@ -111,7 +118,12 @@ const program = Effect.scoped(
}),
)

NodeRuntime.runMain(Effect.provide(program, Layer.merge(nodeServicesLayer, Readiness.NodeHostProber.layer)))
NodeRuntime.runMain(
Effect.provide(
program,
Layer.mergeAll(nodeServicesLayer, NodeCrypto.layerSecureRandom, Readiness.NodeHostProber.layer),
),
)
```

A `JobCompletion` holds:
Expand Down
14 changes: 9 additions & 5 deletions packages/effect-microsandbox/examples/boot-alpine.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { NodeRuntime } from '@effect/platform-node'
import * as NodeCrypto from '@effect/platform-node/NodeCrypto'
import { layer as nodeServicesLayer } from '@effect/platform-node/NodeServices'
import { MicroVM } from '@systemfsoftware/effect-microsandbox'
import { Readiness } from '@systemfsoftware/effect-readiness'
import images from '@systemfsoftware/microvm-test-images' with { type: 'json' }
import { Config, Crypto, Deferred, Effect, Fiber, Layer, Match, Random, Schema } from 'effect'
import { Config, Deferred, Effect, Fiber, Layer, Match, Random, Schema } from 'effect'
import * as SecureRandom from 'effect/crypto/SecureRandom'
import type * as FileSystem from 'effect/FileSystem'
import type * as Scope from 'effect/Scope'
import { Sandbox } from 'microsandbox'
Expand Down Expand Up @@ -41,8 +43,8 @@ const recordGone = (name: string): Effect.Effect<boolean> =>
const alpine = MicroVM.spec(images.alpine).withExposedPorts([8080])
const portless = MicroVM.spec(images.alpine)

const randomToken: Effect.Effect<string, never, Crypto.Crypto> = Effect.map(
Effect.flatMap(Crypto.Crypto, (crypto) => Effect.orDie(crypto.randomBytes(16))),
const randomToken: Effect.Effect<string, never, SecureRandom.SecureRandom> = Effect.map(
Effect.flatMap(SecureRandom.SecureRandom, (random) => random.randomBytes(16)),
(bytes) => Buffer.from(bytes).toString('hex'),
)

Expand Down Expand Up @@ -160,7 +162,7 @@ const interruptBootAt = (
): Effect.Effect<
void,
never,
Crypto.Crypto | FileSystem.FileSystem | Readiness.HostProber
SecureRandom.SecureRandom | FileSystem.FileSystem | Readiness.HostProber
> =>
Effect.gen(function*() {
yield* Effect.logInfo(`[smoke] J14: boot starting, interruption scheduled at ${point}ms`)
Expand Down Expand Up @@ -439,4 +441,6 @@ const main = Effect.gen(
},
)

NodeRuntime.runMain(Effect.provide(main, Layer.merge(nodeServicesLayer, Readiness.NodeHostProber.layer)))
NodeRuntime.runMain(
Effect.provide(main, Layer.mergeAll(nodeServicesLayer, NodeCrypto.layerSecureRandom, Readiness.NodeHostProber.layer)),
)
4 changes: 2 additions & 2 deletions packages/effect-microsandbox/src/boot-microvm.cell.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Cell } from '@systemfsoftware/effect-cell-types'
import { Readiness } from '@systemfsoftware/effect-readiness'
import { Match } from 'effect'
import type * as Crypto from 'effect/Crypto'
import type * as SecureRandom from 'effect/crypto/SecureRandom'
import type * as FileSystem from 'effect/FileSystem'
import type * as Scope from 'effect/Scope'
import { awaitReadiness } from './await-readiness.cell.js'
Expand All @@ -24,7 +24,7 @@ type StartError =
| VirtualizationUnsupportedError
| WaitTimeoutError

type StartServices = Crypto.Crypto | FileSystem.FileSystem | Readiness.HostProber | Scope.Scope
type StartServices = SecureRandom.SecureRandom | FileSystem.FileSystem | Readiness.HostProber | Scope.Scope

type BootedStart = Cell.Cell<MicroVMSpec, AcquiredVM, StartError, StartServices>

Expand Down
6 changes: 3 additions & 3 deletions packages/effect-microsandbox/src/boot-sandbox.cell.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Sandwich } from '@systemfsoftware/effect-cell-types'
import { Effect } from 'effect'
import * as Crypto from 'effect/Crypto'
import * as SecureRandom from 'effect/crypto/SecureRandom'
import type { Sandbox } from 'microsandbox'
import { LoopbackViolationError, PortAllocationError, SandboxBootError } from './MicroVMError.schema.js'
import type { MicroVMSpec } from './MicroVMSpec.schema.js'
Expand Down Expand Up @@ -35,8 +35,8 @@ const allocateBindings = (guests: ReadonlyArray<number>) =>

const readPlanCommand = (spec: MicroVMSpec) =>
Effect.gen(function*() {
const crypto = yield* Crypto.Crypto
const id = yield* crypto.randomUUIDv4().pipe(Effect.orDie)
const crypto = yield* SecureRandom.SecureRandom
const id = yield* crypto.uuidV4()
return new PlanSandbox({
spec,
bindings: [],
Expand Down
6 changes: 3 additions & 3 deletions packages/effect-microsandbox/src/micro-vm.blueprint.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Blueprint, Cell } from '@systemfsoftware/effect-cell-types'
import { Readiness } from '@systemfsoftware/effect-readiness'
import { type Context, Effect, Exit, Layer, Match, Schema } from 'effect'
import * as Crypto from 'effect/Crypto'
import type * as SecureRandom from 'effect/crypto/SecureRandom'
import * as FileSystem from 'effect/FileSystem'
import { dual } from 'effect/Function'
import type * as Scope from 'effect/Scope'
Expand Down Expand Up @@ -91,13 +91,13 @@ const scopedOf = (
): Effect.Effect<
RunningVM,
MicroVMError,
Scope.Scope | Crypto.Crypto | FileSystem.FileSystem | Readiness.HostProber
Scope.Scope | SecureRandom.SecureRandom | FileSystem.FileSystem | Readiness.HostProber
> => Effect.map(bootMicroVM.run(spec), runningVMOf)

const layerOf = (spec: MicroVMSpec) =>
<Id>(
service: Context.Key<Id, RunningVM>,
): Layer.Layer<Id, MicroVMError, Crypto.Crypto | FileSystem.FileSystem | Readiness.HostProber> =>
): Layer.Layer<Id, MicroVMError, SecureRandom.SecureRandom | FileSystem.FileSystem | Readiness.HostProber> =>
Layer.effect(service)(scopedOf(spec))

const reviseJob = (job: JobSpec, patch: Partial<Omit<JobSpec, '_tag'>>): JobSpec => ({ ...job, ...patch })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { layer as nodeFileSystemLayer } from '@effect/platform-node/NodeFileSyst
import { Gherkin, Given, it, makeFeature, Then, When } from '@systemfsoftware/effect-gherkin-spec'
import { MicroVM } from '@systemfsoftware/effect-microsandbox'
import { Readiness } from '@systemfsoftware/effect-readiness'
import { ConfigProvider, Crypto, Effect, Layer, Match, Metric } from 'effect'
import { ConfigProvider, Effect, Layer, Match, Metric } from 'effect'
import * as SecureRandom from 'effect/crypto/SecureRandom'
import * as Result from 'effect/Result'
import type * as Scope from 'effect/Scope'
import { recordingSandboxRuntime, SandboxLedger } from './__fixtures__/sandbox-runtime.fixture.js'
Expand All @@ -14,11 +15,8 @@ const UNSUPPORTED_PLATFORM = 'freebsd'
const twoPortService = MicroVM.spec('alpine:3.20').withExposedPorts([8080, 6379])

const sharedCrypto = Layer.succeed(
Crypto.Crypto,
Crypto.make({
randomBytes: (size) => new Uint8Array(size),
digest: () => Effect.succeed(new Uint8Array()),
}),
SecureRandom.SecureRandom,
SecureRandom.make((size) => new Uint8Array(size)),
)

const hostProber = Layer.succeed(Readiness.HostProber, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { Conformance } from '@systemfsoftware/conformance-spec'
import { Gherkin, Given, it, makeFeature, Then, When } from '@systemfsoftware/effect-gherkin-spec'
import { MicroVM } from '@systemfsoftware/effect-microsandbox'
import { Readiness } from '@systemfsoftware/effect-readiness'
import { ConfigProvider, Crypto, Effect, Layer, Schema } from 'effect'
import { ConfigProvider, Effect, Layer, Schema } from 'effect'
import * as SecureRandom from 'effect/crypto/SecureRandom'
import { nothingLeftHeld, recordingSandboxRuntime, SandboxLedger } from './__fixtures__/sandbox-runtime.fixture.js'

const Feature = makeFeature({ it })
Expand All @@ -12,11 +13,8 @@ const twoPortService = MicroVM.spec('alpine:3.20').withExposedPorts([8080, 6379]

const bootAgainstFakes = Layer.mergeAll(
Layer.succeed(
Crypto.Crypto,
Crypto.make({
randomBytes: (size) => new Uint8Array(size),
digest: () => Effect.succeed(new Uint8Array()),
}),
SecureRandom.SecureRandom,
SecureRandom.make((size) => new Uint8Array(size)),
),
Layer.succeed(Readiness.HostProber, {
dial: () => Effect.succeed({ _tag: 'Connected' as const }),
Expand Down
Loading
Loading