Skip to content

Commit

Permalink
chores: exec db bootstrap in action (#66)
Browse files Browse the repository at this point in the history
* run bootstrap in gh

* move output to cd-template

* add output for register

* set working dir

* add output from terraform apply

* add output from terraform apply

* read tf output file

* echo tf output file

* add bootstrap ts func

* add db setup in _meta.mts

* add db setup in _meta.mts

* add db setup in _meta.mts

* add bootstrap config

* add schema names

* read input for bootstrapping

* update ts filename and add db schema

* remove specified file

* remove specified file

* add tf outputs for access packages

* remove suffix from infra

* add base url

* add line delimiter

* add ws

* add ws a deps

* add ws a deps

* add ws a deps

* add ws a deps

* add ws deps

* add Reader on subscription for application admin

* add Reader on subscription for application admin

* add process

* add database user

* fkn hate js

* add at least 8.0.100 > in global.json

* update conf for access packages
  • Loading branch information
andreasisnes authored Nov 14, 2024
1 parent 78f46c9 commit 560f38f
Show file tree
Hide file tree
Showing 21 changed files with 319 additions and 58 deletions.
23 changes: 23 additions & 0 deletions .github/scripts/_meta.mts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,19 @@ const infraSchema = z.object({
terraform: terraformSchema.optional(),
});

const databaseSchema = z.object({
bootstrap: z.boolean().optional().default(false),
name: z.string().min(3).optional(),
roleprefix: z.string().min(3).optional(),
schema: z.any().optional(),
});

const configSchema = z.object({
name: z.string().optional(),
shortName: z.string().optional(),
image: imageSchema.optional(),
infra: infraSchema.optional(),
database: databaseSchema.optional(),
});

export type VerticalType = "app" | "lib" | "pkg";
Expand All @@ -65,6 +73,13 @@ export type InfraInfo = {
readonly terraform?: TerraformInfo;
};

export type DatabaseInfo = {
readonly bootstrap: boolean;
readonly name: string;
readonly roleprefix: string;
readonly schema: object;
};

export type Vertical = {
readonly type: VerticalType;
readonly name: string;
Expand All @@ -73,6 +88,7 @@ export type Vertical = {
readonly relPath: string;
readonly image?: ImageInfo;
readonly infra?: InfraInfo;
readonly database?: DatabaseInfo;
};

const vertialDirs = {
Expand Down Expand Up @@ -118,6 +134,7 @@ const readVertical = async (

let image: ImageInfo | undefined = void 0;
let infra: InfraInfo | undefined = void 0;
let database: DatabaseInfo | undefined = void 0;
if (type === "app") {
const confImage = config.image ?? { type: "dotnet" };

Expand Down Expand Up @@ -155,6 +172,11 @@ const readVertical = async (
infra = confInfra as InfraInfo;
}

const confDatabase = config.database;
if (confDatabase) {
database = confDatabase as DatabaseInfo;
}

image = confImage as ImageInfo;
}

Expand All @@ -166,6 +188,7 @@ const readVertical = async (
relPath: dirPath,
image,
infra,
database,
};
};

Expand Down
63 changes: 31 additions & 32 deletions .github/scripts/bootstrap-db.mts
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { WebSocket } from "ws";
import { readFileSync } from "node:fs";

const argv = yargs(hideBin(process.argv))
.option("deploy-api", {
description: "Deploy API base path",
})
.option("subscription-id", {
type: "string",
required: true,
required: false,
})
.option("resource-group", {
type: "string",
required: true,
required: false,
})
.option("server-name", {
type: "string",
required: true,
required: false,
})
.option("key-vault-name", {
type: "string",
required: true,
required: false,
})
.option("user", {
type: "string",
required: true,
required: false,
})
.option("database", {
type: "string",
Expand All @@ -37,6 +39,10 @@ const argv = yargs(hideBin(process.argv))
type: "array",
required: true,
})
.option("tf-outfile", {
type: "string",
required: false,
})
.parse();

const request = {
Expand All @@ -52,6 +58,16 @@ const request = {
schemas: Object.fromEntries(argv.schema.map((n) => [n, {}])),
};

if (argv.tfOutfile) {
const buffer = readFileSync(argv.tfOutfile, "utf8");
const tfout = JSON.parse(buffer);
request.resources.subscriptionId ||= tfout.subscription_id.value;
request.resources.resourceGroup ||= tfout.resource_group_name.value;
request.resources.serverName ||= tfout.postgres_server_name.value;
request.resources.keyVaultName ||= tfout.key_vault_name.value;
request.resources.user ||= tfout.database_user.value;
}

// console.log(request);
// process.exit(1);

Expand All @@ -65,45 +81,28 @@ if (baseUrl.protocol === "http:") {
}

const ws = new WebSocket(
new URL("api/v1/database/bootstrap", baseUrl),
new URL("deployapi/api/v1/database/bootstrap", baseUrl).toString(),
"altinn.task-pipeline"
);

let pending = Promise.resolve();

const write = (data: Blob) => {
const buffer = data.arrayBuffer();
pending = pending
.then(() => buffer)
.then((buffer) => {
var arr = new Uint8Array(buffer);
process.stderr.write(arr);
})
.catch((err) => {
console.error(err);
process.exit(1);
});
};

ws.addEventListener("open", (e) => {
ws.on("open", () => {
ws.send(JSON.stringify(request));
});

ws.addEventListener("message", (ev) => {
write(ev.data);
ws.on("message", (data: Buffer) => {
process.stdout.write(data);
});

ws.addEventListener("close", (ev) => {
console.log("closed", ev.code, ev.reason);
ws.on("close", (code, reason) => {
console.log("closed", code, reason);
ws.close();

if (ev.code !== 4000) {
if (code !== 4000) {
process.exit(1);
}
});

ws.addEventListener("error", (ev) => {
if (ev && "message" in ev) {
console.error("error", ev.message);
}
ws.on("error", (err) => {
console.error("error", err.message);
process.exit(1);
});
11 changes: 11 additions & 0 deletions .github/scripts/list-verticals.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { join } from "path";
import { verticals } from "./_meta.mts";
import * as actions from "@actions/core";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { string } from "zod";

const argv = yargs(hideBin(process.argv))
.option("type", {
Expand Down Expand Up @@ -39,6 +41,15 @@ var matrix = {
}
}

if (v.database) {
ret.databaseBootstrap = v.database.bootstrap.toString();
ret.databaseName = v.database.name;
ret.databaseRolePrefix = v.database.roleprefix;
ret.databaseSchema = Object.keys(v.database.schema).join(",");
} else {
ret.databaseBootstrap = "false";
}

return ret;
}),
};
Expand Down
13 changes: 7 additions & 6 deletions .github/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
"devDependencies": {
"@actions/core": "^1.11.1",
"@octokit/action": "^7.0.0",
"@types/node": "^20.11.30",
"@types/node": "^20.16.11",
"chalk": "^5.3.0",
"globby": "^14.0.1",
"tsx": "^4.7.1",
"typescript": "^5.4.3",
"globby": "^14.0.2",
"tsx": "^4.19.1",
"typescript": "^5.6.3",
"ws": "^8.18.0",
"yargs": "^17.7.2",
"zod": "^3.23.8",
"zod-validation-error": "^3.4.0",
"zx": "^8.0.0"
"zx": "^8.1.9"
}
}
}
43 changes: 30 additions & 13 deletions .github/scripts/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 560f38f

Please sign in to comment.