From d27611d035f5b0ff5e5bc8ae262550d9c75b9c8c Mon Sep 17 00:00:00 2001 From: Blake Matthes Date: Wed, 2 Apr 2025 21:26:32 -0500 Subject: [PATCH] feat(servers): all servers in v2 meow --- .../controller/server/serviceControl.ts | 90 +++++++++++++++++++ .../server/route/servers/serverContorl.ts | 66 ++++++++++++++ server/services/server/systemServer.ts | 2 + server/services/server/utils/serverData.json | 75 +++++++++------- server/services/server/utils/serverData.ts | 57 +++++++----- 5 files changed, 233 insertions(+), 57 deletions(-) create mode 100644 server/services/server/controller/server/serviceControl.ts create mode 100644 server/services/server/route/servers/serverContorl.ts diff --git a/server/services/server/controller/server/serviceControl.ts b/server/services/server/controller/server/serviceControl.ts new file mode 100644 index 0000000..9487b73 --- /dev/null +++ b/server/services/server/controller/server/serviceControl.ts @@ -0,0 +1,90 @@ +import { spawn } from "child_process"; +import { eq } from "drizzle-orm"; +import { tryCatch } from "../../../../globalUtils/tryCatch.js"; +import { db } from "../../../../../database/dbclient.js"; +import { settings } from "../../../../../database/schema/settings.js"; +import { createLog } from "../../../logger/logger.js"; +import { serverData } from "../../../../../database/schema/serverData.js"; +import os from "os"; + +export const serviceControl = async ( + plantToken: string, + processType: string, + remote: string | null +) => { + const { data: serverInfo, error: serverError } = await tryCatch( + db + .select() + .from(serverData) + .where(eq(serverData.plantToken, plantToken)) + ); + + if (serverError) { + return createLog( + "error", + "lst", + "serverUpdater", + `Error getting the server settings` + ); + } + + let scriptPath = `${serverInfo[0].serverLoc}\\dist\\server\\scripts\\services.ps1`; + if (os.hostname() != serverInfo[0].serverDNS) { + scriptPath = `${process.env.DEVFOLDER}\\dist\\server\\scripts\\services.ps1`; + } + + const args = [ + "-NoProfile", + "-ExecutionPolicy", + "Bypass", + "-File", + scriptPath, + "-serviceName", + "LSTV2", + "-option", + processType, + "-appPath", + serverInfo[0].serverLoc as string, + "-remote", + remote ?? "", + ]; + const scriptProcess = spawn("powershell", args); + + // Collect stdout data + scriptProcess.stdout.on("data", (data) => { + const output = data.toString().trim(); + createLog("info", "lst", "serverUpdater", `${output}`); + //onData(output); + }); + + // Collect stderr data + scriptProcess.stderr.on("data", (data) => { + const output = data.toString().trim(); + createLog("info", "lst", "serverUpdater", `${output}`); + //onData(output); + }); + + // Handle process close + scriptProcess.on("close", async (code) => { + if (code === 0) { + // if (count >= servers) { + // //onClose(`Server completed with code: ${code}`); + // } + createLog("info", "lst", "serverUpdater", `Finished setting perms`); + + //update the last build. + } else { + const errorMessage = `Process exited with code ${code}`; + + // if (count >= servers) { + // //onClose(code); + // } + } + }); + + // Handle errors with the process itself + scriptProcess.on("error", (error) => { + //onError(err.message); + createLog("error", "lst", "serverUpdater", `${error}`); + }); +}; diff --git a/server/services/server/route/servers/serverContorl.ts b/server/services/server/route/servers/serverContorl.ts new file mode 100644 index 0000000..e8e8796 --- /dev/null +++ b/server/services/server/route/servers/serverContorl.ts @@ -0,0 +1,66 @@ +import { z, createRoute, OpenAPIHono } from "@hono/zod-openapi"; +import { authMiddleware } from "../../../auth/middleware/authMiddleware.js"; +import { responses } from "../../../../globalUtils/routeDefs/responses.js"; +import { tryCatch } from "../../../../globalUtils/tryCatch.js"; +import hasCorrectRole from "../../../auth/middleware/roleCheck.js"; +import { serviceControl } from "../../controller/server/serviceControl.js"; + +// Define the request body schema +const requestSchema = z.object({ + processType: z.string().openapi({ example: "restart" }), + plantToken: z.string().openapi({ example: "usday1" }), + remote: z.string().optional().openapi({ example: "true" }), +}); + +const app = new OpenAPIHono(); + +app.openapi( + createRoute({ + tags: ["server"], + summary: "Starts, Stops, Restarts the server.", + method: "post", + path: "/serviceprocess", + middleware: [authMiddleware, hasCorrectRole(["systemAdmin"], "admin")], + + request: { + body: { + content: { + "application/json": { schema: requestSchema }, + }, + }, + }, + responses: responses(), + }), + async (c) => { + const { data, error } = await tryCatch(c.req.json()); + + if (error) { + return c.json({ + success: false, + message: "Error with the request body.", + }); + } + + const { data: process, error: processError } = await tryCatch( + serviceControl( + data.plantToken, + data.processType!, + data.remote ?? "" + ) + ); + + if (processError) { + return c.json({ + success: false, + message: "There was an error running the service type", + }); + } + + return c.json({ + success: true, + message: `The service was ${data.processType}`, + }); + } +); + +export default app; diff --git a/server/services/server/systemServer.ts b/server/services/server/systemServer.ts index 2b5a074..2f92f54 100644 --- a/server/services/server/systemServer.ts +++ b/server/services/server/systemServer.ts @@ -13,6 +13,7 @@ import { serversCheckPoint } from "./utils/serverData.js"; import getServers from "./route/servers/getServers.js"; import updateServer from "./route/updates/updateServer.js"; import { setPerms } from "./utils/testServerPerms.js"; +import serviceControl from './route/servers/serverContorl.js' // making sure all modules are in properly setTimeout(async () => { @@ -35,6 +36,7 @@ const routes = [ // serverData getServers, updateServer, + serviceControl ] as const; // app.route("/server", modules); diff --git a/server/services/server/utils/serverData.json b/server/services/server/utils/serverData.json index eed81c6..363a8cc 100644 --- a/server/services/server/utils/serverData.json +++ b/server/services/server/utils/serverData.json @@ -18,7 +18,7 @@ "oldVersion": "E:\\LST\\lst_backend", "shippingHours": "[{\"early\": \"06:30\", \"late\": \"23:00\"}]", "tiPostTime": "[{\"from\": \"24\", \"to\": \"24\"}]", - "otherSettings": [{ "specialInstructions": "something for ti" }] + "otherSettings": [{ "specialInstructions": "" }] }, { "sName": "Bethlehem", @@ -38,7 +38,7 @@ "oldVersion": "E:\\LST\\lst_backend", "shippingHours": "[{\"early\": \"06:30\", \"late\": \"23:00\"}]", "tiPostTime": "[{\"from\": \"24\", \"to\": \"24\"}]", - "otherSettings": [{ "specialInstructions": "something for ti" }] + "otherSettings": [{ "specialInstructions": "" }] }, { "sName": "Huston", @@ -58,7 +58,7 @@ "oldVersion": "E:\\LST\\lst_backend", "shippingHours": "[{\"early\": \"06:30\", \"late\": \"23:00\"}]", "tiPostTime": "[{\"from\": \"24\", \"to\": \"24\"}]", - "otherSettings": [{ "specialInstructions": "something for ti" }] + "otherSettings": [{ "specialInstructions": "" }] }, { "sName": "Bowling Green 1", @@ -69,16 +69,20 @@ "streetAddress": "215 Technology Way", "cityState": "Bowling Green, KY", "zipcode": "42101", - "contactEmail": "blake.matthes@alpla.com", - "contactPhone": "6366970253", + "contactEmail": "ShippingReceivingBowlingGreen1@groups.alpla.com", + "contactPhone": "(270) 495-6647", "customerTiAcc": "ALPL01BG1INT", "lstServerPort": "4000", "active": true, "serverLoc": "E:\\LST\\lstv2", "oldVersion": "E:\\LST\\lst_backend", - "shippingHours": "[{\"early\": \"06:30\", \"late\": \"23:00\"}]", + "shippingHours": "[{\"early\": \"00:00\", \"late\": \"23:00\"}]", "tiPostTime": "[{\"from\": \"24\", \"to\": \"24\"}]", - "otherSettings": [{ "specialInstructions": "something for ti" }] + "otherSettings": [ + { + "specialInstructions": "Please be sure to schedule a pick up appointment and bring 2 load bars to secure the load." + } + ] }, { "sName": "Iowa ISBM", @@ -93,12 +97,12 @@ "contactPhone": "6366970253", "customerTiAcc": "ALPL01IA2INT", "lstServerPort": "4001", - "active": false, - "serverLoc": "E:\\LST\\lstv2", - "oldVersion": "E:\\LST\\lst_backend_2", + "active": true, + "serverLoc": "D:\\LST\\lstv2_2", + "oldVersion": "D:\\LST\\lst_backend_2", "shippingHours": "[{\"early\": \"06:30\", \"late\": \"23:00\"}]", "tiPostTime": "[{\"from\": \"24\", \"to\": \"24\"}]", - "otherSettings": [{ "specialInstructions": "something for ti" }] + "otherSettings": [{ "specialInstructions": "" }] }, { "sName": "Kansas City", @@ -113,12 +117,12 @@ "contactPhone": "6366970253", "customerTiAcc": "ALPL01KCINT", "lstServerPort": "4000", - "active": false, + "active": true, "serverLoc": "E:\\LST\\lstv2", "oldVersion": "E:\\LST\\lst_backend", "shippingHours": "[{\"early\": \"06:30\", \"late\": \"23:00\"}]", "tiPostTime": "[{\"from\": \"24\", \"to\": \"24\"}]", - "otherSettings": [{ "specialInstructions": "something for ti" }] + "otherSettings": [{ "specialInstructions": "" }] }, { "sName": "Bowling Green 2", @@ -138,7 +142,7 @@ "oldVersion": "E:\\LST\\lst_backend", "shippingHours": "[{\"early\": \"06:30\", \"late\": \"23:00\"}]", "tiPostTime": "[{\"from\": \"24\", \"to\": \"24\"}]", - "otherSettings": [{ "specialInstructions": "something for ti" }] + "otherSettings": [{ "specialInstructions": "" }] }, { "sName": "MCDonough", @@ -158,7 +162,7 @@ "oldVersion": "E:\\LST\\lst_backend", "shippingHours": "[{\"early\": \"06:30\", \"late\": \"23:00\"}]", "tiPostTime": "[{\"from\": \"24\", \"to\": \"24\"}]", - "otherSettings": [{ "specialInstructions": "something for ti" }] + "otherSettings": [{ "specialInstructions": "" }] }, { "sName": "Dayton", @@ -178,7 +182,7 @@ "oldVersion": "E:\\LST\\lst_backend", "shippingHours": "[{\"early\": \"06:30\", \"late\": \"23:00\"}]", "tiPostTime": "[{\"from\": \"24\", \"to\": \"24\"}]", - "otherSettings": [{ "specialInstructions": "something for ti" }] + "otherSettings": [{ "specialInstructions": "" }] }, { "sName": "Salt Lake City", @@ -189,16 +193,16 @@ "streetAddress": "4324 Commercial Way Suite A", "cityState": "Salt Lake City, UT", "zipcode": "84104", - "contactEmail": "blake.matthes@alpla.com", - "contactPhone": "6366970253", + "contactEmail": "ShippingReceivingSaltLake@groups.alpla.com", + "contactPhone": "801-673-2143", "customerTiAcc": "ALPL01SLCINT", "lstServerPort": "4000", - "active": false, + "active": true, "serverLoc": "E:\\LST\\lstv2", "oldVersion": "E:\\LST\\lst_backend", - "shippingHours": "[{\"early\": \"06:30\", \"late\": \"23:00\"}]", + "shippingHours": "[{\"early\": \"07:00\", \"late\": \"17:00\"}]", "tiPostTime": "[{\"from\": \"24\", \"to\": \"24\"}]", - "otherSettings": [{ "specialInstructions": "something for ti" }] + "otherSettings": [{ "specialInstructions": "Copy of bol" }] }, { "sName": "Lima", @@ -218,7 +222,7 @@ "oldVersion": "E:\\LST\\lst_backend", "shippingHours": "[{\"early\": \"06:30\", \"late\": \"23:00\"}]", "tiPostTime": "[{\"from\": \"24\", \"to\": \"24\"}]", - "otherSettings": [{ "specialInstructions": "something for ti" }] + "otherSettings": [{ "specialInstructions": "" }] }, { "sName": "Florence", @@ -233,12 +237,12 @@ "contactPhone": "6366970253", "customerTiAcc": "ALPL01FLORINT", "lstServerPort": "4000", - "active": false, + "active": true, "serverLoc": "E:\\LST\\lstv2", "oldVersion": "E:\\LST\\lst_backend", "shippingHours": "[{\"early\": \"06:30\", \"late\": \"23:00\"}]", "tiPostTime": "[{\"from\": \"24\", \"to\": \"24\"}]", - "otherSettings": [{ "specialInstructions": "something for ti" }] + "otherSettings": [{ "specialInstructions": "" }] }, { "sName": "Iowa EBM", @@ -253,12 +257,12 @@ "contactPhone": "6366970253", "customerTiAcc": "ALPL01IA1INT", "lstServerPort": "4000", - "active": false, - "serverLoc": "E:\\LST\\lstv2", - "oldVersion": "E:\\LST\\lst_backend", + "active": true, + "serverLoc": "D:\\LST\\lstv2", + "oldVersion": "D:\\LST\\lst_backend", "shippingHours": "[{\"early\": \"06:30\", \"late\": \"23:00\"}]", "tiPostTime": "[{\"from\": \"24\", \"to\": \"24\"}]", - "otherSettings": [{ "specialInstructions": "something for ti" }] + "otherSettings": [{ "specialInstructions": "" }] }, { "sName": "Jefferson city", @@ -278,7 +282,7 @@ "oldVersion": "E:\\LST\\lst_backend", "shippingHours": "[{\"early\": \"06:30\", \"late\": \"23:00\"}]", "tiPostTime": "[{\"from\": \"24\", \"to\": \"24\"}]", - "otherSettings": [{ "specialInstructions": "something for ti" }] + "otherSettings": [{ "specialInstructions": "" }] }, { "sName": "Sherman", @@ -298,7 +302,7 @@ "oldVersion": "E:\\LST\\lst_backend", "shippingHours": "[{\"early\": \"06:30\", \"late\": \"23:00\"}]", "tiPostTime": "[{\"from\": \"24\", \"to\": \"24\"}]", - "otherSettings": [{ "specialInstructions": "something for ti" }] + "otherSettings": [{ "specialInstructions": "" }] }, { "sName": "West Bend", @@ -313,13 +317,16 @@ "contactPhone": "262-808-4211", "customerTiAcc": "ALPL01WBINT", "lstServerPort": "4000", - "active": false, + "active": true, "serverLoc": "E:\\LST\\lstv2", "oldVersion": "E:\\LST\\lst_backend", "shippingHours": "[{\"early\": \"06:30\", \"late\": \"23:00\"}]", "tiPostTime": "[{\"from\": \"24\", \"to\": \"24\"}]", "otherSettings": [ - { "specialInstructions": "something for ti", "active": false } + { + "specialInstructions": "This is a FTL load. The driver will need 2 adjustable load locks to secure the load. The driver will not be loaded without them. Please reference ALPLA pickup [header]", + "active": false + } ] }, { @@ -335,12 +342,12 @@ "contactPhone": "6366970253", "customerTiAcc": "ALPL01STPINT", "lstServerPort": "4000", - "active": false, + "active": true, "serverLoc": "E:\\LST\\lstv2", "oldVersion": "E:\\LST\\lst_backend", "shippingHours": "[{\"early\": \"06:30\", \"late\": \"23:00\"}]", "tiPostTime": "[{\"from\": \"24\", \"to\": \"24\"}]", - "otherSettings": [{ "specialInstructions": "something for ti" }] + "otherSettings": [{ "specialInstructions": "" }] } ] } diff --git a/server/services/server/utils/serverData.ts b/server/services/server/utils/serverData.ts index f14d046..c12a79a 100644 --- a/server/services/server/utils/serverData.ts +++ b/server/services/server/utils/serverData.ts @@ -1,9 +1,9 @@ // This will help maintain the server db so when we run an update it will show up here all the time. // kinda bad too but this will help us keep the db identical. -import {db} from "../../../../database/dbclient.js"; -import {serverData} from "../../../../database/schema/serverData.js"; -import {createLog} from "../../logger/logger.js"; +import { db } from "../../../../database/dbclient.js"; +import { serverData } from "../../../../database/schema/serverData.js"; +import { createLog } from "../../logger/logger.js"; import fs from "fs"; export const serversCheckPoint = async () => { @@ -28,27 +28,38 @@ export const serversCheckPoint = async () => { try { for (let i = 0; i < servers.length; i++) { const serverUpdate = await db - .insert(serverData) - .values(servers[i]) - .onConflictDoUpdate({ - target: serverData.plantToken, - set: { - sName: servers[i].sName, - serverDNS: servers[i].serverDNS, - active: servers[i].active, - contactEmail: servers[i].contactEmail, - contactPhone: servers[i].contactPhone, - shippingHours: servers[i].shippingHours, - customerTiAcc: servers[i].customerTiAcc, - oldVersion: servers[i].oldVersion, - tiPostTime: servers[i].tiPostTime, - otherSettings: servers[i].otherSettings, - }, - }) // this will only update the ones that are new :D - .returning({name: serverData.sName}); + .insert(serverData) + .values(servers[i]) + .onConflictDoUpdate({ + target: serverData.plantToken, + set: { + sName: servers[i].sName, + serverDNS: servers[i].serverDNS, + active: servers[i].active, + contactEmail: servers[i].contactEmail, + contactPhone: servers[i].contactPhone, + shippingHours: servers[i].shippingHours, + customerTiAcc: servers[i].customerTiAcc, + serverLoc: servers[i].serverLoc, + oldVersion: servers[i].oldVersion, + tiPostTime: servers[i].tiPostTime, + otherSettings: servers[i].otherSettings, + }, + }) // this will only update the ones that are new :D + .returning({ name: serverData.sName }); } - createLog("info", "lst", "server", "Servers were just added/updated due to server startup"); + createLog( + "info", + "lst", + "server", + "Servers were just added/updated due to server startup" + ); } catch (error) { - createLog("error", "lst", "server", `There was an error adding/updating serverData to the db, ${error}`); + createLog( + "error", + "lst", + "server", + `There was an error adding/updating serverData to the db, ${error}` + ); } };