poad 2.47.0 → 2.49.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/cli.js +14 -0
- package/global/cloud/action.js +37 -0
- package/global/cloud/create.js +42 -0
- package/global/cloud/index.js +19 -0
- package/global/cloud/info.js +37 -0
- package/global/cloud/pause.js +2 -0
- package/global/cloud/projects.js +32 -0
- package/global/cloud/resume.js +2 -0
- package/global/cloud/upgrade.js +2 -0
- package/global/create.js +4 -4
- package/global/init.js +2 -0
- package/global/login.js +32 -0
- package/global/logout.js +14 -0
- package/global/ls.js +2 -0
- package/global/projects.js +124 -0
- package/global/start.js +2 -0
- package/global/whoami.js +13 -0
- package/package.json +1 -1
- package/poad.js +5 -2
- package/project/cli.js +11 -0
- package/project/db/index.js +2 -0
- package/project/db/pull.js +92 -0
- package/project/db/url.js +33 -0
- package/project/deploy.js +136 -0
- package/project/env/create.js +19 -0
- package/project/env/index.js +2 -0
- package/project/env/ls.js +24 -0
- package/project/env/rm.js +21 -0
- package/project/link.js +38 -0
- package/project/unlink.js +12 -0
- package/project/upgrade.js +1 -0
- package/test/cloud.test.js +589 -0
- package/test/projects.test.js +167 -0
- package/utils/banner.js +3 -0
- package/utils/cloud/api.js +120 -0
- package/utils/cloud/branches.js +63 -0
- package/utils/cloud/bundle.js +226 -0
- package/utils/cloud/cli.js +166 -0
- package/utils/cloud/credentials.js +92 -0
- package/utils/cloud/db.js +34 -0
- package/utils/cloud/device.js +66 -0
- package/utils/cloud/system.js +45 -0
- package/utils/engine.js +1 -0
- package/utils/help-details.js +135 -0
- package/utils/projects.js +338 -0
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
// Registre des projets poad de cette machine : ~/.poad/projects.json.
|
|
2
|
+
//
|
|
3
|
+
// Écrit par le CLI seulement (poad init, create, start, upgrade), jamais lu par
|
|
4
|
+
// l'engine. Aucun secret : identifiant, nom, chemin, dates, version d'engine,
|
|
5
|
+
// modèle. Fichier 0600 dans un dossier 0700, écrit par fichier temporaire puis
|
|
6
|
+
// renommage, sous un verrou (dossier .lock) pour que deux poad lancés en même
|
|
7
|
+
// temps ne perdent pas d'entrée. Un fichier illisible est mis de côté
|
|
8
|
+
// (projects.json.corrupt-<date>) et le registre repart de zéro, puis se
|
|
9
|
+
// reconstruit au fil des commandes ou par poad projects --scan.
|
|
10
|
+
//
|
|
11
|
+
// Une écriture du registre n'arrête jamais une commande : record() avale ses
|
|
12
|
+
// erreurs (le registre est une commodité, pas une source de vérité).
|
|
13
|
+
//
|
|
14
|
+
// Format :
|
|
15
|
+
// { "version": 1, "projects": { "<project_uuid>": { id, name, path, created_at,
|
|
16
|
+
// last_used_at, engine, template } } }
|
|
17
|
+
|
|
18
|
+
const fs = require('fs');
|
|
19
|
+
const os = require('os');
|
|
20
|
+
const path = require('path');
|
|
21
|
+
|
|
22
|
+
const VERSION = 1;
|
|
23
|
+
const LOCK_STALE_MS = 10_000;
|
|
24
|
+
const LOCK_WAIT_MS = 2_000;
|
|
25
|
+
|
|
26
|
+
const dir = (home = os.homedir()) => path.join(home, '.poad');
|
|
27
|
+
const file = (home) => path.join(dir(home), 'projects.json');
|
|
28
|
+
|
|
29
|
+
const ID_RE = /^[A-Za-z0-9_.-]{1,80}$/;
|
|
30
|
+
const VERSION_RE = /^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$/;
|
|
31
|
+
const CLOUD_RE = /^[a-z0-9][a-z0-9-]{2,62}$/;
|
|
32
|
+
|
|
33
|
+
const empty = () => ({ version: VERSION, projects: {} });
|
|
34
|
+
|
|
35
|
+
/** Entrée nettoyée : seulement les champs connus, de types attendus (rien d'autre n'entre) */
|
|
36
|
+
function clean(e) {
|
|
37
|
+
if (!e || typeof e !== 'object' || !ID_RE.test(String(e.id || ''))) return null;
|
|
38
|
+
if (typeof e.path !== 'string' || !path.isAbsolute(e.path)) return null;
|
|
39
|
+
const str = (v, max = 200) => (typeof v === 'string' && v.length <= max ? v : null);
|
|
40
|
+
return {
|
|
41
|
+
id: e.id,
|
|
42
|
+
name: str(e.name) || path.basename(e.path),
|
|
43
|
+
path: e.path,
|
|
44
|
+
created_at: str(e.created_at, 40),
|
|
45
|
+
last_used_at: str(e.last_used_at, 40),
|
|
46
|
+
engine: typeof e.engine === 'string' && VERSION_RE.test(e.engine) ? e.engine : null,
|
|
47
|
+
template: str(e.template, 40),
|
|
48
|
+
// Projet poad.io lié ([cloud] project de poad/config.toml) : un identifiant, jamais un secret
|
|
49
|
+
cloud: typeof e.cloud === 'string' && CLOUD_RE.test(e.cloud) ? e.cloud : null,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Lecture : { data, corrupt } ; un fichier absent donne un registre vide */
|
|
54
|
+
function read(home) {
|
|
55
|
+
const f = file(home);
|
|
56
|
+
let text;
|
|
57
|
+
try {
|
|
58
|
+
text = fs.readFileSync(f, 'utf8');
|
|
59
|
+
} catch (e) {
|
|
60
|
+
if (e.code === 'ENOENT') return { data: empty(), exists: false, corrupt: false };
|
|
61
|
+
throw e;
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
const raw = JSON.parse(text);
|
|
65
|
+
if (!raw || typeof raw !== 'object' || typeof raw.projects !== 'object' || Array.isArray(raw.projects)) throw new Error('bad shape');
|
|
66
|
+
const data = empty();
|
|
67
|
+
for (const v of Object.values(raw.projects)) {
|
|
68
|
+
const c = clean(v);
|
|
69
|
+
if (c) data.projects[c.id] = c;
|
|
70
|
+
}
|
|
71
|
+
return { data, exists: true, corrupt: false };
|
|
72
|
+
} catch {
|
|
73
|
+
// Mis de côté, jamais effacé : on repart d'un registre vide
|
|
74
|
+
const aside = `${f}.corrupt-${new Date().toISOString().replace(/[:.]/g, '-')}`;
|
|
75
|
+
try {
|
|
76
|
+
fs.renameSync(f, aside);
|
|
77
|
+
} catch {}
|
|
78
|
+
return { data: empty(), exists: false, corrupt: aside };
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function ensureDir(home) {
|
|
83
|
+
const d = dir(home);
|
|
84
|
+
fs.mkdirSync(d, { recursive: true, mode: 0o700 });
|
|
85
|
+
try {
|
|
86
|
+
fs.chmodSync(d, 0o700);
|
|
87
|
+
} catch {}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function write(home, data) {
|
|
91
|
+
ensureDir(home);
|
|
92
|
+
const f = file(home);
|
|
93
|
+
const tmp = `${f}.${process.pid}.${Date.now()}.tmp`;
|
|
94
|
+
const sorted = { version: VERSION, projects: {} };
|
|
95
|
+
for (const k of Object.keys(data.projects).sort()) sorted.projects[k] = data.projects[k];
|
|
96
|
+
fs.writeFileSync(tmp, JSON.stringify(sorted, null, 2) + '\n', { mode: 0o600 });
|
|
97
|
+
fs.renameSync(tmp, f);
|
|
98
|
+
try {
|
|
99
|
+
fs.chmodSync(f, 0o600);
|
|
100
|
+
} catch {}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const sleep = (ms) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
104
|
+
|
|
105
|
+
/** Verrou par dossier (mkdir est atomique) ; un verrou de plus de 10 s est repris */
|
|
106
|
+
function lock(home) {
|
|
107
|
+
ensureDir(home);
|
|
108
|
+
const l = `${file(home)}.lock`;
|
|
109
|
+
const until = Date.now() + LOCK_WAIT_MS;
|
|
110
|
+
for (;;) {
|
|
111
|
+
try {
|
|
112
|
+
fs.mkdirSync(l);
|
|
113
|
+
return () => {
|
|
114
|
+
try {
|
|
115
|
+
fs.rmdirSync(l);
|
|
116
|
+
} catch {}
|
|
117
|
+
};
|
|
118
|
+
} catch (e) {
|
|
119
|
+
if (e.code !== 'EEXIST') throw e;
|
|
120
|
+
try {
|
|
121
|
+
if (Date.now() - fs.statSync(l).mtimeMs > LOCK_STALE_MS) {
|
|
122
|
+
fs.rmdirSync(l);
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
} catch {
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (Date.now() > until) throw new Error('projects registry is locked');
|
|
129
|
+
sleep(25);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Relit, applique mutate(data) sous le verrou, réécrit ; renvoie la valeur de mutate */
|
|
135
|
+
function update(mutate, home) {
|
|
136
|
+
const release = lock(home);
|
|
137
|
+
try {
|
|
138
|
+
const { data } = read(home);
|
|
139
|
+
const out = mutate(data);
|
|
140
|
+
write(home, data);
|
|
141
|
+
return out;
|
|
142
|
+
} finally {
|
|
143
|
+
release();
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Champs non secrets de poad/config.toml : { id, name, engine } ou null */
|
|
148
|
+
function projectInfo(root) {
|
|
149
|
+
try {
|
|
150
|
+
const toml = require('toml');
|
|
151
|
+
const c = toml.parse(fs.readFileSync(root, 'utf8'));
|
|
152
|
+
if (!ID_RE.test(String(c.project_uuid || ''))) return null;
|
|
153
|
+
return {
|
|
154
|
+
id: String(c.project_uuid),
|
|
155
|
+
name: typeof c.project_id === 'string' ? c.project_id : null,
|
|
156
|
+
engine: typeof c.engine === 'string' ? c.engine : null,
|
|
157
|
+
port: c.web && Number.isInteger(c.web.port) ? c.web.port : null,
|
|
158
|
+
cloud: c.cloud && typeof c.cloud.project === 'string' && CLOUD_RE.test(c.cloud.project) ? c.cloud.project : null,
|
|
159
|
+
};
|
|
160
|
+
} catch {
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Dossier du projet (celui qui contient poad/) depuis poad/config.toml */
|
|
166
|
+
const projectPath = (root) => path.dirname(path.dirname(path.resolve(root)));
|
|
167
|
+
|
|
168
|
+
/** Ajoute ou rafraîchit l'entrée du projet ; created_at et template sont gardés */
|
|
169
|
+
function upsert(data, entry, now = new Date().toISOString()) {
|
|
170
|
+
const prev = data.projects[entry.id];
|
|
171
|
+
const next = clean({
|
|
172
|
+
...prev,
|
|
173
|
+
...Object.fromEntries(Object.entries(entry).filter(([, v]) => v !== undefined && v !== null)),
|
|
174
|
+
created_at: (prev && prev.created_at) || entry.created_at || now,
|
|
175
|
+
});
|
|
176
|
+
if (!next) return null;
|
|
177
|
+
data.projects[next.id] = next;
|
|
178
|
+
return next;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* poad init / create / start / upgrade : le projet de root est inscrit (ou mis
|
|
183
|
+
* à jour). opts : { template, used (dernière utilisation : start), home }.
|
|
184
|
+
* Ne lève jamais : renvoie l'entrée, ou null.
|
|
185
|
+
*/
|
|
186
|
+
function record(root, opts = {}) {
|
|
187
|
+
try {
|
|
188
|
+
if (process.env.POAD_NO_REGISTRY === '1') return null;
|
|
189
|
+
const info = projectInfo(root);
|
|
190
|
+
if (!info) return null;
|
|
191
|
+
const now = new Date().toISOString();
|
|
192
|
+
return update((data) => {
|
|
193
|
+
const e = upsert(
|
|
194
|
+
data,
|
|
195
|
+
{ id: info.id, name: info.name, path: projectPath(root), engine: info.engine, template: opts.template, last_used_at: opts.used ? now : undefined },
|
|
196
|
+
now,
|
|
197
|
+
);
|
|
198
|
+
// La liaison suit config.toml, y compris quand elle est retirée (poad unlink)
|
|
199
|
+
if (e) e.cloud = info.cloud;
|
|
200
|
+
return e;
|
|
201
|
+
}, opts.home);
|
|
202
|
+
} catch {
|
|
203
|
+
return null;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** Retire des entrées par identifiant ; renvoie le nombre retiré */
|
|
208
|
+
function remove(ids, home) {
|
|
209
|
+
return update((data) => {
|
|
210
|
+
let n = 0;
|
|
211
|
+
for (const id of ids) if (data.projects[id]) (delete data.projects[id], n++);
|
|
212
|
+
return n;
|
|
213
|
+
}, home);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// --- Docker (lecture seule) ------------------------------------------------
|
|
217
|
+
|
|
218
|
+
const { execFile } = require('child_process');
|
|
219
|
+
|
|
220
|
+
function docker(args, { timeout = 5000 } = {}) {
|
|
221
|
+
return new Promise((resolve) => {
|
|
222
|
+
execFile('docker', args, { timeout, maxBuffer: 32 * 1024 * 1024 }, (err, stdout) => resolve(err ? null : stdout));
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Sortie de « docker ps -a --format {{json .}} » -> [{ name, state, image, ports }] */
|
|
227
|
+
function parsePs(text) {
|
|
228
|
+
const out = [];
|
|
229
|
+
for (const line of String(text || '').split('\n')) {
|
|
230
|
+
if (!line.trim()) continue;
|
|
231
|
+
try {
|
|
232
|
+
const j = JSON.parse(line);
|
|
233
|
+
out.push({ id: j.ID, name: String(j.Names || '').split(',')[0], state: String(j.State || '').toLowerCase(), image: j.Image || '', ports: j.Ports || '' });
|
|
234
|
+
} catch {}
|
|
235
|
+
}
|
|
236
|
+
return out;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** Un conteneur appartient au projet id : « id » ou « id-<service> » */
|
|
240
|
+
const belongsTo = (name, id) => name === id || name.startsWith(id + '-');
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* État d'un projet d'après les conteneurs : running (l'engine tourne),
|
|
244
|
+
* stopped (conteneurs présents, engine arrêté), none (aucun conteneur).
|
|
245
|
+
*/
|
|
246
|
+
function stateOf(id, containers) {
|
|
247
|
+
const mine = containers.filter((c) => belongsTo(c.name, id));
|
|
248
|
+
if (!mine.length) return 'none';
|
|
249
|
+
const engine = mine.find((c) => c.name === id);
|
|
250
|
+
return engine && engine.state === 'running' ? 'running' : 'stopped';
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Projets trouvés dans la sortie de « docker inspect » (tableau JSON) : un
|
|
255
|
+
* montage dont la source contient /poad/data donne le dossier du projet.
|
|
256
|
+
* -> [{ id, path, engine, created_at }]
|
|
257
|
+
*/
|
|
258
|
+
function parseInspect(list) {
|
|
259
|
+
const found = new Map();
|
|
260
|
+
for (const c of Array.isArray(list) ? list : []) {
|
|
261
|
+
const name = String(c.Name || '').replace(/^\//, '');
|
|
262
|
+
if (!name) continue;
|
|
263
|
+
const mount = (c.Mounts || []).find((m) => typeof m.Source === 'string' && /\/poad\/data(\/|$)/.test(m.Source));
|
|
264
|
+
if (!mount) continue;
|
|
265
|
+
const p = mount.Source.slice(0, mount.Source.search(/\/poad\/data(\/|$)/));
|
|
266
|
+
if (!p || !path.isAbsolute(p)) continue;
|
|
267
|
+
const image = (c.Config && c.Config.Image) || c.Image || '';
|
|
268
|
+
const m = /^(?:docker\.io\/)?poadio\/engine:([0-9][^@\s]*)$/.exec(image);
|
|
269
|
+
const prev = found.get(p);
|
|
270
|
+
// L'engine (nom sans suffixe) l'emporte sur les services annexes (<id>-valkey…)
|
|
271
|
+
const side = name.includes('-');
|
|
272
|
+
const keepPrev = prev && (!prev.side || side);
|
|
273
|
+
found.set(p, {
|
|
274
|
+
id: keepPrev ? prev.id : side ? name.split('-')[0] : name,
|
|
275
|
+
side: keepPrev ? prev.side : side,
|
|
276
|
+
path: p,
|
|
277
|
+
engine: (m && m[1]) || (prev && prev.engine) || null,
|
|
278
|
+
created_at: (prev && prev.created_at) || (typeof c.Created === 'string' ? c.Created : null),
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
return [...found.values()].map(({ side, ...e }) => e);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Reconstitue le registre depuis Docker (lecture seule : ps et inspect, rien
|
|
286
|
+
* n'est démarré ni arrêté) et depuis le dossier courant. Le config.toml d'un
|
|
287
|
+
* dossier trouvé fait foi pour l'identifiant et le nom. Renvoie { added, seen }.
|
|
288
|
+
*/
|
|
289
|
+
async function scan({ home, cwdRoot } = {}) {
|
|
290
|
+
const ids = await docker(['ps', '-aq']);
|
|
291
|
+
let fromDocker = [];
|
|
292
|
+
if (ids && ids.trim()) {
|
|
293
|
+
const raw = await docker(['inspect', ...ids.trim().split(/\s+/)], { timeout: 15000 });
|
|
294
|
+
try {
|
|
295
|
+
fromDocker = parseInspect(JSON.parse(raw || '[]'));
|
|
296
|
+
} catch {}
|
|
297
|
+
}
|
|
298
|
+
const candidates = fromDocker.map((d) => ({ ...d, root: path.join(d.path, 'poad', 'config.toml') }));
|
|
299
|
+
if (cwdRoot) candidates.push({ root: cwdRoot, path: projectPath(cwdRoot) });
|
|
300
|
+
return update((data) => {
|
|
301
|
+
let added = 0;
|
|
302
|
+
for (const c of candidates) {
|
|
303
|
+
const info = fs.existsSync(c.root) ? projectInfo(c.root) : null;
|
|
304
|
+
const id = (info && info.id) || c.id;
|
|
305
|
+
if (!id || !ID_RE.test(id)) continue;
|
|
306
|
+
const isNew = !data.projects[id];
|
|
307
|
+
const e = upsert(data, { id, name: (info && info.name) || undefined, path: c.path, engine: (info && info.engine) || c.engine || undefined, created_at: c.created_at || undefined });
|
|
308
|
+
if (e && isNew) added++;
|
|
309
|
+
}
|
|
310
|
+
return { added, seen: candidates.length };
|
|
311
|
+
}, home);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** Ports publiés d'un conteneur (« 127.0.0.1:57111->8000/tcp ») : ports de la machine */
|
|
315
|
+
const hostPorts = (ports) => [...String(ports || '').matchAll(/:(\d+)->/g)].map((m) => Number(m[1]));
|
|
316
|
+
|
|
317
|
+
/** Lignes du tableau de poad projects : entrées du registre + état Docker */
|
|
318
|
+
function rows(data, containers, { all = false } = {}) {
|
|
319
|
+
const list = Object.values(data.projects).map((e) => {
|
|
320
|
+
const missing = !fs.existsSync(path.join(e.path, 'poad', 'config.toml'));
|
|
321
|
+
const state = containers === null ? 'unknown' : stateOf(e.id, containers);
|
|
322
|
+
const info = missing ? null : projectInfo(path.join(e.path, 'poad', 'config.toml'));
|
|
323
|
+
const engine = (info && info.engine) || e.engine;
|
|
324
|
+
let port = null;
|
|
325
|
+
if (state === 'running') {
|
|
326
|
+
const c = containers.find((x) => x.name === e.id);
|
|
327
|
+
port = (info && info.port && hostPorts(c && c.ports).includes(info.port) ? info.port : null) || (info && info.port) || null;
|
|
328
|
+
}
|
|
329
|
+
const cloud = info ? info.cloud : e.cloud || null;
|
|
330
|
+
return { ...e, name: (info && info.name) || e.name, engine, cloud, missing, state: missing ? 'missing' : state, port };
|
|
331
|
+
});
|
|
332
|
+
const order = { running: 0, stopped: 1, none: 2, unknown: 2, missing: 3 };
|
|
333
|
+
return list
|
|
334
|
+
.filter((r) => all || !r.missing)
|
|
335
|
+
.sort((a, b) => order[a.state] - order[b.state] || String(b.last_used_at || b.created_at || '').localeCompare(String(a.last_used_at || a.created_at || '')) || a.name.localeCompare(b.name));
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
module.exports = { dir, file, read, write, update, record, remove, upsert, clean, projectInfo, projectPath, parsePs, parseInspect, stateOf, belongsTo, scan, rows, docker, hostPorts };
|