define-alpine-the-sequel/software/fluentbit.ts

130 lines
2.9 KiB
TypeScript
Raw Normal View History

2023-02-19 20:58:18 +00:00
import { constants, copyFile } from "node:fs/promises";
2023-02-18 16:10:21 +00:00
import { join } from "node:path";
import { Alpine } from "../alpine.js";
import { buildRepro, reproRun } from "../helpers/repro-run.js";
const parsersPath = "/etc/fluent-bit/parsers.conf";
export async function installFluentBit(alpine: Alpine): Promise<void> {
const bin = await buildFluentBit();
await saveParsers(alpine);
await alpine.addPackages(["musl-fts", "yaml"]);
2023-02-19 20:58:18 +00:00
await copyFile(
bin,
alpine.path("/usr/local/bin/fluent-bit"),
constants.COPYFILE_FICLONE
);
2023-02-18 16:10:21 +00:00
}
// ## Script generators
// Returns a logScript to be used with runit for logging to Loki
export function runitLokiLogger(parser: FluentBitParser, name: string): string {
return `#!/bin/sh
exec chpst -u nobody:nobody /usr/local/bin/fluent-bit \
--parser='${parsersPath}' \
--input=stdin \
--prop=parser='${parser}' \
--output=loki \
--prop=labels='job=fluentbit,stream=${name}'
`;
}
// ## Parsers
export enum FluentBitParser {
Json = "json",
2023-02-19 16:10:57 +00:00
Logfmt = "logfmt",
2023-02-18 16:10:21 +00:00
// Raw toma todo lo que haya en una línea y lo guarda en `message`. No recomendado.
Raw = "raw",
2023-02-19 15:04:47 +00:00
Forgejo = "forgejo",
2023-02-19 16:10:44 +00:00
Ntpsec = "ntpsec",
2023-02-19 16:19:42 +00:00
Dhcpcd = "dhcpcd",
2023-02-18 16:10:21 +00:00
}
async function saveParsers(alpine: Alpine): Promise<void> {
// https://github.com/fluent/fluent-bit/blob/master/conf/parsers.conf
await alpine.writeFile(
parsersPath,
2023-02-19 15:04:47 +00:00
// https://rubular.com/
2023-02-18 16:10:21 +00:00
`
2023-02-19 16:10:57 +00:00
[PARSER]
name logfmt
format logfmt
2023-02-18 16:10:21 +00:00
[PARSER]
name raw
format regex
regex ^(?<message>.*?)$
2023-02-19 15:04:47 +00:00
[PARSER]
name forgejo
format regex
regex ^((?<time>\\d{4}\\/\\d{2}\\/\\d{2} \\d{2}:\\d{2}:\\d{2}?) )?((?<trace>.+:.+:.+\\(\\)?) \\[(?<level>.?)\\] )?(?<message>.*?)$
2023-02-19 16:10:44 +00:00
[PARSER]
name ntpsec
format regex
regex ^(?<time>\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}) (?<process>.+\\[\\d+\\]): (?<component>\\w+): (?<message>.+)$
2023-02-19 16:19:42 +00:00
[PARSER]
name dhcpcd
format regex
regex ^((?<interface>\\w+): )?(?<message>.+)$
2023-02-18 16:10:21 +00:00
`
);
}
// ## Build binary
function buildFluentBit(): Promise<string> {
const version = "v2.0.9";
return buildRepro(
"fluentbit",
version,
`#!/bin/sh -e
runprint() {
echo "==> $@"
"$@"
}
runprint apk add --quiet \
make gcc g++ patch musl-dev openssl-dev linux-headers \
bison cmake flex musl-fts-dev gtest-dev yaml-dev zlib-dev
wget https://github.com/fluent/fluent-bit/archive/refs/tags/${version}.tar.gz -O- | tar zx
cd fluent-bit-*
patch --strip=1 <<'EOF'
--- a/lib/chunkio/src/CMakeLists.txt
+++ b/lib/chunkio/src/CMakeLists.txt
@@ -12,6 +12,7 @@
)
set(libs cio-crc32)
+set(libs \${libs} fts)
if(\${CMAKE_SYSTEM_NAME} MATCHES "Windows")
set(src
EOF
runprint cmake -B build \
-DFLB_CORO_STACK_SIZE=24576 \
.
runprint make -C build
ls build/bin/
mv build/bin/fluent-bit /fluent-bit
`,
(dir) =>
reproRun({
cwd: dir,
command: "/src/build",
cache: [],
}),
(dir) => join(dir, "rootfs/fluent-bit")
);
}