Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jstzd): docker container #559

Open
wants to merge 6 commits into
base: leo/docker-pull-image
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ actix-web = "4.5.1"
actix-web-lab = "0.20.0"
ansi_term = "0.12.1"
anyhow = "1.0.82"
async-scoped = "0.9.0"
base64 = "0.21.7"
bincode = "1.3.3"
boa_engine = { version = "0.17.0", features = ["fuzz"] }
Expand Down Expand Up @@ -79,6 +80,7 @@ regex = "1"
reqwest = { version = "0.11.24", features = ["json"] }
reqwest-eventsource = "0.5.0"
rustyline = "14.0.0"
serial_test = "3.1.1"
serde = { version = "1.0.196", features = ["derive"] }
serde-wasm-bindgen = "0.6.5"
serde_json = "1.0.107"
Expand All @@ -103,6 +105,7 @@ wasm-bindgen = "0.2.92"
async-dropper = { version = "0.3.1", features = ["tokio", "simple"] }
async-trait = "0.1.82"


[workspace.dependencies.tezos-smart-rollup]
version = "0.2.2"
default-features = false
Expand Down
5 changes: 5 additions & 0 deletions crates/jstzd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ repository.workspace = true
anyhow.workspace = true
async-dropper.workspace = true
async-trait.workspace = true
async-scoped.workspace = true
bollard.workspace = true
bytes.workspace = true
futures-util.workspace = true
tokio.workspace = true
log.workspace = true
Expand All @@ -18,3 +20,6 @@ log.workspace = true
name = "jstzd"
path = "src/main.rs"


[dev-dependencies]
serial_test.workspace = true
86 changes: 86 additions & 0 deletions crates/jstzd/src/docker/container.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use anyhow::Result;
use async_dropper::AsyncDrop;
use async_scoped::TokioScope;
use async_trait::async_trait;
use bollard::Docker;
use log::error;
use std::sync::Arc;

#[derive(Default)]
pub struct Container {
pub id: String,
client: Option<Arc<Docker>>,
dropped: bool,
_private: (),
}

impl Container {
/// Creates a new container with running `id`
pub(super) fn new(client: Arc<Docker>, id: String) -> Self {
Self {
id,
client: Some(client),
dropped: false,
_private: (),
}
}

// Starts the container's entrypoint
pub async fn start(&self) -> Result<()> {
self.client()?
.start_container::<String>(&self.id, None)
.await?;
Ok(())
}

// Stop the container
pub async fn stop(&self) -> Result<()> {
self.client()?.stop_container(&self.id, None).await?;
Ok(())
}

// Remove the container
pub async fn remove(&self) -> Result<()> {
match self.client()?.remove_container(&self.id, None).await {
Ok(_) => Ok(()),
Err(e) => {
if e.to_string().contains("404") {
return Err(anyhow::anyhow!(
"Failed to remove non existent container: {}",
self.id
));
}
Err(e.into())
}
}
}

fn client(&self) -> Result<&Arc<Docker>> {
self.client
.as_ref()
.ok_or_else(|| anyhow::anyhow!("Client does not exist"))
}
}

#[async_trait]
impl AsyncDrop for Container {
async fn async_drop(&mut self) {
self.stop().await.unwrap_or_else(|e| error!("{}", e));
self.remove().await.unwrap_or_else(|e| error!("{}", e));
}
}

impl Drop for Container {
fn drop(&mut self) {
if !self.dropped {
self.dropped = true;
let mut this = std::mem::take(self);
self.dropped = true;
TokioScope::scope_and_block(|s| {
s.spawn(async move {
this.async_drop().await;
})
});
}
}
}
1 change: 1 addition & 0 deletions crates/jstzd/src/docker/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub trait Image: Sized {
}
}

#[derive(Clone)]
pub struct GenericImage {
image_name: String,
image_tag: Option<String>,
Expand Down
2 changes: 2 additions & 0 deletions crates/jstzd/src/docker/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
pub use bollard::Docker;

mod container;
mod image;
mod runnable_image;

pub use container::Container;
pub use image::{GenericImage, Image};
pub use runnable_image::RunnableImage;
Loading
Loading