2023-01-16 15:14:20 +00:00
|
|
|
import { mkdir, mkdtemp } from "node:fs/promises";
|
2023-01-16 12:42:10 +00:00
|
|
|
import { tmpdir } from "node:os";
|
|
|
|
import path from "node:path";
|
2023-02-08 14:46:33 +00:00
|
|
|
import { cwd, exit } from "node:process";
|
2023-01-16 15:14:20 +00:00
|
|
|
import { Alpine } from "./alpine.js";
|
2023-02-08 14:50:07 +00:00
|
|
|
import { generateForgejoSecretsFile } from "./services/forgejo/secrets.js";
|
2023-02-10 02:36:01 +00:00
|
|
|
import { generateGrafanaSecretsFile } from "./services/grafana/secrets.js";
|
2023-02-02 22:02:38 +00:00
|
|
|
import { execFile } from "./helpers/better-api.js";
|
2023-02-10 02:36:01 +00:00
|
|
|
import { sudoChown, sudoChownToRunningUser } from "./helpers/sudo.js";
|
2023-01-23 15:30:21 +00:00
|
|
|
import { setupKernel } from "./kernel.js";
|
2023-02-02 21:25:11 +00:00
|
|
|
import { runQemu } from "./qemu.js";
|
2023-01-16 15:14:20 +00:00
|
|
|
import { Runit } from "./runit/index.js";
|
2023-02-18 16:10:21 +00:00
|
|
|
import { installFluentBit } from "./software/fluentbit";
|
2023-02-10 02:36:01 +00:00
|
|
|
import { setupForgejo } from "./services/forgejo/index.js";
|
2023-02-08 18:05:53 +00:00
|
|
|
import { setupDhcpcd } from "./services/dhcpcd.js";
|
2023-02-08 18:15:07 +00:00
|
|
|
import { setupNtpsec } from "./services/ntpsec.js";
|
2023-02-10 02:36:01 +00:00
|
|
|
import { setupGrafana } from "./services/grafana/index.js";
|
2023-02-17 00:27:28 +00:00
|
|
|
import { setupLoki } from "./services/loki/index.js";
|
2023-01-16 12:42:10 +00:00
|
|
|
|
2023-02-08 14:46:33 +00:00
|
|
|
if (process.argv[2] === "generate-secrets") {
|
|
|
|
await generateForgejoSecretsFile();
|
2023-02-10 02:36:01 +00:00
|
|
|
await generateGrafanaSecretsFile();
|
2023-02-08 14:46:33 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2023-01-16 12:42:10 +00:00
|
|
|
{
|
2023-01-23 15:30:21 +00:00
|
|
|
console.time("Building");
|
|
|
|
|
2023-02-02 21:25:11 +00:00
|
|
|
const artifactsDir = path.join(cwd(), "artifacts/");
|
|
|
|
const kernelDir = path.join(artifactsDir, "kernel");
|
|
|
|
await mkdir(artifactsDir, { recursive: true });
|
|
|
|
await mkdir(kernelDir, { recursive: true });
|
|
|
|
|
2023-01-16 12:42:10 +00:00
|
|
|
const rootfsDir = await mkdtemp(path.join(tmpdir(), "define-alpine-"));
|
2023-02-10 02:36:01 +00:00
|
|
|
await sudoChown(rootfsDir, "root:root");
|
2023-01-16 12:42:10 +00:00
|
|
|
console.debug(rootfsDir);
|
2023-01-16 15:14:20 +00:00
|
|
|
const alpine = await Alpine.makeWorld({ dir: rootfsDir });
|
2023-02-08 14:46:33 +00:00
|
|
|
|
2023-02-10 03:02:00 +00:00
|
|
|
await alpine.addPackages(["helix", "htop", "iproute2-ss", "socat"]);
|
2023-02-19 15:06:56 +00:00
|
|
|
await alpine.writeFile(
|
|
|
|
"/root/.ash_history",
|
|
|
|
`
|
|
|
|
socat tcp-listen:80,reuseaddr,fork tcp:localhost:3050 &
|
|
|
|
`,
|
|
|
|
{ uid: 0, gid: 0 }
|
|
|
|
);
|
2023-02-18 16:10:21 +00:00
|
|
|
await installFluentBit(alpine);
|
2023-01-16 15:14:20 +00:00
|
|
|
const runit = await Runit.setup(alpine);
|
2023-02-08 18:05:53 +00:00
|
|
|
await setupDhcpcd(alpine, runit);
|
2023-02-08 18:15:07 +00:00
|
|
|
await setupNtpsec(alpine, runit);
|
2023-02-08 14:46:33 +00:00
|
|
|
await setupForgejo(alpine, runit);
|
2023-02-17 00:27:28 +00:00
|
|
|
await setupLoki(alpine, runit);
|
2023-02-10 02:36:01 +00:00
|
|
|
await setupGrafana(alpine, runit);
|
2023-02-02 21:25:11 +00:00
|
|
|
const kernel = await setupKernel(alpine, kernelDir);
|
|
|
|
|
|
|
|
const squashfs = path.join(artifactsDir, "image.squashfs");
|
|
|
|
await execFile("sudo", [
|
|
|
|
"mksquashfs",
|
|
|
|
alpine.dir,
|
|
|
|
squashfs,
|
2023-02-10 02:36:01 +00:00
|
|
|
"-root-mode",
|
|
|
|
"755",
|
2023-02-02 21:25:11 +00:00
|
|
|
"-comp",
|
|
|
|
"zstd",
|
|
|
|
"-Xcompression-level",
|
|
|
|
"3",
|
|
|
|
"-noappend",
|
|
|
|
"-quiet",
|
|
|
|
]);
|
|
|
|
await sudoChownToRunningUser(squashfs);
|
2023-01-23 15:30:21 +00:00
|
|
|
|
|
|
|
console.timeEnd("Building");
|
|
|
|
|
2023-02-02 21:28:57 +00:00
|
|
|
runQemu(squashfs, kernel, { graphic: true });
|
2023-01-16 12:42:10 +00:00
|
|
|
}
|