feat(update all): added a new function to update all servers in a row. easier to walk away

This commit is contained in:
2025-04-04 17:10:55 -05:00
parent ad5e77028d
commit 8b5698a839
5 changed files with 248 additions and 29 deletions

View File

@@ -12,7 +12,8 @@ type UpdateServerResponse = {
export const updateServer = async (
devApp: string,
server: string | null
server: string | null,
all?: boolean | null
): Promise<UpdateServerResponse> => {
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.`,
};
}

View File

@@ -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 {
processAllServers,
updateServer,
} from "../../../../scripts/updateServers.js";
// Define the request body schema
const requestSchema = z.object({
@@ -13,10 +16,16 @@ 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(),
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(),
roles: z
.string()
.openapi({ example: `["viewer","technician"]` })
.optional(),
});
const ParamsSchema = z.object({
@@ -33,6 +42,7 @@ const ParamsSchema = z.object({
});
const UpdateServer = z.object({
devDir: z.string().openapi({ example: "C:\\something\\Something" }),
all: z.boolean().optional().openapi({ example: true }),
});
const app = new OpenAPIHono();
@@ -72,6 +82,14 @@ app.openapi(
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);

View File

@@ -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);

View File

@@ -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

View File

@@ -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"
);
}
};