Add admin deployment sync
Some checks failed
Build & Push Docker Image / build-and-push (push) Failing after 14s
Some checks failed
Build & Push Docker Image / build-and-push (push) Failing after 14s
This commit is contained in:
@@ -4,6 +4,7 @@ const jwt = require('jsonwebtoken');
|
||||
const bcrypt = require('bcryptjs');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { execFile } = require('child_process');
|
||||
const db = require('./database');
|
||||
|
||||
const app = express();
|
||||
@@ -18,7 +19,10 @@ const DEV_FULLS_DIR = path.join(__dirname, '../images/fulls');
|
||||
const DEV_THUMBS_DIR = path.join(__dirname, '../images/thumbs');
|
||||
const FULLS_DIR = process.env.FULLS_DIR || (fs.existsSync(DEFAULT_FULLS_DIR) ? DEFAULT_FULLS_DIR : DEV_FULLS_DIR);
|
||||
const THUMBS_DIR = process.env.THUMBS_DIR || (fs.existsSync(DEFAULT_THUMBS_DIR) ? DEFAULT_THUMBS_DIR : DEV_THUMBS_DIR);
|
||||
const SYNC_IMAGE = process.env.SYNC_IMAGE || 'ghcr.io/kroonk/photography:latest';
|
||||
const SYNC_CONTAINER = process.env.SYNC_CONTAINER || 'photography-website';
|
||||
const IMAGE_RE = /\.(jpg|jpeg|png|gif|webp)$/i;
|
||||
let syncInProgress = false;
|
||||
|
||||
console.log('Using full-size images at', FULLS_DIR);
|
||||
console.log('Using thumbnails at', THUMBS_DIR);
|
||||
@@ -134,6 +138,12 @@ function serveProtectedImage(req, res) {
|
||||
});
|
||||
}
|
||||
|
||||
function runDocker(args, callback) {
|
||||
execFile('docker', args, { timeout: 5 * 60 * 1000 }, (err, stdout, stderr) => {
|
||||
callback(err, `${stdout || ''}${stderr || ''}`.trim());
|
||||
});
|
||||
}
|
||||
|
||||
// ==================== AUTH API ====================
|
||||
|
||||
app.post('/api/auth/login', (req, res) => {
|
||||
@@ -309,6 +319,41 @@ app.post('/api/admin/assign', requireAdmin, (req, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
app.post('/api/admin/sync', requireAdmin, (req, res) => {
|
||||
if (syncInProgress) {
|
||||
return res.status(409).json({ error: 'Sync laeuft bereits' });
|
||||
}
|
||||
|
||||
syncInProgress = true;
|
||||
runDocker(['pull', SYNC_IMAGE], (pullErr, pullOutput) => {
|
||||
syncInProgress = false;
|
||||
|
||||
if (pullErr) {
|
||||
console.error('Sync pull failed:', pullOutput || pullErr.message);
|
||||
return res.status(500).json({
|
||||
error: 'Docker pull fehlgeschlagen',
|
||||
details: pullOutput || pullErr.message
|
||||
});
|
||||
}
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Neues Image geladen. Container startet neu.',
|
||||
output: pullOutput
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
runDocker(['restart', SYNC_CONTAINER], (restartErr, restartOutput) => {
|
||||
if (restartErr) {
|
||||
console.error('Sync restart failed:', restartOutput || restartErr.message);
|
||||
return;
|
||||
}
|
||||
console.log('Sync restart complete:', restartOutput);
|
||||
});
|
||||
}, 500);
|
||||
});
|
||||
});
|
||||
|
||||
// ==================== STATIC FILES ====================
|
||||
|
||||
app.get(/^\/images\/(fulls|thumbs)\/(.+)$/, serveProtectedImage);
|
||||
|
||||
Reference in New Issue
Block a user