backend to retrieve pm2 processes from periphery

This commit is contained in:
beckerinj
2022-05-19 15:56:52 -04:00
parent 2b1d4149d1
commit 55e826ef6f
13 changed files with 1023 additions and 20 deletions
+19
View File
@@ -8,6 +8,7 @@ import { FastifyInstance } from "fastify";
import fp from "fastify-plugin";
import {
getPeripheryDockerStats,
getPeripheryPm2Processes,
getPeripherySystemStats,
} from "../util/periphery/server";
import { serverStatusPeriphery } from "../util/periphery/status";
@@ -123,6 +124,24 @@ const servers = fp((app: FastifyInstance, _: {}, done: () => void) => {
}
);
app.get("/api/server/:id/pm2", { onRequest: [app.auth, app.userEnabled] }, async (req, res) => {
const { id } = req.params as { id: string };
const server = await app.servers.findById(id);
if (!server) {
res.status(400);
res.send("server not found");
return;
}
const sender = (await app.users.findById(req.user.id))!;
if (sender.permissions! < 1 && !server.owners.includes(sender.username)) {
res.status(403);
res.send("inadequate permissions");
return;
}
const processes = server.isCore ? [] : await getPeripheryPm2Processes(server);
res.send(processes);
});
app.post(
"/api/server/:id/:owner",
{ onRequest: [app.auth, app.userEnabled] },
+15 -1
View File
@@ -1,4 +1,4 @@
import { CommandLogError, DockerStat, Server, SystemStats } from "@monitor/types";
import { CommandLogError, DockerStat, PM2Process, Server, SystemStats } from "@monitor/types";
import axios from "axios";
import { SECRETS } from "../../config";
@@ -31,3 +31,17 @@ export async function getPeripherySystemStats({ address, passkey }: Server) {
})
.then(({ data }) => data);
}
export async function getPeripheryPm2Processes({ address, passkey }: Server) {
try {
return await axios
.get<PM2Process[]>(`${address}/pm2List`, {
headers: {
Authorization: passkey || SECRETS.PASSKEY,
},
})
.then(({ data }) => data);
} catch (error) {
return []
}
}
+2
View File
@@ -13,4 +13,6 @@ RUN apk add --no-cache --virtual .gyp python3 make g++ \
COPY ./build/main.js ./
ENV PM2_HOME /.pm2
CMD node main.js
+2 -1
View File
@@ -24,6 +24,7 @@
"fastify-helmet": "^7.0.1",
"fastify-plugin": "^3.0.1",
"fs-extra": "^10.0.1",
"node-os-utils": "^1.3.6"
"node-os-utils": "^1.3.6",
"pm2": "^5.2.0"
}
}
+2
View File
@@ -5,12 +5,14 @@ import { LOG, PORT } from "./config";
import docker from "./plugins/docker";
import auth from "./plugins/auth";
import routes from "./routes";
import pm2 from "./plugins/pm2";
const app = fastify({ logger: LOG })
.register(fastifyHelmet)
.register(fastifyCors)
.register(docker)
.register(auth)
.register(pm2)
.register(routes);
app.listen(PORT, "0.0.0.0", (err, address) => {
+30
View File
@@ -0,0 +1,30 @@
import { FastifyInstance } from "fastify";
import PM2 from "pm2"
import fp from "fastify-plugin";
declare module "fastify" {
interface FastifyInstance {
pm2Enabled: {
get: () => boolean;
set: (enabled: boolean) => void;
};
}
}
const pm2 = fp((app: FastifyInstance, _: {}, done: () => void) => {
let pm2Enabled = true;
app.decorate("pm2Enabled", {
get: () => pm2Enabled,
set: (enabled: boolean) => {
pm2Enabled = enabled;
}
})
PM2.connect((err) => {
if (err) {
app.pm2Enabled.set(false)
}
})
done();
});
export default pm2;
+3 -1
View File
@@ -4,6 +4,7 @@ import container from "./container";
import deploy from "./deploy";
import git from "./git";
import networks from "./networks";
import pm2 from "./pm2";
import server from "./server";
import status from "./status";
@@ -14,7 +15,8 @@ const routes = fp((app: FastifyInstance, _: {}, done: () => void) => {
.register(container)
.register(server)
.register(status)
.register(networks);
.register(networks)
.register(pm2);
done();
});
+18
View File
@@ -0,0 +1,18 @@
import { FastifyInstance } from "fastify";
import fp from "fastify-plugin";
import { listPm2Processes } from "@monitor/util-node";
const pm2 = fp((app: FastifyInstance, _: {}, done: () => void) => {
app.get("/pm2List", { onRequest: [app.auth] }, async (_, res) => {
if (app.pm2Enabled.get()) {
const processes = await listPm2Processes();
res.send(processes);
} else {
res.send([]);
}
});
done();
});
export default pm2;
+11
View File
@@ -259,4 +259,15 @@ export type AccountAccess = {
type: "github" | "docker";
users: string[] // list of user usernames;
username: string; // specifies the account corresponding to those defined in secrets;
}
export type PM2Process = {
pid?: number;
name?: string;
status?: string;
cpu?: number;
memory?: number;
uptime?: number;
createdAt?: number;
restarts?: number;
}
+2 -1
View File
@@ -2,4 +2,5 @@ export * from "./helpers";
export * from "./docker";
export * from "./execute";
export * from "./git";
export * from "./sys-stats";
export * from "./sys-stats";
export * from "./pm2";
+2 -1
View File
@@ -14,6 +14,7 @@
"@types/node-os-utils": "^1.2.0",
"dockerode": "^3.3.1",
"fs-extra": "^10.0.1",
"node-os-utils": "^1.3.6"
"node-os-utils": "^1.3.6",
"pm2": "^5.2.0"
}
}
+23
View File
@@ -0,0 +1,23 @@
import PM2 from "pm2";
import { PM2Process } from "@monitor/types"
export function listPm2Processes(): Promise<PM2Process[]> {
return new Promise((res, rej) => {
PM2.list((err, list) => {
if (err) {
rej(err);
} else {
res(list.map(p => ({
pid: p.pid,
name: p.name,
status: p.pm2_env?.status,
cpu: p.monit?.cpu,
memory: p.monit?.memory,
uptime: p.pm2_env?.pm_uptime ? Date.now() - p.pm2_env?.pm_uptime : 0,
createdAt: (p.pm2_env as any)?.created_at,
restarts: p.pm2_env?.unstable_restarts
})))
}
})
})
}
+894 -15
View File
File diff suppressed because it is too large Load Diff