-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
53 lines (42 loc) · 1.8 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import * as pulumi from "@pulumi/pulumi";
import * as docker from "@pulumi/docker";
import * as random from "@pulumi/random";
import { Api, createApiClient } from "@neondatabase/api-client";
import { hostname } from "os";
import { connectionStringsForBranches, getConnectionString } from "./neon";
const cfg = new pulumi.Config();
const baseUrl = cfg.get("baseUrl") ?? `http://${hostname()}`
const neonApiToken = cfg.require("neonApiToken")
const hasuraSecretKey = cfg.requireSecret("hasuraSecretKey")
const neonProjectName = cfg.require("neonProjectName")
const databaseName = cfg.require("databaseName")
export = async () => {
const api = createApiClient({ apiKey: neonApiToken })
const hasuraImage = new docker.RemoteImage("hasura-image", {name: "hasura/graphql-engine:v2.42.0"})
const connStrings = await connectionStringsForBranches(api, neonProjectName, databaseName, cfg.get("neonRoleName"))
const containers: Record<string, docker.Container> = {}
for (const branch of Object.keys(connStrings)) {
const port = new random.RandomInteger(`hasura-port-${branch}`, {
min: 32768,
max: 65535,
});
const connString = connStrings[branch]
containers[branch] = new docker.Container(`hasura-${branch}`, {
image: hasuraImage.repoDigest,
name: `hasura-${branch}`,
ports: [{ internal: 8080, external: port.result }],
networkMode: "bridge",
envs: [
`HASURA_GRAPHQL_DATABASE_URL=${connString}`,
`HASURA_GRAPHQL_ENABLE_CONSOLE=true`,
hasuraSecretKey.apply(s => `HASURA_GRAPHQL_ADMIN_SECRET=${s}`)
],
start: true
}, { ignoreChanges: ["image"] })
}
return Object.keys(containers).reduce((acc, x) => {
acc[`${x}-endpoint`] = containers[x].ports.apply(p => `${baseUrl}:${p!![0].external}`)
acc[`${x}-containerId`] = containers[x].id
return acc;
}, {} as Record<string, any>)
}