regenerate-config 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env node
  2. import * as process from "node:process"; // Use the node: protocol for built-ins
  3. import internalNginx from "../internal/nginx.js";
  4. import { global as logger } from "../logger.js";
  5. import deadHostModel from "../models/dead_host.js";
  6. import proxyHostModel from "../models/proxy_host.js";
  7. import redirectionHostModel from "../models/redirection_host.js";
  8. import streamModel from "../models/stream.js";
  9. const args = process.argv.slice(2);
  10. const UNATTENDED = args.includes("-y") || args.includes("--yes");
  11. const DRY_RUN = args.includes("--dry-run");
  12. if (args.includes("--help") || args.includes("-h")) {
  13. console.log("\nThis will iterate over all Hosts and regnerate their Nginx configs.\n")
  14. console.log("Usage: ./regenerate-config [-h|--help] [-y|--yes] [--dry-run]\n");
  15. process.exit(0);
  16. }
  17. // ask for the user to confirm the action if not in unattended mode
  18. if (!UNATTENDED && !DRY_RUN) {
  19. const readline = await import("node:readline");
  20. const rl = readline.createInterface({
  21. input: process.stdin,
  22. output: process.stdout,
  23. });
  24. const question = (query) =>
  25. new Promise((resolve) => rl.question(query, resolve));
  26. const answer = await question(
  27. "This will iterate over all Hosts and regnerate their Nginx configs.\n\nAre you sure you want to proceed? (y/N) ",
  28. );
  29. rl.close();
  30. if (answer.toLowerCase() !== "y") {
  31. console.log("Aborting.");
  32. process.exit(0);
  33. }
  34. }
  35. const logIt = (msg, type = "info") => logger[type](
  36. `${DRY_RUN ? '[DRY RUN] ' : ''}${msg}`,
  37. );
  38. // Let's do it.
  39. const processItems = async (model, type) => {
  40. const rows = await model
  41. .query()
  42. .where("is_deleted", 0)
  43. .andWhere("enabled", 1)
  44. .groupBy("id")
  45. .allowGraph(model.defaultAllowGraph)
  46. .withGraphFetched(`[${model.defaultExpand.join(", ")}]`)
  47. .orderBy(...model.defaultOrder);
  48. logIt(`[${type}] Found ${rows.length} rows to process...`);
  49. for (const row of rows) {
  50. if (!DRY_RUN) {
  51. logIt(`[${type}] Regenerating config #${row.id}: ${row.domain_names ? row.domain_names.join(", ") : 'port ' + row.incoming_port}`);
  52. await internalNginx.configure(proxyHostModel, "proxy_host", row);
  53. } else {
  54. logIt(`[${type}] Skipping generation of config #${row.id}: ${row.domain_names ? row.domain_names.join(", ") : 'port ' + row.incoming_port}`);
  55. }
  56. }
  57. };
  58. await processItems(proxyHostModel, "Proxy Host");
  59. await processItems(redirectionHostModel, "Redirection Host");
  60. await processItems(deadHostModel, "404 Host");
  61. await processItems(streamModel, "Stream");
  62. logIt("Completed", "success");
  63. process.exit(0);