More auto device removal fixes. #3089

This commit is contained in:
Ylian Saint-Hilaire 2021-09-01 22:30:43 -07:00
parent 7d25391f78
commit b2905bfe29
2 changed files with 54 additions and 53 deletions

106
db.js
View File

@ -156,66 +156,68 @@ module.exports.CreateDB = function (parent, func) {
var expireDays = -1; var expireDays = -1;
if (removeInactiveDevicesPerDomain[docs[j].domain]) { expireDays = removeInactiveDevicesPerDomain[docs[j].domain]; } if (removeInactiveDevicesPerDomain[docs[j].domain]) { expireDays = removeInactiveDevicesPerDomain[docs[j].domain]; }
const mesh = parent.webserver.meshes[docs[j].meshid]; const mesh = parent.webserver.meshes[docs[j].meshid];
if (mesh && (typeof mesh.expireDevs == 'number') && (expireDays > mesh.expireDevs)) { expireDays = mesh.expireDevs; } if (mesh && (typeof mesh.expireDevs == 'number')) { expireDays = mesh.expireDevs; }
var remove = false; var remove = false;
if ((expireDays > 0) && (expireDays < days)) { remove = true; } if (expireDays > 0) {
if (cb) { if (showall || remove) { cb(docs[j]._id.substring(2) + ', ' + days + ' days, expire ' + expireDays + ' days' + (remove ? ', removing' : '')); } } if (expireDays < days) { remove = true; }
if (remove) { if (cb) { if (showall || remove) { cb(docs[j]._id.substring(2) + ', ' + days + ' days, expire ' + expireDays + ' days' + (remove ? ', removing' : '')); } }
// Check if this device is connected right now if (remove) {
const nodeid = docs[j]._id.substring(2); // Check if this device is connected right now
const conn = parent.GetConnectivityState(nodeid); const nodeid = docs[j]._id.substring(2);
if (conn == null) { const conn = parent.GetConnectivityState(nodeid);
// Remove the device if (conn == null) {
obj.Get(nodeid, function (err, docs) { // Remove the device
if (err != null) return; obj.Get(nodeid, function (err, docs) {
if ((docs == null) || (docs.length != 1)) { obj.Remove('lc' + nodeid); return; } // Remove last connect time if (err != null) return;
const node = docs[0]; if ((docs == null) || (docs.length != 1)) { obj.Remove('lc' + nodeid); return; } // Remove last connect time
const node = docs[0];
// Delete this node including network interface information, events and timeline // Delete this node including network interface information, events and timeline
obj.Remove(node._id); // Remove node with that id obj.Remove(node._id); // Remove node with that id
obj.Remove('if' + node._id); // Remove interface information obj.Remove('if' + node._id); // Remove interface information
obj.Remove('nt' + node._id); // Remove notes obj.Remove('nt' + node._id); // Remove notes
obj.Remove('lc' + node._id); // Remove last connect time obj.Remove('lc' + node._id); // Remove last connect time
obj.Remove('si' + node._id); // Remove system information obj.Remove('si' + node._id); // Remove system information
obj.Remove('al' + node._id); // Remove error log last time obj.Remove('al' + node._id); // Remove error log last time
if (obj.RemoveSMBIOS) { obj.RemoveSMBIOS(node._id); } // Remove SMBios data if (obj.RemoveSMBIOS) { obj.RemoveSMBIOS(node._id); } // Remove SMBios data
obj.RemoveAllNodeEvents(node._id); // Remove all events for this node obj.RemoveAllNodeEvents(node._id); // Remove all events for this node
obj.removeAllPowerEventsForNode(node._id); // Remove all power events for this node obj.removeAllPowerEventsForNode(node._id); // Remove all power events for this node
if (typeof node.pmt == 'string') { obj.Remove('pmt_' + node.pmt); } // Remove Push Messaging Token if (typeof node.pmt == 'string') { obj.Remove('pmt_' + node.pmt); } // Remove Push Messaging Token
obj.Get('ra' + node._id, function (err, nodes) { obj.Get('ra' + node._id, function (err, nodes) {
if ((nodes != null) && (nodes.length == 1)) { obj.Remove('da' + nodes[0].daid); } // Remove diagnostic agent to real agent link if ((nodes != null) && (nodes.length == 1)) { obj.Remove('da' + nodes[0].daid); } // Remove diagnostic agent to real agent link
obj.Remove('ra' + node._id); // Remove real agent to diagnostic agent link obj.Remove('ra' + node._id); // Remove real agent to diagnostic agent link
}); });
// Remove any user node links // Remove any user node links
if (node.links != null) { if (node.links != null) {
for (var i in node.links) { for (var i in node.links) {
if (i.startsWith('user/')) { if (i.startsWith('user/')) {
var cuser = parent.webserver.users[i]; var cuser = parent.webserver.users[i];
if ((cuser != null) && (cuser.links != null) && (cuser.links[node._id] != null)) { if ((cuser != null) && (cuser.links != null) && (cuser.links[node._id] != null)) {
// Remove the user link & save the user // Remove the user link & save the user
delete cuser.links[node._id]; delete cuser.links[node._id];
if (Object.keys(cuser.links).length == 0) { delete cuser.links; } if (Object.keys(cuser.links).length == 0) { delete cuser.links; }
obj.SetUser(cuser); obj.SetUser(cuser);
// Notify user change // Notify user change
var targets = ['*', 'server-users', cuser._id]; var targets = ['*', 'server-users', cuser._id];
var event = { etype: 'user', userid: cuser._id, username: cuser.name, action: 'accountchange', msgid: 86, msgArgs: [cuser.name], msg: 'Removed user device rights for ' + cuser.name, domain: node.domain, account: parent.webserver.CloneSafeUser(cuser) }; var event = { etype: 'user', userid: cuser._id, username: cuser.name, action: 'accountchange', msgid: 86, msgArgs: [cuser.name], msg: 'Removed user device rights for ' + cuser.name, domain: node.domain, account: parent.webserver.CloneSafeUser(cuser) };
if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the user. Another event will come. if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the user. Another event will come.
parent.DispatchEvent(targets, obj, event); parent.DispatchEvent(targets, obj, event);
}
} }
} }
} }
}
// Event node deletion // Event node deletion
var meshname = '(unknown)'; var meshname = '(unknown)';
if ((parent.webserver.meshes[node.meshid] != null) && (parent.webserver.meshes[node.meshid].name != null)) { meshname = parent.webserver.meshes[node.meshid].name; } if ((parent.webserver.meshes[node.meshid] != null) && (parent.webserver.meshes[node.meshid].name != null)) { meshname = parent.webserver.meshes[node.meshid].name; }
var event = { etype: 'node', action: 'removenode', nodeid: node._id, msgid: 87, msgArgs: [node.name, meshname], msg: 'Removed device ' + node.name + ' from device group ' + meshname, domain: node.domain }; var event = { etype: 'node', action: 'removenode', nodeid: node._id, msgid: 87, msgArgs: [node.name, meshname], msg: 'Removed device ' + node.name + ' from device group ' + meshname, domain: node.domain };
// TODO: We can't use the changeStream for node delete because we will not know the meshid the device was in. // TODO: We can't use the changeStream for node delete because we will not know the meshid the device was in.
//if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to remove the node. Another event will come. //if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to remove the node. Another event will come.
parent.DispatchEvent(parent.webserver.CreateNodeDispatchTargets(node.meshid, node._id), obj, event); parent.DispatchEvent(parent.webserver.CreateNodeDispatchTargets(node.meshid, node._id), obj, event);
}); });
}
} }
} }
} }

View File

@ -6255,7 +6255,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
cmdData.result = 'Usage: removeinactivedevices [showremoved|showall]'; cmdData.result = 'Usage: removeinactivedevices [showremoved|showall]';
} else { } else {
parent.db.removeInactiveDevices((arg == 'showall'), function (msg) { try { ws.send(JSON.stringify({ action: 'serverconsole', value: msg, tag: cmdData.command.tag })); } catch (ex) { } }); parent.db.removeInactiveDevices((arg == 'showall'), function (msg) { try { ws.send(JSON.stringify({ action: 'serverconsole', value: msg, tag: cmdData.command.tag })); } catch (ex) { } });
cmdData.result = 'Ok';
} }
} }