pacework 1.7.15 → 1.7.17

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,133 @@
1
+ /**
2
+ * Throwaway production-like environment (PWK-182), the part that needs no
3
+ * Docker to test: which containers a repository's production profile calls
4
+ * for, how each is started for one run, the variables a run gets, and how
5
+ * they are found again to remove. Starting them in runs is not wired yet.
6
+ *
7
+ * The image follows production: the profile's engine, its major version and
8
+ * its extensions (Postgres with pgvector needs the pgvector image, not plain
9
+ * postgres). A profile that names something with no known image says so.
10
+ */
11
+
12
+ const LABEL = "pacework.run"
13
+
14
+ /** Postgres extensions that need an image of their own. */
15
+ const POSTGRES_EXTENSION_IMAGES = {
16
+ vector: (major) => `pgvector/pgvector:pg${major}`,
17
+ pgvector: (major) => `pgvector/pgvector:pg${major}`,
18
+ postgis: (major) => `postgis/postgis:${major}-3.4`,
19
+ timescaledb: (major) => `timescale/timescaledb:latest-pg${major}`,
20
+ }
21
+ /** Extensions every Postgres image has (contrib), so the plain image is enough. */
22
+ const POSTGRES_CONTRIB = new Set(["pgcrypto", "uuid-ossp", "citext", "hstore", "pg_trgm", "btree_gin", "btree_gist", "unaccent", "pg_stat_statements"])
23
+
24
+ const major = (version, fallback) => {
25
+ const match = String(version ?? "").match(/\d+/)
26
+ return match ? match[0] : fallback
27
+ }
28
+
29
+ /**
30
+ * The containers a profile calls for. Each: name, image, the port inside the
31
+ * container, its environment, and a function from the host port to the
32
+ * variables the run gets. `problems` lists what has no known image.
33
+ *
34
+ * @param {{ database?: { engine: string, version?: string|null, extensions?: string[] }|null, services?: Array<{ value: string }> } | null} profile
35
+ */
36
+ export function profileContainers(profile) {
37
+ const containers = []
38
+ const problems = []
39
+ const db = profile?.database
40
+ if (db) {
41
+ const engine = String(db.engine || "").toLowerCase()
42
+ if (engine === "postgres") {
43
+ const version = major(db.version, "16")
44
+ const extensions = (db.extensions ?? []).map((ext) => String(ext).toLowerCase())
45
+ const special = extensions.filter((ext) => POSTGRES_EXTENSION_IMAGES[ext])
46
+ const unknown = extensions.filter((ext) => !POSTGRES_EXTENSION_IMAGES[ext] && !POSTGRES_CONTRIB.has(ext))
47
+ if (special.length > 1) problems.push(`no single image has the Postgres extensions ${special.join(" and ")}; using ${special[0]}'s`)
48
+ if (unknown.length) problems.push(`the Postgres extension${unknown.length === 1 ? "" : "s"} ${unknown.join(", ")} may be missing from the image`)
49
+ containers.push({
50
+ name: "postgres",
51
+ image: special.length ? POSTGRES_EXTENSION_IMAGES[special[0]](version) : `postgres:${version}`,
52
+ port: 5432,
53
+ env: { POSTGRES_PASSWORD: "postgres", POSTGRES_DB: "app" },
54
+ exports: (hostPort) => ({
55
+ DATABASE_URL: `postgresql://postgres:postgres@127.0.0.1:${hostPort}/app`,
56
+ PGHOST: "127.0.0.1", PGPORT: String(hostPort), PGUSER: "postgres", PGPASSWORD: "postgres", PGDATABASE: "app",
57
+ }),
58
+ ready: ["pg_isready", "-U", "postgres"],
59
+ })
60
+ } else if (engine === "mysql") {
61
+ containers.push({
62
+ name: "mysql", image: `mysql:${major(db.version, "8")}`, port: 3306,
63
+ env: { MYSQL_ROOT_PASSWORD: "mysql", MYSQL_DATABASE: "app" },
64
+ exports: (hostPort) => ({ DATABASE_URL: `mysql://root:mysql@127.0.0.1:${hostPort}/app` }),
65
+ ready: ["mysqladmin", "ping", "-h", "127.0.0.1", "-pmysql"],
66
+ })
67
+ } else if (engine === "mongodb") {
68
+ containers.push({
69
+ name: "mongodb", image: `mongo:${major(db.version, "7")}`, port: 27017, env: {},
70
+ exports: (hostPort) => ({ MONGODB_URI: `mongodb://127.0.0.1:${hostPort}/app` }),
71
+ ready: ["mongosh", "--quiet", "--eval", "db.runCommand({ ping: 1 })"],
72
+ })
73
+ } else if (engine === "sqlite") {
74
+ // A file, not a service: nothing to start.
75
+ } else if (engine) {
76
+ problems.push(`no known image for the database ${engine}`)
77
+ }
78
+ }
79
+ for (const service of profile?.services ?? []) {
80
+ const name = String(service?.value ?? service ?? "").toLowerCase()
81
+ if (!name || containers.some((container) => container.name === name)) continue
82
+ if (name === "redis") {
83
+ containers.push({
84
+ name: "redis", image: "redis:7", port: 6379, env: {},
85
+ exports: (hostPort) => ({ REDIS_URL: `redis://127.0.0.1:${hostPort}` }),
86
+ ready: ["redis-cli", "ping"],
87
+ })
88
+ } else if (name === "rabbitmq") {
89
+ containers.push({
90
+ name: "rabbitmq", image: "rabbitmq:3", port: 5672, env: {},
91
+ exports: (hostPort) => ({ AMQP_URL: `amqp://guest:guest@127.0.0.1:${hostPort}` }),
92
+ ready: ["rabbitmq-diagnostics", "-q", "ping"],
93
+ })
94
+ } else {
95
+ problems.push(`no known image for the service ${name}`)
96
+ }
97
+ }
98
+ return { containers, problems }
99
+ }
100
+
101
+ /** The container's name for one run: unique per run, so runs never share one. */
102
+ export function containerName(runId, container) {
103
+ return `pacework-${String(runId).replace(/[^a-zA-Z0-9]/g, "").slice(0, 12)}-${container.name}`
104
+ }
105
+
106
+ /**
107
+ * `docker run` arguments: throwaway (--rm), bound to localhost on a free host
108
+ * port, labelled with the run so clean-up finds it, and with memory and CPU
109
+ * limits.
110
+ */
111
+ export function dockerRunArgs(runId, container, hostPort, { memory = "1g", cpus = "1" } = {}) {
112
+ return [
113
+ "run", "-d", "--rm",
114
+ "--name", containerName(runId, container),
115
+ "--label", `${LABEL}=${runId}`,
116
+ "--memory", memory, "--cpus", cpus,
117
+ "-p", `127.0.0.1:${hostPort}:${container.port}`,
118
+ ...Object.entries(container.env).flatMap(([key, value]) => ["-e", `${key}=${value}`]),
119
+ container.image,
120
+ ]
121
+ }
122
+
123
+ /** Arguments to list every container left by a run, for removal on success, failure or cancel. */
124
+ export function leftoverArgs(runId) {
125
+ return ["ps", "-aq", "--filter", `label=${LABEL}=${runId}`]
126
+ }
127
+
128
+ /** What a run's evidence says when there is no container runtime. */
129
+ export function noDockerNote(containers) {
130
+ return containers.length
131
+ ? `Docker is not available on this runner, so ${containers.map((container) => `${container.name} (${container.image})`).join(", ")} could not be started and nothing was verified against a production-like environment.`
132
+ : null
133
+ }
@@ -70,8 +70,13 @@ export function closestShellCommand(input) {
70
70
  return distance <= 2 ? best : null
71
71
  }
72
72
 
73
+ /**
74
+ * Commands the main screen hands the terminal to: they stream, ask questions
75
+ * on stdin (the agent picker of /run, the login prompt), or start a process
76
+ * that prints straight to the terminal (start, restart).
77
+ */
73
78
  export function commandTakesTerminal(command, args) {
74
- return ["login", "connect", "tail", "watch", "logtail", "update"].includes(command)
79
+ return ["login", "connect", "tail", "watch", "logtail", "update", "run", "start", "restart"].includes(command)
75
80
  || (command === "mcp" && args[0] === "install")
76
81
  }
77
82