oneuptime/Nginx/Utils/NginxConfigurator.ts
Nawaz Dhandala e31417c5bf
Some checks are pending
Compile / compile-home (push) Waiting to run
Compile / compile-admin-dashboard (push) Waiting to run
Compile / compile-dashboard (push) Waiting to run
Compile / compile-e2e (push) Waiting to run
Compile / compile-probe (push) Waiting to run
Compile / compile-probe-ingest (push) Waiting to run
Compile / compile-server-monitor-ingest (push) Waiting to run
Compile / compile-open-telemetry-ingest (push) Waiting to run
Compile / compile-incoming-request-ingest (push) Waiting to run
Compile / compile-status-page (push) Waiting to run
Compile / compile-test-server (push) Waiting to run
Compile / compile-mcp (push) Waiting to run
OpenAPI Spec Generation / generate-openapi-spec (push) Waiting to run
Build / docker-build-otel-collector (push) Waiting to run
Build / docker-build-app (push) Waiting to run
Build / docker-build-copilot (push) Waiting to run
Build / docker-build-e2e (push) Waiting to run
Build / docker-build-accounts (push) Waiting to run
Build / docker-build-isolated-vm (push) Waiting to run
Build / docker-build-home (push) Waiting to run
Build / docker-build-worker (push) Waiting to run
Build / docker-build-workflow (push) Waiting to run
Build / docker-build-api-reference (push) Waiting to run
Build / docker-build-docs (push) Waiting to run
Build / docker-build-admin-dashboard (push) Waiting to run
Build / docker-build-dashboard (push) Waiting to run
Build / docker-build-probe (push) Waiting to run
Build / docker-build-probe-ingest (push) Waiting to run
Build / docker-build-server-monitor-ingest (push) Waiting to run
Build / docker-build-open-telemetry-ingest (push) Waiting to run
Build / docker-build-incoming-request-ingest (push) Waiting to run
Build / docker-build-fluent-ingest (push) Waiting to run
Build / docker-build-status-page (push) Waiting to run
Build / docker-build-test-server (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
Common Jobs / helm-lint (push) Waiting to run
Common Jobs / js-lint (push) Waiting to run
Compile / compile-worker (push) Waiting to run
Compile / compile-workflow (push) Waiting to run
Compile / compile-api-reference (push) Waiting to run
Compile / compile-docs-reference (push) Waiting to run
Compile / compile-copilot (push) Waiting to run
Compile / compile-nginx (push) Waiting to run
Compile / compile-infrastructure-agent (push) Waiting to run
Compile / compile-fluent-ingest (push) Waiting to run
Compile / compile-accounts (push) Waiting to run
Compile / compile-isolated-vm (push) Waiting to run
Compile / compile-common (push) Waiting to run
Compile / compile-app (push) Waiting to run
Push Test Images to Docker Hub and GitHub Container Registry / home-docker-image-deploy (push) Blocked by required conditions
Push Test Images to Docker Hub and GitHub Container Registry / status-page-docker-image-deploy (push) Blocked by required conditions
Push Test Images to Docker Hub and GitHub Container Registry / test-docker-image-deploy (push) Blocked by required conditions
Push Test Images to Docker Hub and GitHub Container Registry / probe-ingest-docker-image-deploy (push) Blocked by required conditions
Push Test Images to Docker Hub and GitHub Container Registry / server-monitor-ingest-docker-image-deploy (push) Blocked by required conditions
Push Test Images to Docker Hub and GitHub Container Registry / incoming-request-ingest-docker-image-deploy (push) Blocked by required conditions
Push Test Images to Docker Hub and GitHub Container Registry / open-telemetry-ingest-docker-image-deploy (push) Blocked by required conditions
Push Test Images to Docker Hub and GitHub Container Registry / fluent-ingest-docker-image-deploy (push) Blocked by required conditions
Push Test Images to Docker Hub and GitHub Container Registry / probe-docker-image-deploy (push) Blocked by required conditions
Push Test Images to Docker Hub and GitHub Container Registry / dashboard-docker-image-deploy (push) Blocked by required conditions
Push Test Images to Docker Hub and GitHub Container Registry / admin-dashboard-docker-image-deploy (push) Blocked by required conditions
Push Test Images to Docker Hub and GitHub Container Registry / app-docker-image-deploy (push) Blocked by required conditions
Push Test Images to Docker Hub and GitHub Container Registry / api-reference-docker-image-deploy (push) Blocked by required conditions
OneUptime Reliability Copilot / Analyze Code (push) Waiting to run
Terraform Provider Generation / generate-terraform-provider (push) Waiting to run
Push Test Images to Docker Hub and GitHub Container Registry / publish-mcp-server (push) Blocked by required conditions
Push Test Images to Docker Hub and GitHub Container Registry / test-e2e-test-saas (push) Blocked by required conditions
Push Test Images to Docker Hub and GitHub Container Registry / generate-build-number (push) Waiting to run
Push Test Images to Docker Hub and GitHub Container Registry / read-version (push) Waiting to run
Push Test Images to Docker Hub and GitHub Container Registry / publish-terraform-provider (push) Blocked by required conditions
Push Test Images to Docker Hub and GitHub Container Registry / llm-docker-image-deploy (push) Blocked by required conditions
Push Test Images to Docker Hub and GitHub Container Registry / nginx-docker-image-deploy (push) Blocked by required conditions
Push Test Images to Docker Hub and GitHub Container Registry / e2e-docker-image-deploy (push) Blocked by required conditions
Push Test Images to Docker Hub and GitHub Container Registry / test-server-docker-image-deploy (push) Blocked by required conditions
Push Test Images to Docker Hub and GitHub Container Registry / otel-collector-docker-image-deploy (push) Blocked by required conditions
Push Test Images to Docker Hub and GitHub Container Registry / isolated-vm-docker-image-deploy (push) Blocked by required conditions
Push Test Images to Docker Hub and GitHub Container Registry / accounts-docker-image-deploy (push) Blocked by required conditions
Push Test Images to Docker Hub and GitHub Container Registry / worker-docker-image-deploy (push) Blocked by required conditions
Push Test Images to Docker Hub and GitHub Container Registry / copilot-docker-image-deploy (push) Blocked by required conditions
Push Test Images to Docker Hub and GitHub Container Registry / workflow-docker-image-deploy (push) Blocked by required conditions
Push Test Images to Docker Hub and GitHub Container Registry / docs-docker-image-deploy (push) Blocked by required conditions
Push Test Images to Docker Hub and GitHub Container Registry / test-helm-chart (push) Blocked by required conditions
Push Test Images to Docker Hub and GitHub Container Registry / test-e2e-test-self-hosted (push) Blocked by required conditions
Push Test Images to Docker Hub and GitHub Container Registry / infrastructure-agent-deploy (push) Blocked by required conditions
Common Test / test (push) Waiting to run
Fluent Ingest Test / test (push) Waiting to run
Incoming Request Ingest Test / test (push) Waiting to run
MCP Server Test / test (push) Waiting to run
OpenTelemetryIngest Test / test (push) Waiting to run
ProbeIngest Test / test (push) Waiting to run
Probe Test / test (push) Waiting to run
Tests / test-app (push) Waiting to run
Tests / test-home (push) Waiting to run
Tests / test-worker (push) Waiting to run
style(exec, nginx, docs): tidy formatting and indentation across Execute.ts, NginxConfigurator.ts, and Telemetry Documentation
2025-11-06 19:41:57 +00:00

117 lines
3.5 KiB
TypeScript

import Exec from "Common/Server/Utils/Execute";
import LocalFile from "Common/Server/Utils/LocalFile";
import logger from "Common/Server/Utils/Logger";
export interface EnsurePrimarySslOptions {
hostname: string;
forceReload?: boolean;
}
export default class NginxConfigurator {
private static readonly DEFAULT_CONF_PATH: string =
"/etc/nginx/conf.d/default.conf";
private static readonly ENVSUBST_SCRIPT_PATH: string =
"/etc/nginx/envsubst-on-templates.sh";
private static readonly NGINX_LOG_DIRECTORY: string = "/var/log/nginx";
private static readonly NGINX_ACCESS_LOG_PATH: string = `${NginxConfigurator.NGINX_LOG_DIRECTORY}/access.log`;
private static readonly NGINX_ERROR_LOG_PATH: string = `${NginxConfigurator.NGINX_LOG_DIRECTORY}/error.log`;
private static async ensureLogFiles(): Promise<void> {
try {
await LocalFile.makeDirectory(this.NGINX_LOG_DIRECTORY);
const accessLogExists: boolean = await LocalFile.doesFileExist(
this.NGINX_ACCESS_LOG_PATH,
);
if (!accessLogExists) {
await LocalFile.write(this.NGINX_ACCESS_LOG_PATH, "");
}
const errorLogExists: boolean = await LocalFile.doesFileExist(
this.NGINX_ERROR_LOG_PATH,
);
if (!errorLogExists) {
await LocalFile.write(this.NGINX_ERROR_LOG_PATH, "");
}
} catch (err) {
logger.error(
"[NginxConfigurator] Failed to ensure nginx log files exist before reload.",
);
logger.error(err);
throw err;
}
}
public static async ensurePrimarySslConfigured(
options: EnsurePrimarySslOptions,
): Promise<void> {
const normalizedHost: string = options.hostname.trim().toLowerCase();
if (!normalizedHost) {
logger.warn(
"[NginxConfigurator] Cannot configure SSL because hostname is empty.",
);
return;
}
const certificateDirective: string = `ssl_certificate /etc/nginx/certs/ServerCerts/${normalizedHost}.crt;`;
let nginxConfig: string = "";
try {
nginxConfig = await LocalFile.read(this.DEFAULT_CONF_PATH);
} catch (err) {
logger.debug(
`[NginxConfigurator] Unable to read ${this.DEFAULT_CONF_PATH}; regenerating configuration.`,
);
logger.debug(err);
}
const templateHasDirective: boolean =
nginxConfig.includes(certificateDirective);
const shouldRefreshTemplate: boolean = !templateHasDirective;
const shouldReload: boolean =
options.forceReload === true || shouldRefreshTemplate;
if (!shouldReload) {
return;
}
const originalPrimaryDomain: string | undefined =
process.env["PRIMARY_DOMAIN"];
try {
process.env["PRIMARY_DOMAIN"] = normalizedHost;
if (shouldRefreshTemplate) {
await Exec.executeCommand(this.ENVSUBST_SCRIPT_PATH);
}
await this.ensureLogFiles();
await Exec.executeCommandInheritStdio({
command: "nginx",
args: ["-t", "-c", "/etc/nginx/nginx.conf"],
});
await Exec.executeCommandInheritStdio({
command: "nginx",
args: ["-s", "reload"],
});
logger.info(
`[NginxConfigurator] Reloaded nginx after updating certificate for ${normalizedHost}.`,
);
} catch (err) {
logger.error(
"[NginxConfigurator] Failed to reload nginx after certificate update.",
);
logger.error(err);
throw err;
} finally {
if (originalPrimaryDomain !== undefined) {
process.env["PRIMARY_DOMAIN"] = originalPrimaryDomain;
} else {
delete process.env["PRIMARY_DOMAIN"];
}
}
}
}