podmanager 0.0.0-stage → 1.0.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,404 @@
1
+ const { execFile, spawn } = require('child_process');
2
+
3
+ /**
4
+ * Execute podman command safely using execFile
5
+ */
6
+ function runPodman(args, options = {}) {
7
+ return new Promise((resolve, reject) => {
8
+ const maxBuffer = options.maxBuffer || 10 * 1024 * 1024; // 10MB default
9
+ const timeout = options.timeout || 60000; // 60s default timeout
10
+ execFile('podman', args, { maxBuffer, timeout, ...options }, (err, stdout, stderr) => {
11
+ if (err) {
12
+ const error = new Error(stderr.trim() || err.message);
13
+ error.code = err.code;
14
+ error.stdout = stdout;
15
+ error.stderr = stderr;
16
+ return reject(error);
17
+ }
18
+ resolve({ stdout: stdout.trim(), stderr: stderr.trim() });
19
+ });
20
+ });
21
+ }
22
+
23
+ /**
24
+ * Execute podman command and parse JSON output
25
+ */
26
+ async function runPodmanJson(args) {
27
+ const { stdout } = await runPodman(args);
28
+ if (!stdout) return [];
29
+ try {
30
+ return JSON.parse(stdout);
31
+ } catch (err) {
32
+ throw new Error(`Failed to parse Podman JSON output: ${err.message}. Raw output: ${stdout.slice(0, 200)}`);
33
+ }
34
+ }
35
+
36
+ const Podman = {
37
+ // System
38
+ async getSystemInfo() {
39
+ const raw = await runPodmanJson(['system', 'info', '--format', 'json']);
40
+ return {
41
+ version: raw.version || {},
42
+ host: {
43
+ os: raw.host?.os || 'Unknown',
44
+ arch: raw.host?.arch || 'Unknown',
45
+ distribution: raw.host?.distribution || {},
46
+ kernel: raw.host?.kernel || 'Unknown',
47
+ cpus: raw.host?.cpus || 0,
48
+ memTotal: raw.host?.memTotal || 0,
49
+ memFree: raw.host?.memFree || 0,
50
+ hostname: raw.host?.hostname || '',
51
+ rootless: raw.host?.security?.rootless ?? true,
52
+ cgroupVersion: raw.host?.cgroupVersion || ''
53
+ },
54
+ store: {
55
+ graphDriverName: raw.store?.graphDriverName || '',
56
+ graphRoot: raw.store?.graphRoot || '',
57
+ imageStore: raw.store?.imageStore?.number || 0,
58
+ containerStore: raw.store?.containerStore?.number || 0
59
+ },
60
+ raw
61
+ };
62
+ },
63
+
64
+ async getSystemDf() {
65
+ try {
66
+ const data = await runPodmanJson(['system', 'df', '--format', 'json']);
67
+ return Array.isArray(data) ? data : [];
68
+ } catch {
69
+ return [];
70
+ }
71
+ },
72
+
73
+ async prune(type = 'all') {
74
+ let args = [];
75
+ if (type === 'containers') {
76
+ args = ['container', 'prune', '-f'];
77
+ } else if (type === 'images') {
78
+ args = ['image', 'prune', '-f'];
79
+ } else if (type === 'volumes') {
80
+ args = ['volume', 'prune', '-f'];
81
+ } else if (type === 'pods') {
82
+ args = ['pod', 'prune', '-f'];
83
+ } else {
84
+ args = ['system', 'prune', '-f', '--volumes'];
85
+ }
86
+ const { stdout, stderr } = await runPodman(args);
87
+ return { stdout, stderr };
88
+ },
89
+
90
+ // Containers
91
+ async listContainers(all = true) {
92
+ const args = ['ps', '--format', 'json'];
93
+ if (all) args.push('-a');
94
+ const containers = await runPodmanJson(args);
95
+ return Array.isArray(containers) ? containers : [];
96
+ },
97
+
98
+ async inspectContainer(id) {
99
+ const result = await runPodmanJson(['inspect', id]);
100
+ return Array.isArray(result) && result.length > 0 ? result[0] : result;
101
+ },
102
+
103
+ async startContainer(id) {
104
+ return await runPodman(['start', id]);
105
+ },
106
+
107
+ async stopContainer(id, timeout = 10) {
108
+ const args = ['stop'];
109
+ if (typeof timeout === 'number') {
110
+ args.push('-t', String(timeout));
111
+ }
112
+ args.push(id);
113
+ return await runPodman(args);
114
+ },
115
+
116
+ async restartContainer(id, timeout = 10) {
117
+ const args = ['restart'];
118
+ if (typeof timeout === 'number') {
119
+ args.push('-t', String(timeout));
120
+ }
121
+ args.push(id);
122
+ return await runPodman(args);
123
+ },
124
+
125
+ async killContainer(id, signal = 'SIGKILL') {
126
+ return await runPodman(['kill', '-s', signal, id]);
127
+ },
128
+
129
+ async pauseContainer(id) {
130
+ return await runPodman(['pause', id]);
131
+ },
132
+
133
+ async unpauseContainer(id) {
134
+ return await runPodman(['unpause', id]);
135
+ },
136
+
137
+ async removeContainer(id, force = false, volumes = false) {
138
+ const args = ['rm'];
139
+ if (force) args.push('-f');
140
+ if (volumes) args.push('-v');
141
+ args.push(id);
142
+ return await runPodman(args);
143
+ },
144
+
145
+ async createRunContainer(params) {
146
+ const {
147
+ image,
148
+ name,
149
+ ports = [],
150
+ envs = [],
151
+ volumes = [],
152
+ cmd = '',
153
+ restart,
154
+ pod,
155
+ network,
156
+ autoRemove = false,
157
+ detach = true,
158
+ privileged = false
159
+ } = params;
160
+
161
+ if (!image) {
162
+ throw new Error('Image name is required');
163
+ }
164
+
165
+ const args = ['run'];
166
+ if (detach) args.push('-d');
167
+ if (autoRemove) args.push('--rm');
168
+ if (privileged) args.push('--privileged');
169
+ if (name && name.trim()) args.push('--name', name.trim());
170
+ if (restart) args.push('--restart', restart);
171
+ if (pod) args.push('--pod', pod);
172
+ else if (network) args.push('--network', network);
173
+
174
+ // Ports
175
+ const portList = Array.isArray(ports) ? ports : ports.split('\n');
176
+ for (const p of portList) {
177
+ const trimmed = p.trim();
178
+ if (trimmed) args.push('-p', trimmed);
179
+ }
180
+
181
+ // Envs
182
+ const envList = Array.isArray(envs) ? envs : envs.split('\n');
183
+ for (const e of envList) {
184
+ const trimmed = e.trim();
185
+ if (trimmed) args.push('-e', trimmed);
186
+ }
187
+
188
+ // Volumes
189
+ const volList = Array.isArray(volumes) ? volumes : volumes.split('\n');
190
+ for (const v of volList) {
191
+ const trimmed = v.trim();
192
+ if (trimmed) args.push('-v', trimmed);
193
+ }
194
+
195
+ args.push(image.trim());
196
+
197
+ if (cmd && cmd.trim()) {
198
+ // Split command respecting basic space separation or pass as shell
199
+ const cmdParts = cmd.trim().split(/\s+/);
200
+ args.push(...cmdParts);
201
+ }
202
+
203
+ const { stdout, stderr } = await runPodman(args);
204
+ return { containerId: stdout.trim(), stderr };
205
+ },
206
+
207
+ async getLogs(id, tail = 100, timestamps = false) {
208
+ const args = ['logs'];
209
+ if (tail && tail !== 'all') {
210
+ args.push('--tail', String(tail));
211
+ }
212
+ if (timestamps) {
213
+ args.push('-t');
214
+ }
215
+ args.push(id);
216
+ const { stdout, stderr } = await runPodman(args);
217
+ return stdout + (stderr ? (stdout ? '\n' : '') + stderr : '');
218
+ },
219
+
220
+ spawnLogs(id, tail = 100, timestamps = false) {
221
+ const args = ['logs', '-f'];
222
+ if (tail && tail !== 'all') {
223
+ args.push('--tail', String(tail));
224
+ }
225
+ if (timestamps) {
226
+ args.push('-t');
227
+ }
228
+ args.push(id);
229
+ return spawn('podman', args);
230
+ },
231
+
232
+ async execCommand(id, command, workdir) {
233
+ if (!command) throw new Error('Command is required');
234
+ const args = ['exec'];
235
+ if (workdir) args.push('-w', workdir);
236
+ args.push(id, 'sh', '-c', command);
237
+ return await runPodman(args, { timeout: 30000 });
238
+ },
239
+
240
+ // Pods
241
+ async listPods() {
242
+ const pods = await runPodmanJson(['pod', 'ps', '--format', 'json']);
243
+ return Array.isArray(pods) ? pods : [];
244
+ },
245
+
246
+ async inspectPod(id) {
247
+ const result = await runPodmanJson(['pod', 'inspect', id]);
248
+ return Array.isArray(result) && result.length > 0 ? result[0] : result;
249
+ },
250
+
251
+ async startPod(id) {
252
+ return await runPodman(['pod', 'start', id]);
253
+ },
254
+
255
+ async stopPod(id) {
256
+ return await runPodman(['pod', 'stop', id]);
257
+ },
258
+
259
+ async restartPod(id) {
260
+ return await runPodman(['pod', 'restart', id]);
261
+ },
262
+
263
+ async removePod(id, force = false) {
264
+ const args = ['pod', 'rm'];
265
+ if (force) args.push('-f');
266
+ args.push(id);
267
+ return await runPodman(args);
268
+ },
269
+
270
+ async createPod(params) {
271
+ const { name, ports = [] } = params;
272
+ const args = ['pod', 'create'];
273
+ if (name && name.trim()) {
274
+ args.push('--name', name.trim());
275
+ }
276
+ const portList = Array.isArray(ports) ? ports : ports.split('\n');
277
+ for (const p of portList) {
278
+ const trimmed = p.trim();
279
+ if (trimmed) args.push('-p', trimmed);
280
+ }
281
+ const { stdout, stderr } = await runPodman(args);
282
+ return { podId: stdout.trim(), stderr };
283
+ },
284
+
285
+ // Images
286
+ async listImages() {
287
+ const images = await runPodmanJson(['images', '--format', 'json']);
288
+ return Array.isArray(images) ? images : [];
289
+ },
290
+
291
+ async inspectImage(id) {
292
+ const result = await runPodmanJson(['image', 'inspect', id]);
293
+ return Array.isArray(result) && result.length > 0 ? result[0] : result;
294
+ },
295
+
296
+ async pullImage(imageName, onProgress = null) {
297
+ if (!imageName) throw new Error('Image name is required');
298
+ if (!onProgress) {
299
+ return await runPodman(['pull', imageName], { timeout: 300000 });
300
+ }
301
+ return new Promise((resolve, reject) => {
302
+ const proc = spawn('podman', ['pull', imageName]);
303
+ let fullOutput = '';
304
+ proc.stdout.on('data', chunk => {
305
+ const text = chunk.toString();
306
+ fullOutput += text;
307
+ onProgress(text);
308
+ });
309
+ proc.stderr.on('data', chunk => {
310
+ const text = chunk.toString();
311
+ fullOutput += text;
312
+ onProgress(text);
313
+ });
314
+ proc.on('close', code => {
315
+ if (code === 0) resolve({ stdout: fullOutput });
316
+ else reject(new Error(`podman pull exited with code ${code}:\n${fullOutput}`));
317
+ });
318
+ proc.on('error', err => reject(err));
319
+ });
320
+ },
321
+
322
+ async removeImage(id, force = false) {
323
+ const args = ['rmi'];
324
+ if (force) args.push('-f');
325
+ args.push(id);
326
+ return await runPodman(args);
327
+ },
328
+
329
+ // Volumes
330
+ async listVolumes() {
331
+ const volumes = await runPodmanJson(['volume', 'ls', '--format', 'json']);
332
+ return Array.isArray(volumes) ? volumes : [];
333
+ },
334
+
335
+ async inspectVolume(name) {
336
+ const result = await runPodmanJson(['volume', 'inspect', name]);
337
+ return Array.isArray(result) && result.length > 0 ? result[0] : result;
338
+ },
339
+
340
+ async createVolume(name, driver) {
341
+ const args = ['volume', 'create'];
342
+ if (driver) args.push('--driver', driver);
343
+ if (name) args.push(name);
344
+ return await runPodman(args);
345
+ },
346
+
347
+ async removeVolume(name, force = false) {
348
+ const args = ['volume', 'rm'];
349
+ if (force) args.push('-f');
350
+ args.push(name);
351
+ return await runPodman(args);
352
+ },
353
+
354
+ // Networks
355
+ async listNetworks() {
356
+ const networks = await runPodmanJson(['network', 'ls', '--format', 'json']);
357
+ return Array.isArray(networks) ? networks : [];
358
+ },
359
+
360
+ async inspectNetwork(name) {
361
+ const result = await runPodmanJson(['network', 'inspect', name]);
362
+ return Array.isArray(result) && result.length > 0 ? result[0] : result;
363
+ },
364
+
365
+ async createNetwork(params) {
366
+ const { name, subnet, gateway, driver } = params;
367
+ const args = ['network', 'create'];
368
+ if (driver) args.push('--driver', driver);
369
+ if (subnet) args.push('--subnet', subnet);
370
+ if (gateway) args.push('--gateway', gateway);
371
+ if (name) args.push(name);
372
+ return await runPodman(args);
373
+ },
374
+
375
+ async removeNetwork(name) {
376
+ return await runPodman(['network', 'rm', name]);
377
+ },
378
+
379
+ // Events
380
+ async getEvents(since = '24h') {
381
+ try {
382
+ const args = ['events', '--stream=false', '--format', 'json'];
383
+ if (since) args.push('--since', since);
384
+ const { stdout } = await runPodman(args);
385
+ if (!stdout) return [];
386
+ const lines = stdout.split('\n').filter(Boolean);
387
+ const events = [];
388
+ for (const line of lines) {
389
+ try {
390
+ events.push(JSON.parse(line));
391
+ } catch {}
392
+ }
393
+ return events.reverse();
394
+ } catch {
395
+ return [];
396
+ }
397
+ },
398
+
399
+ spawnEvents() {
400
+ return spawn('podman', ['events', '--format', 'json']);
401
+ }
402
+ };
403
+
404
+ module.exports = Podman;