herolib-virt 0.3.13

Virtualization and container management for herolib (buildah, nerdctl, kubernetes)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
//! Rhai wrappers for Nerdctl module functions
//!
//! This module provides Rhai wrappers for the functions in the Nerdctl module.

use crate::nerdctl::{self, Container, ContainerStatus, Image, NerdctlError};
use rhai::{Array, Dynamic, Engine, EvalAltResult, Map};
use crate::process::CommandResult;

// Helper functions for error conversion with improved context
fn nerdctl_error_to_rhai_error<T>(
    result: Result<T, NerdctlError>,
) -> Result<T, Box<EvalAltResult>> {
    result.map_err(|e| {
        // Create a more detailed error message based on the error type
        let error_message = match &e {
            NerdctlError::CommandExecutionFailed(io_err) => {
                format!("Failed to execute nerdctl command: {}. This may indicate nerdctl is not installed or not in PATH.", io_err)
            },
            NerdctlError::CommandFailed(msg) => {
                format!("Nerdctl command failed: {}. Check container status and logs for more details.", msg)
            },
            NerdctlError::JsonParseError(msg) => {
                format!("Failed to parse nerdctl JSON output: {}. This may indicate an incompatible nerdctl version.", msg)
            },
            NerdctlError::ConversionError(msg) => {
                format!("Data conversion error: {}. This may indicate unexpected output format from nerdctl.", msg)
            },
            NerdctlError::Other(msg) => {
                format!("Nerdctl error: {}. This is an unexpected error.", msg)
            },
        };
        Box::new(EvalAltResult::ErrorRuntime(
            error_message.into(),
            rhai::Position::NONE
        ))
    })
}

//
// Container Builder Pattern Implementation
//

/// Create a new Container
pub fn container_new(name: &str) -> Result<Container, Box<EvalAltResult>> {
    nerdctl_error_to_rhai_error(Container::new(name))
}

/// Create a Container from an image
pub fn container_from_image(name: &str, image: &str) -> Result<Container, Box<EvalAltResult>> {
    nerdctl_error_to_rhai_error(Container::from_image(name, image))
}

/// Reset the container configuration to defaults while keeping the name and image
pub fn container_reset(container: Container) -> Container {
    container.reset()
}

/// Add a port mapping to a Container
pub fn container_with_port(container: Container, port: &str) -> Container {
    container.with_port(port)
}

/// Add a volume mount to a Container
pub fn container_with_volume(container: Container, volume: &str) -> Container {
    container.with_volume(volume)
}

/// Add an environment variable to a Container
pub fn container_with_env(container: Container, key: &str, value: &str) -> Container {
    container.with_env(key, value)
}

/// Set the network for a Container
pub fn container_with_network(container: Container, network: &str) -> Container {
    container.with_network(network)
}

/// Add a network alias to a Container
pub fn container_with_network_alias(container: Container, alias: &str) -> Container {
    container.with_network_alias(alias)
}

/// Set CPU limit for a Container
pub fn container_with_cpu_limit(container: Container, cpus: &str) -> Container {
    container.with_cpu_limit(cpus)
}

/// Set memory limit for a Container
pub fn container_with_memory_limit(container: Container, memory: &str) -> Container {
    container.with_memory_limit(memory)
}

/// Set restart policy for a Container
pub fn container_with_restart_policy(container: Container, policy: &str) -> Container {
    container.with_restart_policy(policy)
}

/// Set health check for a Container
pub fn container_with_health_check(container: Container, cmd: &str) -> Container {
    container.with_health_check(cmd)
}

/// Add multiple port mappings to a Container
pub fn container_with_ports(mut container: Container, ports: Array) -> Container {
    for port in ports.iter() {
        if port.is_string() {
            let port_str = port.clone().cast::<String>();
            container = container.with_port(&port_str);
        }
    }
    container
}

/// Add multiple volume mounts to a Container
pub fn container_with_volumes(mut container: Container, volumes: Array) -> Container {
    for volume in volumes.iter() {
        if volume.is_string() {
            let volume_str = volume.clone().cast::<String>();
            container = container.with_volume(&volume_str);
        }
    }
    container
}

/// Add multiple environment variables to a Container
pub fn container_with_envs(mut container: Container, env_map: Map) -> Container {
    for (key, value) in env_map.iter() {
        if value.is_string() {
            let value_str = value.clone().cast::<String>();
            container = container.with_env(&key, &value_str);
        }
    }
    container
}

/// Add multiple network aliases to a Container
pub fn container_with_network_aliases(mut container: Container, aliases: Array) -> Container {
    for alias in aliases.iter() {
        if alias.is_string() {
            let alias_str = alias.clone().cast::<String>();
            container = container.with_network_alias(&alias_str);
        }
    }
    container
}

/// Set memory swap limit for a Container
pub fn container_with_memory_swap_limit(container: Container, memory_swap: &str) -> Container {
    container.with_memory_swap_limit(memory_swap)
}

/// Set CPU shares for a Container
pub fn container_with_cpu_shares(container: Container, shares: &str) -> Container {
    container.with_cpu_shares(shares)
}

/// Set health check with options for a Container
pub fn container_with_health_check_options(
    container: Container,
    cmd: &str,
    interval: Option<&str>,
    timeout: Option<&str>,
    retries: Option<i64>,
    start_period: Option<&str>,
) -> Container {
    // Convert i64 to u32 for retries
    let retries_u32 = retries.map(|r| r as u32);
    container.with_health_check_options(cmd, interval, timeout, retries_u32, start_period)
}

/// Set snapshotter for a Container
pub fn container_with_snapshotter(container: Container, snapshotter: &str) -> Container {
    container.with_snapshotter(snapshotter)
}

/// Set detach mode for a Container
pub fn container_with_detach(container: Container, detach: bool) -> Container {
    container.with_detach(detach)
}

/// Set runtime for a Container
pub fn container_with_runtime(container: Container, runtime: &str) -> Container {
    container.with_runtime(runtime)
}

/// Set disk size limit for a Container
///
/// For Kata containers, this sets the VM block device size via OCI annotation.
pub fn container_with_disk_limit(container: Container, size: &str) -> Container {
    container.with_disk_limit(size)
}

/// Add an OCI annotation to a Container
pub fn container_with_annotation(container: Container, key: &str, value: &str) -> Container {
    container.with_annotation(key, value)
}

/// Add multiple OCI annotations to a Container
pub fn container_with_annotations(mut container: Container, annotations_map: Map) -> Container {
    for (key, value) in annotations_map.iter() {
        if value.is_string() {
            let value_str = value.clone().cast::<String>();
            container = container.with_annotation(&key, &value_str);
        }
    }
    container
}

/// Set privileged mode for a Container
pub fn container_with_privileged(container: Container, privileged: bool) -> Container {
    container.with_privileged(privileged)
}

/// Add a device to a Container
pub fn container_with_device(container: Container, device: &str) -> Container {
    container.with_device(device)
}

/// Add multiple devices to a Container
pub fn container_with_devices(mut container: Container, devices: Array) -> Container {
    for device in devices.iter() {
        if device.is_string() {
            let device_str = device.clone().cast::<String>();
            container = container.with_device(&device_str);
        }
    }
    container
}

/// Build and run the Container
///
/// This function builds and runs the container using the configured options.
/// It provides detailed error information if the build fails.
pub fn container_build(container: Container) -> Result<Container, Box<EvalAltResult>> {
    // Get container details for better error reporting
    let container_name = container.name.clone();
    let image = container
        .image
        .clone()
        .unwrap_or_else(|| "none".to_string());
    let ports = container.ports.clone();
    let volumes = container.volumes.clone();
    let env_vars = container.env_vars.clone();

    // Try to build the container
    let build_result = container.build();

    // Handle the result with improved error context
    match build_result {
        Ok(built_container) => {
            // Container built successfully
            Ok(built_container)
        }
        Err(err) => {
            // Add more context to the error
            let enhanced_error = match err {
                NerdctlError::CommandFailed(msg) => {
                    // Provide more detailed error information
                    let mut enhanced_msg = format!(
                        "Failed to build container '{}' from image '{}': {}",
                        container_name, image, msg
                    );

                    // Add information about configured options that might be relevant
                    if !ports.is_empty() {
                        enhanced_msg.push_str(&format!("\nConfigured ports: {:?}", ports));
                    }

                    if !volumes.is_empty() {
                        enhanced_msg.push_str(&format!("\nConfigured volumes: {:?}", volumes));
                    }

                    if !env_vars.is_empty() {
                        enhanced_msg.push_str(&format!(
                            "\nConfigured environment variables: {:?}",
                            env_vars
                        ));
                    }

                    // Add suggestions for common issues
                    if msg.contains("not found") || msg.contains("no such image") {
                        enhanced_msg.push_str("\nSuggestion: The specified image may not exist or may not be pulled yet. Try pulling the image first with nerdctl_image_pull().");
                    } else if msg.contains("port is already allocated") {
                        enhanced_msg.push_str("\nSuggestion: One of the specified ports is already in use. Try using a different port or stopping the container using that port.");
                    } else if msg.contains("permission denied") {
                        enhanced_msg.push_str("\nSuggestion: Permission issues detected. Check if you have the necessary permissions to create containers or access the specified volumes.");
                    }

                    NerdctlError::CommandFailed(enhanced_msg)
                }
                _ => err,
            };

            nerdctl_error_to_rhai_error(Err(enhanced_error))
        }
    }
}

/// Start the Container and verify it's running
///
/// This function starts the container and verifies that it's actually running.
/// It returns detailed error information if the container fails to start or
/// if it starts but stops immediately.
pub fn container_start(container: &mut Container) -> Result<CommandResult, Box<EvalAltResult>> {
    // Get container details for better error reporting
    let container_name = container.name.clone();
    let container_id = container
        .container_id
        .clone()
        .unwrap_or_else(|| "unknown".to_string());

    // Try to start the container
    let start_result = container.start();

    // Handle the result with improved error context
    match start_result {
        Ok(result) => {
            // Container started successfully
            Ok(result)
        }
        Err(err) => {
            // Add more context to the error
            let enhanced_error = match err {
                NerdctlError::CommandFailed(msg) => {
                    // Check if this is a "container already running" error, which is not really an error
                    if msg.contains("already running") {
                        return Ok(CommandResult {
                            stdout: format!("Container {} is already running", container_name),
                            stderr: "".to_string(),
                            success: true,
                            code: 0,
                        });
                    }

                    // Try to get more information about why the container might have failed to start
                    let mut enhanced_msg = format!(
                        "Failed to start container '{}' (ID: {}): {}",
                        container_name, container_id, msg
                    );

                    // Try to check if the image exists
                    if let Some(image) = &container.image {
                        enhanced_msg.push_str(&format!("\nContainer was using image: {}", image));
                    }

                    NerdctlError::CommandFailed(enhanced_msg)
                }
                _ => err,
            };

            nerdctl_error_to_rhai_error(Err(enhanced_error))
        }
    }
}

/// Stop the Container
pub fn container_stop(container: &mut Container) -> Result<CommandResult, Box<EvalAltResult>> {
    nerdctl_error_to_rhai_error(container.stop())
}

/// Remove the Container
pub fn container_remove(container: &mut Container) -> Result<CommandResult, Box<EvalAltResult>> {
    nerdctl_error_to_rhai_error(container.remove())
}

/// Execute a command in the Container
pub fn container_exec(
    container: &mut Container,
    command: &str,
) -> Result<CommandResult, Box<EvalAltResult>> {
    nerdctl_error_to_rhai_error(container.exec(command))
}

/// Get container status
pub fn container_status(container: &mut Container) -> Result<ContainerStatus, Box<EvalAltResult>> {
    nerdctl_error_to_rhai_error(container.status())
}

/// Get container logs
pub fn container_logs(container: &mut Container) -> Result<CommandResult, Box<EvalAltResult>> {
    // Get container details for better error reporting
    let container_name = container.name.clone();
    let container_id = container
        .container_id
        .clone()
        .unwrap_or_else(|| "unknown".to_string());

    // Use the nerdctl::logs function
    let logs_result = nerdctl::logs(&container_id);

    match logs_result {
        Ok(result) => Ok(result),
        Err(err) => {
            // Add more context to the error
            let enhanced_error = NerdctlError::CommandFailed(format!(
                "Failed to get logs for container '{}' (ID: {}): {}",
                container_name, container_id, err
            ));

            nerdctl_error_to_rhai_error(Err(enhanced_error))
        }
    }
}

/// Copy files between the Container and local filesystem
pub fn container_copy(
    container: &mut Container,
    source: &str,
    dest: &str,
) -> Result<CommandResult, Box<EvalAltResult>> {
    nerdctl_error_to_rhai_error(container.copy(source, dest))
}

/// Create a new Map with default run options
pub fn new_run_options() -> Map {
    let mut map = Map::new();
    map.insert("name".into(), Dynamic::UNIT);
    map.insert("detach".into(), Dynamic::from(true));
    map.insert("ports".into(), Dynamic::from(Array::new()));
    map.insert("snapshotter".into(), Dynamic::from("native"));
    map
}

//
// Container Function Wrappers
//

/// Wrapper for nerdctl::run
///
/// Run a container from an image.
pub fn nerdctl_run(image: &str) -> Result<CommandResult, Box<EvalAltResult>> {
    nerdctl_error_to_rhai_error(nerdctl::run(image, None, true, None, None))
}

/// Run a container with a name
pub fn nerdctl_run_with_name(image: &str, name: &str) -> Result<CommandResult, Box<EvalAltResult>> {
    nerdctl_error_to_rhai_error(nerdctl::run(image, Some(name), true, None, None))
}

/// Run a container with a port mapping
pub fn nerdctl_run_with_port(
    image: &str,
    name: &str,
    port: &str,
) -> Result<CommandResult, Box<EvalAltResult>> {
    let ports = vec![port];
    nerdctl_error_to_rhai_error(nerdctl::run(image, Some(name), true, Some(&ports), None))
}

/// Wrapper for nerdctl::exec
///
/// Execute a command in a container.
pub fn nerdctl_exec(container: &str, command: &str) -> Result<CommandResult, Box<EvalAltResult>> {
    nerdctl_error_to_rhai_error(nerdctl::exec(container, command))
}

/// Wrapper for nerdctl::copy
///
/// Copy files between container and local filesystem.
pub fn nerdctl_copy(source: &str, dest: &str) -> Result<CommandResult, Box<EvalAltResult>> {
    nerdctl_error_to_rhai_error(nerdctl::copy(source, dest))
}

/// Wrapper for nerdctl::stop
///
/// Stop a container.
pub fn nerdctl_stop(container: &str) -> Result<CommandResult, Box<EvalAltResult>> {
    nerdctl_error_to_rhai_error(nerdctl::stop(container))
}

/// Wrapper for nerdctl::remove
///
/// Remove a container.
pub fn nerdctl_remove(container: &str) -> Result<CommandResult, Box<EvalAltResult>> {
    nerdctl_error_to_rhai_error(nerdctl::remove(container))
}

/// Wrapper for nerdctl::list
///
/// List containers.
pub fn nerdctl_list(all: bool) -> Result<CommandResult, Box<EvalAltResult>> {
    nerdctl_error_to_rhai_error(nerdctl::list(all))
}

/// Wrapper for nerdctl::logs
///
/// Get container logs.
pub fn nerdctl_logs(container: &str) -> Result<CommandResult, Box<EvalAltResult>> {
    nerdctl_error_to_rhai_error(nerdctl::logs(container))
}

//
// Image Function Wrappers
//

/// Wrapper for nerdctl::images
///
/// List images in local storage.
pub fn nerdctl_images() -> Result<CommandResult, Box<EvalAltResult>> {
    nerdctl_error_to_rhai_error(nerdctl::images())
}

/// Wrapper for nerdctl::image_remove
///
/// Remove one or more images.
pub fn nerdctl_image_remove(image: &str) -> Result<CommandResult, Box<EvalAltResult>> {
    nerdctl_error_to_rhai_error(nerdctl::image_remove(image))
}

/// Wrapper for nerdctl::image_push
///
/// Push an image to a registry.
pub fn nerdctl_image_push(
    image: &str,
    destination: &str,
) -> Result<CommandResult, Box<EvalAltResult>> {
    nerdctl_error_to_rhai_error(nerdctl::image_push(image, destination))
}

/// Wrapper for nerdctl::image_tag
///
/// Add an additional name to a local image.
pub fn nerdctl_image_tag(image: &str, new_name: &str) -> Result<CommandResult, Box<EvalAltResult>> {
    nerdctl_error_to_rhai_error(nerdctl::image_tag(image, new_name))
}

/// Wrapper for nerdctl::image_pull
///
/// Pull an image from a registry.
pub fn nerdctl_image_pull(image: &str) -> Result<CommandResult, Box<EvalAltResult>> {
    nerdctl_error_to_rhai_error(nerdctl::image_pull(image))
}

/// Wrapper for nerdctl::image_commit
///
/// Commit a container to an image.
pub fn nerdctl_image_commit(
    container: &str,
    image_name: &str,
) -> Result<CommandResult, Box<EvalAltResult>> {
    nerdctl_error_to_rhai_error(nerdctl::image_commit(container, image_name))
}

/// Wrapper for nerdctl::image_build
///
/// Build an image using a Dockerfile.
pub fn nerdctl_image_build(
    tag: &str,
    context_path: &str,
) -> Result<CommandResult, Box<EvalAltResult>> {
    nerdctl_error_to_rhai_error(nerdctl::image_build(tag, context_path))
}

/// Register Nerdctl module functions with the Rhai engine
///
/// # Arguments
///
/// * `engine` - The Rhai engine to register the functions with
///
/// # Returns
///
/// * `Result<(), Box<EvalAltResult>>` - Ok if registration was successful, Err otherwise
pub fn register_nerdctl_module(engine: &mut Engine) -> Result<(), Box<EvalAltResult>> {
    // Register types
    register_nerdctl_types(engine)?;

    // Register Container constructor
    engine.register_fn("nerdctl_container_new", container_new);
    engine.register_fn("nerdctl_container_from_image", container_from_image);

    // Register Container instance methods
    engine.register_fn("reset", container_reset);
    engine.register_fn("with_port", container_with_port);
    engine.register_fn("with_volume", container_with_volume);
    engine.register_fn("with_env", container_with_env);
    engine.register_fn("with_network", container_with_network);
    engine.register_fn("with_network_alias", container_with_network_alias);
    engine.register_fn("with_cpu_limit", container_with_cpu_limit);
    engine.register_fn("with_memory_limit", container_with_memory_limit);
    engine.register_fn("with_restart_policy", container_with_restart_policy);
    engine.register_fn("with_health_check", container_with_health_check);
    engine.register_fn("with_ports", container_with_ports);
    engine.register_fn("with_volumes", container_with_volumes);
    engine.register_fn("with_envs", container_with_envs);
    engine.register_fn("with_network_aliases", container_with_network_aliases);
    engine.register_fn("with_memory_swap_limit", container_with_memory_swap_limit);
    engine.register_fn("with_cpu_shares", container_with_cpu_shares);
    engine.register_fn(
        "with_health_check_options",
        container_with_health_check_options,
    );
    engine.register_fn("with_snapshotter", container_with_snapshotter);
    engine.register_fn("with_detach", container_with_detach);
    engine.register_fn("with_runtime", container_with_runtime);
    engine.register_fn("with_disk_limit", container_with_disk_limit);
    engine.register_fn("with_annotation", container_with_annotation);
    engine.register_fn("with_annotations", container_with_annotations);
    engine.register_fn("with_privileged", container_with_privileged);
    engine.register_fn("with_device", container_with_device);
    engine.register_fn("with_devices", container_with_devices);
    engine.register_fn("build", container_build);
    engine.register_fn("start", container_start);
    engine.register_fn("stop", container_stop);
    engine.register_fn("remove", container_remove);
    engine.register_fn("exec", container_exec);
    engine.register_fn("status", container_status);
    engine.register_fn("logs", container_logs);
    engine.register_fn("copy", container_copy);

    // Register legacy container functions (for backward compatibility)
    engine.register_fn("nerdctl_run", nerdctl_run);
    engine.register_fn("nerdctl_run_with_name", nerdctl_run_with_name);
    engine.register_fn("nerdctl_run_with_port", nerdctl_run_with_port);
    engine.register_fn("new_run_options", new_run_options);
    engine.register_fn("nerdctl_exec", nerdctl_exec);
    engine.register_fn("nerdctl_copy", nerdctl_copy);
    engine.register_fn("nerdctl_stop", nerdctl_stop);
    engine.register_fn("nerdctl_remove", nerdctl_remove);
    engine.register_fn("nerdctl_list", nerdctl_list);
    engine.register_fn("nerdctl_logs", nerdctl_logs);

    // Register image functions
    engine.register_fn("nerdctl_images", nerdctl_images);
    engine.register_fn("nerdctl_image_remove", nerdctl_image_remove);
    engine.register_fn("nerdctl_image_push", nerdctl_image_push);
    engine.register_fn("nerdctl_image_tag", nerdctl_image_tag);
    engine.register_fn("nerdctl_image_pull", nerdctl_image_pull);
    engine.register_fn("nerdctl_image_commit", nerdctl_image_commit);
    engine.register_fn("nerdctl_image_build", nerdctl_image_build);

    Ok(())
}

/// Register Nerdctl module types with the Rhai engine
fn register_nerdctl_types(engine: &mut Engine) -> Result<(), Box<EvalAltResult>> {
    // Register Container type
    engine.register_type_with_name::<Container>("NerdctlContainer");

    // Register getters for Container properties
    engine.register_get("name", |container: &mut Container| container.name.clone());
    engine.register_get(
        "container_id",
        |container: &mut Container| match &container.container_id {
            Some(id) => id.clone(),
            None => "".to_string(),
        },
    );
    engine.register_get("image", |container: &mut Container| {
        match &container.image {
            Some(img) => img.clone(),
            None => "".to_string(),
        }
    });
    engine.register_get("ports", |container: &mut Container| {
        let mut array = Array::new();
        for port in &container.ports {
            array.push(Dynamic::from(port.clone()));
        }
        array
    });
    engine.register_get("volumes", |container: &mut Container| {
        let mut array = Array::new();
        for volume in &container.volumes {
            array.push(Dynamic::from(volume.clone()));
        }
        array
    });
    engine.register_get("detach", |container: &mut Container| container.detach);

    // Register Image type and methods
    engine.register_type_with_name::<Image>("NerdctlImage");

    // Register getters for Image properties
    engine.register_get("id", |img: &mut Image| img.id.clone());
    engine.register_get("repository", |img: &mut Image| img.repository.clone());
    engine.register_get("tag", |img: &mut Image| img.tag.clone());
    engine.register_get("size", |img: &mut Image| img.size.clone());
    engine.register_get("created", |img: &mut Image| img.created.clone());

    // Register ContainerStatus type and methods
    engine.register_type_with_name::<ContainerStatus>("ContainerStatus");

    // Register getters for ContainerStatus properties
    engine.register_get("state", |status: &mut ContainerStatus| status.state.clone());
    engine.register_get("status", |status: &mut ContainerStatus| status.status.clone());
    engine.register_get("created", |status: &mut ContainerStatus| status.created.clone());
    engine.register_get("started", |status: &mut ContainerStatus| status.started.clone());
    engine.register_get("health_status", |status: &mut ContainerStatus| {
        status.health_status.clone().unwrap_or_else(|| "".to_string())
    });
    engine.register_get("health_output", |status: &mut ContainerStatus| {
        status.health_output.clone().unwrap_or_else(|| "".to_string())
    });

    Ok(())
}