@blackbelt-technology/pi-dashboard-document-converter 0.10.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -6,6 +6,8 @@ The server is started on-demand and can be stopped when no longer needed.
|
|
|
6
6
|
Uses Gotenberg (https://gotenberg.dev/) which supports ARM64/Apple Silicon natively.
|
|
7
7
|
"""
|
|
8
8
|
import atexit
|
|
9
|
+
import json
|
|
10
|
+
import logging
|
|
9
11
|
import socket
|
|
10
12
|
import subprocess
|
|
11
13
|
import time
|
|
@@ -13,6 +15,10 @@ from typing import Optional
|
|
|
13
15
|
import urllib.request
|
|
14
16
|
import urllib.error
|
|
15
17
|
|
|
18
|
+
logger = logging.getLogger(__name__)
|
|
19
|
+
|
|
20
|
+
LOOPBACK_HOSTS = ('127.0.0.1', '::1')
|
|
21
|
+
|
|
16
22
|
|
|
17
23
|
class PDFServerError(Exception):
|
|
18
24
|
"""Exception raised for PDF server errors."""
|
|
@@ -26,7 +32,7 @@ def get_available_port() -> int:
|
|
|
26
32
|
An available port number.
|
|
27
33
|
"""
|
|
28
34
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
29
|
-
s.bind(('', 0))
|
|
35
|
+
s.bind(('127.0.0.1', 0)) # loopback probe only (CodeQL py/bind-socket-all-network-interfaces)
|
|
30
36
|
s.listen(1)
|
|
31
37
|
port = s.getsockname()[1]
|
|
32
38
|
return port
|
|
@@ -167,6 +173,51 @@ class PDFServer:
|
|
|
167
173
|
f"Failed to start container: {result.stderr}"
|
|
168
174
|
)
|
|
169
175
|
|
|
176
|
+
def _published_on_loopback(self) -> bool:
|
|
177
|
+
"""True only if every host binding of the container port is a literal loopback IP.
|
|
178
|
+
|
|
179
|
+
Uses `docker inspect` (works for stopped containers, unlike `docker port`).
|
|
180
|
+
Inspect failure, unparsable output, no binding, an empty/wildcard HostIp,
|
|
181
|
+
`localhost` or any other address all count as non-loopback.
|
|
182
|
+
"""
|
|
183
|
+
result = subprocess.run(
|
|
184
|
+
['docker', 'inspect', '-f', '{{json .HostConfig.PortBindings}}',
|
|
185
|
+
self.CONTAINER_NAME],
|
|
186
|
+
capture_output=True,
|
|
187
|
+
text=True
|
|
188
|
+
)
|
|
189
|
+
if result.returncode != 0:
|
|
190
|
+
return False
|
|
191
|
+
try:
|
|
192
|
+
data = json.loads(result.stdout)
|
|
193
|
+
except ValueError:
|
|
194
|
+
return False
|
|
195
|
+
if not isinstance(data, dict):
|
|
196
|
+
return False
|
|
197
|
+
entries = data.get(f'{self.CONTAINER_PORT}/tcp')
|
|
198
|
+
if not isinstance(entries, list) or not entries:
|
|
199
|
+
return False
|
|
200
|
+
return all(
|
|
201
|
+
isinstance(e, dict) and e.get('HostIp') in LOOPBACK_HOSTS
|
|
202
|
+
for e in entries
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
def _remove_container(self) -> None:
|
|
206
|
+
"""Force-remove the container (running or stopped).
|
|
207
|
+
|
|
208
|
+
Raises:
|
|
209
|
+
PDFServerError: If removal fails, unless the container is already gone.
|
|
210
|
+
"""
|
|
211
|
+
result = subprocess.run(
|
|
212
|
+
['docker', 'rm', '-f', self.CONTAINER_NAME],
|
|
213
|
+
capture_output=True,
|
|
214
|
+
text=True
|
|
215
|
+
)
|
|
216
|
+
if result.returncode != 0 and 'No such container' not in (result.stderr or ''):
|
|
217
|
+
raise PDFServerError(
|
|
218
|
+
f"Failed to remove container: {result.stderr}"
|
|
219
|
+
)
|
|
220
|
+
|
|
170
221
|
def _create_container(self) -> None:
|
|
171
222
|
"""Create and start a new container."""
|
|
172
223
|
self._port = get_available_port()
|
|
@@ -174,7 +225,7 @@ class PDFServer:
|
|
|
174
225
|
[
|
|
175
226
|
'docker', 'run', '-d',
|
|
176
227
|
'--name', self.CONTAINER_NAME,
|
|
177
|
-
'-p', f'{self._port}:{self.CONTAINER_PORT}',
|
|
228
|
+
'-p', f'127.0.0.1:{self._port}:{self.CONTAINER_PORT}',
|
|
178
229
|
self.IMAGE_NAME
|
|
179
230
|
],
|
|
180
231
|
capture_output=True,
|
|
@@ -209,15 +260,23 @@ class PDFServer:
|
|
|
209
260
|
"Docker is not available. Please install and start Docker."
|
|
210
261
|
)
|
|
211
262
|
|
|
212
|
-
|
|
213
|
-
if self.is_container_running():
|
|
214
|
-
port = self.get_container_port()
|
|
215
|
-
if port:
|
|
216
|
-
self._port = port
|
|
217
|
-
return f"http://localhost:{port}"
|
|
263
|
+
exists = self.is_container_running() or self.is_container_exists()
|
|
218
264
|
|
|
265
|
+
# Legacy container published on all interfaces: recreate before any reuse/start.
|
|
266
|
+
if exists and not self._published_on_loopback():
|
|
267
|
+
logger.warning(
|
|
268
|
+
"Container %s is not published on loopback only; recreating it",
|
|
269
|
+
self.CONTAINER_NAME,
|
|
270
|
+
)
|
|
271
|
+
self._remove_container()
|
|
272
|
+
self._create_container()
|
|
273
|
+
self._started_by_us = True
|
|
274
|
+
# Check if already running
|
|
275
|
+
elif self.is_container_running() and (port := self.get_container_port()):
|
|
276
|
+
self._port = port
|
|
277
|
+
return f"http://localhost:{port}"
|
|
219
278
|
# Start or create container
|
|
220
|
-
|
|
279
|
+
elif exists:
|
|
221
280
|
self._start_container()
|
|
222
281
|
self._started_by_us = True
|
|
223
282
|
# Get the port after starting
|
|
@@ -4,6 +4,8 @@ This module manages a Docker-based PlantUML server for rendering diagrams.
|
|
|
4
4
|
The server is started on-demand and can be stopped when no longer needed.
|
|
5
5
|
"""
|
|
6
6
|
import atexit
|
|
7
|
+
import json
|
|
8
|
+
import logging
|
|
7
9
|
import socket
|
|
8
10
|
import subprocess
|
|
9
11
|
import time
|
|
@@ -11,6 +13,10 @@ from typing import Optional
|
|
|
11
13
|
import urllib.request
|
|
12
14
|
import urllib.error
|
|
13
15
|
|
|
16
|
+
logger = logging.getLogger(__name__)
|
|
17
|
+
|
|
18
|
+
LOOPBACK_HOSTS = ('127.0.0.1', '::1')
|
|
19
|
+
|
|
14
20
|
|
|
15
21
|
class PlantUMLServerError(Exception):
|
|
16
22
|
"""Exception raised for PlantUML server errors."""
|
|
@@ -24,7 +30,7 @@ def get_available_port() -> int:
|
|
|
24
30
|
An available port number.
|
|
25
31
|
"""
|
|
26
32
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
27
|
-
s.bind(('', 0))
|
|
33
|
+
s.bind(('127.0.0.1', 0)) # loopback probe only (CodeQL py/bind-socket-all-network-interfaces)
|
|
28
34
|
s.listen(1)
|
|
29
35
|
port = s.getsockname()[1]
|
|
30
36
|
return port
|
|
@@ -157,6 +163,51 @@ class PlantUMLServer:
|
|
|
157
163
|
f"Failed to start container: {result.stderr}"
|
|
158
164
|
)
|
|
159
165
|
|
|
166
|
+
def _published_on_loopback(self) -> bool:
|
|
167
|
+
"""True only if every host binding of the container port is a literal loopback IP.
|
|
168
|
+
|
|
169
|
+
Uses `docker inspect` (works for stopped containers, unlike `docker port`).
|
|
170
|
+
Inspect failure, unparsable output, no binding, an empty/wildcard HostIp,
|
|
171
|
+
`localhost` or any other address all count as non-loopback.
|
|
172
|
+
"""
|
|
173
|
+
result = subprocess.run(
|
|
174
|
+
['docker', 'inspect', '-f', '{{json .HostConfig.PortBindings}}',
|
|
175
|
+
self.CONTAINER_NAME],
|
|
176
|
+
capture_output=True,
|
|
177
|
+
text=True
|
|
178
|
+
)
|
|
179
|
+
if result.returncode != 0:
|
|
180
|
+
return False
|
|
181
|
+
try:
|
|
182
|
+
data = json.loads(result.stdout)
|
|
183
|
+
except ValueError:
|
|
184
|
+
return False
|
|
185
|
+
if not isinstance(data, dict):
|
|
186
|
+
return False
|
|
187
|
+
entries = data.get('8080/tcp')
|
|
188
|
+
if not isinstance(entries, list) or not entries:
|
|
189
|
+
return False
|
|
190
|
+
return all(
|
|
191
|
+
isinstance(e, dict) and e.get('HostIp') in LOOPBACK_HOSTS
|
|
192
|
+
for e in entries
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
def _remove_container(self) -> None:
|
|
196
|
+
"""Force-remove the container (running or stopped).
|
|
197
|
+
|
|
198
|
+
Raises:
|
|
199
|
+
PlantUMLServerError: If removal fails, unless the container is already gone.
|
|
200
|
+
"""
|
|
201
|
+
result = subprocess.run(
|
|
202
|
+
['docker', 'rm', '-f', self.CONTAINER_NAME],
|
|
203
|
+
capture_output=True,
|
|
204
|
+
text=True
|
|
205
|
+
)
|
|
206
|
+
if result.returncode != 0 and 'No such container' not in (result.stderr or ''):
|
|
207
|
+
raise PlantUMLServerError(
|
|
208
|
+
f"Failed to remove container: {result.stderr}"
|
|
209
|
+
)
|
|
210
|
+
|
|
160
211
|
def _create_container(self) -> None:
|
|
161
212
|
"""Create and start a new container."""
|
|
162
213
|
self._port = get_available_port()
|
|
@@ -164,7 +215,7 @@ class PlantUMLServer:
|
|
|
164
215
|
[
|
|
165
216
|
'docker', 'run', '-d',
|
|
166
217
|
'--name', self.CONTAINER_NAME,
|
|
167
|
-
'-p', f'{self._port}:8080',
|
|
218
|
+
'-p', f'127.0.0.1:{self._port}:8080',
|
|
168
219
|
self.IMAGE_NAME
|
|
169
220
|
],
|
|
170
221
|
capture_output=True,
|
|
@@ -199,15 +250,23 @@ class PlantUMLServer:
|
|
|
199
250
|
"Docker is not available. Please install and start Docker."
|
|
200
251
|
)
|
|
201
252
|
|
|
202
|
-
|
|
203
|
-
if self.is_container_running():
|
|
204
|
-
port = self.get_container_port()
|
|
205
|
-
if port:
|
|
206
|
-
self._port = port
|
|
207
|
-
return f"http://localhost:{port}"
|
|
253
|
+
exists = self.is_container_running() or self.is_container_exists()
|
|
208
254
|
|
|
255
|
+
# Legacy container published on all interfaces: recreate before any reuse/start.
|
|
256
|
+
if exists and not self._published_on_loopback():
|
|
257
|
+
logger.warning(
|
|
258
|
+
"Container %s is not published on loopback only; recreating it",
|
|
259
|
+
self.CONTAINER_NAME,
|
|
260
|
+
)
|
|
261
|
+
self._remove_container()
|
|
262
|
+
self._create_container()
|
|
263
|
+
self._started_by_us = True
|
|
264
|
+
# Check if already running
|
|
265
|
+
elif self.is_container_running() and (port := self.get_container_port()):
|
|
266
|
+
self._port = port
|
|
267
|
+
return f"http://localhost:{port}"
|
|
209
268
|
# Start or create container
|
|
210
|
-
|
|
269
|
+
elif exists:
|
|
211
270
|
self._start_container()
|
|
212
271
|
self._started_by_us = True
|
|
213
272
|
# Get the port after starting
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blackbelt-technology/pi-dashboard-document-converter",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "TypeScript facade over a Dockerized Python document engine (pi-doc-engine). Bidirectional: ingest any format to provenance-stamped Markdown for kb, produce templated DOCX/PDF with diagrams. Engine quarantined in Docker; TS is the only call surface.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"test": "vitest run"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@blackbelt-technology/pi-dashboard-shared": "^0.
|
|
39
|
+
"@blackbelt-technology/pi-dashboard-shared": "^0.12.0"
|
|
40
40
|
},
|
|
41
41
|
"files": [
|
|
42
42
|
"dist",
|