From dc96394d5e1f27b13eb144aa75915c28606a28e7 Mon Sep 17 00:00:00 2001 From: Nulo Date: Mon, 20 Feb 2023 15:20:37 -0300 Subject: [PATCH] =?UTF-8?q?retrasar=20instalaci=C3=B3n=20de=20paquetes=20p?= =?UTF-8?q?ara=20instalarlos=20todos=20juntos=20(perf)=20(y=20refactor)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit asdfasdf --- alpine.ts | 35 +++++++++++++++++++++++++++++++---- index.ts | 31 ++++++++++++++++--------------- kernel.ts | 2 +- services/grafana/index.ts | 17 ++++++++++------- 4 files changed, 58 insertions(+), 27 deletions(-) diff --git a/alpine.ts b/alpine.ts index a054dd6..6963d34 100644 --- a/alpine.ts +++ b/alpine.ts @@ -21,6 +21,7 @@ export class Alpine { this.dir = dir; } fstab: Fstab = new Fstab(this); + packages: string[] = []; async mkdirP(dir: string): Promise { await mkdir(this.path(dir), { recursive: true }); @@ -69,8 +70,27 @@ export class Alpine { readPasswd(): Promise { return readPasswd(this.path("/etc/passwd")); } - async userAdd(user: string): Promise { - await execFile("useradd", ["--user-group", "--root", this.dir, user]); + async userAdd( + user: string, + { + system, + homeDir, + createHome, + }: { system: boolean; homeDir?: string; createHome: boolean } = { + system: false, + homeDir: "", + createHome: true, + } + ): Promise { + await execFile("useradd", [ + "--user-group", + ...(system ? ["--system"] : []), + ...(homeDir ? ["--home-dir", homeDir] : []), + ...(createHome ? ["--create-home"] : []), + "--root", + this.dir, + user, + ]); const passwd = await this.readPasswd(); const entry = passwd.find((e) => e.name === user); if (!entry) { @@ -79,9 +99,13 @@ export class Alpine { return entry; } - async addPackages(packages: string[]): Promise { + async actuallyInstallPackages(): Promise { + await this.installPackages(this.packages); + } + + async installPackages(packages: string[]): Promise { logDebug( - "addPackages", + "installPackages", await execFile("apk", [ "add", "--clean-protected", @@ -91,6 +115,9 @@ export class Alpine { ]) ); } + async addPackages(packages: string[]): Promise { + this.packages = this.packages.concat(packages); + } static async makeWorld({ dir, diff --git a/index.ts b/index.ts index d83ab1e..244a44b 100644 --- a/index.ts +++ b/index.ts @@ -41,9 +41,7 @@ async function timed(fn: () => Promise): Promise { console.debug(rootfsDir); const alpine = await timed(() => Alpine.makeWorld({ dir: rootfsDir })); - await timed(() => - alpine.addPackages(["helix", "htop", "iproute2-ss", "socat"]) - ); + await alpine.addPackages(["helix", "htop", "iproute2-ss", "socat"]); await alpine.writeFile( "/root/.ash_history", ` @@ -60,19 +58,22 @@ socat tcp-listen:80,reuseaddr,fork tcp:localhost:3050 & await timed(() => setupGrafana(alpine, runit)); const kernel = await timed(() => setupKernel(alpine, kernelDir)); + await timed(() => alpine.actuallyInstallPackages()); const squashfs = path.join(artifactsDir, "image.squashfs"); - await execFile("mksquashfs", [ - alpine.dir, - squashfs, - "-root-mode", - "755", - "-comp", - "zstd", - "-Xcompression-level", - "3", - "-noappend", - "-quiet", - ]); + await timed(() => + execFile("mksquashfs", [ + alpine.dir, + squashfs, + "-root-mode", + "755", + "-comp", + "zstd", + "-Xcompression-level", + "3", + "-noappend", + "-quiet", + ]) + ); console.timeEnd("Building"); diff --git a/kernel.ts b/kernel.ts index edfd479..4797742 100644 --- a/kernel.ts +++ b/kernel.ts @@ -63,7 +63,7 @@ default=lts `features="${features.join(" ")}"` ); - await alpine.addPackages([`linux-${kind}`]); + await alpine.installPackages([`linux-${kind}`]); const initramfs = path.join(alpine.dir, `/boot/initramfs-${kind}`); const vmlinuz = path.join(alpine.dir, `/boot/vmlinuz-${kind}`); diff --git a/services/grafana/index.ts b/services/grafana/index.ts index bd934b7..13b501e 100644 --- a/services/grafana/index.ts +++ b/services/grafana/index.ts @@ -6,6 +6,7 @@ import { FluentBitParser, runitLokiLogger } from "../../software/fluentbit.js"; import { loadGrafanaSecretsFile } from "./secrets.js"; const provisioningDir = "/etc/grafana/provisioning/"; +const grafanaHome = "/var/lib/grafana"; // TODO: grafana-image-renderer? // /etc/conf.d/grafana @@ -23,12 +24,14 @@ export async function setupGrafana( ): Promise { await alpine.addPackages(["grafana"]); - const passwd = await alpine.readPasswd(); - const user = passwd.find((e) => e.name === "grafana"); - assert(!!user, "no existe el usuario grafana"); + const user = await alpine.userAdd("grafana", { + system: true, + homeDir: grafanaHome, + createHome: false, + }); // TODO: data - await alpine.fstab.addTmpfs("/var/lib/grafana", { + await alpine.fstab.addTmpfs(grafanaHome, { uid: user.uid, gid: user.gid, mode: "700", @@ -59,7 +62,7 @@ exec chpst -u grafana:grafana grafana-cli --homepath /usr/share/grafana --config await runit.addService( "grafana", `#!/bin/sh -export GRAFANA_HOME=/var/lib/grafana +export GRAFANA_HOME='${grafanaHome}' cd "$GRAFANA_HOME" @@ -80,7 +83,7 @@ async function genConfig(): Promise { #################################### Paths #################################### [paths] -data = /var/lib/grafana +data = ${grafanaHome} # Temporary files in \`data\` directory older than given duration will be removed ;temp_data_lifetime = 24h @@ -88,7 +91,7 @@ data = /var/lib/grafana # Directory where grafana can store logs ;logs = /var/log/grafana -plugins = /var/lib/grafana/plugins +plugins = ${grafanaHome}/plugins # folder that contains provisioning config files that grafana will apply on startup and while running. provisioning = ${provisioningDir}