mirror of
https://github.com/fosrl/pangolin.git
synced 2026-05-25 10:12:35 +00:00
Add logging when manually changing the hc status
This commit is contained in:
@@ -6,7 +6,7 @@ import {
|
||||
} from "@server/db";
|
||||
import { eq } from "drizzle-orm";
|
||||
import logger from "@server/logger";
|
||||
import { fireSiteOfflineAlert } from "@server/lib/alerts";
|
||||
import { fireSiteOfflineAlert } from "#dynamic/lib/alerts";
|
||||
|
||||
/**
|
||||
* Handles disconnecting messages from sites to show disconnected in the ui
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
import {
|
||||
db,
|
||||
newts,
|
||||
sites,
|
||||
targetHealthCheck,
|
||||
targets,
|
||||
statusHistory
|
||||
sites
|
||||
} from "@server/db";
|
||||
import { hasActiveConnections } from "#dynamic/routers/ws";
|
||||
import { eq, lt, isNull, and, or, ne, not, inArray } from "drizzle-orm";
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
import { z } from "zod";
|
||||
import { db } from "@server/db";
|
||||
import { db, statusHistory } from "@server/db";
|
||||
import {
|
||||
siteProvisioningKeys,
|
||||
siteProvisioningKeyOrg,
|
||||
@@ -223,6 +223,14 @@ export async function registerNewt(
|
||||
})
|
||||
.returning();
|
||||
|
||||
await trx.insert(statusHistory).values({
|
||||
entityType: "site",
|
||||
entityId: newSite.siteId,
|
||||
orgId: orgId,
|
||||
status: "offline",
|
||||
timestamp: Math.floor(Date.now() / 1000)
|
||||
});
|
||||
|
||||
newSiteId = newSite.siteId;
|
||||
|
||||
// Grant admin role access to the new site
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
import { z } from "zod";
|
||||
import { clients, db, exitNodes } from "@server/db";
|
||||
import { clients, db, exitNodes, statusHistory } from "@server/db";
|
||||
import { roles, userSites, sites, roleSites, Site, orgs } from "@server/db";
|
||||
import response from "@server/lib/response";
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
@@ -321,12 +321,7 @@ export async function createSite(
|
||||
const existingSite = await db
|
||||
.select()
|
||||
.from(sites)
|
||||
.where(
|
||||
and(
|
||||
eq(sites.niceId, niceId),
|
||||
eq(sites.orgId, orgId)
|
||||
)
|
||||
)
|
||||
.where(and(eq(sites.niceId, niceId), eq(sites.orgId, orgId)))
|
||||
.limit(1);
|
||||
|
||||
if (existingSite.length > 0) {
|
||||
@@ -344,7 +339,8 @@ export async function createSite(
|
||||
if (type == "newt") {
|
||||
[newSite] = await trx
|
||||
.insert(sites)
|
||||
.values({ // NOTE: NO SUBNET OR EXIT NODE ID PASSED IN HERE BECAUSE ITS NOW CHOSEN ON CONNECT
|
||||
.values({
|
||||
// NOTE: NO SUBNET OR EXIT NODE ID PASSED IN HERE BECAUSE ITS NOW CHOSEN ON CONNECT
|
||||
orgId,
|
||||
name,
|
||||
niceId: updatedNiceId!,
|
||||
@@ -354,6 +350,14 @@ export async function createSite(
|
||||
status: "approved"
|
||||
})
|
||||
.returning();
|
||||
|
||||
await trx.insert(statusHistory).values({
|
||||
entityType: "site",
|
||||
entityId: newSite.siteId,
|
||||
orgId: orgId,
|
||||
status: "offline",
|
||||
timestamp: Math.floor(Date.now() / 1000)
|
||||
});
|
||||
} else if (type == "wireguard") {
|
||||
// we are creating a site with an exit node (tunneled)
|
||||
if (!subnet) {
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
import { z } from "zod";
|
||||
import { db, TargetHealthCheck, targetHealthCheck } from "@server/db";
|
||||
import {
|
||||
db,
|
||||
statusHistory,
|
||||
TargetHealthCheck,
|
||||
targetHealthCheck
|
||||
} from "@server/db";
|
||||
import { newts, resources, sites, Target, targets } from "@server/db";
|
||||
import response from "@server/lib/response";
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
@@ -14,6 +19,7 @@ import { eq } from "drizzle-orm";
|
||||
import { pickPort } from "./helpers";
|
||||
import { isTargetValid } from "@server/lib/validators";
|
||||
import { OpenAPITags, registry } from "@server/openApi";
|
||||
import { fireHealthCheckHealthyAlert, fireHealthCheckUnhealthyAlert, fireHealthCheckUnknownAlert } from "#dynamic/lib/alerts";
|
||||
|
||||
const createTargetParamsSchema = z.strictObject({
|
||||
resourceId: z.string().transform(Number).pipe(z.int().positive())
|
||||
@@ -252,6 +258,36 @@ export async function createTarget(
|
||||
})
|
||||
.returning();
|
||||
|
||||
if (healthCheck[0].hcHealth === "unhealthy") {
|
||||
await fireHealthCheckUnhealthyAlert(
|
||||
healthCheck[0].orgId,
|
||||
healthCheck[0].targetHealthCheckId,
|
||||
healthCheck[0].name,
|
||||
undefined,
|
||||
undefined,
|
||||
false // dont send the alert because we just want to create the alert, not notify users yet
|
||||
);
|
||||
} else if (healthCheck[0].hcHealth === "unknown") {
|
||||
// if the health is unknown, we want to fire an alert to notify users to enable health checks
|
||||
await fireHealthCheckUnknownAlert(
|
||||
healthCheck[0].orgId,
|
||||
healthCheck[0].targetHealthCheckId,
|
||||
healthCheck[0].name,
|
||||
undefined,
|
||||
undefined,
|
||||
false // dont send the alert because we just want to create the alert, not notify users yet
|
||||
);
|
||||
} else if (healthCheck[0].hcHealth === "healthy") {
|
||||
await fireHealthCheckHealthyAlert(
|
||||
healthCheck[0].orgId,
|
||||
healthCheck[0].targetHealthCheckId,
|
||||
healthCheck[0].name,
|
||||
undefined,
|
||||
undefined,
|
||||
false // dont send the alert because we just want to create the alert, not notify users yet
|
||||
);
|
||||
}
|
||||
|
||||
if (site.pubKey) {
|
||||
if (site.type == "wireguard") {
|
||||
await addPeer(site.exitNodeId!, {
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
import {
|
||||
db,
|
||||
targets,
|
||||
resources,
|
||||
sites,
|
||||
targetHealthCheck,
|
||||
statusHistory
|
||||
targetHealthCheck
|
||||
} from "@server/db";
|
||||
import { MessageHandler } from "@server/routers/ws";
|
||||
import { Newt } from "@server/db";
|
||||
@@ -142,6 +138,7 @@ export const handleHealthcheckStatusMessage: MessageHandler = async (
|
||||
targetCheck.name ?? undefined,
|
||||
targetCheck.targetId,
|
||||
undefined,
|
||||
true,
|
||||
trx
|
||||
);
|
||||
} else if (healthStatus.status === "healthy") {
|
||||
@@ -151,6 +148,7 @@ export const handleHealthcheckStatusMessage: MessageHandler = async (
|
||||
targetCheck.name ?? undefined,
|
||||
targetCheck.targetId,
|
||||
undefined,
|
||||
true,
|
||||
trx
|
||||
);
|
||||
}
|
||||
|
||||
@@ -10,10 +10,11 @@ import logger from "@server/logger";
|
||||
import { fromError } from "zod-validation-error";
|
||||
import { addPeer } from "../gerbil/peers";
|
||||
import { addTargets } from "../newt/targets";
|
||||
import { fireHealthCheckUnknownAlert } from "#dynamic/lib/alerts";
|
||||
import { fireHealthCheckHealthyAlert, fireHealthCheckUnknownAlert } from "#dynamic/lib/alerts";
|
||||
import { pickPort } from "./helpers";
|
||||
import { isTargetValid } from "@server/lib/validators";
|
||||
import { OpenAPITags, registry } from "@server/openApi";
|
||||
import { fireHealthCheckUnhealthyAlert } from "@server/lib/alerts";
|
||||
|
||||
|
||||
const updateTargetParamsSchema = z.strictObject({
|
||||
@@ -256,12 +257,33 @@ export async function updateTarget(
|
||||
.where(eq(targetHealthCheck.targetId, targetId))
|
||||
.returning();
|
||||
|
||||
if (isDisablingHc) {
|
||||
if (updatedHc.hcHealth === "unhealthy" && existingHc.hcHealth !== "unhealthy") {
|
||||
await fireHealthCheckUnhealthyAlert(
|
||||
updatedHc.orgId,
|
||||
updatedHc.targetHealthCheckId,
|
||||
updatedHc.name || "",
|
||||
undefined,
|
||||
undefined,
|
||||
false // dont send the alert because we just want to create the alert, not notify users yet
|
||||
);
|
||||
} else if (updatedHc.hcHealth === "unknown" && existingHc.hcHealth !== "unknown") {
|
||||
// if the health is unknown, we want to fire an alert to notify users to enable health checks
|
||||
await fireHealthCheckUnknownAlert(
|
||||
resource.orgId,
|
||||
existingHc.targetHealthCheckId,
|
||||
existingHc.name,
|
||||
updatedHc.targetId
|
||||
updatedHc.orgId,
|
||||
updatedHc.targetHealthCheckId,
|
||||
updatedHc.name,
|
||||
undefined,
|
||||
undefined,
|
||||
false // dont send the alert because we just want to create the alert, not notify users yet
|
||||
);
|
||||
} else if (updatedHc.hcHealth === "healthy" && existingHc.hcHealth !== "healthy") {
|
||||
await fireHealthCheckHealthyAlert(
|
||||
updatedHc.orgId,
|
||||
updatedHc.targetHealthCheckId,
|
||||
updatedHc.name,
|
||||
undefined,
|
||||
undefined,
|
||||
false // dont send the alert because we just want to create the alert, not notify users yet
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user