diff --git a/Cargo.lock b/Cargo.lock index 33339ba1..14bce4eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2638,6 +2638,7 @@ dependencies = [ "async-dropper-simple", "async-trait", "bollard", + "env_logger", "futures-util", "log", "tokio", diff --git a/crates/jstzd/Cargo.toml b/crates/jstzd/Cargo.toml index 1524ef08..03766e93 100644 --- a/crates/jstzd/Cargo.toml +++ b/crates/jstzd/Cargo.toml @@ -10,6 +10,7 @@ anyhow.workspace = true async-dropper-simple.workspace = true async-trait.workspace = true bollard.workspace = true +env_logger.workspace = true futures-util.workspace = true log.workspace = true tokio.workspace = true \ No newline at end of file diff --git a/crates/jstzd/src/docker/container.rs b/crates/jstzd/src/docker/container.rs index d0bcfb10..cf79022f 100644 --- a/crates/jstzd/src/docker/container.rs +++ b/crates/jstzd/src/docker/container.rs @@ -8,7 +8,7 @@ use std::sync::Arc; use crate::docker::Network; struct ContainerInner { - name: String, + id: String, client: Arc, // https://linear.app/tezos/issue/JSTZ-111/support-for-adding-network-to-a-container // Implement network: Option, @@ -18,9 +18,9 @@ struct ContainerInner { pub struct Container(AsyncDropper); impl ContainerInner { /// Creates a new container with running `id` - pub fn new(client: Arc, name: &str, _network: Option) -> Self { + pub fn new(client: Arc, id: String, _network: Option) -> Self { Self { - name: name.to_string(), + id, client, // network, _private: (), @@ -30,14 +30,14 @@ impl ContainerInner { /// Starts the container's entrypoint async fn start(&self) -> Result<()> { self.client - .start_container(&self.name, None::>) + .start_container(&self.id, None::>) .await?; Ok(()) } /// Stop the container async fn stop(&self) -> Result<()> { - self.client.stop_container(&self.name, None).await?; + self.client.stop_container(&self.id, None).await?; Ok(()) } } @@ -48,9 +48,9 @@ impl AsyncDrop for ContainerInner { self.stop() .await .unwrap_or_else(|e| error!("Error stopping container: {}", e)); - match self.client.remove_container(&self.name, None).await { + match self.client.remove_container(&self.id, None).await { Ok(_) => {} - Err(e) => error!("Error removing container {}: {}", self.name, e), + Err(e) => error!("Error removing container {}: {}", self.id, e), } // TODO: https://linear.app/tezos/issue/JSTZ-111/support-for-adding-network-to-a-container // Handle network drop @@ -58,14 +58,12 @@ impl AsyncDrop for ContainerInner { } impl Container { - pub fn new(client: Arc, name: &str, network: Option) -> Self { - Self(AsyncDropper::new(ContainerInner::new( - client, name, network, - ))) + pub fn new(client: Arc, id: String, network: Option) -> Self { + Self(AsyncDropper::new(ContainerInner::new(client, id, network))) } - pub fn name(&self) -> &str { - &self.0.inner().name + pub fn id(&self) -> &str { + &self.0.inner().id } pub async fn start(&self) -> Result<()> { diff --git a/crates/jstzd/src/docker/image.rs b/crates/jstzd/src/docker/image.rs index adfb4e83..e1ea6203 100644 --- a/crates/jstzd/src/docker/image.rs +++ b/crates/jstzd/src/docker/image.rs @@ -13,6 +13,9 @@ pub trait Image: Sized { fn image_tag(&self) -> &str { Self::LATEST_TAG } + fn image_uri(&self) -> String { + format!("{}:{}", self.image_name(), self.image_tag()) + } // Overrides the entrypoint of the image fn entrypoint(&self) -> Option<&str> { None @@ -42,10 +45,6 @@ impl Image for GenericImage { &self.image_name } - fn entrypoint(&self) -> Option<&str> { - self.entrypoint.as_deref() - } - fn image_tag(&self) -> &str { if let Some(tag) = &self.image_tag { return tag; @@ -53,6 +52,10 @@ impl Image for GenericImage { Self::LATEST_TAG } + fn entrypoint(&self) -> Option<&str> { + self.entrypoint.as_deref() + } + fn exposed_ports(&self) -> &[u16] { if let Some(ports) = &self.exposed_ports { return ports; diff --git a/crates/jstzd/src/docker/network.rs b/crates/jstzd/src/docker/network.rs index 7a48becc..d10cc0c7 100644 --- a/crates/jstzd/src/docker/network.rs +++ b/crates/jstzd/src/docker/network.rs @@ -7,7 +7,7 @@ use std::sync::Arc; struct NetworkInner { name: String, - client: Option>, + client: Arc, } pub struct Network(AsyncDropper); @@ -20,21 +20,16 @@ impl NetworkInner { ..CreateNetworkOptions::default() }; client.create_network(config).await?; - Ok(Self { - client: Some(client), - name, - }) + Ok(Self { client, name }) } } #[async_trait] impl AsyncDrop for NetworkInner { async fn async_drop(&mut self) { - if let Some(client) = self.client.take() { - match client.remove_network(&self.name).await { - Ok(_) => {} - Err(e) => error!("Error removing network {}: {}", self.name, e), - } + match self.client.remove_network(&self.name).await { + Ok(_) => {} + Err(e) => error!("Error removing network {}: {}", self.name, e), } } } diff --git a/crates/jstzd/src/docker/runnable_image.rs b/crates/jstzd/src/docker/runnable_image.rs index 36f30c8d..6d619455 100644 --- a/crates/jstzd/src/docker/runnable_image.rs +++ b/crates/jstzd/src/docker/runnable_image.rs @@ -118,7 +118,7 @@ impl RunnableImage { pub async fn create_container(&self, client: Arc) -> Result { self.all_container_ports_are_exposed()?; let config = Config:: { - image: Some(self.image.image_name().to_string()), + image: Some(self.image.image_uri().to_string()), host_config: Some(self.host_config()), entrypoint: self.entrypoint(), cmd: self.overridden_cmd.clone(), @@ -207,8 +207,7 @@ impl RunnableImage { where T: Image + ?Sized, { - let reference = format!("{}:{}", image.image_name(), image.image_tag()); - let filters = [("reference".to_string(), vec![reference])] + let filters = [("reference".to_string(), vec![image.image_uri()])] .into_iter() .collect::>(); let images = &client diff --git a/crates/jstzd/src/main.rs b/crates/jstzd/src/main.rs index 60383062..1728e495 100644 --- a/crates/jstzd/src/main.rs +++ b/crates/jstzd/src/main.rs @@ -1,11 +1,12 @@ use bollard::Docker; +use env_logger::Env; use jstzd::docker::{Container, GenericImage, Image, RunnableImage}; use std::sync::Arc; pub async fn example() -> anyhow::Result<()> { let docker = Docker::connect_with_socket_defaults().unwrap(); let docker = Arc::new(docker); - let image = GenericImage::new("busybox").with_tag("stable"); + let image = GenericImage::new("busybox").with_tag("latest"); image.pull_image(docker.clone()).await?; let cmd = vec![ "sh", @@ -18,7 +19,7 @@ pub async fn example() -> anyhow::Result<()> { let runnable_image = RunnableImage::new(image, "busybox_test").with_overridden_cmd(cmd); let id = runnable_image.create_container(docker.clone()).await?; - let container = Container::new(docker.clone(), &id, None); + let container = Container::new(docker.clone(), id, None); container.start().await?; Ok(()) @@ -26,8 +27,9 @@ pub async fn example() -> anyhow::Result<()> { #[tokio::main] async fn main() { + env_logger::init_from_env(Env::default().default_filter_or("info")); match example().await { - Ok(_) => println!("Success"), - Err(e) => eprintln!("Error: {}", e), + Ok(_) => log::info!("Success"), + Err(e) => log::error!("Error: {}", e), } }