swiftide_docker_executor/
docker_tool_executor.rs

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