add pm2 flush support

This commit is contained in:
beckerinj
2022-05-30 13:35:33 +03:00
parent 72faf36719
commit b1def791ca
3 changed files with 50 additions and 2 deletions

View File

@@ -1,7 +1,6 @@
import { FastifyInstance } from "fastify";
import fp from "fastify-plugin";
import { deletePeripheryPm2, getPeripheryPm2Log, getPeripheryPm2Processes, restartPeripheryPm2, startPeripheryPm2, stopPeripheryPm2 } from "../util/periphery/pm2";
// import { addPm2Update } from "../util/updates";
import { deletePeripheryPm2, flushPm2Logs, getPeripheryPm2Log, getPeripheryPm2Processes, restartPeripheryPm2, startPeripheryPm2, stopPeripheryPm2 } from "../util/periphery/pm2";
const pm2 = fp((app: FastifyInstance, _: {}, done: () => void) => {
app.get("/api/server/:id/pm2/processes", { onRequest: [app.auth, app.userEnabled] }, async (req, res) => {
@@ -143,6 +142,30 @@ const pm2 = fp((app: FastifyInstance, _: {}, done: () => void) => {
res.send(log);
});
app.get("/api/server/:id/pm2/flush", { onRequest: [app.auth, app.userEnabled] }, async (req, res) => {
const { id } = req.params as { id: string };
const { name } = req.query as { name?: string };
const server = await app.servers.findById(id);
if (!server) {
res.status(400);
res.send("server not found");
return;
}
const user = (await app.users.findById(req.user.id))!;
if (user.permissions! < 1 && !server.owners.includes(user.username)) {
res.status(403);
res.send("inadequate permissions");
return;
}
if (server.isCore) {
res.status(400);
res.send("monitor core does not support pm2");
return;
}
const log = await flushPm2Logs(server, name);
res.send(log);
});
done();
});

View File

@@ -85,4 +85,18 @@ export async function deletePeripheryPm2({ address, passkey }: Server, name: str
} catch (error) {
return { command: "try to delete process", log: { stderr: "could not reach pm2 client" }, isError: true };
}
}
export async function flushPm2Logs({ address, passkey }: Server, name?: string) {
try {
return await axios
.get<CommandLogError>(`${address}/pm2/flush` + generateQuery({ name }), {
headers: {
Authorization: passkey || SECRETS.PASSKEY,
},
})
.then(({ data }) => data);
} catch (error) {
return { command: "try to flush logs", log: { stderr: "could not reach pm2 client" }, isError: true };
}
}

View File

@@ -96,6 +96,12 @@ const pm2 = fp((app: FastifyInstance, _: {}, done: () => void) => {
}
});
app.get("/pm2/flush", { onRequest: [app.auth] }, async (req, res) => {
const { name } = req.query as { name?: string };
const log = await flushLogs(name);
res.send(log);
});
done();
});
@@ -129,4 +135,9 @@ async function restartPm2(name: string) {
async function deletePm2(name: string) {
return await axios.get(`http://host.docker.internal:${PM2_CLIENT_PORT}/delete/${name}`)
.then(({ data }) => data);
}
async function flushLogs(name?: string) {
return await axios.get(`http://host.docker.internal:${PM2_CLIENT_PORT}/flush` + generateQuery({ name }))
.then(({ data }) => data);
}