Redeploy-Funktion fertig
This commit is contained in:
+76
-24
@@ -5,6 +5,8 @@ import dotenv from 'dotenv';
|
||||
dotenv.config();
|
||||
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
|
||||
const PORT = process.env.PORT || 4000;
|
||||
const PORTAINER_URL = process.env.PORTAINER_URL;
|
||||
const PORTAINER_API_KEY = process.env.PORTAINER_API_KEY;
|
||||
@@ -16,33 +18,25 @@ app.get('/', (req, res) => {
|
||||
res.send('StackPulse Backend läuft. Nutze /api/stacks für die Daten.');
|
||||
});
|
||||
|
||||
// Alle Stacks abrufen und Status prüfen
|
||||
app.get('/api/stacks', async (req, res) => {
|
||||
try {
|
||||
// 1. Alle Stacks abrufen
|
||||
const stacksUrl = `${PORTAINER_URL}/api/stacks`;
|
||||
const stacksRes = await axios.get(stacksUrl, {
|
||||
const stacksRes = await axios.get(`${PORTAINER_URL}/api/stacks`, {
|
||||
headers: { 'X-API-Key': PORTAINER_API_KEY }
|
||||
});
|
||||
|
||||
const stacksWithStatus = await Promise.all(
|
||||
stacksRes.data.map(async (stack) => {
|
||||
try {
|
||||
// 2. Für jeden Stack den Image-Status abrufen
|
||||
const statusUrl = `${PORTAINER_URL}/api/stacks/${stack.Id}/images_status?refresh=true`;
|
||||
const statusRes = await axios.get(statusUrl, {
|
||||
headers: { 'X-API-Key': PORTAINER_API_KEY }
|
||||
});
|
||||
const statusRes = await axios.get(
|
||||
`${PORTAINER_URL}/api/stacks/${stack.Id}/images_status?refresh=true`,
|
||||
{ headers: { 'X-API-Key': PORTAINER_API_KEY } }
|
||||
);
|
||||
|
||||
// 3. Status prüfen und Emoji setzen
|
||||
let statusEmoji = '✅'; // Standard: aktuell
|
||||
if (statusRes.data.Status === 'outdated') {
|
||||
statusEmoji = '⚠️';
|
||||
}
|
||||
let statusEmoji = '✅';
|
||||
if (statusRes.data.Status === 'outdated') statusEmoji = '⚠️';
|
||||
|
||||
return {
|
||||
...stack,
|
||||
updateStatus: statusEmoji
|
||||
};
|
||||
return { ...stack, updateStatus: statusEmoji };
|
||||
} catch (err) {
|
||||
console.error(`Fehler beim Abrufen Remote Digest für Stack ${stack.Id}:`, err.message);
|
||||
return { ...stack, updateStatus: '❌' };
|
||||
@@ -50,20 +44,78 @@ app.get('/api/stacks', async (req, res) => {
|
||||
})
|
||||
);
|
||||
|
||||
// 4. Alphabetisch nach Name sortieren
|
||||
stacksWithStatus.sort((a, b) => a.Name.localeCompare(b.Name));
|
||||
|
||||
res.json(stacksWithStatus);
|
||||
} catch (err) {
|
||||
console.error('Fehler beim Abrufen der Stacks:', err.message);
|
||||
if (err.response) {
|
||||
res.status(err.response.status).json(err.response.data);
|
||||
} else {
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
if (err.response) res.status(err.response.status).json(err.response.data);
|
||||
else res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Redeploy eines Stacks (alle Typen)
|
||||
app.put('/api/stacks/:id/redeploy', async (req, res) => {
|
||||
const { id } = req.params;
|
||||
|
||||
try {
|
||||
const stackRes = await axios.get(`${PORTAINER_URL}/api/stacks/${id}`, {
|
||||
headers: { 'X-API-Key': PORTAINER_API_KEY }
|
||||
});
|
||||
const stack = stackRes.data;
|
||||
|
||||
if (stack.Type === 1) {
|
||||
await axios.put(
|
||||
`${PORTAINER_URL}/api/stacks/${id}/git/redeploy?endpointId=${stack.EndpointId}`,
|
||||
{},
|
||||
{ headers: { 'X-API-Key': PORTAINER_API_KEY } }
|
||||
);
|
||||
return res.json({ success: true, message: 'Git Stack redeployed' });
|
||||
|
||||
} else if (stack.Type === 2) {
|
||||
const fileRes = await axios.get(`${PORTAINER_URL}/api/stacks/${id}/file`, {
|
||||
headers: { 'X-API-Key': PORTAINER_API_KEY }
|
||||
});
|
||||
|
||||
const stackFileContent = fileRes.data?.StackFileContent;
|
||||
if (!stackFileContent) return res.status(500).json({ 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 {
|
||||
await axios.post(
|
||||
`${PORTAINER_URL}/api/endpoints/${stack.EndpointId}/docker/images/create?fromImage=${encodeURIComponent(imageName)}`,
|
||||
{},
|
||||
{ headers: { 'X-API-Key': PORTAINER_API_KEY } }
|
||||
);
|
||||
} catch (pullErr) {
|
||||
console.error(`Fehler beim Pull von ${imageName}:`, pullErr.message);
|
||||
}
|
||||
}
|
||||
|
||||
await axios.put(
|
||||
`${PORTAINER_URL}/api/stacks/${id}?endpointId=${stack.EndpointId}`,
|
||||
{
|
||||
StackFileContent: stackFileContent,
|
||||
Prune: false,
|
||||
PullImage: true
|
||||
},
|
||||
{ headers: { 'X-API-Key': PORTAINER_API_KEY } }
|
||||
);
|
||||
|
||||
return res.json({ success: true, message: 'Compose/Local Stack redeployed' });
|
||||
|
||||
} else {
|
||||
return res.status(400).json({ error: 'Unbekannter oder noch nicht unterstützter Stack-Typ' });
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Redeploy fehlgeschlagen:', err.response?.data || err.message);
|
||||
res.status(err.response?.status || 500).json({ success: false, error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
app.listen(PORT, '0.0.0.0', () => {
|
||||
console.log(`Backend läuft auf http://0.0.0.0:${PORT}`);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user