updatenotification: update_helper.js recode with pm2 library (v2.27.x) (#3332)

#3285

Because there is so many conflit with package,
I have rewrite the code with v2.27.0-develop

For remember:

 * recode: `update_helper.js` with `pm2` library
 * fix: default config -> `updates` is a array
 * delete: `command-exists` library (not used)
 * delete: `PM2_GetList()`  function (not used)
 * add: check `updates.length` (prevent crash)
 * add: `[PM2]` tag in log (for better visibility)
 * add: `pm2` library
 
advantage:
  * we use the pm2 library directly
* avoids weird returns from child_process.exec when requesting a json
format from pm2
  * simplified the code

inconvenient:
  * we have vulnerabilities with axios

240120 Fix:
* use `pm2_env.pm_cwd` instead of `pm2_env.PWD` : prevent using `pm2
restart <id> --update-env` in other directory (for enable GPU rendering
for exemple)
 * resolve packages (again)
This commit is contained in:
Bugsounet - Cédric 2024-01-20 17:38:22 +01:00 committed by GitHub
parent 995b61b689
commit c96ced9137
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 1065 additions and 88 deletions

View File

@ -16,3 +16,5 @@ jobs:
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: "Dependency Review" - name: "Dependency Review"
uses: actions/dependency-review-action@v3 uses: actions/dependency-review-action@v3
with:
allow-ghsas: GHSA-wf5p-g6vw-rhxx

View File

@ -15,6 +15,7 @@ _This release is scheduled to be released on 2024-04-01._
### Updated ### Updated
- Update updatenotification (update_helper.js): Recode with pm2 library (#3332)
- Removing lodash dependency by replacing merge by spread operator (#3339) - Removing lodash dependency by replacing merge by spread operator (#3339)
- Use node prefix for build-in modules (#3340) - Use node prefix for build-in modules (#3340)
- Rework logging colors (#3350) - Rework logging colors (#3350)

View File

@ -1,6 +1,7 @@
const Exec = require("node:child_process").exec; const Exec = require("node:child_process").exec;
const Spawn = require("node:child_process").spawn; const Spawn = require("node:child_process").spawn;
const commandExists = require("command-exists"); const pm2 = require("pm2");
const Log = require("logger"); const Log = require("logger");
/* class Updater /* class Updater
@ -138,7 +139,7 @@ class Updater {
// restart MagicMiror with "pm2" // restart MagicMiror with "pm2"
pm2Restart () { pm2Restart () {
Log.info("updatenotification: PM2 will restarting MagicMirror..."); Log.info("updatenotification: PM2 will restarting MagicMirror...");
Exec(`pm2 restart ${this.PM2}`, (err, std, sde) => { pm2.restart(this.PM2, (err, proc) => {
if (err) { if (err) {
Log.error("updatenotification:[PM2] restart Error", err); Log.error("updatenotification:[PM2] restart Error", err);
} }
@ -159,54 +160,36 @@ class Updater {
check_PM2_Process () { check_PM2_Process () {
Log.info("updatenotification: Checking PM2 using..."); Log.info("updatenotification: Checking PM2 using...");
return new Promise((resolve) => { return new Promise((resolve) => {
commandExists("pm2") pm2.connect((err) => {
.then(async () => { if (err) {
var PM2_List = await this.PM2_GetList(); Log.error("updatenotification: [PM2]", err);
if (!PM2_List) { this.usePM2 = false;
resolve(false);
return;
}
pm2.list((err, list) => {
if (err) {
Log.error("updatenotification: [PM2] Can't get process List!"); Log.error("updatenotification: [PM2] Can't get process List!");
this.usePM2 = false; this.usePM2 = false;
resolve(false); resolve(false);
return; return;
} }
PM2_List.forEach((pm) => { list.forEach((pm) => {
if (pm.pm2_env.version === this.version && pm.pm2_env.status === "online" && pm.pm2_env.PWD.includes(this.root_path)) { if (pm.pm2_env.version === this.version && pm.pm2_env.status === "online" && pm.pm2_env.pm_cwd.includes(`${this.root_path}/`)) {
this.PM2 = pm.name; this.PM2 = pm.name;
this.usePM2 = true; this.usePM2 = true;
Log.info("updatenotification: You are using pm2 with", this.PM2); Log.info("updatenotification: [PM2] You are using pm2 with", this.PM2);
resolve(true); resolve(true);
} }
}); });
pm2.disconnect();
if (!this.PM2) { if (!this.PM2) {
Log.info("updatenotification: You are not using pm2"); Log.info("updatenotification: [PM2] You are not using pm2");
this.usePM2 = false; this.usePM2 = false;
resolve(false); resolve(false);
} }
})
.catch(() => {
Log.info("updatenotification: You are not using pm2");
this.usePM2 = false;
resolve(false);
}); });
}); });
}
// Get the list of pm2 process
PM2_GetList () {
return new Promise((resolve) => {
Exec("pm2 jlist", (err, std, sde) => {
if (err) {
resolve(null);
return;
}
try {
let result = JSON.parse(std);
resolve(result);
} catch (e) {
Log.error("updatenotification: [PM2] can't GetList!");
Log.debug("updatenotification: [PM2] GetList is not an JSON format", e);
resolve(null);
}
});
}); });
} }
@ -218,7 +201,7 @@ class Updater {
// search update module command // search update module command
applyCommand (module) { applyCommand (module) {
if (this.isMagicMirror(module.module)) return null; if (this.isMagicMirror(module.module) || !this.updates.length) return null;
let command = null; let command = null;
this.updates.forEach((updater) => { this.updates.forEach((updater) => {
if (updater[module]) command = updater[module]; if (updater[module]) command = updater[module];

View File

@ -18,7 +18,7 @@ Module.register("updatenotification", {
suspended: false, suspended: false,
moduleList: {}, moduleList: {},
needRestart: false, needRestart: false,
updates: {}, updates: [],
start () { start () {
Log.info(`Starting module: ${this.name}`); Log.info(`Starting module: ${this.name}`);

1093
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -72,7 +72,6 @@
}, },
"dependencies": { "dependencies": {
"ansis": "^2.0.3", "ansis": "^2.0.3",
"command-exists": "^1.2.9",
"console-stamp": "^3.1.2", "console-stamp": "^3.1.2",
"envsub": "^4.1.0", "envsub": "^4.1.0",
"eslint": "^8.56.0", "eslint": "^8.56.0",
@ -85,6 +84,7 @@
"module-alias": "^2.2.3", "module-alias": "^2.2.3",
"moment": "^2.30.1", "moment": "^2.30.1",
"node-ical": "^0.17.1", "node-ical": "^0.17.1",
"pm2": "^5.3.0",
"socket.io": "^4.7.4", "socket.io": "^4.7.4",
"systeminformation": "^5.21.22" "systeminformation": "^5.21.22"
}, },