swiftide_docker_executor/
docker_tool_executor.rs

1use std::{collections::HashMap, 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    pub(crate) context_path: PathBuf,
10    pub(crate) image_name: String,
11    pub(crate) dockerfile: Option<PathBuf>,
12    pub(crate) container_uuid: Uuid,
13    pub(crate) user: Option<String>,
14    pub(crate) env_clear: bool,
15    pub(crate) remove_env: Vec<String>,
16    pub(crate) env: HashMap<String, String>,
17    pub(crate) retain_on_drop: bool,
18}
19
20impl Default for DockerExecutor {
21    fn default() -> Self {
22        Self {
23            container_uuid: Uuid::new_v4(),
24            context_path: ".".into(),
25            image_name: "docker-executor".into(),
26            dockerfile: Some("Dockerfile".into()),
27            user: None,
28            env: HashMap::new(),
29            env_clear: false,
30            remove_env: vec![],
31            retain_on_drop: false,
32        }
33    }
34}
35
36impl DockerExecutor {
37    /// Set the path to build the context from (default ".")
38    pub fn with_context_path(&mut self, path: impl Into<PathBuf>) -> &mut Self {
39        self.context_path = path.into();
40
41        self
42    }
43
44    /// Instead of killing the container on drop, retain it for inspection. Default is false.
45    pub fn retain_on_drop(&mut self, retain: bool) -> &mut Self {
46        self.retain_on_drop = retain;
47
48        self
49    }
50
51    /// Clear the environment variables before starting the service in the container
52    pub fn clear_env(&mut self) -> &mut Self {
53        self.env_clear = true;
54
55        self
56    }
57
58    /// Remove an environment variable from the service in the container
59    pub fn remove_env(&mut self, env: impl Into<String>) -> &mut Self {
60        self.remove_env.push(env.into());
61
62        self
63    }
64
65    /// Set an environment variable for the service in the container
66    pub fn with_env(&mut self, key: impl Into<String>, value: impl Into<String>) -> &mut Self {
67        self.env.insert(key.into(), value.into());
68
69        self
70    }
71
72    /// Set multiple environment variables for the service in the container
73    pub fn with_envs(&mut self, envs: impl Into<HashMap<String, String>>) -> &mut Self {
74        self.env.extend(envs.into());
75
76        self
77    }
78
79    /// Set the user (or user_id:group_id) to run the container as (default None, which means root)
80    pub fn with_user(&mut self, user: impl Into<String>) -> &mut Self {
81        self.user = Some(user.into());
82
83        self
84    }
85
86    /// Start with an existing image (full tag). Will skip building the image, unless you set a new
87    /// Dockerfile. Note that this requires that the image has the service available as a binary.
88    pub fn with_existing_image(&mut self, path: impl Into<String>) -> &mut Self {
89        self.image_name = path.into();
90
91        // If an existing image is used, we don't need to build it
92        self.dockerfile = None;
93
94        self
95    }
96
97    /// Set the name of the image to build (default "docker-executor")
98    pub fn with_image_name(&mut self, name: impl Into<String>) -> &mut Self {
99        self.image_name = name.into();
100
101        self
102    }
103
104    /// Overwrite the uuid that is added as suffix to the running container
105    pub fn with_container_uuid(&mut self, uuid: impl Into<Uuid>) -> &mut Self {
106        self.container_uuid = uuid.into();
107
108        self
109    }
110
111    /// Overwrite the dockerfile to use (default "Dockerfile")
112    pub fn with_dockerfile(&mut self, path: impl Into<PathBuf>) -> &mut Self {
113        self.dockerfile = Some(path.into());
114        self
115    }
116
117    /// Starts the docker executor
118    ///
119    /// Note that on dropping the `RunningDockerExecutor`, the container will be stopped
120    pub async fn start(self) -> Result<RunningDockerExecutor, DockerExecutorError> {
121        RunningDockerExecutor::start(&self).await
122    }
123}