Harden unknown-project Node-RED functions for missing labels

This commit is contained in:
beatz174-bit
2026-04-13 06:19:57 +10:00
parent 9f9cfaf4be
commit 30f53eb668
4 changed files with 369 additions and 9 deletions
+172 -9
View File
@@ -460,7 +460,7 @@
"type": "function",
"z": "00b02bbd01c91485",
"name": "Unknown Project",
"func": "const labels = msg.payload.labels || {};\nconst container = labels.container;\nconst image = labels.compose_image || labels.running_image || labels.image;\nconst project = labels.com_docker_compose_project;\n\nnode.warn(`Unable to map project name ${project} to host.\n\n Updates for ${container} (${image}) failed`);\nreturn msg;",
"func": "const payload = (msg.payload && typeof msg.payload === \"object\") ? msg.payload : {};\nconst labels = (payload.labels && typeof payload.labels === \"object\") ? payload.labels : {};\n\nconst container = labels.container || \"unknown container\";\nconst image = labels.compose_image || labels.running_image || labels.image || \"unknown image\";\nconst project = labels.com_docker_compose_project || \"unknown project\";\n\nnode.warn(`Unable to map project name ${project} to host.\n\nUpdates for ${container} (${image}) failed`);\nreturn msg;",
"outputs": 1,
"timeout": "",
"noerr": 0,
@@ -936,7 +936,7 @@
"y": 200,
"wires": [
[
"0135d283b9edfb01"
"c1aa11bb22cc33dd"
]
]
},
@@ -972,7 +972,7 @@
"y": 300,
"wires": [
[
"4eafade32c867e40"
"d2aa11bb22cc33dd"
]
]
},
@@ -1008,7 +1008,7 @@
"y": 380,
"wires": [
[
"7d8200040f9b1e83"
"e3aa11bb22cc33dd"
]
]
},
@@ -1017,7 +1017,7 @@
"type": "function",
"z": "c5240b64a962ea54",
"name": "Docker updates Unknown Project",
"func": "const container = msg.container || \"unknown container\";\nconst code = msg.payload.code;\nconst stderr = flow.get(\"pull_stderr\") || \"Unknown error\";\nconst project = msg.payload.labels.com_docker_compose_project\nmsg.payload = {\n title: \"Container Updates Failed\",\n message: `The ${container} container has failed.\\n\n Unknown project ${project}`,\n priority: 8\n};\n\nreturn msg;",
"func": "const payload = (msg.payload && typeof msg.payload === \"object\") ? msg.payload : {};\nconst labels = (payload.labels && typeof payload.labels === \"object\") ? payload.labels : {};\n\nconst container = msg.container || labels.container || \"unknown container\";\nconst project = labels.com_docker_compose_project || msg.project || \"unknown project\";\n\nmsg.payload = {\n title: \"Container Updates Failed\",\n message: `The ${container} container has failed.\n\nUnknown project ${project}`,\n priority: 8\n};\n\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
@@ -1044,7 +1044,7 @@
"y": 460,
"wires": [
[
"c3d07241f4a570af"
"f4aa11bb22cc33dd"
]
]
},
@@ -1078,7 +1078,7 @@
"y": 520,
"wires": [
[
"d1346f7151103832"
"a5aa11bb22cc33dd"
]
]
},
@@ -1095,7 +1095,7 @@
"y": 580,
"wires": [
[
"1a9798d5c081240a"
"b6aa11bb22cc33dd"
]
]
},
@@ -1118,5 +1118,168 @@
"8630c7dfcdbcce50"
]
]
},
{
"id": "a1f8e9b2c3d4e5f6",
"type": "function",
"z": "c5240b64a962ea54",
"name": "Build update log event",
"func": "const nowIso = new Date().toISOString();\nconst startedAt = msg.update_started_at || Date.now();\nconst durationMs = Math.max(0, Date.now() - startedAt);\n\nconst payload = (msg.payload && typeof msg.payload === \"object\") ? msg.payload : {};\nconst labels = payload.labels || {};\n\nconst status = (msg.update_status || payload.status || \"unknown\").toString().toLowerCase();\n\nmsg.payload = JSON.stringify({\n ts: nowIso,\n flow: \"docker-updates\",\n event: msg.update_event || \"attempt\",\n container: msg.container || labels.container || \"unknown\",\n project: labels.com_docker_compose_project || msg.project || \"unknown\",\n host: msg.host || \"unknown\",\n status,\n success: status === \"success\" ? 1 : 0,\n failed: [\"failed\", \"locked\"].includes(status) ? 1 : 0,\n duration_ms: durationMs,\n code: Number.isFinite(Number(payload.code)) ? Number(payload.code) : 0,\n error: (msg.update_error || payload.error || \"\").toString().slice(0, 300)\n});\n\nreturn msg;",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 690,
"y": 660,
"wires": [
[
"b1c2d3e4f5a69788"
]
]
},
{
"id": "b1c2d3e4f5a69788",
"type": "file",
"z": "c5240b64a962ea54",
"name": "Write update event log",
"filename": "/data/update-events.ndjson",
"filenameType": "str",
"appendNewline": true,
"createDir": false,
"overwriteFile": "false",
"encoding": "none",
"x": 930,
"y": 660,
"wires": [
[]
]
},
{
"id": "c1aa11bb22cc33dd",
"type": "function",
"z": "c5240b64a962ea54",
"name": "Mark Docker Pull Failed",
"func": "msg.update_status = \"failed\";\nmsg.update_event = \"completed\";\nreturn msg;",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 470,
"y": 200,
"wires": [
[
"0135d283b9edfb01",
"a1f8e9b2c3d4e5f6"
]
]
},
{
"id": "d2aa11bb22cc33dd",
"type": "function",
"z": "c5240b64a962ea54",
"name": "Mark Docker Test Failed",
"func": "msg.update_status = \"failed\";\nmsg.update_event = \"completed\";\nreturn msg;",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 470,
"y": 300,
"wires": [
[
"4eafade32c867e40",
"a1f8e9b2c3d4e5f6"
]
]
},
{
"id": "e3aa11bb22cc33dd",
"type": "function",
"z": "c5240b64a962ea54",
"name": "Mark Docker Update Success",
"func": "msg.update_status = \"success\";\nmsg.update_event = \"completed\";\nreturn msg;",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 470,
"y": 380,
"wires": [
[
"7d8200040f9b1e83",
"a1f8e9b2c3d4e5f6"
]
]
},
{
"id": "f4aa11bb22cc33dd",
"type": "function",
"z": "c5240b64a962ea54",
"name": "Mark Docker Unknown Project",
"func": "msg.update_status = \"failed\";\nmsg.update_event = \"completed\";\nreturn msg;",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 470,
"y": 460,
"wires": [
[
"c3d07241f4a570af",
"a1f8e9b2c3d4e5f6"
]
]
},
{
"id": "a5aa11bb22cc33dd",
"type": "function",
"z": "c5240b64a962ea54",
"name": "Mark Docker Update Attempt",
"func": "msg.update_status = \"attempt\";\nmsg.update_event = \"attempt\";\nreturn msg;",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 470,
"y": 520,
"wires": [
[
"d1346f7151103832",
"a1f8e9b2c3d4e5f6"
]
]
},
{
"id": "b6aa11bb22cc33dd",
"type": "function",
"z": "c5240b64a962ea54",
"name": "Mark Docker Update Locked",
"func": "msg.update_status = \"locked\";\nmsg.update_event = \"completed\";\nreturn msg;",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 470,
"y": 580,
"wires": [
[
"1a9798d5c081240a",
"a1f8e9b2c3d4e5f6"
]
]
}
]
]