podmanager 0.0.0-stage → 1.0.1

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