poad 2.50.0 → 2.51.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.
- package/global/start.js +13 -2
- package/package.json +1 -1
- package/project/logs.js +2 -0
- package/test/fronts.test.js +173 -0
- package/utils/fronts.js +213 -0
- package/utils/logline.js +2 -2
- package/utils/report.js +12 -0
- package/utils/tomlfile.js +3 -2
package/global/start.js
CHANGED
|
@@ -44,7 +44,7 @@ module.exports = async (args = {}) => {
|
|
|
44
44
|
require('../utils/console-services').write(root);
|
|
45
45
|
if (targets.length) await require('./run')({ services: targets.map((s) => s.id), quiet: true });
|
|
46
46
|
console.log(' ');
|
|
47
|
-
await require('../utils/report').printReport(root, require('../utils/config').loadConfig(root));
|
|
47
|
+
await require('../utils/report').printReport(root, require('../utils/config').loadConfig(root), { justStarted: true });
|
|
48
48
|
};
|
|
49
49
|
|
|
50
50
|
// --quiet : rien à dire ni à faire si le projet tourne déjà (bun start du front)
|
|
@@ -95,13 +95,21 @@ module.exports = async (args = {}) => {
|
|
|
95
95
|
await logs.ensureGelfPort(root, config);
|
|
96
96
|
if (mailpit) await mail.stopMailpit(docker, config);
|
|
97
97
|
const { reassignBusyPorts, describeMoves } = require('../utils/ports');
|
|
98
|
+
// Fronts (engine 0.46.0+) : un port par front, et une réserve pour ceux que la
|
|
99
|
+
// console crée pendant que le projet tourne (un port ne s'ajoute pas à un conteneur lancé)
|
|
100
|
+
const fronts = require('../utils/fronts');
|
|
101
|
+
const withFronts = fronts.supportsFronts(config);
|
|
102
|
+
if (withFronts) await fronts.ensureSpare(root, config);
|
|
98
103
|
const moved = await reassignBusyPorts(root, config, [
|
|
99
104
|
{ section: 'db', key: 'port', label: 'Database' },
|
|
100
105
|
{ section: 'web', key: 'port', label: 'API' },
|
|
101
106
|
...(mailpit ? [{ section: 'mail', key: 'port', label: 'Mailpit' }] : []),
|
|
102
107
|
{ section: 'logs', key: 'gelf_port', label: 'Logs collector', udp: true },
|
|
108
|
+
...(withFronts ? fronts.busyEntries(config) : []),
|
|
103
109
|
]);
|
|
110
|
+
if (withFronts) fronts.foldMoved(config);
|
|
104
111
|
describeMoves(moved).forEach((line) => console.log(chalk.yellowBright(` ${line}`)));
|
|
112
|
+
const frontPorts = withFronts ? fronts.bindings(config) : { exposed: {}, bindings: {} };
|
|
105
113
|
|
|
106
114
|
const progress = ora('Starting containers').start();
|
|
107
115
|
try {
|
|
@@ -144,14 +152,17 @@ module.exports = async (args = {}) => {
|
|
|
144
152
|
...gateway.engineEnv(config),
|
|
145
153
|
// Socle IA : [ai] de poad/config.toml (engine 0.30.0+ ; un engine plus ancien les ignore)
|
|
146
154
|
...aiSettings.env,
|
|
155
|
+
// Fronts : port de chacun, réserve, nom du projet (page « En construction »)
|
|
156
|
+
...(withFronts ? fronts.engineEnv(config) : []),
|
|
147
157
|
],
|
|
148
|
-
ExposedPorts: { '5432/tcp': {}, '8000/tcp': {}, ...gelf.exposed },
|
|
158
|
+
ExposedPorts: { '5432/tcp': {}, '8000/tcp': {}, ...gelf.exposed, ...frontPorts.exposed },
|
|
149
159
|
HostConfig: {
|
|
150
160
|
NetworkMode: net.NetworkMode,
|
|
151
161
|
PortBindings: {
|
|
152
162
|
'5432/tcp': [{ HostIp: require('../utils/ports').hostIp(config), HostPort: config.db.port.toString() }],
|
|
153
163
|
'8000/tcp': [{ HostIp: require('../utils/ports').hostIp(config), HostPort: config.web.port.toString() }],
|
|
154
164
|
...gelf.bindings,
|
|
165
|
+
...frontPorts.bindings,
|
|
155
166
|
},
|
|
156
167
|
// poad/migrations monté : la console y écrit les migrations qu'elle crée.
|
|
157
168
|
// poad/auth et poad/storage : manifestes versionnés des réglages d'auth et des buckets.
|
package/package.json
CHANGED
package/project/logs.js
CHANGED
|
@@ -27,6 +27,8 @@
|
|
|
27
27
|
// poad logs data event=realtime_change les changements livrés (realtime_subscribed,
|
|
28
28
|
// realtime_unsubscribed, realtime_joined, realtime_left,
|
|
29
29
|
// realtime_closed, realtime_error, realtime_change_summary)
|
|
30
|
+
// poad logs app front=<id> un front dessiné dans poad (engine 0.46+) : le serveur du moteur
|
|
31
|
+
// et le navigateur (source=server|browser) ; page=<id> pour une page
|
|
30
32
|
// poad logs limit=500 nombre de lignes (100 par défaut)
|
|
31
33
|
// poad logs -f | --follow suivi en direct
|
|
32
34
|
// poad logs --json une ligne JSON par entrée (pour jq)
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
// Fronts dessinés dans poad : un port par front dans poad/config.toml, réserve,
|
|
2
|
+
// variables et ports de l'engine, suivi des fronts créés depuis la console.
|
|
3
|
+
const { test } = require('node:test');
|
|
4
|
+
const assert = require('node:assert');
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const os = require('os');
|
|
7
|
+
const path = require('path');
|
|
8
|
+
const toml = require('toml');
|
|
9
|
+
const fronts = require('../utils/fronts');
|
|
10
|
+
const { setInText, removeSectionInText } = require('../utils/tomlfile');
|
|
11
|
+
const { attrConditions } = require('../utils/logline');
|
|
12
|
+
|
|
13
|
+
const A = 'k3x9q2m7a1bc';
|
|
14
|
+
const B = 'zq8w7e6r5t4y';
|
|
15
|
+
|
|
16
|
+
const project = (text) => {
|
|
17
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'poad-fronts-'));
|
|
18
|
+
const file = path.join(dir, 'config.toml');
|
|
19
|
+
fs.writeFileSync(file, text);
|
|
20
|
+
return file;
|
|
21
|
+
};
|
|
22
|
+
const parse = (file) => toml.parse(fs.readFileSync(file, 'utf-8'));
|
|
23
|
+
const BASE = 'project_id = "bdtheque"\nengine = "0.46.0"\n\n[db]\nport = 15432\n\n[web]\nport = 18000\n';
|
|
24
|
+
|
|
25
|
+
test('tomlfile : tableau sur une ligne, section imbriquée, retrait', () => {
|
|
26
|
+
let text = setInText(BASE, 'fronts', { spare_ports: [41001, 41002] });
|
|
27
|
+
text = setInText(text, `fronts.${A}`, { port: 41003 });
|
|
28
|
+
const c = toml.parse(text);
|
|
29
|
+
assert.deepStrictEqual(c.fronts.spare_ports, [41001, 41002]);
|
|
30
|
+
assert.strictEqual(c.fronts[A].port, 41003);
|
|
31
|
+
// La réserve se réécrit sans toucher à la section du front qui suit
|
|
32
|
+
text = setInText(text, 'fronts', { spare_ports: [41002] });
|
|
33
|
+
assert.match(text, /spare_ports = \[41002\]/);
|
|
34
|
+
assert.strictEqual(toml.parse(text).fronts[A].port, 41003);
|
|
35
|
+
text = removeSectionInText(text, `fronts.${A}`);
|
|
36
|
+
assert.strictEqual(toml.parse(text).fronts[A], undefined);
|
|
37
|
+
assert.strictEqual(toml.parse(text).web.port, 18000);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('read : identifiants valides seulement, réserve sans doublon ni port de front', () => {
|
|
41
|
+
const s = fronts.read({ fronts: { spare_ports: [41001, 41001, 41003, 'x'], [A]: { port: 41003 }, 'Pas-un-id': { port: 41005 }, [B]: { port: 0 } } });
|
|
42
|
+
assert.deepStrictEqual(s.fronts, [{ id: A, port: 41003 }]);
|
|
43
|
+
assert.deepStrictEqual(s.spare, [41001]);
|
|
44
|
+
assert.deepStrictEqual(fronts.read({}), { fronts: [], spare: [] });
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('ensureSpare : deux ports libres, jamais un port du projet, écrits dans config.toml', async () => {
|
|
48
|
+
const file = project(BASE);
|
|
49
|
+
const config = parse(file);
|
|
50
|
+
const offered = [18000, 15432, 41001, 41001, 41002];
|
|
51
|
+
const added = await fronts.ensureSpare(file, config, { findFreePorts: async () => [offered.shift()], isPortFree: async () => true });
|
|
52
|
+
assert.deepStrictEqual(added, [41001, 41002]);
|
|
53
|
+
assert.deepStrictEqual(parse(file).fronts.spare_ports, [41001, 41002]);
|
|
54
|
+
assert.deepStrictEqual(config.fronts.spare_ports, [41001, 41002]);
|
|
55
|
+
// Déjà complète : rien d'ajouté ; un port de réserve pris ailleurs est remplacé
|
|
56
|
+
assert.deepStrictEqual(await fronts.ensureSpare(file, config, { findFreePorts: async () => [41009], isPortFree: async () => true }), []);
|
|
57
|
+
const again = await fronts.ensureSpare(file, config, { findFreePorts: async () => [41009], isPortFree: async (p) => p !== 41001 });
|
|
58
|
+
assert.deepStrictEqual(again, [41009]);
|
|
59
|
+
assert.deepStrictEqual(parse(file).fronts.spare_ports, [41002, 41009]);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test('busyEntries et foldMoved : un port de front déplacé revient dans config.fronts', () => {
|
|
63
|
+
const config = { fronts: { spare_ports: [], [A]: { port: 41003 } } };
|
|
64
|
+
const entries = fronts.busyEntries(config);
|
|
65
|
+
assert.deepStrictEqual(entries, [{ section: `fronts.${A}`, key: 'port', label: `Front ${A}` }]);
|
|
66
|
+
config[`fronts.${A}`] = { port: 42000 }; // ce que fait reassignBusyPorts
|
|
67
|
+
fronts.foldMoved(config);
|
|
68
|
+
assert.strictEqual(config.fronts[A].port, 42000);
|
|
69
|
+
assert.strictEqual(config[`fronts.${A}`], undefined);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test('engineEnv et bindings : tous les ports vers 8100/tcp, même interface que les autres', () => {
|
|
73
|
+
const config = { project_id: 'bdtheque', web: { port: 18000 }, fronts: { spare_ports: [41001, 41002], [A]: { port: 41003 } } };
|
|
74
|
+
assert.deepStrictEqual(fronts.engineEnv(config), [`POAD_FRONT_PORTS=${A}=41003`, 'POAD_FRONT_SPARE_PORTS=41001,41002', 'POAD_PROJECT_NAME=bdtheque']);
|
|
75
|
+
const b = fronts.bindings(config);
|
|
76
|
+
assert.deepStrictEqual(b.exposed, { '8100/tcp': {} });
|
|
77
|
+
assert.deepStrictEqual(b.bindings['8100/tcp'], [
|
|
78
|
+
{ HostIp: '127.0.0.1', HostPort: '41003' },
|
|
79
|
+
{ HostIp: '127.0.0.1', HostPort: '41001' },
|
|
80
|
+
{ HostIp: '127.0.0.1', HostPort: '41002' },
|
|
81
|
+
]);
|
|
82
|
+
assert.strictEqual(fronts.bindings({ ...config, web: { port: 18000, expose: true } }).bindings['8100/tcp'][0].HostIp, '0.0.0.0');
|
|
83
|
+
assert.deepStrictEqual(fronts.bindings({}), { exposed: {}, bindings: {} });
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test('supportsFronts : engine 0.46.0 et plus (0.45.0 publiée sans les fronts)', () => {
|
|
87
|
+
assert.strictEqual(fronts.supportsFronts({ engine: '0.44.2' }), false);
|
|
88
|
+
assert.strictEqual(fronts.supportsFronts({ engine: '0.45.0' }), false);
|
|
89
|
+
assert.strictEqual(fronts.supportsFronts({ engine: '0.46.0' }), true);
|
|
90
|
+
assert.strictEqual(fronts.supportsFronts({ engine: '1.0.0' }), true);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const fakeFetch = (body, status = 200) => {
|
|
94
|
+
const calls = [];
|
|
95
|
+
const fn = async (url, init) => {
|
|
96
|
+
calls.push({ url, init });
|
|
97
|
+
return { ok: status < 400, status, json: async () => body };
|
|
98
|
+
};
|
|
99
|
+
fn.calls = calls;
|
|
100
|
+
return fn;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
test('sync : un front créé depuis la console prend sa section, son port quitte la réserve ; un front supprimé part', async () => {
|
|
104
|
+
const file = project(`${BASE}\n[fronts]\nspare_ports = [41001, 41002]\n\n[fronts.${B}]\nport = 41000\n`);
|
|
105
|
+
const config = { ...parse(file), auth: { service: 'svc' } };
|
|
106
|
+
const fetchFn = fakeFetch([
|
|
107
|
+
{ id: A, name: 'Gestion', address: 'http://localhost:41001', port_pending: false },
|
|
108
|
+
{ id: 'abcdefghijkl', name: 'Sans port', address: null, port_pending: true },
|
|
109
|
+
]);
|
|
110
|
+
const out = await fronts.sync(file, config, fetchFn);
|
|
111
|
+
assert.strictEqual(fetchFn.calls[0].url, 'http://localhost:18000/meta/v1/fronts');
|
|
112
|
+
assert.strictEqual(fetchFn.calls[0].init.headers.authorization, 'Bearer svc');
|
|
113
|
+
const c = parse(file);
|
|
114
|
+
assert.strictEqual(c.fronts[A].port, 41001);
|
|
115
|
+
assert.deepStrictEqual(c.fronts.spare_ports, [41002]);
|
|
116
|
+
assert.strictEqual(c.fronts[B], undefined);
|
|
117
|
+
assert.deepStrictEqual(fronts.read(config), { fronts: [{ id: A, port: 41001 }], spare: [41002] });
|
|
118
|
+
assert.deepStrictEqual(out.fronts.map((f) => [f.name, f.address, f.port_pending]), [
|
|
119
|
+
['Gestion', 'http://localhost:41001', false],
|
|
120
|
+
['Sans port', null, true],
|
|
121
|
+
]);
|
|
122
|
+
const chalk = require('chalk-v2');
|
|
123
|
+
const lines = fronts.describe(config, out, chalk).join('\n');
|
|
124
|
+
assert.match(lines, /Front Gestion : .*http:\/\/localhost:41001/);
|
|
125
|
+
assert.match(lines, /Sans port : .*restart the project/);
|
|
126
|
+
assert.match(lines, /Spare front ports: 41002/);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test('sync : meta absent (engine ancien, 404) ou injoignable : rien ne change', async () => {
|
|
130
|
+
const text = `${BASE}\n[fronts]\nspare_ports = [41001, 41002]\n`;
|
|
131
|
+
const file = project(text);
|
|
132
|
+
const config = { ...parse(file), auth: { service: 'svc' } };
|
|
133
|
+
assert.strictEqual(await fronts.sync(file, config, fakeFetch({ error: 'route inconnue' }, 404)), null);
|
|
134
|
+
assert.strictEqual(
|
|
135
|
+
await fronts.sync(file, config, async () => {
|
|
136
|
+
throw new Error('ECONNREFUSED');
|
|
137
|
+
}),
|
|
138
|
+
null,
|
|
139
|
+
);
|
|
140
|
+
assert.strictEqual(fs.readFileSync(file, 'utf-8'), text);
|
|
141
|
+
// Sans sync, l'affichage se rabat sur config.toml
|
|
142
|
+
assert.match(fronts.describe({ fronts: { [A]: { port: 41003 } } }, null, require('chalk-v2')).join('\n'), new RegExp(`Front ${A} : .*41003`));
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
test('portOf : seulement une adresse locale', () => {
|
|
146
|
+
assert.strictEqual(fronts.portOf('http://localhost:41001'), 41001);
|
|
147
|
+
assert.strictEqual(fronts.portOf('http://127.0.0.1:41001/'), 41001);
|
|
148
|
+
assert.strictEqual(fronts.portOf(`https://${A}.app.poad.io`), null);
|
|
149
|
+
assert.strictEqual(fronts.portOf(null), null);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
test('poad logs : front= et page= filtrent attrs', () => {
|
|
153
|
+
const r = attrConditions({ front: A, page: 'accueil' });
|
|
154
|
+
assert.deepStrictEqual(r.conditions, [
|
|
155
|
+
["attrs->>'front' = ?", A],
|
|
156
|
+
["attrs->>'page' = ?", 'accueil'],
|
|
157
|
+
]);
|
|
158
|
+
assert.match(attrConditions({ front: true }).error, /front=k3x9q2m7a1bc/);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
test('fetchFronts : nouvel essai tant que la passerelle répond 502 juste après le démarrage, abandon sur 404', async () => {
|
|
162
|
+
const fronts = require('../utils/fronts');
|
|
163
|
+
const config = { web: { port: 1 }, auth: { service: 's' } };
|
|
164
|
+
let calls = 0;
|
|
165
|
+
const flaky = async () => (++calls < 3 ? { ok: false, status: 502 } : { ok: true, status: 200, json: async () => [{ id: 'abc123def456', name: 'Démo', address: 'http://localhost:41001' }] });
|
|
166
|
+
const list = await fronts.fetchFronts(config, flaky, { attempts: 5, delayMs: 1 });
|
|
167
|
+
require('node:assert').equal(calls, 3);
|
|
168
|
+
require('node:assert').equal(list[0].name, 'Démo');
|
|
169
|
+
calls = 0;
|
|
170
|
+
const gone = async () => (++calls, { ok: false, status: 404 });
|
|
171
|
+
require('node:assert').equal(await fronts.fetchFronts(config, gone, { attempts: 5, delayMs: 1 }), null);
|
|
172
|
+
require('node:assert').equal(calls, 1);
|
|
173
|
+
});
|
package/utils/fronts.js
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
// Fronts dessinés dans poad (engine 0.46.0+) : un port de cette machine par
|
|
2
|
+
// front, comme sur poad.io un sous-domaine par front (<id>.app.poad.io). Même
|
|
3
|
+
// origine isolée en local qu'en ligne : cookies, stockage et service workers
|
|
4
|
+
// propres au front, et des adresses de pages identiques (pas de /apps/<nom>).
|
|
5
|
+
//
|
|
6
|
+
// poad/config.toml :
|
|
7
|
+
// [fronts]
|
|
8
|
+
// spare_ports = [41001, 41002] réservés d'avance, publiés à chaque poad start
|
|
9
|
+
// [fronts.k3x9q2m7a1bc]
|
|
10
|
+
// port = 41003
|
|
11
|
+
//
|
|
12
|
+
// Chaque port est publié sur le port 8100 du conteneur (second écouteur de la
|
|
13
|
+
// passerelle de l'engine) ; l'engine reconnaît le front à l'hôte d'arrivée
|
|
14
|
+
// (localhost:<port>). Un port ne s'ajoute pas à un conteneur qui tourne :
|
|
15
|
+
// la réserve permet à la console de créer un front pendant que le projet
|
|
16
|
+
// tourne, joignable aussitôt. Réserve vide : le front attend le prochain
|
|
17
|
+
// poad start (la console le dit).
|
|
18
|
+
const SPARE = 2;
|
|
19
|
+
const CONTAINER_PORT = '8100/tcp';
|
|
20
|
+
const ID_RE = /^[a-z][a-z0-9]{11}$/;
|
|
21
|
+
|
|
22
|
+
/** Engine 0.46.0 et plus : fronts servis par l'engine */
|
|
23
|
+
const supportsFronts = (config) => {
|
|
24
|
+
const [major, minor] = String(config.engine || '').split('.').map(Number);
|
|
25
|
+
return major > 0 || minor >= 46;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const portOk = (p) => Number.isInteger(p) && p > 0 && p < 65536;
|
|
29
|
+
|
|
30
|
+
/** { fronts: [{ id, port }], spare: [port…] } d'après config.fronts */
|
|
31
|
+
const read = (config) => {
|
|
32
|
+
const section = (config && config.fronts) || {};
|
|
33
|
+
const fronts = [];
|
|
34
|
+
for (const [id, v] of Object.entries(section)) {
|
|
35
|
+
if (!ID_RE.test(id) || !v || typeof v !== 'object' || Array.isArray(v)) continue;
|
|
36
|
+
const port = Number(v.port);
|
|
37
|
+
if (portOk(port)) fronts.push({ id, port });
|
|
38
|
+
}
|
|
39
|
+
const taken = new Set(fronts.map((f) => f.port));
|
|
40
|
+
const spare = [...new Set((Array.isArray(section.spare_ports) ? section.spare_ports : []).map(Number))].filter((p) => portOk(p) && !taken.has(p));
|
|
41
|
+
return { fronts, spare };
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/** Ports du projet hors fronts (base, API, Mailpit, collecteur…) : jamais pris pour un front */
|
|
45
|
+
const projectPorts = (config) =>
|
|
46
|
+
[config.db && config.db.port, config.web && config.web.port, config.mail && config.mail.port, config.logs && config.logs.gelf_port, config.valkey && config.valkey.port]
|
|
47
|
+
.map(Number)
|
|
48
|
+
.filter(Boolean);
|
|
49
|
+
|
|
50
|
+
/** Écrit la réserve, et les sections [fronts.<id>] données ({ id: port | null }) */
|
|
51
|
+
const write = (root, { spare, fronts = {} }) => {
|
|
52
|
+
const { setKeys, removeSection } = require('./tomlfile');
|
|
53
|
+
if (spare) setKeys(root, 'fronts', { spare_ports: spare });
|
|
54
|
+
for (const [id, port] of Object.entries(fronts)) {
|
|
55
|
+
if (!ID_RE.test(id)) continue;
|
|
56
|
+
if (port === null) removeSection(root, `fronts.${id}`);
|
|
57
|
+
else setKeys(root, `fronts.${id}`, { port });
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/** Met config.fronts à jour sur place d'après un état { fronts, spare } */
|
|
62
|
+
const apply = (config, state) => {
|
|
63
|
+
const section = { spare_ports: state.spare };
|
|
64
|
+
for (const f of state.fronts) section[f.id] = { port: f.port };
|
|
65
|
+
config.fronts = section;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Réserve complétée à SPARE ports libres (avant poad start). Renvoie les ports
|
|
70
|
+
* ajoutés. deps.findFreePorts et deps.isPortFree pour les tests.
|
|
71
|
+
*/
|
|
72
|
+
const ensureSpare = async (root, config, deps = {}) => {
|
|
73
|
+
const ports = require('./ports');
|
|
74
|
+
const findFreePorts = deps.findFreePorts || ports.findFreePorts;
|
|
75
|
+
const isPortFree = deps.isPortFree || ports.isPortFree;
|
|
76
|
+
const state = read(config);
|
|
77
|
+
const used = new Set([...projectPorts(config), ...state.fronts.map((f) => f.port), ...state.spare]);
|
|
78
|
+
const added = [];
|
|
79
|
+
// Ports de réserve pris sur cette machine par autre chose : remplacés
|
|
80
|
+
const spare = [];
|
|
81
|
+
for (const p of state.spare) if (await isPortFree(p)) spare.push(p);
|
|
82
|
+
while (spare.length < SPARE) {
|
|
83
|
+
const [p] = await findFreePorts(1, 20000, 60000);
|
|
84
|
+
if (used.has(p)) continue;
|
|
85
|
+
used.add(p);
|
|
86
|
+
spare.push(p);
|
|
87
|
+
added.push(p);
|
|
88
|
+
}
|
|
89
|
+
if (added.length || spare.length !== state.spare.length || (config.fronts || {}).spare_ports === undefined) write(root, { spare });
|
|
90
|
+
apply(config, { ...state, spare });
|
|
91
|
+
return added;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/** Entrées pour ports.reassignBusyPorts : le port de chaque front (section fronts.<id>) */
|
|
95
|
+
const busyEntries = (config) =>
|
|
96
|
+
read(config).fronts.map((f) => {
|
|
97
|
+
// reassignBusyPorts lit config[section][key] : alias à plat, replié par foldMoved
|
|
98
|
+
config[`fronts.${f.id}`] = { port: f.port };
|
|
99
|
+
return { section: `fronts.${f.id}`, key: 'port', label: `Front ${f.id}` };
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Après reassignBusyPorts, config['fronts.<id>'] porte le port déplacé : le
|
|
104
|
+
* ramener dans config.fronts.<id>
|
|
105
|
+
*/
|
|
106
|
+
const foldMoved = (config) => {
|
|
107
|
+
for (const key of Object.keys(config)) {
|
|
108
|
+
const m = /^fronts\.([a-z][a-z0-9]{11})$/.exec(key);
|
|
109
|
+
if (!m) continue;
|
|
110
|
+
config.fronts = config.fronts || {};
|
|
111
|
+
config.fronts[m[1]] = { ...(config.fronts[m[1]] || {}), ...config[key] };
|
|
112
|
+
delete config[key];
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
/** Variables de l'engine : fronts connus, réserve, nom du projet */
|
|
117
|
+
const engineEnv = (config) => {
|
|
118
|
+
const { fronts, spare } = read(config);
|
|
119
|
+
return [
|
|
120
|
+
`POAD_FRONT_PORTS=${fronts.map((f) => `${f.id}=${f.port}`).join(',')}`,
|
|
121
|
+
`POAD_FRONT_SPARE_PORTS=${spare.join(',')}`,
|
|
122
|
+
`POAD_PROJECT_NAME=${projectName(config)}`,
|
|
123
|
+
];
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
/** Nom du projet (project_id de config.toml, celui que montre poad projects) */
|
|
127
|
+
const projectName = (config) => String((config && (config.project_id || config.project_uuid)) || 'poad').replace(/[\r\n]/g, ' ').slice(0, 80);
|
|
128
|
+
|
|
129
|
+
/** Ports publiés : tous sur 8100/tcp du conteneur, à la même interface que les autres ports */
|
|
130
|
+
const bindings = (config) => {
|
|
131
|
+
const { fronts, spare } = read(config);
|
|
132
|
+
const all = [...fronts.map((f) => f.port), ...spare];
|
|
133
|
+
if (!all.length) return { exposed: {}, bindings: {} };
|
|
134
|
+
const HostIp = require('./ports').hostIp(config);
|
|
135
|
+
return { exposed: { [CONTAINER_PORT]: {} }, bindings: { [CONTAINER_PORT]: all.map((p) => ({ HostIp, HostPort: String(p) })) } };
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
/** Fronts de l'engine (GET /meta/v1/fronts) ; null si meta ne répond pas ou ne les connaît pas */
|
|
139
|
+
// attempts : juste après poad start, meta peut ne pas répondre encore (passerelle pas encore ouverte, 502) :
|
|
140
|
+
// nouvel essai toutes les 1,5 s ; 404 (engine sans fronts) ou réponse illisible : abandon tout de suite
|
|
141
|
+
const fetchFronts = async (config, fetchFn = globalThis.fetch, { attempts = 1, delayMs = 1500 } = {}) => {
|
|
142
|
+
for (let i = 0; i < attempts; i++) {
|
|
143
|
+
if (i) await new Promise((r) => setTimeout(r, delayMs));
|
|
144
|
+
try {
|
|
145
|
+
const res = await fetchFn(`http://localhost:${config.web.port}/meta/v1/fronts`, {
|
|
146
|
+
headers: { apikey: config.auth.service, authorization: `Bearer ${config.auth.service}` },
|
|
147
|
+
signal: AbortSignal.timeout(8000),
|
|
148
|
+
});
|
|
149
|
+
if (res.status >= 502 && res.status <= 504) continue;
|
|
150
|
+
if (!res.ok) return null;
|
|
151
|
+
const list = await res.json();
|
|
152
|
+
return Array.isArray(list) ? list.filter((f) => f && typeof f.id === 'string' && ID_RE.test(f.id)) : null;
|
|
153
|
+
} catch {
|
|
154
|
+
/* pas encore joignable : essai suivant */
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return null;
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
/** Port local d'une adresse http://localhost:<port> (ou 127.0.0.1) */
|
|
161
|
+
const portOf = (address) => {
|
|
162
|
+
const m = /^http:\/\/(?:localhost|127\.0\.0\.1):(\d{1,5})\/?$/.exec(String(address || ''));
|
|
163
|
+
return m && portOk(Number(m[1])) ? Number(m[1]) : null;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Après le démarrage : config.toml suit les fronts de l'engine. Un front qui a
|
|
168
|
+
* pris un port de réserve reçoit sa section, et ce port quitte la réserve ;
|
|
169
|
+
* la section d'un front supprimé part. Ne fait rien si meta ne répond pas.
|
|
170
|
+
* Renvoie { fronts: [{ id, name, address, port_pending }] } ou null.
|
|
171
|
+
*/
|
|
172
|
+
const sync = async (root, config, fetchFn, options) => {
|
|
173
|
+
const list = await fetchFronts(config, fetchFn, options);
|
|
174
|
+
if (!list) return null;
|
|
175
|
+
const state = read(config);
|
|
176
|
+
const known = new Map(state.fronts.map((f) => [f.id, f.port]));
|
|
177
|
+
const changes = {};
|
|
178
|
+
let spare = state.spare;
|
|
179
|
+
for (const f of list) {
|
|
180
|
+
const port = portOf(f.address);
|
|
181
|
+
if (port && known.get(f.id) !== port) {
|
|
182
|
+
changes[f.id] = port;
|
|
183
|
+
spare = spare.filter((p) => p !== port);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
const ids = new Set(list.map((f) => f.id));
|
|
187
|
+
for (const f of state.fronts) if (!ids.has(f.id)) changes[f.id] = null;
|
|
188
|
+
if (Object.keys(changes).length || spare.length !== state.spare.length) {
|
|
189
|
+
write(root, { spare, fronts: changes });
|
|
190
|
+
const fronts = state.fronts.filter((f) => changes[f.id] !== null).map((f) => (changes[f.id] ? { id: f.id, port: changes[f.id] } : f));
|
|
191
|
+
for (const [id, port] of Object.entries(changes)) if (port && !known.has(id)) fronts.push({ id, port });
|
|
192
|
+
apply(config, { fronts, spare });
|
|
193
|
+
}
|
|
194
|
+
return { fronts: list.map((f) => ({ id: f.id, name: typeof f.name === 'string' ? f.name : f.id, address: f.address || null, port_pending: f.port_pending === true })) };
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
/** Lignes d'affichage (poad start, poad info) */
|
|
198
|
+
const describe = (config, synced, chalk) => {
|
|
199
|
+
const lines = [];
|
|
200
|
+
const { fronts, spare } = read(config);
|
|
201
|
+
if (synced) {
|
|
202
|
+
for (const f of synced.fronts) {
|
|
203
|
+
if (f.address) lines.push(`Front ${f.name} : ${chalk.bold.cyanBright(f.address)}`);
|
|
204
|
+
else lines.push(`Front ${f.name} : ${chalk.yellowBright('no port yet, restart the project (poad stop, then poad start)')}`);
|
|
205
|
+
}
|
|
206
|
+
} else {
|
|
207
|
+
for (const f of fronts) lines.push(`Front ${f.id} : ${chalk.bold.cyanBright(`http://localhost:${f.port}`)}`);
|
|
208
|
+
}
|
|
209
|
+
if (spare.length) lines.push(chalk.grey(`Spare front ports: ${spare.join(', ')} (for fronts created from the console)`));
|
|
210
|
+
return lines;
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
module.exports = { SPARE, CONTAINER_PORT, supportsFronts, read, write, ensureSpare, busyEntries, foldMoved, engineEnv, projectName, bindings, fetchFronts, portOf, sync, describe };
|
package/utils/logline.js
CHANGED
|
@@ -5,8 +5,8 @@ const LEVELS = ['debug', 'info', 'warn', 'error', 'fatal'];
|
|
|
5
5
|
|
|
6
6
|
// Options de filtre → attribut de la ligne (attrs). Mêmes noms et même sens
|
|
7
7
|
// que les paramètres de /logs/v1 (services/logs/src/api.ts de l'infra).
|
|
8
|
-
const ATTR_OPTIONS = { flow: 'flow', step: 'step', function: 'function', tool: 'tool', outcome: 'outcome', provider: 'provider', model: 'model', event: 'event', table: 'table', channel: 'channel' };
|
|
9
|
-
const EXAMPLES = { flow: 'demo-flux', step: 'appel', function: 'demo-stats', tool: 'select_rows', outcome: 'refused', provider: 'anthropic', model: 'claude-opus-5-5', event: 'ai_default_model_changed', table: 'notes', channel: 'notes' };
|
|
8
|
+
const ATTR_OPTIONS = { flow: 'flow', step: 'step', function: 'function', tool: 'tool', outcome: 'outcome', provider: 'provider', model: 'model', event: 'event', table: 'table', channel: 'channel', front: 'front', page: 'page' };
|
|
9
|
+
const EXAMPLES = { flow: 'demo-flux', step: 'appel', function: 'demo-stats', tool: 'select_rows', outcome: 'refused', provider: 'anthropic', model: 'claude-opus-5-5', event: 'ai_default_model_changed', table: 'notes', channel: 'notes', front: 'k3x9q2m7a1bc', page: 'accueil' };
|
|
10
10
|
// Identifiant d'exécution (UUID) entier, ou son début : 8 caractères au moins
|
|
11
11
|
const RUN_RE = /^[0-9a-f-]{8,36}$/;
|
|
12
12
|
|
package/utils/report.js
CHANGED
|
@@ -71,6 +71,18 @@ async function printProject(root, config, opts = {}) {
|
|
|
71
71
|
const line = await migrationsLine(root, config);
|
|
72
72
|
if (line) info.Migrations = line;
|
|
73
73
|
console.log(prettyoutput(info));
|
|
74
|
+
// Fronts dessinés dans poad (engine 0.46.0+) : config.toml suit l'engine (port pris dans la réserve), puis une ligne par front
|
|
75
|
+
const fronts = require('./fronts');
|
|
76
|
+
if (fronts.supportsFronts(config)) {
|
|
77
|
+
// Juste après poad start (opts.justStarted) : meta peut mettre quelques secondes à répondre
|
|
78
|
+
const synced = opts.offline ? null : await fronts.sync(root, config, undefined, opts.justStarted ? { attempts: 20 } : undefined);
|
|
79
|
+
const lines = fronts.describe(config, synced, chalk);
|
|
80
|
+
if (lines.length) {
|
|
81
|
+
console.log(chalk.bold('Fronts'));
|
|
82
|
+
lines.forEach((l) => console.log(` ${l}`));
|
|
83
|
+
console.log(' ');
|
|
84
|
+
}
|
|
85
|
+
}
|
|
74
86
|
}
|
|
75
87
|
|
|
76
88
|
async function printServices(root, config, opts = {}) {
|
package/utils/tomlfile.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
// Modifications ciblées d'un fichier TOML simple (config.toml, secrets.toml) :
|
|
2
2
|
// le texte est modifié ligne à ligne, les commentaires et l'ordre restent.
|
|
3
|
-
// Valeurs : chaîne, nombre, booléen
|
|
3
|
+
// Valeurs : chaîne, nombre, booléen, tableau de ces valeurs (sur une ligne) ;
|
|
4
|
+
// null retire la clé. Section imbriquée : son nom pointé (« fronts.k3x9q2m7a1bc »).
|
|
4
5
|
const fs = require('fs');
|
|
5
6
|
|
|
6
|
-
const format = (v) => (typeof v === 'string' ? JSON.stringify(v) : String(v));
|
|
7
|
+
const format = (v) => (Array.isArray(v) ? `[${v.map(format).join(', ')}]` : typeof v === 'string' ? JSON.stringify(v) : String(v));
|
|
7
8
|
|
|
8
9
|
/** Bornes [début, fin) des lignes de la section (début = ligne d'en-tête), ou null */
|
|
9
10
|
function sectionRange(lines, section) {
|