Skip to content
Draft
60 changes: 60 additions & 0 deletions packages/oyasai-alloy.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
lib,
grafana-alloy,
oyasaiDockerTools,
stdenv,
writeTextFile,
}:

let
alloyConfig = writeTextFile {
name = "config.alloy";
text = ''
// Discover all Docker containers via the Docker socket.
discovery.docker "all" {
host = "unix:///var/run/docker.sock"
}

// Relabel: expose container name and log stream as Loki labels.
discovery.relabel "containers" {
targets = discovery.docker.all.targets

rule {
source_labels = ["__meta_docker_container_name"]
regex = "/(.*)"
target_label = "container"
}

rule {
source_labels = ["__meta_docker_container_log_stream"]
target_label = "stream"
}
}

// Tail logs from Docker containers and forward to Loki.
loki.source.docker "all" {
host = "unix:///var/run/docker.sock"
targets = discovery.relabel.containers.output
forward_to = [loki.write.default.receiver]
}

loki.write "default" {
endpoint {
url = "http://loki:3100/loki/api/v1/push"
}
}
'';
};
in
lib.optionalAttrs stdenv.hostPlatform.isLinux {
docker = oyasaiDockerTools.buildLayeredImage {
name = "oyasai-alloy";
config = {
Cmd = [
"${grafana-alloy}/bin/alloy"
"run"
"${alloyConfig}"
];
};
};
}
67 changes: 67 additions & 0 deletions packages/oyasai-cdktf/src/stacks/platform-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,30 @@ export class PlatformServices extends OyasaiPlatformTerraformStack {
const imageIds = JSON.parse(process.env.OYASAI_IMAGE_ID as string);
const images = {
// keep-sorted start
alloy: imageIds["oyasai-alloy"],
grafana: imageIds["oyasai-grafana"],
loki: imageIds["oyasai-loki"],
mariadb: imageIds.mariadb,
mcMonitorExporter: imageIds["oyasai-mc-monitor-exporter"],
minecraftAxiom: imageIds["oyasai-minecraft-axiom"],
minecraftBackup: imageIds["mc-backup"],
minecraftLobby: imageIds["oyasai-minecraft-lobby"],
minecraftMain: imageIds["oyasai-minecraft-main"],
mysqlBackup: imageIds["mysql-backup"],
prometheus: imageIds["oyasai-prometheus"],
velocity: imageIds["oyasai-velocity"],
// keep-sorted end
} as const;

const baseHostPath = join("/opt/platform", this.environment);
const hostPaths = {
// keep-sorted start
loki: join(baseHostPath, "loki"),
mariadb: join(baseHostPath, "mariadb"),
minecraftAxiom: join(baseHostPath, "minecraft-axiom"),
minecraftLobby: join(baseHostPath, "minecraft-lobby"),
minecraftMain: join(baseHostPath, "minecraft-main"),
prometheus: join(baseHostPath, "prometheus"),
velocity: join(baseHostPath, "velocity"),
// keep-sorted end
} as const;
Expand Down Expand Up @@ -290,5 +297,65 @@ export class PlatformServices extends OyasaiPlatformTerraformStack {
}),
});
}

new Container(this, this.t("mc-monitor-exporter-container"), {
image: images.mcMonitorExporter,
name: "mc-monitor-exporter",
restart: "unless-stopped",
networksAdvanced: [network],
env: envs({
EXPORT_SERVERS: [
`${minecraftMainContainer.name}:25565`,
`${minecraftLobbyContainer.name}:25565`,
].join(","),
}),
});

new Container(this, this.t("loki-container"), {
image: images.loki,
name: "loki",
restart: "unless-stopped",
networksAdvanced: [network],
volumes: [
{
containerPath: "/data",
hostPath: hostPaths.loki,
},
],
});

new Container(this, this.t("alloy-container"), {
image: images.alloy,
name: "alloy",
restart: "unless-stopped",
networksAdvanced: [network],
volumes: [
{
containerPath: "/var/run/docker.sock",
hostPath: "/var/run/docker.sock",
},
],
});

new Container(this, this.t("prometheus-container"), {
image: images.prometheus,
name: "prometheus",
restart: "unless-stopped",
networksAdvanced: [network],
volumes: [
{
containerPath: "/data",
hostPath: hostPaths.prometheus,
},
],
});

new Container(this, this.t("grafana-container"), {
image: images.grafana,
name: "grafana",
restart: "unless-stopped",
networksAdvanced: [network],
ports: ports({ tcp: [3000] }),
});
}
}
Loading
Loading