swiftide_docker_executor/
docker_tool_executor.rs

1use std::path::PathBuf;
2use uuid::Uuid;
3
4use crate::{DockerExecutorError, RunningDockerExecutor};
5
6/// Build a docker image with bollard and start it up
7#[derive(Clone, Debug)]
8pub struct DockerExecutor {
9    context_path: PathBuf,
10    image_name: String,
11    dockerfile: Option<PathBuf>,
12    container_uuid: Uuid,
13}
14
15impl Default for DockerExecutor {
16    fn default() -> Self {
17        Self {
18            container_uuid: Uuid::new_v4(),
19            context_path: ".".into(),
20            image_name: "docker-executor".into(),
21            dockerfile: Some("Dockerfile".into()),
22        }
23    }
24}
25
26impl DockerExecutor {
27    /// Set the path to build the context from (default ".")
28    pub fn with_context_path(&mut self, path: impl Into<PathBuf>) -> &mut Self {
29        self.context_path = path.into();
30
31        self
32    }
33
34    /// Start with an existing image (full tag). Will skip building the image, unless you set a new
35    /// Dockerfile. Note that this requires that the image has the service available as a binary.
36    pub fn with_existing_image(&mut self, path: impl Into<String>) -> &mut Self {
37        self.image_name = path.into();
38
39        // If an existing image is used, we don't need to build it
40        self.dockerfile = None;
41
42        self
43    }
44
45    /// Set the name of the image to build (default "docker-executor")
46    pub fn with_image_name(&mut self, name: impl Into<String>) -> &mut Self {
47        self.image_name = name.into();
48
49        self
50    }
51
52    /// Overwrite the uuid that is added as suffix to the running container
53    pub fn with_container_uuid(&mut self, uuid: impl Into<Uuid>) -> &mut Self {
54        self.container_uuid = uuid.into();
55
56        self
57    }
58
59    /// Overwrite the dockerfile to use (default "Dockerfile")
60    pub fn with_dockerfile(&mut self, path: impl Into<PathBuf>) -> &mut Self {
61        self.dockerfile = Some(path.into());
62        self
63    }
64
65    /// Starts the docker executor
66    ///
67    /// Note that on dropping the `RunningDockerExecutor`, the container will be stopped
68    pub async fn start(self) -> Result<RunningDockerExecutor, DockerExecutorError> {
69        RunningDockerExecutor::start(
70            self.container_uuid,
71            &self.context_path,
72            self.dockerfile.as_deref(),
73            &self.image_name,
74        )
75        .await
76    }
77}