poad 2.19.0 → 2.20.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.
@@ -0,0 +1,113 @@
1
+ // poad clean restes des projets poad sur cette machine, à purger :
2
+ // - services annexes (Mailpit, Keycloak, Valkey) dont l'engine
3
+ // ne tourne plus (engine arrêté hors de poad stop : Docker
4
+ // Desktop, redémarrage, ancienne version du CLI) ;
5
+ // - conteneurs poad arrêtés, réseaux des projets arrêtés ;
6
+ // - Keycloak partagé qui ne sert plus aucun projet en marche ;
7
+ // - anciennes images d'engine (environ 3 Go chacune) qu'aucun
8
+ // conteneur n'utilise.
9
+ // Rien de ce qui appartient à un projet en marche n'est touché.
10
+ // poad clean --dry-run liste seulement
11
+ // poad clean --yes sans question (hors terminal)
12
+ //
13
+ // Un projet arrêté ne perd rien : ses données sont dans son dossier poad/data,
14
+ // et une image d'engine supprimée est retéléchargée par poad start au besoin.
15
+ module.exports = async (args = {}) => {
16
+ const chalk = require('chalk-v2');
17
+ const Docker = require('dockerode');
18
+ const docker = new Docker();
19
+ const flag = (f) => Boolean(args[f] || args[`--${f}`]);
20
+ const dry = flag('dry-run') || flag('dry');
21
+ const yes = flag('yes') || Boolean(args['-y']);
22
+ const shared = require('../utils/keycloak-shared');
23
+
24
+ const UUID = '[a-z0-9]{20}';
25
+ const containers = await docker.listContainers({ all: true });
26
+ const nameOf = (c) => (c.Names[0] || '').replace(/^\//, '');
27
+ const engines = containers.filter((c) => new RegExp(`^${UUID}$`).test(nameOf(c)) && String(c.Image).startsWith('poadio/engine'));
28
+ const running = new Set(engines.filter((c) => c.State === 'running').map(nameOf));
29
+
30
+ // Conteneurs à retirer : annexes orphelines, poad arrêtés
31
+ const doomed = [];
32
+ for (const c of containers) {
33
+ const name = nameOf(c);
34
+ const side = new RegExp(`^(${UUID})-(mail|keycloak|valkey)$`).exec(name);
35
+ if (side && !running.has(side[1])) doomed.push({ c, why: `${side[2]} of a stopped project` });
36
+ else if (engines.includes(c) && c.State !== 'running') doomed.push({ c, why: 'stopped engine' });
37
+ }
38
+ // Keycloak partagé : utile tant qu'un projet en marche y est rattaché
39
+ const kcShared = containers.find((c) => nameOf(c) === shared.NAME);
40
+ if (kcShared) {
41
+ const nets = Object.keys((kcShared.NetworkSettings && kcShared.NetworkSettings.Networks) || {}).filter((n) => n.startsWith('poad-'));
42
+ const used = nets.some((n) => running.has(n.slice(5)));
43
+ if (!used) doomed.push({ c: kcShared, why: 'shared Keycloak, no running project uses it' });
44
+ }
45
+
46
+ // Réseaux des projets arrêtés
47
+ const networks = (await docker.listNetworks()).filter((n) => {
48
+ const m = new RegExp(`^poad-(${UUID})$`).exec(n.Name);
49
+ return m && !running.has(m[1]);
50
+ });
51
+
52
+ // Images d'engine qu'aucun conteneur n'utilise (sauf celle du CLI)
53
+ const usedImages = new Set(containers.map((c) => c.Image));
54
+ const keep = `poadio/engine:${global.package.engine}`;
55
+ const images = (await docker.listImages())
56
+ .flatMap((i) => (i.RepoTags || []).map((tag) => ({ tag, size: i.Size })))
57
+ .filter((i) => i.tag.startsWith('poadio/engine:') && i.tag !== keep && !usedImages.has(i.tag));
58
+ const gb = (n) => `${(n / 1e9).toFixed(1)} GB`;
59
+
60
+ console.log(' ');
61
+ if (!doomed.length && !networks.length && !images.length) return console.log(`${chalk.greenBright('✔')} Nothing to clean.`);
62
+ if (doomed.length || networks.length) {
63
+ console.log(chalk.bold('Leftovers of stopped projects'));
64
+ for (const d of doomed) console.log(` ${chalk.yellowBright('•')} ${nameOf(d.c)} ${chalk.grey(`(${d.why})`)}`);
65
+ for (const n of networks) console.log(` ${chalk.yellowBright('•')} network ${n.Name}`);
66
+ console.log(' ');
67
+ }
68
+ if (images.length) {
69
+ console.log(chalk.bold(`Engine images no container uses ${chalk.grey(`(${gb(images.reduce((s, i) => s + i.size, 0))}; kept: ${keep})`)}`));
70
+ for (const i of images) console.log(` ${chalk.yellowBright('•')} ${i.tag} ${chalk.grey(gb(i.size))}`);
71
+ console.log(' ');
72
+ }
73
+ if (dry) return console.log(chalk.grey(' --dry-run: nothing removed. "poad clean" removes them.'));
74
+
75
+ const ask = async (message) => {
76
+ if (yes) return true;
77
+ if (!(process.stdin.isTTY && process.stdout.isTTY)) {
78
+ console.log(chalk.redBright('Not a terminal: confirm with --yes.'));
79
+ return false;
80
+ }
81
+ const { confirm } = require('@inquirer/prompts');
82
+ return confirm({ message, default: true }).catch(() => false);
83
+ };
84
+
85
+ if ((doomed.length || networks.length) && (await ask('Remove these containers and networks?'))) {
86
+ for (const d of doomed) {
87
+ try {
88
+ await docker.getContainer(d.c.Id).remove({ force: true });
89
+ console.log(`${chalk.greenBright('✔')} ${nameOf(d.c)}`);
90
+ } catch (err) {
91
+ console.log(`${chalk.redBright('✖')} ${nameOf(d.c)}: ${err.message}`);
92
+ }
93
+ }
94
+ for (const n of networks) {
95
+ try {
96
+ await docker.getNetwork(n.Id).remove();
97
+ console.log(`${chalk.greenBright('✔')} network ${n.Name}`);
98
+ } catch (err) {
99
+ console.log(`${chalk.redBright('✖')} network ${n.Name}: ${err.message.split('\n')[0]}`);
100
+ }
101
+ }
102
+ }
103
+ if (images.length && (await ask(`Remove ${images.length} engine image(s) (${gb(images.reduce((s, i) => s + i.size, 0))})? A project that needs one downloads it again on "poad start".`))) {
104
+ for (const i of images) {
105
+ try {
106
+ await docker.getImage(i.tag).remove();
107
+ console.log(`${chalk.greenBright('✔')} ${i.tag}`);
108
+ } catch (err) {
109
+ console.log(`${chalk.redBright('✖')} ${i.tag}: ${err.message.split('\n')[0]}`);
110
+ }
111
+ }
112
+ }
113
+ };
package/global/cli.js CHANGED
@@ -12,4 +12,5 @@ module.exports = {
12
12
  start: 'Start the project, apply migrations, start the services chosen with poad setup, then show its state (--all: every configured service, --bare: engine and mail only)',
13
13
  stop: 'Stop container for poad local development (stop kc|valkey: that service only)',
14
14
  run: 'Run an optional service next to the project (keycloak|kc, valkey|redis, --all)',
15
+ clean: 'Remove what stopped poad projects left on this machine: orphan Mailpit/Keycloak/Valkey, networks, unused engine images (--dry-run to list, --yes without question)',
15
16
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "poad",
3
- "version": "2.19.0",
4
- "engine": "0.11.0",
3
+ "version": "2.20.0",
4
+ "engine": "0.11.1",
5
5
  "main": "index.js",
6
6
  "license": "MIT",
7
7
  "bin": {