995 lines
32 KiB
JavaScript
995 lines
32 KiB
JavaScript
import express from 'express';
|
||
import dotenv from 'dotenv';
|
||
import https from 'https';
|
||
import axios from 'axios';
|
||
import http from 'http';
|
||
import { Server } from 'socket.io';
|
||
import path from 'path';
|
||
import { fileURLToPath } from 'url';
|
||
import { db } from './db/index.js';
|
||
import {
|
||
logRedeployEvent,
|
||
buildLogFilter,
|
||
deleteLogById,
|
||
deleteLogsByFilters,
|
||
exportLogsByFilters
|
||
} from './db/redeployLogs.js';
|
||
|
||
dotenv.config();
|
||
|
||
const __filename = fileURLToPath(import.meta.url);
|
||
const __dirname = path.dirname(__filename);
|
||
|
||
const app = express();
|
||
app.use(express.json());
|
||
app.use(express.static(path.join(__dirname, 'public')));
|
||
|
||
app.get('*', (req, res, next) => {
|
||
if (req.path.startsWith('/api')) return next();
|
||
res.sendFile(path.join(__dirname, 'public', 'index.html'));
|
||
});
|
||
|
||
const PORT = 4001;
|
||
const ENDPOINT_ID = Number(process.env.PORTAINER_ENDPOINT_ID);
|
||
|
||
const agent = new https.Agent({ rejectUnauthorized: false });
|
||
const axiosInstance = axios.create({
|
||
httpsAgent: agent,
|
||
headers: { "X-API-Key": process.env.PORTAINER_API_KEY },
|
||
baseURL: process.env.PORTAINER_URL,
|
||
});
|
||
|
||
const redeployingStacks = {};
|
||
|
||
const server = http.createServer(app);
|
||
const io = new Server(server, {
|
||
path: "/socket.io",
|
||
cors: { origin: "*" }
|
||
});
|
||
io.on("connection", (socket) => {
|
||
console.log(`🔌 [Socket] Client verbunden: ${socket.id}`);
|
||
});
|
||
|
||
const broadcastRedeployStatus = ({ stackId, stackName, phase, message }) => {
|
||
const normalizedPhase = phase || (message ? 'info' : undefined);
|
||
const isRedeploying = normalizedPhase === 'started';
|
||
redeployingStacks[stackId] = Boolean(isRedeploying);
|
||
|
||
const payload = {
|
||
stackId,
|
||
stackName,
|
||
phase: normalizedPhase,
|
||
message,
|
||
isRedeploying
|
||
};
|
||
|
||
io.emit("redeployStatus", payload);
|
||
|
||
const label = stackName ? `${stackName} (${stackId})` : `Stack ${stackId}`;
|
||
const phaseLabel = normalizedPhase ?? (isRedeploying ? 'started' : 'success');
|
||
console.log(`🔄 [RedeployStatus] ${label} -> ${phaseLabel}${message ? `: ${message}` : ""}`);
|
||
};
|
||
|
||
const REDEPLOY_TYPES = {
|
||
SINGLE: 'Einzeln',
|
||
ALL: 'Alle',
|
||
SELECTION: 'Auswahl',
|
||
MAINTENANCE: 'Wartung'
|
||
};
|
||
|
||
const SELF_STACK_ID = process.env.SELF_STACK_ID ? String(process.env.SELF_STACK_ID) : null;
|
||
|
||
const fetchPortainerStacks = async () => {
|
||
const stacksRes = await axiosInstance.get('/api/stacks');
|
||
return stacksRes.data.filter((stack) => stack.EndpointId === ENDPOINT_ID);
|
||
};
|
||
|
||
const buildStackCollections = (stacks = []) => {
|
||
const collections = new Map();
|
||
|
||
stacks.forEach((stack) => {
|
||
const name = stack.Name || 'Unbenannt';
|
||
const isSelf = SELF_STACK_ID && String(stack.Id) === SELF_STACK_ID;
|
||
const entry = collections.get(name);
|
||
|
||
if (!entry) {
|
||
collections.set(name, {
|
||
canonical: stack,
|
||
isSelf,
|
||
members: [stack]
|
||
});
|
||
return;
|
||
}
|
||
|
||
entry.members.push(stack);
|
||
|
||
if (!entry.isSelf && isSelf) {
|
||
entry.canonical = stack;
|
||
entry.isSelf = true;
|
||
}
|
||
});
|
||
|
||
const canonicalStacks = [];
|
||
const duplicates = [];
|
||
|
||
collections.forEach((entry, name) => {
|
||
canonicalStacks.push(entry.canonical);
|
||
|
||
if (entry.members.length > 1) {
|
||
const seenIds = new Set();
|
||
const duplicateEntries = entry.members.filter((member) => {
|
||
const id = String(member.Id);
|
||
if (id === String(entry.canonical.Id)) {
|
||
return false;
|
||
}
|
||
if (seenIds.has(id)) {
|
||
return false;
|
||
}
|
||
seenIds.add(id);
|
||
return true;
|
||
});
|
||
|
||
if (duplicateEntries.length > 0) {
|
||
duplicates.push({
|
||
name,
|
||
canonical: entry.canonical,
|
||
members: duplicateEntries
|
||
});
|
||
}
|
||
}
|
||
});
|
||
|
||
return { canonicalStacks, duplicates };
|
||
};
|
||
|
||
const loadStackCollections = async () => {
|
||
const filteredStacks = await fetchPortainerStacks();
|
||
return {
|
||
filteredStacks,
|
||
...buildStackCollections(filteredStacks)
|
||
};
|
||
};
|
||
|
||
const isStackOutdated = async (stackId) => {
|
||
try {
|
||
const statusRes = await axiosInstance.get(`/api/stacks/${stackId}/images_status?refresh=true`);
|
||
return statusRes.data?.Status === 'outdated';
|
||
} catch (err) {
|
||
console.error(`⚠️ Konnte Update-Status für Stack ${stackId} nicht ermitteln:`, err.message);
|
||
return true;
|
||
}
|
||
};
|
||
|
||
const filterOutdatedStacks = async (stacks = []) => {
|
||
const results = await Promise.all(
|
||
stacks.map(async (stack) => ({
|
||
stack,
|
||
outdated: SELF_STACK_ID && String(stack.Id) === SELF_STACK_ID
|
||
? false
|
||
: await isStackOutdated(stack.Id)
|
||
}))
|
||
);
|
||
|
||
return {
|
||
eligibleStacks: results.filter((entry) => entry.outdated).map((entry) => entry.stack),
|
||
skippedStacks: results.filter((entry) => !entry.outdated).map((entry) => entry.stack),
|
||
};
|
||
};
|
||
|
||
// --- API Endpoints ---
|
||
|
||
// Stacks abrufen
|
||
app.get('/api/stacks', async (req, res) => {
|
||
console.log("ℹ️ [API] GET /api/stacks: Abruf gestartet");
|
||
try {
|
||
const { canonicalStacks, duplicates } = await loadStackCollections();
|
||
const duplicateNames = duplicates.map((entry) => entry.name);
|
||
const duplicateNameSet = new Set(duplicateNames);
|
||
|
||
if (duplicateNames.length) {
|
||
console.warn(`⚠️ [API] GET /api/stacks: Doppelte Stack-Namen erkannt: ${duplicateNames.join(', ')}`);
|
||
}
|
||
|
||
const stacksWithStatus = await Promise.all(
|
||
canonicalStacks.map(async (stack) => {
|
||
try {
|
||
const statusRes = await axiosInstance.get(
|
||
`/api/stacks/${stack.Id}/images_status?refresh=true`
|
||
);
|
||
const statusEmoji = statusRes.data.Status === 'outdated' ? '⚠️' : '✅';
|
||
return {
|
||
...stack,
|
||
updateStatus: statusEmoji,
|
||
redeploying: redeployingStacks[stack.Id] || false,
|
||
redeployDisabled: SELF_STACK_ID ? String(stack.Id) === SELF_STACK_ID : false,
|
||
duplicateName: duplicateNameSet.has(stack.Name)
|
||
};
|
||
} catch (err) {
|
||
console.error(`❌ Fehler beim Abrufen des Status für Stack ${stack.Id}:`, err.message);
|
||
return {
|
||
...stack,
|
||
updateStatus: '❌',
|
||
redeploying: redeployingStacks[stack.Id] || false,
|
||
redeployDisabled: SELF_STACK_ID ? String(stack.Id) === SELF_STACK_ID : false,
|
||
duplicateName: duplicateNameSet.has(stack.Name)
|
||
};
|
||
}
|
||
})
|
||
);
|
||
|
||
stacksWithStatus.sort((a, b) => a.Name.localeCompare(b.Name));
|
||
console.log(`✅ GET /api/stacks: Abruf erfolgreich, ${stacksWithStatus.length} Stacks geladen`);
|
||
res.json(stacksWithStatus);
|
||
} catch (err) {
|
||
console.error(`❌ Fehler beim Abrufen der Stacks:`, err.message);
|
||
res.status(500).json({ error: err.message });
|
||
}
|
||
});
|
||
|
||
app.get('/api/maintenance/duplicates', async (req, res) => {
|
||
console.log("🧹 [Maintenance] GET /api/maintenance/duplicates: Abruf gestartet");
|
||
try {
|
||
const { duplicates } = await loadStackCollections();
|
||
|
||
const payload = duplicates
|
||
.sort((a, b) => a.name.localeCompare(b.name))
|
||
.map((entry) => ({
|
||
name: entry.name,
|
||
canonical: {
|
||
Id: entry.canonical.Id,
|
||
Name: entry.canonical.Name,
|
||
EndpointId: entry.canonical.EndpointId,
|
||
Type: entry.canonical.Type,
|
||
Created: entry.canonical.Created
|
||
},
|
||
duplicates: entry.members.map((stack) => ({
|
||
Id: stack.Id,
|
||
Name: stack.Name,
|
||
EndpointId: stack.EndpointId,
|
||
Type: stack.Type,
|
||
Created: stack.Created
|
||
}))
|
||
}));
|
||
|
||
res.json({
|
||
total: payload.length,
|
||
items: payload
|
||
});
|
||
} catch (err) {
|
||
console.error(`❌ [Maintenance] Fehler beim Abrufen der Duplikate:`, err.message);
|
||
res.status(500).json({ error: 'Fehler beim Abrufen der doppelten Stacks' });
|
||
}
|
||
});
|
||
|
||
app.post('/api/maintenance/duplicates/cleanup', async (req, res) => {
|
||
const canonicalId = req.body?.canonicalId;
|
||
const duplicateIdsInput = Array.isArray(req.body?.duplicateIds) ? req.body.duplicateIds : [];
|
||
const duplicateIds = duplicateIdsInput
|
||
.map((value) => String(value).trim())
|
||
.filter((value) => value.length > 0);
|
||
|
||
if (!canonicalId) {
|
||
return res.status(400).json({ error: 'canonicalId ist erforderlich' });
|
||
}
|
||
|
||
if (!duplicateIds.length) {
|
||
return res.status(400).json({ error: 'duplicateIds ist erforderlich' });
|
||
}
|
||
|
||
const canonicalIdStr = String(canonicalId);
|
||
console.log(`🧹 [Maintenance] Bereinigung angefordert für Stack ${canonicalIdStr}. Ziel-IDs: ${duplicateIds.join(', ')}`);
|
||
|
||
try {
|
||
const { duplicates } = await loadStackCollections();
|
||
const target = duplicates.find((entry) => String(entry.canonical.Id) === canonicalIdStr);
|
||
|
||
if (!target) {
|
||
return res.status(404).json({ error: 'Kein doppelter Stack für diese ID gefunden' });
|
||
}
|
||
|
||
const duplicatesToDelete = target.members.filter((stack) => duplicateIds.includes(String(stack.Id)));
|
||
|
||
if (!duplicatesToDelete.length) {
|
||
return res.status(400).json({ error: 'Keine passenden Duplikate gefunden' });
|
||
}
|
||
|
||
logRedeployEvent({
|
||
stackId: target.canonical.Id,
|
||
stackName: target.canonical.Name,
|
||
status: 'started',
|
||
message: `Bereinigung doppelter Stacks gestartet (${duplicatesToDelete.length} Einträge)`,
|
||
endpoint: target.canonical.EndpointId,
|
||
redeployType: REDEPLOY_TYPES.MAINTENANCE
|
||
});
|
||
|
||
const results = [];
|
||
const errors = [];
|
||
|
||
for (const stack of duplicatesToDelete) {
|
||
try {
|
||
await axiosInstance.delete(`/api/stacks/${stack.Id}`, {
|
||
params: { endpointId: stack.EndpointId }
|
||
});
|
||
console.log(`🧹 [Maintenance] Stack entfernt: ${stack.Name} (${stack.Id})`);
|
||
results.push({
|
||
id: stack.Id,
|
||
name: stack.Name,
|
||
endpointId: stack.EndpointId,
|
||
status: 'deleted'
|
||
});
|
||
} catch (err) {
|
||
const message = err.response?.data?.message || err.message;
|
||
console.error(`❌ [Maintenance] Fehler beim Entfernen von Stack ${stack.Id}:`, message);
|
||
errors.push({ id: stack.Id, message });
|
||
results.push({
|
||
id: stack.Id,
|
||
name: stack.Name,
|
||
endpointId: stack.EndpointId,
|
||
status: 'error',
|
||
message
|
||
});
|
||
}
|
||
}
|
||
|
||
if (errors.length) {
|
||
const failedIds = errors.map((entry) => entry.id).join(', ');
|
||
logRedeployEvent({
|
||
stackId: target.canonical.Id,
|
||
stackName: target.canonical.Name,
|
||
status: 'error',
|
||
message: `Bereinigung fehlgeschlagen für IDs: ${failedIds}`,
|
||
endpoint: target.canonical.EndpointId,
|
||
redeployType: REDEPLOY_TYPES.MAINTENANCE
|
||
});
|
||
|
||
return res.status(500).json({
|
||
success: false,
|
||
canonical: {
|
||
id: target.canonical.Id,
|
||
name: target.canonical.Name,
|
||
endpointId: target.canonical.EndpointId
|
||
},
|
||
results
|
||
});
|
||
}
|
||
|
||
logRedeployEvent({
|
||
stackId: target.canonical.Id,
|
||
stackName: target.canonical.Name,
|
||
status: 'success',
|
||
message: `Bereinigung abgeschlossen. Entfernte IDs: ${results.map((entry) => entry.id).join(', ')}`,
|
||
endpoint: target.canonical.EndpointId,
|
||
redeployType: REDEPLOY_TYPES.MAINTENANCE
|
||
});
|
||
|
||
res.json({
|
||
success: true,
|
||
canonical: {
|
||
id: target.canonical.Id,
|
||
name: target.canonical.Name,
|
||
endpointId: target.canonical.EndpointId
|
||
},
|
||
removed: results.length,
|
||
results
|
||
});
|
||
} catch (err) {
|
||
const message = err.response?.data?.message || err.message;
|
||
console.error(`❌ [Maintenance] Fehler bei der Bereinigung:`, message);
|
||
res.status(500).json({ error: message || 'Fehler bei der Bereinigung' });
|
||
}
|
||
});
|
||
|
||
// Redeploy-Logs abrufen
|
||
app.get('/api/logs', (req, res) => {
|
||
const perPageParam = req.query.perPage ?? req.query.limit;
|
||
const perPage = perPageParam === 'all' ? 'all' : Math.min(parseInt(perPageParam, 10) || 50, 500);
|
||
const page = Math.max(parseInt(req.query.page, 10) || 1, 1);
|
||
|
||
const limit = perPage === 'all' ? undefined : perPage;
|
||
const offset = perPage === 'all' ? 0 : (page - 1) * perPage;
|
||
|
||
const { whereClause, params } = buildLogFilter(req.query);
|
||
const baseQuery = `
|
||
SELECT
|
||
id,
|
||
timestamp,
|
||
stack_id AS stackId,
|
||
stack_name AS stackName,
|
||
status,
|
||
message,
|
||
endpoint,
|
||
redeploy_type AS redeployType
|
||
FROM redeploy_logs
|
||
${whereClause}
|
||
ORDER BY datetime(timestamp) DESC
|
||
`;
|
||
|
||
const countQuery = `
|
||
SELECT COUNT(*) as total
|
||
FROM redeploy_logs
|
||
${whereClause}
|
||
`;
|
||
|
||
const query = limit !== undefined
|
||
? `${baseQuery} LIMIT @limit OFFSET @offset`
|
||
: baseQuery;
|
||
|
||
if (limit !== undefined) {
|
||
params.limit = limit;
|
||
params.offset = offset;
|
||
}
|
||
|
||
try {
|
||
const stmt = db.prepare(query);
|
||
const logs = stmt.all(params);
|
||
const total = db.prepare(countQuery).get(params)?.total ?? logs.length;
|
||
|
||
res.json({
|
||
total,
|
||
items: logs,
|
||
page,
|
||
perPage: perPage === 'all' ? 'all' : limit
|
||
});
|
||
} catch (err) {
|
||
console.error('❌ Fehler beim Abrufen der Redeploy-Logs:', err.message);
|
||
if (err.message.includes('no such table')) {
|
||
return res.status(500).json({ error: 'redeploy_logs table nicht gefunden. Bitte Migration ausführen.' });
|
||
}
|
||
res.status(500).json({ error: 'Fehler beim Abrufen der Redeploy-Logs' });
|
||
}
|
||
});
|
||
|
||
app.delete('/api/logs/:id', (req, res) => {
|
||
const id = Number(req.params.id);
|
||
if (Number.isNaN(id)) {
|
||
return res.status(400).json({ error: 'Ungültige ID' });
|
||
}
|
||
|
||
try {
|
||
const changes = deleteLogById(id);
|
||
if (!changes) {
|
||
return res.status(404).json({ error: 'Eintrag nicht gefunden' });
|
||
}
|
||
res.json({ success: true, deleted: changes });
|
||
} catch (err) {
|
||
console.error('❌ Fehler beim Löschen des Redeploy-Logs:', err.message);
|
||
res.status(500).json({ error: 'Fehler beim Löschen des Redeploy-Logs' });
|
||
}
|
||
});
|
||
|
||
app.delete('/api/logs', (req, res) => {
|
||
try {
|
||
const deleted = deleteLogsByFilters(req.query);
|
||
res.json({ success: true, deleted });
|
||
} catch (err) {
|
||
console.error('❌ Fehler beim Löschen der Redeploy-Logs:', err.message);
|
||
res.status(500).json({ error: 'Fehler beim Löschen der Redeploy-Logs' });
|
||
}
|
||
});
|
||
|
||
app.get('/api/logs/export', (req, res) => {
|
||
const format = (req.query.format || 'txt').toLowerCase();
|
||
if (!['txt', 'sql'].includes(format)) {
|
||
return res.status(400).json({ error: 'Ungültiges Export-Format' });
|
||
}
|
||
|
||
try {
|
||
const payload = exportLogsByFilters(req.query, format);
|
||
res.setHeader('Content-Type', payload.contentType);
|
||
res.setHeader('Content-Disposition', `attachment; filename="${payload.filename}"`);
|
||
res.send(payload.content);
|
||
} catch (err) {
|
||
console.error('❌ Fehler beim Export der Redeploy-Logs:', err.message);
|
||
res.status(500).json({ error: 'Fehler beim Export der Redeploy-Logs' });
|
||
}
|
||
});
|
||
|
||
// Einzel-Redeploy
|
||
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 {
|
||
const stackRes = await axiosInstance.get(`/api/stacks/${id}`);
|
||
stack = stackRes.data;
|
||
|
||
if (stack.EndpointId !== ENDPOINT_ID) {
|
||
throw new Error(`Stack gehört nicht zum Endpoint ${ENDPOINT_ID}`);
|
||
}
|
||
|
||
broadcastRedeployStatus({
|
||
stackId: stack.Id || id,
|
||
stackName: stack.Name,
|
||
phase: 'started'
|
||
});
|
||
|
||
logRedeployEvent({
|
||
stackId: stack.Id || id,
|
||
stackName: stack.Name,
|
||
status: 'started',
|
||
message: 'Redeploy gestartet',
|
||
endpoint: stack.EndpointId,
|
||
redeployType: REDEPLOY_TYPES.SINGLE
|
||
});
|
||
|
||
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}`);
|
||
} else if (stack.Type === 2) {
|
||
console.log(`🔄 [Redeploy] Compose Stack "${stack.Name}" (${id}) wird redeployed`);
|
||
const fileRes = await axiosInstance.get(`/api/stacks/${id}/file`);
|
||
const stackFileContent = fileRes.data?.StackFileContent;
|
||
if (!stackFileContent) throw new Error("Stack file konnte nicht geladen werden");
|
||
|
||
const services = fileRes.data?.Config?.services || {};
|
||
for (const serviceName in services) {
|
||
const imageName = services[serviceName].image;
|
||
if (!imageName) continue;
|
||
try {
|
||
console.log(`🖼️ Pulling image "${imageName}" für Service "${serviceName}"`);
|
||
await axiosInstance.post(
|
||
`/api/endpoints/${stack.EndpointId}/docker/images/create?fromImage=${encodeURIComponent(imageName)}`
|
||
);
|
||
} catch (err) {
|
||
console.error(`❌ Fehler beim Pulling von Image "${imageName}":`, err.message);
|
||
}
|
||
}
|
||
|
||
await axiosInstance.put(`/api/stacks/${id}`,
|
||
{ StackFileContent: stackFileContent, Prune: false, PullImage: true },
|
||
{ params: { endpointId: stack.EndpointId } }
|
||
);
|
||
}
|
||
|
||
broadcastRedeployStatus({
|
||
stackId: stack.Id || id,
|
||
stackName: stack.Name,
|
||
phase: 'success'
|
||
});
|
||
logRedeployEvent({
|
||
stackId: stack.Id || id,
|
||
stackName: stack.Name,
|
||
status: 'success',
|
||
message: 'Redeploy erfolgreich abgeschlossen',
|
||
endpoint: stack.EndpointId,
|
||
redeployType: REDEPLOY_TYPES.SINGLE
|
||
});
|
||
console.log(`✅ PUT /api/stacks/${id}/redeploy: Redeploy erfolgreich abgeschlossen`);
|
||
res.json({ success: true, message: 'Stack redeployed' });
|
||
} catch (err) {
|
||
const errorMessage = err.response?.data?.message || err.message;
|
||
broadcastRedeployStatus({
|
||
stackId: stack?.Id || id,
|
||
stackName: stack?.Name,
|
||
phase: 'error',
|
||
message: errorMessage
|
||
});
|
||
logRedeployEvent({
|
||
stackId: stack?.Id || id,
|
||
stackName: stack?.Name || `Stack ${id}`,
|
||
status: 'error',
|
||
message: errorMessage,
|
||
endpoint: stack?.EndpointId || ENDPOINT_ID,
|
||
redeployType: REDEPLOY_TYPES.SINGLE
|
||
});
|
||
console.error(`❌ Fehler beim Redeploy von Stack ${id}:`, errorMessage);
|
||
res.status(500).json({ error: errorMessage });
|
||
}
|
||
});
|
||
|
||
// Redeploy ALL
|
||
app.put('/api/stacks/redeploy-all', async (req, res) => {
|
||
console.log(`🚀 PUT /api/stacks/redeploy-all: Redeploy ALL gestartet`);
|
||
|
||
try {
|
||
const stacksRes = await axiosInstance.get('/api/stacks');
|
||
const filteredStacks = stacksRes.data.filter(stack => stack.EndpointId === ENDPOINT_ID);
|
||
|
||
console.log("📦 Redeploy ALL für folgende Stacks:");
|
||
filteredStacks.forEach(s => console.log(` - ${s.Name}`));
|
||
|
||
const { eligibleStacks, skippedStacks } = await filterOutdatedStacks(filteredStacks);
|
||
|
||
if (skippedStacks.length) {
|
||
skippedStacks.forEach((stack) => {
|
||
console.log(`⏭️ Übersprungen (aktuell): ${stack.Name} (${stack.Id})`);
|
||
});
|
||
}
|
||
|
||
const stackSummaryList = eligibleStacks.map((stack) => `${stack.Name} (${stack.Id})`);
|
||
const stackSummary = stackSummaryList.length ? stackSummaryList.join(', ') : 'keine Stacks';
|
||
logRedeployEvent({
|
||
stackId: '---',
|
||
stackName: '---',
|
||
status: 'started',
|
||
message: `Redeploy ALL gestartet für: ${stackSummary}`,
|
||
endpoint: ENDPOINT_ID,
|
||
redeployType: REDEPLOY_TYPES.ALL
|
||
});
|
||
|
||
if (!eligibleStacks.length) {
|
||
logRedeployEvent({
|
||
stackId: '---',
|
||
stackName: '---',
|
||
status: 'success',
|
||
message: 'Redeploy ALL übersprungen: keine veralteten Stacks',
|
||
endpoint: ENDPOINT_ID,
|
||
redeployType: REDEPLOY_TYPES.ALL
|
||
});
|
||
return res.json({ success: true, message: 'Keine veralteten Stacks für Redeploy ALL' });
|
||
}
|
||
|
||
for (const stack of eligibleStacks) {
|
||
try {
|
||
broadcastRedeployStatus({
|
||
stackId: stack.Id,
|
||
stackName: stack.Name,
|
||
phase: 'started'
|
||
});
|
||
logRedeployEvent({
|
||
stackId: stack.Id,
|
||
stackName: stack.Name,
|
||
status: 'started',
|
||
message: 'Redeploy ALL gestartet',
|
||
endpoint: stack.EndpointId,
|
||
redeployType: REDEPLOY_TYPES.ALL
|
||
});
|
||
|
||
if (stack.Type === 1) {
|
||
console.log(`🔄 [Redeploy] Git Stack "${stack.Name}" (${stack.Id})`);
|
||
await axiosInstance.put(`/api/stacks/${stack.Id}/git/redeploy?endpointId=${stack.EndpointId}`);
|
||
} else if (stack.Type === 2) {
|
||
console.log(`🔄 [Redeploy] Compose Stack "${stack.Name}" (${stack.Id})`);
|
||
const fileRes = await axiosInstance.get(`/api/stacks/${stack.Id}/file`);
|
||
const stackFileContent = fileRes.data?.StackFileContent;
|
||
if (stackFileContent) {
|
||
await axiosInstance.put(`/api/stacks/${stack.Id}`,
|
||
{ StackFileContent: stackFileContent, Prune: false, PullImage: true },
|
||
{ params: { endpointId: stack.EndpointId } }
|
||
);
|
||
}
|
||
}
|
||
|
||
broadcastRedeployStatus({
|
||
stackId: stack.Id,
|
||
stackName: stack.Name,
|
||
phase: 'success'
|
||
});
|
||
logRedeployEvent({
|
||
stackId: stack.Id,
|
||
stackName: stack.Name,
|
||
status: 'success',
|
||
message: 'Redeploy ALL abgeschlossen',
|
||
endpoint: stack.EndpointId,
|
||
redeployType: REDEPLOY_TYPES.ALL
|
||
});
|
||
console.log(`✅ Redeploy abgeschlossen: ${stack.Name}`);
|
||
} catch (err) {
|
||
const errorMessage = err.response?.data?.message || err.message;
|
||
broadcastRedeployStatus({
|
||
stackId: stack.Id,
|
||
stackName: stack.Name,
|
||
phase: 'error',
|
||
message: errorMessage
|
||
});
|
||
logRedeployEvent({
|
||
stackId: stack.Id,
|
||
stackName: stack.Name,
|
||
status: 'error',
|
||
message: errorMessage,
|
||
endpoint: stack.EndpointId,
|
||
redeployType: REDEPLOY_TYPES.ALL
|
||
});
|
||
console.error(`❌ Fehler beim Redeploy von Stack ${stack.Name}:`, errorMessage);
|
||
}
|
||
}
|
||
|
||
res.json({ success: true, message: 'Redeploy ALL gestartet' });
|
||
} catch (err) {
|
||
console.error(`❌ Fehler beim Redeploy ALL:`, err.message);
|
||
logRedeployEvent({
|
||
stackId: '---',
|
||
stackName: '---',
|
||
status: 'error',
|
||
message: err.message,
|
||
endpoint: ENDPOINT_ID,
|
||
redeployType: REDEPLOY_TYPES.ALL
|
||
});
|
||
res.status(500).json({ error: err.message });
|
||
}
|
||
});
|
||
|
||
app.get('/api/maintenance/duplicates', async (req, res) => {
|
||
console.log("🧹 [Maintenance] GET /api/maintenance/duplicates: Abruf gestartet");
|
||
try {
|
||
const { duplicates } = await loadStackCollections();
|
||
|
||
const payload = duplicates
|
||
.sort((a, b) => a.name.localeCompare(b.name))
|
||
.map((entry) => ({
|
||
name: entry.name,
|
||
canonical: {
|
||
Id: entry.canonical.Id,
|
||
Name: entry.canonical.Name,
|
||
EndpointId: entry.canonical.EndpointId,
|
||
Type: entry.canonical.Type,
|
||
Created: entry.canonical.Created
|
||
},
|
||
duplicates: entry.members.map((stack) => ({
|
||
Id: stack.Id,
|
||
Name: stack.Name,
|
||
EndpointId: stack.EndpointId,
|
||
Type: stack.Type,
|
||
Created: stack.Created
|
||
}))
|
||
}));
|
||
|
||
res.json({
|
||
total: payload.length,
|
||
items: payload
|
||
});
|
||
} catch (err) {
|
||
console.error(`❌ [Maintenance] Fehler beim Abrufen der Duplikate:`, err.message);
|
||
res.status(500).json({ error: 'Fehler beim Abrufen der doppelten Stacks' });
|
||
}
|
||
});
|
||
|
||
app.post('/api/maintenance/duplicates/cleanup', async (req, res) => {
|
||
const canonicalId = req.body?.canonicalId;
|
||
const duplicateIdsInput = Array.isArray(req.body?.duplicateIds) ? req.body.duplicateIds : [];
|
||
const duplicateIds = duplicateIdsInput
|
||
.map((value) => String(value).trim())
|
||
.filter((value) => value.length > 0);
|
||
|
||
if (!canonicalId) {
|
||
return res.status(400).json({ error: 'canonicalId ist erforderlich' });
|
||
}
|
||
|
||
if (!duplicateIds.length) {
|
||
return res.status(400).json({ error: 'duplicateIds ist erforderlich' });
|
||
}
|
||
|
||
const canonicalIdStr = String(canonicalId);
|
||
console.log(`🧹 [Maintenance] Bereinigung angefordert für Stack ${canonicalIdStr}. Ziel-IDs: ${duplicateIds.join(', ')}`);
|
||
|
||
try {
|
||
const { duplicates } = await loadStackCollections();
|
||
const target = duplicates.find((entry) => String(entry.canonical.Id) === canonicalIdStr);
|
||
|
||
if (!target) {
|
||
return res.status(404).json({ error: 'Kein doppelter Stack für diese ID gefunden' });
|
||
}
|
||
|
||
const duplicatesToDelete = target.members.filter((stack) => duplicateIds.includes(String(stack.Id)));
|
||
|
||
if (!duplicatesToDelete.length) {
|
||
return res.status(400).json({ error: 'Keine passenden Duplikate gefunden' });
|
||
}
|
||
|
||
logRedeployEvent({
|
||
stackId: target.canonical.Id,
|
||
stackName: target.canonical.Name,
|
||
status: 'started',
|
||
message: `Bereinigung doppelter Stacks gestartet (${duplicatesToDelete.length} Einträge)`,
|
||
endpoint: target.canonical.EndpointId,
|
||
redeployType: REDEPLOY_TYPES.MAINTENANCE
|
||
});
|
||
|
||
const results = [];
|
||
const errors = [];
|
||
|
||
for (const stack of duplicatesToDelete) {
|
||
try {
|
||
await axiosInstance.delete(`/api/stacks/${stack.Id}`, {
|
||
params: { endpointId: stack.EndpointId }
|
||
});
|
||
console.log(`🧹 [Maintenance] Stack entfernt: ${stack.Name} (${stack.Id})`);
|
||
results.push({
|
||
id: stack.Id,
|
||
name: stack.Name,
|
||
endpointId: stack.EndpointId,
|
||
status: 'deleted'
|
||
});
|
||
} catch (err) {
|
||
const message = err.response?.data?.message || err.message;
|
||
console.error(`❌ [Maintenance] Fehler beim Entfernen von Stack ${stack.Id}:`, message);
|
||
errors.push({ id: stack.Id, message });
|
||
results.push({
|
||
id: stack.Id,
|
||
name: stack.Name,
|
||
endpointId: stack.EndpointId,
|
||
status: 'error',
|
||
message
|
||
});
|
||
}
|
||
}
|
||
|
||
if (errors.length) {
|
||
const failedIds = errors.map((entry) => entry.id).join(', ');
|
||
logRedeployEvent({
|
||
stackId: target.canonical.Id,
|
||
stackName: target.canonical.Name,
|
||
status: 'error',
|
||
message: `Bereinigung fehlgeschlagen für IDs: ${failedIds}`,
|
||
endpoint: target.canonical.EndpointId,
|
||
redeployType: REDEPLOY_TYPES.MAINTENANCE
|
||
});
|
||
|
||
return res.status(500).json({
|
||
success: false,
|
||
canonical: {
|
||
id: target.canonical.Id,
|
||
name: target.canonical.Name,
|
||
endpointId: target.canonical.EndpointId
|
||
},
|
||
results
|
||
});
|
||
}
|
||
|
||
logRedeployEvent({
|
||
stackId: target.canonical.Id,
|
||
stackName: target.canonical.Name,
|
||
status: 'success',
|
||
message: `Bereinigung abgeschlossen. Entfernte IDs: ${results.map((entry) => entry.id).join(', ')}`,
|
||
endpoint: target.canonical.EndpointId,
|
||
redeployType: REDEPLOY_TYPES.MAINTENANCE
|
||
});
|
||
|
||
res.json({
|
||
success: true,
|
||
canonical: {
|
||
id: target.canonical.Id,
|
||
name: target.canonical.Name,
|
||
endpointId: target.canonical.EndpointId
|
||
},
|
||
removed: results.length,
|
||
results
|
||
});
|
||
} catch (err) {
|
||
const message = err.response?.data?.message || err.message;
|
||
console.error(`❌ [Maintenance] Fehler bei der Bereinigung:`, message);
|
||
res.status(500).json({ error: message || 'Fehler bei der Bereinigung' });
|
||
}
|
||
});
|
||
|
||
app.put('/api/stacks/redeploy-selection', async (req, res) => {
|
||
const { stackIds } = req.body || {};
|
||
console.log(`🚀 PUT /api/stacks/redeploy-selection: Redeploy Auswahl gestartet (${Array.isArray(stackIds) ? stackIds.length : 0} Stacks)`);
|
||
|
||
if (!Array.isArray(stackIds) || !stackIds.length) {
|
||
return res.status(400).json({ error: 'stackIds (array) erforderlich' });
|
||
}
|
||
|
||
const normalizedIds = stackIds.map((id) => String(id));
|
||
|
||
try {
|
||
const stacksRes = await axiosInstance.get('/api/stacks');
|
||
const endpointStacks = stacksRes.data.filter(stack => stack.EndpointId === ENDPOINT_ID);
|
||
const selectedStacks = endpointStacks.filter((stack) => normalizedIds.includes(String(stack.Id)));
|
||
|
||
if (!selectedStacks.length) {
|
||
return res.status(400).json({ error: 'Keine gültigen Stacks für Redeploy Auswahl gefunden' });
|
||
}
|
||
|
||
const missingIds = normalizedIds.filter((id) => !selectedStacks.some((stack) => String(stack.Id) === id));
|
||
if (missingIds.length) {
|
||
return res.status(400).json({ error: `Ungültige Stack-IDs: ${missingIds.join(', ')}` });
|
||
}
|
||
|
||
const { eligibleStacks, skippedStacks } = await filterOutdatedStacks(selectedStacks);
|
||
|
||
if (skippedStacks.length) {
|
||
skippedStacks.forEach((stack) => {
|
||
console.log(`⏭️ Übersprungen (aktuell): ${stack.Name} (${stack.Id})`);
|
||
});
|
||
}
|
||
|
||
const stackSummaryList = eligibleStacks.map((stack) => `${stack.Name} (${stack.Id})`);
|
||
const stackSummary = stackSummaryList.length ? stackSummaryList.join(', ') : 'keine Stacks';
|
||
logRedeployEvent({
|
||
stackId: '---',
|
||
stackName: '---',
|
||
status: 'started',
|
||
message: `Redeploy Auswahl gestartet für: ${stackSummary}`,
|
||
endpoint: ENDPOINT_ID,
|
||
redeployType: REDEPLOY_TYPES.SELECTION
|
||
});
|
||
|
||
if (!eligibleStacks.length) {
|
||
logRedeployEvent({
|
||
stackId: '---',
|
||
stackName: '---',
|
||
status: 'success',
|
||
message: 'Redeploy Auswahl übersprungen: keine veralteten Stacks',
|
||
endpoint: ENDPOINT_ID,
|
||
redeployType: REDEPLOY_TYPES.SELECTION
|
||
});
|
||
return res.json({ success: true, message: 'Keine veralteten Stacks in der Auswahl' });
|
||
}
|
||
|
||
for (const stack of eligibleStacks) {
|
||
try {
|
||
broadcastRedeployStatus({
|
||
stackId: stack.Id,
|
||
stackName: stack.Name,
|
||
phase: 'started'
|
||
});
|
||
logRedeployEvent({
|
||
stackId: stack.Id,
|
||
stackName: stack.Name,
|
||
status: 'started',
|
||
message: 'Redeploy Auswahl gestartet',
|
||
endpoint: stack.EndpointId,
|
||
redeployType: REDEPLOY_TYPES.SELECTION
|
||
});
|
||
|
||
if (stack.Type === 1) {
|
||
console.log(`🔄 [Redeploy Auswahl] Git Stack "${stack.Name}" (${stack.Id})`);
|
||
await axiosInstance.put(`/api/stacks/${stack.Id}/git/redeploy?endpointId=${stack.EndpointId}`);
|
||
} else if (stack.Type === 2) {
|
||
console.log(`🔄 [Redeploy Auswahl] Compose Stack "${stack.Name}" (${stack.Id})`);
|
||
const fileRes = await axiosInstance.get(`/api/stacks/${stack.Id}/file`);
|
||
const stackFileContent = fileRes.data?.StackFileContent;
|
||
if (stackFileContent) {
|
||
await axiosInstance.put(`/api/stacks/${stack.Id}`,
|
||
{ StackFileContent: stackFileContent, Prune: false, PullImage: true },
|
||
{ params: { endpointId: stack.EndpointId } }
|
||
);
|
||
}
|
||
}
|
||
|
||
broadcastRedeployStatus({
|
||
stackId: stack.Id,
|
||
stackName: stack.Name,
|
||
phase: 'success'
|
||
});
|
||
logRedeployEvent({
|
||
stackId: stack.Id,
|
||
stackName: stack.Name,
|
||
status: 'success',
|
||
message: 'Redeploy Auswahl erfolgreich abgeschlossen',
|
||
endpoint: stack.EndpointId,
|
||
redeployType: REDEPLOY_TYPES.SELECTION
|
||
});
|
||
console.log(`✅ Redeploy Auswahl abgeschlossen: ${stack.Name}`);
|
||
} catch (err) {
|
||
const errorMessage = err.response?.data?.message || err.message;
|
||
broadcastRedeployStatus({
|
||
stackId: stack.Id,
|
||
stackName: stack.Name,
|
||
phase: 'error',
|
||
message: errorMessage
|
||
});
|
||
logRedeployEvent({
|
||
stackId: stack.Id,
|
||
stackName: stack.Name,
|
||
status: 'error',
|
||
message: errorMessage,
|
||
endpoint: stack.EndpointId,
|
||
redeployType: REDEPLOY_TYPES.SELECTION
|
||
});
|
||
console.error(`❌ Fehler beim Redeploy Auswahl für Stack ${stack.Name}:`, errorMessage);
|
||
}
|
||
}
|
||
|
||
res.json({ success: true, message: 'Redeploy Auswahl gestartet' });
|
||
} catch (err) {
|
||
const errorMessage = err.response?.data?.message || err.message;
|
||
console.error(`❌ Fehler beim Redeploy Auswahl:`, errorMessage);
|
||
logRedeployEvent({
|
||
stackId: '---',
|
||
stackName: '---',
|
||
status: 'error',
|
||
message: errorMessage,
|
||
endpoint: ENDPOINT_ID,
|
||
redeployType: REDEPLOY_TYPES.SELECTION
|
||
});
|
||
res.status(500).json({ error: errorMessage });
|
||
}
|
||
});
|
||
|
||
server.listen(PORT, '0.0.0.0', () => {
|
||
console.log(`🚀 Backend läuft auf Port ${PORT}`);
|
||
});
|