Fix code to pull CouchDB URL out of docker ps.

This commit is contained in:
Sam Rose 2024-03-25 17:11:41 +00:00
parent d77b6f82e0
commit 44d4e496b6
No known key found for this signature in database
1 changed files with 46 additions and 65 deletions

View File

@ -1,80 +1,58 @@
import { DatabaseImpl } from "../../../src/db"
import { execSync } from "child_process" import { execSync } from "child_process"
let dockerPsResult: string | undefined interface ContainerInfo {
Command: string
function formatDockerPsResult(serverName: string, port: number) { CreatedAt: string
const lines = dockerPsResult?.split("\n") ID: string
let first = true Image: string
if (!lines) { Labels: string
return null LocalVolumes: string
} Mounts: string
for (let line of lines) { Names: string
if (first) { Networks: string
first = false Ports: string
continue RunningFor: string
} Size: string
let toLookFor = serverName.split("-service")[0] State: string
if (!line.includes(toLookFor)) { Status: string
continue
}
const regex = new RegExp(`0.0.0.0:([0-9]*)->${port}`, "g")
const found = line.match(regex)
if (found) {
return found[0].split(":")[1].split("->")[0]
}
}
return null
} }
function getTestContainerSettings( function getTestcontainers(): ContainerInfo[] {
serverName: string, return execSync("docker ps --format json")
key: string .toString()
): string | null { .split("\n")
const entry = Object.entries(global).find( .filter(x => x.length > 0)
([k]) => .map(x => JSON.parse(x) as ContainerInfo)
k.includes(`${serverName.toUpperCase()}`) && .filter(x => x.Labels.includes("org.testcontainers=true"))
k.includes(`${key.toUpperCase()}`)
)
if (!entry) {
return null
}
return entry[1]
} }
function getContainerInfo(containerName: string, port: number) { function getContainerByImage(image: string) {
let assignedPort = getTestContainerSettings( return getTestcontainers().find(x => x.Image.startsWith(image))
containerName.toUpperCase(),
`PORT_${port}`
)
if (!dockerPsResult) {
try {
const outputBuffer = execSync("docker ps")
dockerPsResult = outputBuffer.toString("utf8")
} catch (err) {
//no-op
}
}
const possiblePort = formatDockerPsResult(containerName, port)
if (possiblePort) {
assignedPort = possiblePort
}
const host = getTestContainerSettings(containerName.toUpperCase(), "IP")
return {
port: assignedPort,
host,
url: host && assignedPort && `http://${host}:${assignedPort}`,
}
} }
function getCouchConfig() { function getExposedPort(container: ContainerInfo, port: number) {
return getContainerInfo("couchdb", 5984) const match = container.Ports.match(new RegExp(`0.0.0.0:(\\d+)->${port}/tcp`))
if (!match) {
return undefined
}
return parseInt(match[1])
} }
export function setupEnv(...envs: any[]) { export function setupEnv(...envs: any[]) {
const couch = getCouchConfig() const couch = getContainerByImage("budibase/couchdb")
if (!couch) {
throw new Error("CouchDB container not found")
}
const couchPort = getExposedPort(couch, 5984)
if (!couchPort) {
throw new Error("CouchDB port not found")
}
const configs = [ const configs = [
{ key: "COUCH_DB_PORT", value: couch.port }, { key: "COUCH_DB_PORT", value: `${couchPort}` },
{ key: "COUCH_DB_URL", value: couch.url }, { key: "COUCH_DB_URL", value: `http://localhost:${couchPort}` },
] ]
for (const config of configs.filter(x => !!x.value)) { for (const config of configs.filter(x => !!x.value)) {
@ -82,4 +60,7 @@ export function setupEnv(...envs: any[]) {
env._set(config.key, config.value) env._set(config.key, config.value)
} }
} }
// @ts-expect-error
DatabaseImpl.nano = undefined
} }