@sixb/cli 0.1.14 → 0.1.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,32 @@
1
+ import { resolve } from "node:path"
2
+ import { pathToFileURL } from "node:url"
3
+ import type { DeployConfig } from "@sixb/core/deploy"
4
+ import { validateDeployConfig } from "@sixb/core/internal/deploy"
5
+ import { SixbCliError } from "./errors"
6
+
7
+ export const DEPLOY_CONFIG_FILE = "sixb.deploy.ts"
8
+
9
+ export interface LoadedDeployConfig {
10
+ readonly path: string
11
+ readonly config: DeployConfig
12
+ }
13
+
14
+ /** Loads `sixb.deploy.ts` from the project directory. */
15
+ export async function loadDeployConfig(cwd = process.cwd()): Promise<LoadedDeployConfig> {
16
+ const path = resolve(cwd, DEPLOY_CONFIG_FILE)
17
+ if (!(await Bun.file(path).exists())) {
18
+ throw new SixbCliError(`[SixbDeploy] No ${DEPLOY_CONFIG_FILE} in ${cwd}.`, {
19
+ remediation:
20
+ `Create ${DEPLOY_CONFIG_FILE} in the project directory, exporting ` +
21
+ "`defineDeploy({ ... })` from @sixb/core/deploy as its default.",
22
+ })
23
+ }
24
+
25
+ const module = (await import(pathToFileURL(path).href)) as { readonly default?: unknown }
26
+ if (module.default === undefined) {
27
+ throw new SixbCliError(`[SixbDeploy] ${path} has no default export.`, {
28
+ remediation: "Export the deployment: `export default defineDeploy({ ... })`.",
29
+ })
30
+ }
31
+ return { path, config: validateDeployConfig(module.default) }
32
+ }
@@ -0,0 +1,250 @@
1
+ import type {
2
+ DeployCommand,
3
+ DeployConfig,
4
+ DeployEnv,
5
+ DeployHttpServiceName,
6
+ DeployProcessDefinition,
7
+ DeployRelease,
8
+ DeployReleaseHttp,
9
+ DeployReleaseProcess,
10
+ DeployReleaseService,
11
+ DeployScalableProcessOptions,
12
+ DeployServiceName,
13
+ DeployWorkersConfig,
14
+ } from "@sixb/core/deploy"
15
+ import {
16
+ DEPLOY_SERVICES,
17
+ isDeployHttpService,
18
+ isDeploySingletonService,
19
+ } from "@sixb/core/internal/deploy"
20
+ import { parseAgentTurnTimeoutMs } from "./agent-turn-timeout"
21
+ import { SixbCliError } from "./errors"
22
+ import { type ProductionRole, productionRoleFacts } from "./production-roles"
23
+
24
+ /**
25
+ * The service that deploys each production role. Total over the role union: a new role does not
26
+ * compile until it names its service, or `null` for a role a deployment never runs on its own.
27
+ */
28
+ const ROLE_SERVICES = {
29
+ api: "api",
30
+ atlas: "atlas",
31
+ app: "app",
32
+ orchestrator: "orchestrator",
33
+ scheduler: "scheduler",
34
+ rules: "rules",
35
+ "worker-group": "workers",
36
+ // A deployment co-hosts its workers in one `worker-group`; `workers.process.instances` scales it.
37
+ worker: null,
38
+ } as const satisfies Record<ProductionRole, DeployServiceName | null>
39
+
40
+ type RoleOf<Service extends DeployServiceName> = {
41
+ [Role in ProductionRole]: (typeof ROLE_SERVICES)[Role] extends Service ? Role : never
42
+ }[ProductionRole]
43
+
44
+ /** The role behind each service. A service that no role deploys does not compile. */
45
+ const SERVICE_ROLES: { readonly [Service in DeployServiceName]: RoleOf<Service> } = {
46
+ api: "api",
47
+ atlas: "atlas",
48
+ app: "app",
49
+ orchestrator: "orchestrator",
50
+ scheduler: "scheduler",
51
+ rules: "rules",
52
+ workers: "worker-group",
53
+ }
54
+
55
+ /** The variable each role reads for a surface's public origin. */
56
+ const PUBLIC_ORIGIN_ENV = {
57
+ api: "SIXB_API_PUBLIC_ORIGIN",
58
+ atlas: "SIXB_ATLAS_PUBLIC_ORIGIN",
59
+ app: "SIXB_APP_PUBLIC_ORIGIN",
60
+ } as const satisfies Record<DeployHttpServiceName, string>
61
+
62
+ /** Answers 200 once storage is reachable and its schema current; see `server/src/routes/status.ts`. */
63
+ const API_READINESS_PATH = "/ready"
64
+
65
+ const DEFAULT_KILL_TIMEOUT_MS = 10_000
66
+ const DEFAULT_RESTART_DELAY_MS = 1_000
67
+
68
+ export interface DeployReleaseOptions {
69
+ /** The Bun version the project runs on, from its `packageManager` or the running Bun. */
70
+ readonly bunVersion: string
71
+ }
72
+
73
+ export function buildDeployRelease(
74
+ config: DeployConfig,
75
+ options: DeployReleaseOptions
76
+ ): DeployRelease {
77
+ const enabled = DEPLOY_SERVICES.filter((service) => config.services?.[service] !== false)
78
+ const http = resolveHttpServices(config, enabled)
79
+
80
+ // Every service gets every origin: the API checks browser origins, and agent workers call it.
81
+ const env: DeployEnv = {
82
+ NODE_ENV: "production",
83
+ ...Object.fromEntries(
84
+ [...http].map(([service, address]) => [PUBLIC_ORIGIN_ENV[service], address.publicOrigin])
85
+ ),
86
+ ...config.env,
87
+ }
88
+
89
+ return {
90
+ name: config.name,
91
+ target: { kind: config.target.kind, location: config.target.location },
92
+ bunVersion: options.bunVersion,
93
+ env,
94
+ steps: {
95
+ build: [sixb("build")],
96
+ // The order `docs/deployment` gives. `check` probes every provider, so a broker or queue
97
+ // the services cannot reach fails the deploy instead of passing for healthy; it needs the
98
+ // schema migrated first.
99
+ beforeStart: [sixb("db", "migrate"), sixb("check"), sixb("lake", "check")],
100
+ },
101
+ services: [
102
+ ...enabled.map((service) => sixbService(config, service, env, http)),
103
+ ...Object.entries(config.processes ?? {}).map(([name, definition]) =>
104
+ projectProcess(name, definition, env)
105
+ ),
106
+ ],
107
+ }
108
+ }
109
+
110
+ function sixbService(
111
+ config: DeployConfig,
112
+ service: DeployServiceName,
113
+ env: DeployEnv,
114
+ http: ReadonlyMap<DeployHttpServiceName, DeployReleaseHttp>
115
+ ): DeployReleaseService {
116
+ const role = SERVICE_ROLES[service]
117
+ const override = config.services?.[service]
118
+ const options = typeof override === "object" ? override : {}
119
+ const address = isDeployHttpService(service) ? http.get(service) : undefined
120
+ // The release migrates storage once, before any service starts, so no service races it.
121
+ const noMigrate = productionRoleFacts(role).usesStorageSchema ? ["--no-migrate"] : []
122
+
123
+ const workers = config.services?.workers
124
+ const args = address
125
+ ? [role, "--host", address.host, "--port", String(address.port)]
126
+ : service === "workers"
127
+ ? workerGroupArgs(typeof workers === "object" ? workers : {})
128
+ : [role]
129
+
130
+ const processOptions: DeployScalableProcessOptions = options.process ?? {}
131
+ return {
132
+ name: service,
133
+ kind: address ? "http" : isDeploySingletonService(service) ? "singleton" : "workers",
134
+ command: { program: "sixb", args: [...args, ...noMigrate] },
135
+ env: { ...env, ...options.env },
136
+ instances: processOptions.instances ?? 1,
137
+ ...(address ? { http: address } : {}),
138
+ process: releaseProcess(processOptions),
139
+ }
140
+ }
141
+
142
+ function workerGroupArgs(options: DeployWorkersConfig): string[] {
143
+ const types = options.types === undefined || options.types === "all" ? [] : options.types
144
+ const args = ["worker-group", ...types]
145
+
146
+ if (options.agentTurnTimeout !== undefined) {
147
+ if (parseAgentTurnTimeoutMs(options.agentTurnTimeout) === null) {
148
+ throw new SixbCliError(
149
+ `[SixbDeploy] services.workers.agentTurnTimeout '${options.agentTurnTimeout}' is not a ` +
150
+ "duration.",
151
+ { remediation: 'Use a positive duration such as "30s", "10m", or "1h".' }
152
+ )
153
+ }
154
+ args.push("--agent-turn-timeout", options.agentTurnTimeout)
155
+ }
156
+ for (const [workerType, concurrency] of Object.entries(options.concurrency ?? {})) {
157
+ args.push("--concurrency", `${workerType}=${concurrency}`)
158
+ }
159
+ return args
160
+ }
161
+
162
+ function projectProcess(
163
+ name: string,
164
+ definition: DeployProcessDefinition,
165
+ env: DeployEnv
166
+ ): DeployReleaseService {
167
+ const processOptions = definition.process ?? {}
168
+ return {
169
+ name,
170
+ kind: "script",
171
+ command: { program: "bun", args: [definition.entrypoint, ...(definition.args ?? [])] },
172
+ env: { ...env, ...definition.env },
173
+ instances: processOptions.instances ?? 1,
174
+ process: releaseProcess(processOptions),
175
+ }
176
+ }
177
+
178
+ function resolveHttpServices(
179
+ config: DeployConfig,
180
+ enabled: readonly DeployServiceName[]
181
+ ): Map<DeployHttpServiceName, DeployReleaseHttp> {
182
+ const resolved = new Map<DeployHttpServiceName, DeployReleaseHttp>()
183
+ const domains = new Map<string, DeployHttpServiceName>()
184
+ const ports = new Map<number, DeployHttpServiceName>()
185
+
186
+ for (const service of enabled.filter(isDeployHttpService)) {
187
+ const override = config.services?.[service]
188
+ const domain =
189
+ (typeof override === "object" ? override.domain : undefined) ?? defaultDomain(config, service)
190
+ const address = config.target.listenAddress(service)
191
+
192
+ const sharedDomain = domains.get(domain.toLowerCase())
193
+ if (sharedDomain) {
194
+ throw new SixbCliError(
195
+ `[SixbDeploy] services.${sharedDomain} and services.${service} both use ${domain}.`,
196
+ { remediation: "Give each HTTP service its own domain." }
197
+ )
198
+ }
199
+ const sharedPort = ports.get(address.port)
200
+ if (sharedPort) {
201
+ throw new SixbCliError(
202
+ `[SixbDeploy] ${sharedPort} and ${service} both listen on port ${address.port}.`,
203
+ { remediation: "Give each HTTP service its own port in the target's `ports`." }
204
+ )
205
+ }
206
+ domains.set(domain.toLowerCase(), service)
207
+ ports.set(address.port, service)
208
+ resolved.set(service, {
209
+ ...address,
210
+ domain,
211
+ publicOrigin: `https://${domain}`,
212
+ ...(service === "api" ? { readinessPath: API_READINESS_PATH } : {}),
213
+ })
214
+ }
215
+
216
+ return resolved
217
+ }
218
+
219
+ function defaultDomain(config: DeployConfig, service: DeployHttpServiceName): string {
220
+ const label = `${config.name}-${service}`
221
+ if (config.domain === undefined) {
222
+ throw new SixbCliError(`[SixbDeploy] services.${service} has no domain.`, {
223
+ remediation:
224
+ `Set \`domain\` to serve it at ${label}.<domain>, set services.${service}.domain, or ` +
225
+ `turn it off with \`${service}: false\`.`,
226
+ })
227
+ }
228
+ // A hostname label holds at most 63 characters.
229
+ if (label.length > 63) {
230
+ throw new SixbCliError(
231
+ `[SixbDeploy] The default domain ${label}.${config.domain} is too long.`,
232
+ {
233
+ remediation: `Set services.${service}.domain.`,
234
+ }
235
+ )
236
+ }
237
+ return `${label}.${config.domain}`
238
+ }
239
+
240
+ function releaseProcess(options: DeployScalableProcessOptions): DeployReleaseProcess {
241
+ return {
242
+ killTimeoutMs: options.killTimeoutMs ?? DEFAULT_KILL_TIMEOUT_MS,
243
+ restartDelayMs: options.restartDelayMs ?? DEFAULT_RESTART_DELAY_MS,
244
+ ...(options.maxMemory === undefined ? {} : { maxMemory: options.maxMemory.trim() }),
245
+ }
246
+ }
247
+
248
+ function sixb(...args: string[]): DeployCommand {
249
+ return { program: "sixb", args }
250
+ }
@@ -0,0 +1,189 @@
1
+ import { userInfo } from "node:os"
2
+ import { dirname, join, posix, resolve } from "node:path"
3
+ import type { DeploySource, DeploySourceArchive } from "@sixb/core/deploy"
4
+ import { SixbCliError } from "./errors"
5
+
6
+ export interface PackedSource extends DeploySource {
7
+ /** Whether the working tree has changes the deployed commit does not. */
8
+ readonly dirty: boolean
9
+ }
10
+
11
+ /**
12
+ * The committed files at `ref`, with every submodule at the commit its parent records. Nothing
13
+ * uncommitted or ignored is sent, so what runs is always a commit anyone can look up.
14
+ */
15
+ export async function packSource(projectDir: string, ref = "HEAD"): Promise<PackedSource> {
16
+ const root = await git(projectDir, ["rev-parse", "--show-toplevel"]).catch(() => {
17
+ throw new SixbCliError(`[SixbDeploy] ${projectDir} is not in a git repository.`, {
18
+ remediation: "sixb deploy sends a commit. Commit the project to a git repository first.",
19
+ })
20
+ })
21
+ const commit = await git(root, ["rev-parse", "--verify", "--quiet", `${ref}^{commit}`]).catch(
22
+ () => {
23
+ throw new SixbCliError(`[SixbDeploy] '${ref}' is not a commit in this repository.`, {
24
+ remediation: "Fetch it first, or pass a branch, tag, or commit that exists locally.",
25
+ })
26
+ }
27
+ )
28
+ // Asked of git rather than computed from paths, which a symlinked directory would skew.
29
+ const projectPath =
30
+ (await git(projectDir, ["rev-parse", "--show-prefix"])).replace(/\/$/, "") || "."
31
+
32
+ if (!(await isFile(root, commit, posix.join(projectPath, "package.json")))) {
33
+ throw new SixbCliError(
34
+ `[SixbDeploy] Commit ${commit.slice(0, 12)} has no ${posix.join(projectPath, "package.json")}.`,
35
+ { remediation: "Commit the project before deploying it." }
36
+ )
37
+ }
38
+
39
+ return {
40
+ commit,
41
+ ref,
42
+ projectPath,
43
+ dirty: (await git(root, ["status", "--porcelain"])) !== "",
44
+ archives: [
45
+ { path: ".", open: () => archive(root, commit) },
46
+ ...(await submoduleArchives(root, commit, "")),
47
+ ],
48
+ }
49
+ }
50
+
51
+ /**
52
+ * The Bun version the project pins with `packageManager` (`"bun@1.4.2"`) in its `package.json` or
53
+ * the nearest one above it, up to the repository root; else the Bun running this CLI. Read from the
54
+ * deployed commit when there is one, and from the files on disk for a dry run.
55
+ */
56
+ export async function resolveBunVersion(
57
+ projectDir: string,
58
+ source?: Pick<DeploySource, "commit" | "projectPath">
59
+ ): Promise<string> {
60
+ const root = source ? await git(projectDir, ["rev-parse", "--show-toplevel"]) : null
61
+ let dir = source ? source.projectPath : resolve(projectDir)
62
+ for (;;) {
63
+ const manifest = await readPackageJson(dir, root && source ? source.commit : null)
64
+ const pinned =
65
+ typeof manifest?.packageManager === "string"
66
+ ? manifest.packageManager.match(/^bun@(\d+\.\d+\.\d+)/)?.[1]
67
+ : undefined
68
+ if (pinned) return pinned
69
+
70
+ const atTop = source
71
+ ? dir === "."
72
+ : (await Bun.file(join(dir, ".git")).exists()) || dirname(dir) === dir
73
+ if (atTop) return Bun.version
74
+ dir = source ? posix.dirname(dir) : dirname(dir)
75
+ }
76
+
77
+ async function readPackageJson(
78
+ at: string,
79
+ commit: string | null
80
+ ): Promise<{ readonly packageManager?: unknown } | null> {
81
+ const text =
82
+ commit && root
83
+ ? await git(root, ["show", `${commit}:${posix.join(at, "package.json")}`]).catch(() => null)
84
+ : await Bun.file(join(at, "package.json"))
85
+ .text()
86
+ .catch(() => null)
87
+ if (text === null) return null
88
+ try {
89
+ return JSON.parse(text) as { readonly packageManager?: unknown }
90
+ } catch {
91
+ return null
92
+ }
93
+ }
94
+ }
95
+
96
+ /** The project's directory inside its repository, `.` at its root or outside git. */
97
+ export async function projectPathOf(projectDir: string): Promise<string> {
98
+ const prefix = await git(projectDir, ["rev-parse", "--show-prefix"]).catch(() => "")
99
+ return prefix.replace(/\/$/, "") || "."
100
+ }
101
+
102
+ /** The root of the git repository the project is in. */
103
+ export function repoRootOf(projectDir: string): Promise<string> {
104
+ return git(projectDir, ["rev-parse", "--show-toplevel"])
105
+ }
106
+
107
+ /** Who deploys, recorded with the release. */
108
+ export async function deployer(projectDir: string): Promise<string> {
109
+ if (process.env.GITHUB_ACTIONS && process.env.GITHUB_ACTOR) {
110
+ return `${process.env.GITHUB_ACTOR} (GitHub Actions)`
111
+ }
112
+ const email = await git(projectDir, ["config", "user.email"]).catch(() => "")
113
+ return email || userInfo().username
114
+ }
115
+
116
+ async function submoduleArchives(
117
+ repo: string,
118
+ commit: string,
119
+ prefix: string
120
+ ): Promise<DeploySourceArchive[]> {
121
+ const entries = (await git(repo, ["ls-tree", "-r", "-z", commit])).split("\0").filter(Boolean)
122
+ const archives: DeploySourceArchive[] = []
123
+
124
+ for (const entry of entries) {
125
+ const [meta, path] = entry.split("\t")
126
+ const [mode, , submoduleCommit] = meta?.split(" ") ?? []
127
+ if (mode !== "160000" || !path || !submoduleCommit) continue
128
+
129
+ const submodule = posix.join(repo, path)
130
+ const at = posix.join(prefix, path)
131
+ const checkedOut = await git(submodule, ["cat-file", "-e", `${submoduleCommit}^{commit}`])
132
+ .then(() => true)
133
+ .catch(() => false)
134
+ if (!checkedOut) {
135
+ throw new SixbCliError(
136
+ `[SixbDeploy] Submodule ${at} is not checked out at ${submoduleCommit.slice(0, 12)}.`,
137
+ { remediation: "Run `git submodule update --init --recursive` and deploy again." }
138
+ )
139
+ }
140
+ archives.push({ path: at, open: () => archive(submodule, submoduleCommit) })
141
+ archives.push(...(await submoduleArchives(submodule, submoduleCommit, at)))
142
+ }
143
+ return archives
144
+ }
145
+
146
+ /** `git archive` as a stream that fails if git does. */
147
+ function archive(repo: string, commit: string): ReadableStream<Uint8Array> {
148
+ const child = Bun.spawn(["git", "archive", "--format=tar", commit], {
149
+ cwd: repo,
150
+ stdout: "pipe",
151
+ stderr: "pipe",
152
+ })
153
+ const reader = child.stdout.getReader()
154
+ return new ReadableStream<Uint8Array>({
155
+ async pull(controller) {
156
+ const { done, value } = await reader.read()
157
+ if (!done) {
158
+ controller.enqueue(value)
159
+ return
160
+ }
161
+ const exitCode = await child.exited
162
+ if (exitCode === 0) controller.close()
163
+ else {
164
+ const stderr = await new Response(child.stderr).text()
165
+ controller.error(new Error(`[SixbDeploy] git archive failed in ${repo}: ${stderr.trim()}`))
166
+ }
167
+ },
168
+ cancel() {
169
+ child.kill()
170
+ },
171
+ })
172
+ }
173
+
174
+ async function isFile(root: string, commit: string, path: string): Promise<boolean> {
175
+ return git(root, ["cat-file", "-e", `${commit}:${path}`])
176
+ .then(() => true)
177
+ .catch(() => false)
178
+ }
179
+
180
+ async function git(cwd: string, args: readonly string[]): Promise<string> {
181
+ const child = Bun.spawn(["git", ...args], { cwd, stdout: "pipe", stderr: "pipe" })
182
+ const [exitCode, stdout, stderr] = await Promise.all([
183
+ child.exited,
184
+ new Response(child.stdout).text(),
185
+ new Response(child.stderr).text(),
186
+ ])
187
+ if (exitCode !== 0) throw new Error(stderr.trim() || `git ${args.join(" ")} failed`)
188
+ return stdout.trim()
189
+ }