use std::time::Duration;
use bollard_stubs::models::ResourcesUlimits;
use crate::{
core::{logs::consumer::LogConsumer, CgroupnsMode, ContainerPort, Host, Mount, PortMapping},
ContainerRequest, Image,
};
pub trait ImageExt<I: Image> {
fn with_cmd(self, cmd: impl IntoIterator<Item = impl Into<String>>) -> ContainerRequest<I>;
fn with_name(self, name: impl Into<String>) -> ContainerRequest<I>;
fn with_tag(self, tag: impl Into<String>) -> ContainerRequest<I>;
fn with_container_name(self, name: impl Into<String>) -> ContainerRequest<I>;
fn with_network(self, network: impl Into<String>) -> ContainerRequest<I>;
fn with_env_var(self, name: impl Into<String>, value: impl Into<String>)
-> ContainerRequest<I>;
fn with_host(self, key: impl Into<String>, value: impl Into<Host>) -> ContainerRequest<I>;
fn with_mount(self, mount: impl Into<Mount>) -> ContainerRequest<I>;
fn with_mapped_port(self, host_port: u16, container_port: ContainerPort)
-> ContainerRequest<I>;
fn with_ulimit(self, name: &str, soft: i64, hard: Option<i64>) -> ContainerRequest<I>;
fn with_privileged(self, privileged: bool) -> ContainerRequest<I>;
fn with_cgroupns_mode(self, cgroupns_mode: CgroupnsMode) -> ContainerRequest<I>;
fn with_userns_mode(self, userns_mode: &str) -> ContainerRequest<I>;
fn with_shm_size(self, bytes: u64) -> ContainerRequest<I>;
fn with_startup_timeout(self, timeout: Duration) -> ContainerRequest<I>;
fn with_working_dir(self, working_dir: impl Into<String>) -> ContainerRequest<I>;
fn with_log_consumer(self, log_consumer: impl LogConsumer + 'static) -> ContainerRequest<I>;
}
impl<RI: Into<ContainerRequest<I>>, I: Image> ImageExt<I> for RI {
fn with_cmd(self, cmd: impl IntoIterator<Item = impl Into<String>>) -> ContainerRequest<I> {
let container_req = self.into();
ContainerRequest {
overridden_cmd: cmd.into_iter().map(Into::into).collect(),
..container_req
}
}
fn with_name(self, name: impl Into<String>) -> ContainerRequest<I> {
let container_req = self.into();
ContainerRequest {
image_name: Some(name.into()),
..container_req
}
}
fn with_tag(self, tag: impl Into<String>) -> ContainerRequest<I> {
let container_req = self.into();
ContainerRequest {
image_tag: Some(tag.into()),
..container_req
}
}
fn with_container_name(self, name: impl Into<String>) -> ContainerRequest<I> {
let container_req = self.into();
ContainerRequest {
container_name: Some(name.into()),
..container_req
}
}
fn with_network(self, network: impl Into<String>) -> ContainerRequest<I> {
let container_req = self.into();
ContainerRequest {
network: Some(network.into()),
..container_req
}
}
fn with_env_var(
self,
name: impl Into<String>,
value: impl Into<String>,
) -> ContainerRequest<I> {
let mut container_req = self.into();
container_req.env_vars.insert(name.into(), value.into());
container_req
}
fn with_host(self, key: impl Into<String>, value: impl Into<Host>) -> ContainerRequest<I> {
let mut container_req = self.into();
container_req.hosts.insert(key.into(), value.into());
container_req
}
fn with_mount(self, mount: impl Into<Mount>) -> ContainerRequest<I> {
let mut container_req = self.into();
container_req.mounts.push(mount.into());
container_req
}
fn with_mapped_port(
self,
host_port: u16,
container_port: ContainerPort,
) -> ContainerRequest<I> {
let container_req = self.into();
let mut ports = container_req.ports.unwrap_or_default();
ports.push(PortMapping::new(host_port, container_port));
ContainerRequest {
ports: Some(ports),
..container_req
}
}
fn with_ulimit(self, name: &str, soft: i64, hard: Option<i64>) -> ContainerRequest<I> {
let container_req = self.into();
let mut ulimits = container_req.ulimits.unwrap_or_default();
ulimits.push(ResourcesUlimits {
name: Some(name.into()),
soft: Some(soft),
hard,
});
ContainerRequest {
ulimits: Some(ulimits),
..container_req
}
}
fn with_privileged(self, privileged: bool) -> ContainerRequest<I> {
let container_req = self.into();
ContainerRequest {
privileged,
..container_req
}
}
fn with_cgroupns_mode(self, cgroupns_mode: CgroupnsMode) -> ContainerRequest<I> {
let container_req = self.into();
ContainerRequest {
cgroupns_mode: Some(cgroupns_mode),
..container_req
}
}
fn with_userns_mode(self, userns_mode: &str) -> ContainerRequest<I> {
let container_req = self.into();
ContainerRequest {
userns_mode: Some(String::from(userns_mode)),
..container_req
}
}
fn with_shm_size(self, bytes: u64) -> ContainerRequest<I> {
let container_req = self.into();
ContainerRequest {
shm_size: Some(bytes),
..container_req
}
}
fn with_startup_timeout(self, timeout: Duration) -> ContainerRequest<I> {
let container_req = self.into();
ContainerRequest {
startup_timeout: Some(timeout),
..container_req
}
}
fn with_working_dir(self, working_dir: impl Into<String>) -> ContainerRequest<I> {
let container_req = self.into();
ContainerRequest {
working_dir: Some(working_dir.into()),
..container_req
}
}
fn with_log_consumer(self, log_consumer: impl LogConsumer + 'static) -> ContainerRequest<I> {
let mut container_req = self.into();
container_req.log_consumers.push(Box::new(log_consumer));
container_req
}
}