From 38b96da4e7f1aefadf99349111d5bf5c6c81efa8 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 29 Sep 2025 19:18:12 +0000 Subject: [PATCH] db write --- backend/db/redeployLogs.js | 20 ++++++++++++++ backend/index.js | 55 +++++++++++++++++++++++++++++++++++--- 2 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 backend/db/redeployLogs.js diff --git a/backend/db/redeployLogs.js b/backend/db/redeployLogs.js new file mode 100644 index 0000000..31efa33 --- /dev/null +++ b/backend/db/redeployLogs.js @@ -0,0 +1,20 @@ +import { db } from './index.js'; + +const insertRedeployLogStmt = db.prepare(` + INSERT INTO redeploy_logs (stack_id, stack_name, status, message, endpoint) + VALUES (@stackId, @stackName, @status, @message, @endpoint) +`); + +export function logRedeployEvent({ stackId, stackName, status, message = null, endpoint = null }) { + try { + insertRedeployLogStmt.run({ + stackId: String(stackId), + stackName: stackName ?? 'Unknown', + status, + message, + endpoint + }); + } catch (err) { + console.error('❌ Fehler beim Speichern des Redeploy-Logs:', err.message); + } +} diff --git a/backend/index.js b/backend/index.js index 50e9a2c..7d2f30d 100644 --- a/backend/index.js +++ b/backend/index.js @@ -7,6 +7,7 @@ import { Server } from 'socket.io'; import path from 'path'; import { fileURLToPath } from 'url'; import { db } from './db/index.js'; +import { logRedeployEvent } from './db/redeployLogs.js'; dotenv.config(); @@ -155,16 +156,25 @@ app.put('/api/stacks/:id/redeploy', async (req, res) => { const { id } = req.params; console.log(`🔄 PUT /api/stacks/${id}/redeploy: Redeploy gestartet`); + let stack; try { broadcastRedeployStatus(id, true); const stackRes = await axiosInstance.get(`/api/stacks/${id}`); - const stack = stackRes.data; + stack = stackRes.data; if (stack.EndpointId !== ENDPOINT_ID) { throw new Error(`Stack gehört nicht zum Endpoint ${ENDPOINT_ID}`); } + logRedeployEvent({ + stackId: stack.Id || id, + stackName: stack.Name, + status: 'started', + message: 'Redeploy gestartet', + endpoint: stack.EndpointId + }); + if (stack.Type === 1) { console.log(`🔄 [Redeploy] Git Stack "${stack.Name}" (${id}) wird redeployed`); await axiosInstance.put(`/api/stacks/${id}/git/redeploy?endpointId=${stack.EndpointId}`); @@ -195,12 +205,27 @@ app.put('/api/stacks/:id/redeploy', async (req, res) => { } broadcastRedeployStatus(id, false); + logRedeployEvent({ + stackId: stack.Id || id, + stackName: stack.Name, + status: 'success', + message: 'Redeploy erfolgreich abgeschlossen', + endpoint: stack.EndpointId + }); console.log(`✅ PUT /api/stacks/${id}/redeploy: Redeploy erfolgreich abgeschlossen`); res.json({ success: true, message: 'Stack redeployed' }); } catch (err) { broadcastRedeployStatus(id, false); - console.error(`❌ Fehler beim Redeploy von Stack ${id}:`, err.message); - res.status(500).json({ error: err.message }); + const errorMessage = err.response?.data?.message || err.message; + logRedeployEvent({ + stackId: stack?.Id || id, + stackName: stack?.Name || `Stack ${id}`, + status: 'error', + message: errorMessage, + endpoint: stack?.EndpointId || ENDPOINT_ID + }); + console.error(`❌ Fehler beim Redeploy von Stack ${id}:`, errorMessage); + res.status(500).json({ error: errorMessage }); } }); @@ -218,6 +243,13 @@ app.put('/api/stacks/redeploy-all', async (req, res) => { filteredStacks.forEach(async (stack) => { try { broadcastRedeployStatus(stack.Id, true); + logRedeployEvent({ + stackId: stack.Id, + stackName: stack.Name, + status: 'started', + message: 'Redeploy über Redeploy ALL gestartet', + endpoint: stack.EndpointId + }); if (stack.Type === 1) { console.log(`🔄 [Redeploy] Git Stack "${stack.Name}" (${stack.Id})`); @@ -235,10 +267,25 @@ app.put('/api/stacks/redeploy-all', async (req, res) => { } broadcastRedeployStatus(stack.Id, false); + logRedeployEvent({ + stackId: stack.Id, + stackName: stack.Name, + status: 'success', + message: 'Redeploy über Redeploy ALL abgeschlossen', + endpoint: stack.EndpointId + }); console.log(`✅ Redeploy abgeschlossen: ${stack.Name}`); } catch (err) { broadcastRedeployStatus(stack.Id, false); - console.error(`❌ Fehler beim Redeploy von Stack ${stack.Name}:`, err.message); + const errorMessage = err.response?.data?.message || err.message; + logRedeployEvent({ + stackId: stack.Id, + stackName: stack.Name, + status: 'error', + message: errorMessage, + endpoint: stack.EndpointId + }); + console.error(`❌ Fehler beim Redeploy von Stack ${stack.Name}:`, errorMessage); } });