From 8b5698a8391260fcc400117562668434bbc9e8af Mon Sep 17 00:00:00 2001 From: Blake Matthes Date: Fri, 4 Apr 2025 17:10:55 -0500 Subject: [PATCH] feat(update all): added a new function to update all servers in a row. easier to walk away --- server/scripts/updateServers.ts | 27 ++- .../server/route/updates/updateServer.ts | 60 +++--- server/services/server/systemServer.ts | 6 +- server/services/server/utils/serverData.ts | 7 +- .../services/server/utils/subModuleCheck.ts | 177 ++++++++++++++++++ 5 files changed, 248 insertions(+), 29 deletions(-) create mode 100644 server/services/server/utils/subModuleCheck.ts diff --git a/server/scripts/updateServers.ts b/server/scripts/updateServers.ts index 15060c5..b0ec951 100644 --- a/server/scripts/updateServers.ts +++ b/server/scripts/updateServers.ts @@ -12,7 +12,8 @@ type UpdateServerResponse = { export const updateServer = async ( devApp: string, - server: string | null + server: string | null, + all?: boolean | null ): Promise => { const app = await getAppInfo(devApp); const serverInfo = await db @@ -34,7 +35,7 @@ export const updateServer = async ( }; } - if (serverInfo[0].isUpgrading) { + if (serverInfo[0].isUpgrading && !all) { createLog( "error", "lst", @@ -165,6 +166,9 @@ export const updateServer = async ( export async function processAllServers(devApp: string) { const servers = await db.select().from(serverData); + //change all servers to be upgrading + await db.update(serverData).set({ isUpgrading: true }); + createLog( "info", "lst", @@ -176,7 +180,8 @@ export async function processAllServers(devApp: string) { try { const updateToServer = await updateServer( devApp, - server.plantToken + server.plantToken, + true ); createLog( "info", @@ -186,7 +191,11 @@ export async function processAllServers(devApp: string) { ); count = count + 1; - //return {success: true, message: `${server.sName} was updated.`, data: updateToServer}; + // return { + // success: true, + // message: `${server.sName} was updated.`, + // data: updateToServer, + // }; } catch (error: any) { createLog( "info", @@ -194,7 +203,15 @@ export async function processAllServers(devApp: string) { "serverUpdater", `Error updating ${server.sName}: ${error.message}` ); - //return {success: false, message: `Error updating ${server.sName}: ${error.message}`}; + // return { + // success: false, + // message: `Error updating ${server.sName}: ${error.message}`, + // }; } } + + return { + success: true, + message: `All Servers are being updated this will take some time.`, + }; } diff --git a/server/services/server/route/updates/updateServer.ts b/server/services/server/route/updates/updateServer.ts index c323c27..d463995 100644 --- a/server/services/server/route/updates/updateServer.ts +++ b/server/services/server/route/updates/updateServer.ts @@ -1,6 +1,9 @@ -import {z, createRoute, OpenAPIHono} from "@hono/zod-openapi"; -import {authMiddleware} from "../../../auth/middleware/authMiddleware.js"; -import {updateServer} from "../../../../scripts/updateServers.js"; +import { z, createRoute, OpenAPIHono } from "@hono/zod-openapi"; +import { authMiddleware } from "../../../auth/middleware/authMiddleware.js"; +import { + processAllServers, + updateServer, +} from "../../../../scripts/updateServers.js"; // Define the request body schema const requestSchema = z.object({ @@ -13,26 +16,33 @@ const requestSchema = z.object({ // Define the response schema const responseSchema = z.object({ message: z.string().optional(), - module_id: z.string().openapi({example: "6c922c6c-7de3-4ec4-acb0-f068abdc"}).optional(), - name: z.string().openapi({example: "Production"}).optional(), - active: z.boolean().openapi({example: true}).optional(), - roles: z.string().openapi({example: `["viewer","technician"]`}).optional(), + module_id: z + .string() + .openapi({ example: "6c922c6c-7de3-4ec4-acb0-f068abdc" }) + .optional(), + name: z.string().openapi({ example: "Production" }).optional(), + active: z.boolean().openapi({ example: true }).optional(), + roles: z + .string() + .openapi({ example: `["viewer","technician"]` }) + .optional(), }); const ParamsSchema = z.object({ server: z - .string() - .min(3) - .openapi({ - param: { - name: "server", - in: "path", - }, - example: "usbow1", - }), + .string() + .min(3) + .openapi({ + param: { + name: "server", + in: "path", + }, + example: "usbow1", + }), }); const UpdateServer = z.object({ - devDir: z.string().openapi({example: "C:\\something\\Something"}), + devDir: z.string().openapi({ example: "C:\\something\\Something" }), + all: z.boolean().optional().openapi({ example: true }), }); const app = new OpenAPIHono(); @@ -48,30 +58,38 @@ app.openapi( params: ParamsSchema, body: { content: { - "application/json": {schema: UpdateServer}, + "application/json": { schema: UpdateServer }, }, }, }, responses: { 200: { content: { - "application/json": {schema: responseSchema}, + "application/json": { schema: responseSchema }, }, description: "Response message", }, 400: { content: { - "application/json": {schema: responseSchema}, + "application/json": { schema: responseSchema }, }, description: "Response message", }, }, }), async (c) => { - const {server} = c.req.valid("param"); + const { server } = c.req.valid("param"); const body = await c.req.json(); // fire off the update we wont make this way + if (body.all) { + const update = await processAllServers(body.devDir); + return c.json({ + success: update?.success ?? false, + message: update?.message, + data: [], + }); + } try { const update = await updateServer(body.devDir, server); diff --git a/server/services/server/systemServer.ts b/server/services/server/systemServer.ts index 2f92f54..1d3cd4f 100644 --- a/server/services/server/systemServer.ts +++ b/server/services/server/systemServer.ts @@ -13,7 +13,8 @@ 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' +import serviceControl from "./route/servers/serverContorl.js"; +import { areSubModulesIn } from "./utils/subModuleCheck.js"; // making sure all modules are in properly setTimeout(async () => { @@ -21,6 +22,7 @@ setTimeout(async () => { await areModulesIn(); await serversCheckPoint(); await setPerms(); + await areSubModulesIn(); }, 5000); const app = new OpenAPIHono(); @@ -36,7 +38,7 @@ const routes = [ // serverData getServers, updateServer, - serviceControl + serviceControl, ] as const; // app.route("/server", modules); diff --git a/server/services/server/utils/serverData.ts b/server/services/server/utils/serverData.ts index c12a79a..5751316 100644 --- a/server/services/server/utils/serverData.ts +++ b/server/services/server/utils/serverData.ts @@ -20,7 +20,12 @@ export const serversCheckPoint = async () => { const serverData = JSON.parse(data); servers = serverData.servers; } catch (err) { - console.error("Error reading JSON file:", err); + createLog( + "error", + "server", + "server", + `Error reading JSON file: ${JSON.stringify(err)}` + ); } // get the roles diff --git a/server/services/server/utils/subModuleCheck.ts b/server/services/server/utils/subModuleCheck.ts new file mode 100644 index 0000000..4c85466 --- /dev/null +++ b/server/services/server/utils/subModuleCheck.ts @@ -0,0 +1,177 @@ +/** + * check if the modules are in and if not add them. + * this will only run on a server start up + */ + +import { db } from "../../../../database/dbclient.js"; +import { subModules } from "../../../../database/schema/subModules.js"; +import { createLog } from "../../logger/logger.js"; +// "view", "technician", "supervisor","manager", "admin", "systemAdmin" +const newSubModules = [ + { + name: "Silo Adjustmnet", + moduleName: "logistics", + description: "Do a silo adjustmnet", + link: "/sa", + icon: "Cylinder", + active: false, + roles: ["tester", "systemAdmin"], + subSubModule: [], + }, + { + name: "Bulk orders", + moduleName: "logistics", + description: "", + link: "#", + icon: "Truck", + role: ["systemAdmin"], + active: false, + subSubModule: [], + }, + { + name: "Forecast", + moduleName: "logistics", + description: "", + link: "#", + icon: "Truck", + role: ["systemAdmin"], + active: false, + subSubModule: [], + }, + { + name: "Ocme cycle counts", + moduleName: "logistics", + description: "", + link: "#", + icon: "Package", + role: ["technician", "supervisor", "manager", "admin", "systemAdmin"], + active: false, + subSubModule: [], + }, + { + name: "Material Helper", + moduleName: "logistics", + description: "", + link: "/materialHelper/consumption", + icon: "Package", + role: ["technician", "supervisor", "manager", "admin", "systemAdmin"], + active: false, + subSubModule: [], + }, + { + name: "Ocme Cyclecount", + moduleName: "logistics", + description: "", + link: "/cyclecount", + icon: "Package", + role: ["technician", "supervisor", "manager", "admin", "systemAdmin"], + active: false, + subSubModule: [], + }, + + // admin module + { + name: "Servers", + moduleName: "admin", + description: "Do a silo adjustmnet", + link: "/servers", + icon: "Server", + roles: ["tester", "systemAdmin"], + isActive: true, + subSubModule: [], + }, + { + name: "Admin", + moduleName: "admin", + description: "Do a silo adjustmnet", + link: "#", // when link is # this will mean its a button + icon: "ShieldCheck", + active: true, + roles: ["tester", "systemAdmin"], + subSubModule: [ + { + name: "Settings", + link: "/settings", + icon: "Settings", + newWindow: false, + isActive: true, + }, + { + name: "Modules", + link: "/modules", + icon: "Settings", + newWindow: false, + isActive: false, + }, + { + name: "Swagger", + link: "#", + icon: "Webhook", + newWindow: false, + isActive: true, + }, + { + name: "Logs", + link: "#", + icon: "Logs", + newWindow: false, + isActive: false, + }, + { + name: "Users", + link: "/users", + icon: "Users", + newWindow: false, + isActive: true, + }, + { + name: "UCD", + link: "https://ucd.alpla.net:8443/", + icon: "Atom", + newWindow: false, + isActive: true, + }, + { + name: "Lst Api", + link: "/api/docs", + icon: "Webhook", + newWindow: false, + isActive: true, + }, + ], + }, +]; +export const areSubModulesIn = async () => { + try { + for (let i = 0; i < newSubModules.length; i++) { + const subModuleUpdate = await db + .insert(subModules) + .values(newSubModules[i]) + .onConflictDoUpdate({ + target: subModules.name, + set: { + name: newSubModules[i].name, + moduleName: newSubModules[i].moduleName, + description: newSubModules[i].description, + roles: newSubModules[i].roles, + link: newSubModules[i].link, + subSubModule: newSubModules[i].subSubModule, + }, + }) // this will only update the ones that are new :D + .returning({ name: subModules.name }); + } + createLog( + "info", + "lst", + "server", + "SubModules were just added due to missing them on server startup" + ); + } catch (error) { + createLog( + "error", + "lst", + "server", + "There was an error adding new subModules to the db" + ); + } +};