buildah and nerdctl fixes

This commit is contained in:
Maxime Van Hees
2025-07-09 16:27:11 +02:00
parent d431705501
commit 568a5b0a49
6 changed files with 375 additions and 240 deletions

View File

@@ -52,6 +52,20 @@ impl Container {
}
}
/// Add an image
///
/// # Arguments
///
/// * `image` - Image to create the container from
///
/// # Returns
///
/// * `Self` - The container instance for method chaining
pub fn with_image(mut self, image: &str) -> Self {
self.image = Some(image.to_string());
self
}
/// Add a port mapping
///
/// # Arguments

View File

@@ -2,7 +2,7 @@
//!
//! This module provides Rhai wrappers for the functions in the Nerdctl module.
use crate::nerdctl::{self, Container, Image, NerdctlError};
use crate::nerdctl::{self, Container, HealthCheck, Image, NerdctlError};
use rhai::{Array, Dynamic, Engine, EvalAltResult, Map};
use sal_process::CommandResult;
@@ -55,105 +55,7 @@ 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)
}
// TODO: remove?
/// Set health check with options for a Container
pub fn container_with_health_check_options(
container: Container,
@@ -168,16 +70,6 @@ pub fn container_with_health_check_options(
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)
}
/// Build and run the Container
///
/// This function builds and runs the container using the configured options.
@@ -514,29 +406,34 @@ pub fn register_nerdctl_module(engine: &mut Engine) -> Result<(), Box<EvalAltRes
engine.register_fn("nerdctl_container_new", container_new);
engine.register_fn("nerdctl_container_from_image", container_from_image);
// TODO: check if this works!
// 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);
// TODO: these functions should be getters and setter like the buildah example
// engine.register_fn("with_image", container_with_image);
// 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("build", container_build);
engine.register_fn("start", container_start);
engine.register_fn("stop", container_stop);
@@ -573,9 +470,47 @@ pub fn register_nerdctl_module(engine: &mut Engine) -> Result<(), Box<EvalAltRes
fn register_nerdctl_types(engine: &mut Engine) -> Result<(), Box<EvalAltResult>> {
// Register Container type
engine.register_type_with_name::<Container>("NerdctlContainer");
engine.register_type_with_name::<HealthCheck>("NerdctlHealthCheck");
// Register getters for Container properties
// Register getters & setters for HealthCheck properties
engine.register_get("cmd", |hc: &mut HealthCheck| hc.cmd.clone());
engine.register_set("cmd", |hc: &mut HealthCheck, cmd: &str| {
hc.cmd = cmd.to_string();
});
engine.register_get("interval", |hc: &mut HealthCheck| {
hc.interval.clone().unwrap_or_default()
});
engine.register_set("interval", |hc: &mut HealthCheck, interval: &str| {
hc.interval = Some(interval.to_string());
});
engine.register_get("timeout", |hc: &mut HealthCheck| {
hc.timeout.clone().unwrap_or_default()
});
engine.register_set("timeout", |hc: &mut HealthCheck, timeout: &str| {
hc.timeout = Some(timeout.to_string());
});
engine.register_get("retries", |hc: &mut HealthCheck| {
hc.retries.map_or(0, |r| r as i64)
});
engine.register_set("retries", |hc: &mut HealthCheck, retries: i64| {
hc.retries = Some(retries as u32);
});
engine.register_get("start_period", |hc: &mut HealthCheck| {
hc.start_period.clone().unwrap_or_default()
});
engine.register_set("start_period", |hc: &mut HealthCheck, start_period: &str| {
hc.start_period = Some(start_period.to_string());
});
// Register getters & setters for Container properties
// -- name
engine.register_get("name", |container: &mut Container| container.name.clone());
engine.register_set("image", |container: &mut Container, image: &str| {
container.image = Some(image.to_string());
});
// -- container_id
engine.register_get(
"container_id",
|container: &mut Container| match &container.container_id {
@@ -583,12 +518,37 @@ fn register_nerdctl_types(engine: &mut Engine) -> Result<(), Box<EvalAltResult>>
None => "".to_string(),
},
);
engine.register_set("container_id", |container: &mut Container, container_id: &str| {
container.container_id = Some(container_id.to_string());
});
// -- image
engine.register_get("image", |container: &mut Container| {
match &container.image {
Some(img) => img.clone(),
None => "".to_string(),
}
});
engine.register_set("image", |container: &mut Container, image: &str| {
container.image = Some(image.to_string());
});
// -- config
engine.register_get("config", |container: &mut Container| {
container
.config
.iter()
.map(|(k, v)| (k.clone().into(), v.clone().into()))
.collect::<Map>()
});
engine.register_set("config", |container: &mut Container, config: Map| {
container.config = config
.into_iter()
.map(|(k, v)| (k.to_string(), v.into_string().unwrap_or_default()))
.collect();
});
// -- ports
engine.register_get("ports", |container: &mut Container| {
let mut array = Array::new();
for port in &container.ports {
@@ -596,6 +556,14 @@ fn register_nerdctl_types(engine: &mut Engine) -> Result<(), Box<EvalAltResult>>
}
array
});
engine.register_set("ports", |container: &mut Container, ports: Array| {
container.ports = ports
.into_iter()
.map(|v| v.into_string().unwrap_or_default())
.collect();
});
// -- volumes
engine.register_get("volumes", |container: &mut Container| {
let mut array = Array::new();
for volume in &container.volumes {
@@ -603,7 +571,125 @@ fn register_nerdctl_types(engine: &mut Engine) -> Result<(), Box<EvalAltResult>>
}
array
});
engine.register_set("volumes", |container: &mut Container, volumes: Array| {
container.volumes = volumes
.into_iter()
.map(|v| v.into_string().unwrap_or_default())
.collect();
});
// -- env_vars
engine.register_get("env_vars", |container: &mut Container| {
container
.env_vars
.iter()
.map(|(k, v)| (k.clone().into(), v.clone().into()))
.collect::<Map>()
});
engine.register_set("env_vars", |container: &mut Container, env_vars: Map| {
container.env_vars = env_vars
.into_iter()
.map(|(k, v)| (k.to_string(), v.into_string().unwrap_or_default()))
.collect();
});
// -- network
engine.register_get("network", |container: &mut Container| {
container.network.clone().unwrap_or_default()
});
engine.register_set("network", |container: &mut Container, network: &str| {
container.network = Some(network.to_string());
});
// -- network_aliases
engine.register_get("network_aliases", |container: &mut Container| {
container
.network_aliases
.iter()
.map(|alias| Dynamic::from(alias.clone()))
.collect::<Array>()
});
engine.register_set(
"network_aliases",
|container: &mut Container, aliases: Array| {
container.network_aliases = aliases
.into_iter()
.map(|a| a.into_string().unwrap_or_default())
.collect();
},
);
// -- cpu_limit
engine.register_get("cpu_limit", |container: &mut Container| {
container.cpu_limit.clone().unwrap_or_default()
});
engine.register_set("cpu_limit", |container: &mut Container, limit: &str| {
container.cpu_limit = Some(limit.to_string());
});
// -- memory_limit
engine.register_get("memory_limit", |container: &mut Container| {
container.memory_limit.clone().unwrap_or_default()
});
engine.register_set("memory_limit", |container: &mut Container, limit: &str| {
container.memory_limit = Some(limit.to_string());
});
// -- memory_swap_limit
engine.register_get("memory_swap_limit", |container: &mut Container| {
container.memory_swap_limit.clone().unwrap_or_default()
});
engine.register_set(
"memory_swap_limit",
|container: &mut Container, limit: &str| {
container.memory_swap_limit = Some(limit.to_string());
},
);
// -- cpu_shares
engine.register_get("cpu_shares", |container: &mut Container| {
container.cpu_shares.clone().unwrap_or_default()
});
engine.register_set("cpu_shares", |container: &mut Container, shares: &str| {
container.cpu_shares = Some(shares.to_string());
});
// -- restart_policy
engine.register_get("restart_policy", |container: &mut Container| {
container.restart_policy.clone().unwrap_or_default()
});
engine.register_set(
"restart_policy",
|container: &mut Container, policy: &str| {
container.restart_policy = Some(policy.to_string());
},
);
// TODO: setters and getters for health_check
// -- health_check
// engine.register_get("health_check", |container: &mut Container| {
// container.health_check.clone()
// });
// engine.register_set(
// "health_check",
// |container: &mut Container, health_check: HealthCheck| {
// container.health_check = Some(health_check);
// },
// );
// -- detach
engine.register_get("detach", |container: &mut Container| container.detach);
engine.register_set("detach", |container: &mut Container, detach: bool| {
container.detach = detach;
});
// -- snapshotter
engine.register_get("snapshotter", |container: &mut Container| {
container.snapshotter.clone().unwrap_or_default()
});
engine.register_set("snapshotter", |container: &mut Container, snapshotter: &str| {
container.snapshotter = Some(snapshotter.to_string());
});
// Register Image type and methods
engine.register_type_with_name::<Image>("NerdctlImage");