86 lines
2.2 KiB
TypeScript
86 lines
2.2 KiB
TypeScript
import express from 'express';
|
|
import {existsSync, readdirSync, readFileSync} from 'fs';
|
|
|
|
import {Config} from './config.js';
|
|
|
|
import {RegHelper} from './deployments.js';
|
|
|
|
const app = express();
|
|
app.use(express.json());
|
|
|
|
app.use(function (_req, res, next) {
|
|
res.header('Access-Control-Allow-Origin', '*');
|
|
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept',);
|
|
next();
|
|
});
|
|
|
|
const logForRequest = (req_id) => {
|
|
let ret = null;
|
|
const logDir = `${Config.BUILD_LOGS}/${req_id}`;
|
|
if (existsSync(logDir)) {
|
|
const logFiles = readdirSync(logDir).sort();
|
|
if (logFiles.length) {
|
|
ret = `${logDir}/${logFiles.pop()}`;
|
|
}
|
|
}
|
|
return ret;
|
|
};
|
|
|
|
const adjustFromState = (regStatus: any[]) => {
|
|
let deployerState = {};
|
|
if (Config.DEPLOYER_STATE && existsSync(Config.DEPLOYER_STATE)) {
|
|
deployerState = JSON.parse(readFileSync(Config.DEPLOYER_STATE).toString());
|
|
}
|
|
for (const r of regStatus) {
|
|
r.logAvailable = null != logForRequest(r.id);
|
|
// If we know something more specific in the local state, use that ...
|
|
if (deployerState[r.id]) {
|
|
switch (deployerState[r.id].status) {
|
|
case 'DEPLOYED': {
|
|
r.lastState = deployerState[r.id].status;
|
|
break;
|
|
}
|
|
case 'DEPLOYING': {
|
|
r.lastState = deployerState[r.id].status;
|
|
break;
|
|
}
|
|
case 'ERROR': {
|
|
r.lastState = deployerState[r.id].status;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return regStatus;
|
|
}
|
|
|
|
app.get('/', async (_req, res) => {
|
|
const reg = new RegHelper();
|
|
const regStatus = adjustFromState(await reg.deploymentRequestStatus());
|
|
res.send(regStatus);
|
|
});
|
|
|
|
app.get('/:id', async (req, res) => {
|
|
const reg = new RegHelper();
|
|
const records = adjustFromState(await reg.deploymentRequestStatus(req.params.id));
|
|
if (records.length) {
|
|
res.send(records[0]);
|
|
} else {
|
|
res.sendStatus(410);
|
|
}
|
|
});
|
|
|
|
app.get('/log/:id', async (req, res) => {
|
|
const logFile = logForRequest(req.params.id);
|
|
if (!logFile) {
|
|
res.sendStatus(410);
|
|
return;
|
|
}
|
|
|
|
res.send(readFileSync(logFile));
|
|
});
|
|
|
|
app.listen(Config.LISTEN_PORT, Config.LISTEN_ADDR, () => {
|
|
console.log(`listening on ${Config.LISTEN_ADDR}:${Config.LISTEN_PORT}`);
|
|
});
|