From b1def791ca4d72bc50cbb9905027da3fb29d8f40 Mon Sep 17 00:00:00 2001 From: beckerinj Date: Mon, 30 May 2022 13:35:33 +0300 Subject: [PATCH] add pm2 flush support --- core/src/routes/pm2.ts | 27 +++++++++++++++++++++++++-- core/src/util/periphery/pm2.ts | 14 ++++++++++++++ periphery/src/routes/pm2.ts | 11 +++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/core/src/routes/pm2.ts b/core/src/routes/pm2.ts index 9e2f4059a..d16c33106 100644 --- a/core/src/routes/pm2.ts +++ b/core/src/routes/pm2.ts @@ -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(); }); diff --git a/core/src/util/periphery/pm2.ts b/core/src/util/periphery/pm2.ts index 15ace2dad..cb2bc92ed 100644 --- a/core/src/util/periphery/pm2.ts +++ b/core/src/util/periphery/pm2.ts @@ -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(`${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 }; + } } \ No newline at end of file diff --git a/periphery/src/routes/pm2.ts b/periphery/src/routes/pm2.ts index bf5134ba3..c29d00b96 100644 --- a/periphery/src/routes/pm2.ts +++ b/periphery/src/routes/pm2.ts @@ -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); } \ No newline at end of file