Regenerate configs improvements
All checks were successful
Close stale issues and PRs / stale (push) Successful in 33s

- Fix certificates
- Adds dry run
- code cleanup
This commit is contained in:
Jamie Curnow
2026-03-03 08:44:42 +10:00
parent f1e95c9c52
commit eb67b3bfb6
9 changed files with 99 additions and 81 deletions

View File

@@ -2,7 +2,6 @@
import * as process from "node:process"; // Use the node: protocol for built-ins
import internalNginx from "../internal/nginx.js";
import { castJsonIfNeed } from "../lib/helpers.js";
import { global as logger } from "../logger.js";
import deadHostModel from "../models/dead_host.js";
import proxyHostModel from "../models/proxy_host.js";
@@ -11,12 +10,16 @@ import streamModel from "../models/stream.js";
const args = process.argv.slice(2);
const UNATTENDED = args.includes("-y") || args.includes("--yes");
if (args.includes("--help")) {
console.log("Usage: ./regenerate-config [--help] [-y|--yes]");
const DRY_RUN = args.includes("--dry-run");
if (args.includes("--help") || args.includes("-h")) {
console.log("\nThis will iterate over all Hosts and regnerate their Nginx configs.\n")
console.log("Usage: ./regenerate-config [-h|--help] [-y|--yes] [--dry-run]\n");
process.exit(0);
}
// ask for the user to confirm the action if not in unattended mode
if (!UNATTENDED) {
if (!UNATTENDED && !DRY_RUN) {
const readline = await import("node:readline");
const rl = readline.createInterface({
input: process.stdin,
@@ -37,68 +40,37 @@ if (!UNATTENDED) {
}
}
const logIt = (msg, type = "info") => logger[type](
`${DRY_RUN ? '[DRY RUN] ' : ''}${msg}`,
);
// Let's do it.
// Proxy hosts
const proxyRows = await proxyHostModel
.query()
.where("is_deleted", 0)
.andWhere("enabled", 1)
.groupBy("id")
.allowGraph("[owner,access_list,certificate]")
.orderBy(castJsonIfNeed("domain_names"), "ASC");
const processItems = async (model, type) => {
const rows = await model
.query()
.where("is_deleted", 0)
.andWhere("enabled", 1)
.groupBy("id")
.allowGraph(model.defaultAllowGraph)
.withGraphFetched(`[${model.defaultExpand.join(", ")}]`)
.orderBy(...model.defaultOrder);
for (const row of proxyRows) {
logger.info(
`Regenerating config for Proxy Host #${row.id}: ${row.domain_names.join(", ")}`,
);
await internalNginx.configure(proxyHostModel, "proxy_host", row);
}
logIt(`[${type}] Found ${rows.length} rows to process...`);
for (const row of rows) {
if (!DRY_RUN) {
logIt(`[${type}] Regenerating config #${row.id}: ${row.domain_names ? row.domain_names.join(", ") : 'port ' + row.incoming_port}`);
await internalNginx.configure(proxyHostModel, "proxy_host", row);
} else {
logIt(`[${type}] Skipping generation of config #${row.id}: ${row.domain_names ? row.domain_names.join(", ") : 'port ' + row.incoming_port}`);
}
}
};
// Redirection hosts
const redirectionRows = await redirectionHostModel
.query()
.where("is_deleted", 0)
.andWhere("enabled", 1)
.groupBy("id")
.allowGraph("[owner,access_list,certificate]")
.orderBy(castJsonIfNeed("domain_names"), "ASC");
await processItems(proxyHostModel, "Proxy Host");
await processItems(redirectionHostModel, "Redirection Host");
await processItems(deadHostModel, "404 Host");
await processItems(streamModel, "Stream");
for (const row of redirectionRows) {
logger.info(
`Regenerating config for Redirection Host #${row.id}: ${row.domain_names.join(", ")}`,
);
await internalNginx.configure(redirectionHostModel, "redirection_host", row);
}
// 404 hosts
const deadRows = await deadHostModel
.query()
.where("is_deleted", 0)
.andWhere("enabled", 1)
.groupBy("id")
.allowGraph("[owner,access_list,certificate]")
.orderBy(castJsonIfNeed("domain_names"), "ASC");
for (const row of deadRows) {
logger.info(
`Regenerating config for 404 Host #${row.id}: ${row.domain_names.join(", ")}`,
);
await internalNginx.configure(deadHostModel, "dead_host", row);
}
// Streams
const streamRows = await streamModel
.query()
.where("is_deleted", 0)
.andWhere("enabled", 1)
.groupBy("id")
.allowGraph("[owner,access_list,certificate]");
for (const row of streamRows) {
logger.info(`Regenerating config for Stream #${row.id}: ${row.incoming_port} -> ${row.forwarding_host}:${row.forwarding_port}`);
await internalNginx.configure(deadHostModel, "stream", row);
}
logger.success("Completed");
logIt("Completed", "success");
process.exit(0);