1use std::collections::HashMap;
4use std::path::Path;
5use std::pin::Pin;
6use std::time::{Duration, Instant, SystemTime};
7
8use bollard::Docker;
9use bollard::container::LogOutput;
10use bollard::models::{
11 ContainerCreateBody, ContainerSummaryStateEnum, HealthConfig, HostConfig,
12 PortBinding as BollardPortBinding,
13};
14use bollard::query_parameters::{
15 BuildImageOptionsBuilder, CreateContainerOptionsBuilder, CreateImageOptionsBuilder,
16 ListContainersOptionsBuilder, LogsOptionsBuilder, RemoveContainerOptionsBuilder,
17 StartContainerOptions, StopContainerOptionsBuilder,
18};
19use bytes::Bytes;
20use futures::stream::{Stream, StreamExt};
21
22use crate::error::{Result, RuntimeError};
23use crate::runtime::{
24 ContainerId, ContainerRuntime, ContainerStatus, LogChunk, LogChunkStream, LogStream,
25};
26use crate::spec::{
27 ContainerSpec, HealthcheckSpec, ImageSource, PortBinding, VolumeBinding, VolumeSource,
28};
29
30const POLL_INTERVAL: Duration = Duration::from_millis(500);
31
32pub struct DockerRuntime {
37 client: Docker,
38}
39
40impl DockerRuntime {
41 pub fn connect() -> Result<Self> {
43 let client = Docker::connect_with_local_defaults().map_err(RuntimeError::Connect)?;
44 Ok(Self { client })
45 }
46
47 #[must_use]
50 pub fn from_client(client: Docker) -> Self {
51 Self { client }
52 }
53
54 async fn ensure_image(&self, image: &str) -> Result<()> {
55 let (from_image, tag) = split_image_ref(image);
56 let options = CreateImageOptionsBuilder::default()
57 .from_image(from_image)
58 .tag(tag)
59 .build();
60 let mut stream = self.client.create_image(Some(options), None, None);
61 while let Some(event) = stream.next().await {
62 event.map_err(|e| RuntimeError::ImagePull {
63 image: image.to_owned(),
64 source: e,
65 })?;
66 }
67 Ok(())
68 }
69
70 pub async fn list_managed(&self, project: &str) -> Result<Vec<ManagedContainer>> {
74 let label_filter = format!("{LABEL_PROJECT}={project}");
75 let mut filters: HashMap<String, Vec<String>> = HashMap::new();
76 filters.insert("label".to_owned(), vec![label_filter]);
77 let options = ListContainersOptionsBuilder::default()
78 .all(true)
79 .filters(&filters)
80 .build();
81 let summaries = self
82 .client
83 .list_containers(Some(options))
84 .await
85 .map_err(|source| RuntimeError::Inspect {
86 id: format!("project={project}"),
87 source,
88 })?;
89
90 let mut out = Vec::with_capacity(summaries.len());
91 for summary in summaries {
92 let Some(id) = summary.id else { continue };
93 let resource = summary
94 .labels
95 .as_ref()
96 .and_then(|labels| labels.get(LABEL_RESOURCE))
97 .cloned()
98 .unwrap_or_else(|| "<unknown>".to_owned());
99 let status = parse_summary_state(summary.state.as_ref());
100 out.push(ManagedContainer {
101 id: ContainerId::new(id),
102 resource,
103 status,
104 });
105 }
106 out.sort_by(|a, b| a.resource.cmp(&b.resource));
107 Ok(out)
108 }
109
110 async fn build_image(
111 &self,
112 context: &str,
113 dockerfile: &str,
114 build_args: &HashMap<String, String>,
115 target: Option<&str>,
116 tag: &str,
117 ) -> Result<()> {
118 let context_owned = context.to_owned();
119 let tar_bytes =
120 tokio::task::spawn_blocking(move || build_tar_archive(Path::new(&context_owned)))
121 .await
122 .map_err(|join_err| {
123 RuntimeError::InvalidSpec(format!("tar build task panicked: {join_err}"))
124 })?
125 .map_err(|io_err| {
126 RuntimeError::InvalidSpec(format!("failed to build tar archive: {io_err}"))
127 })?;
128
129 let options = BuildImageOptionsBuilder::default()
130 .dockerfile(dockerfile)
131 .t(tag)
132 .rm(true)
133 .buildargs(build_args)
134 .target(target.unwrap_or(""))
135 .build();
136
137 let mut stream = self.client.build_image(
138 options,
139 None,
140 Some(bollard::body_full(Bytes::from(tar_bytes))),
141 );
142 while let Some(event) = stream.next().await {
143 event.map_err(RuntimeError::Build)?;
144 }
145 Ok(())
146 }
147}
148
149fn build_tar_archive(context: &Path) -> std::io::Result<Vec<u8>> {
152 use ignore::WalkBuilder;
153
154 let mut buf: Vec<u8> = Vec::new();
155 {
156 let mut builder = tar::Builder::new(&mut buf);
157 builder.follow_symlinks(false);
158
159 let walker = WalkBuilder::new(context)
160 .add_custom_ignore_filename(".dockerignore")
161 .git_ignore(false)
162 .git_exclude(false)
163 .git_global(false)
164 .hidden(false)
165 .build();
166
167 for entry in walker {
168 let entry = entry.map_err(|e| std::io::Error::other(format!("walk error: {e}")))?;
169 let path = entry.path();
170 let relative = match path.strip_prefix(context) {
171 Ok(p) if !p.as_os_str().is_empty() => p,
172 _ => continue,
173 };
174 let Some(file_type) = entry.file_type() else {
175 continue;
176 };
177 if file_type.is_dir() {
178 builder.append_dir(relative, path)?;
179 } else if file_type.is_file() {
180 let mut file = std::fs::File::open(path)?;
181 builder.append_file(relative, &mut file)?;
182 }
183 }
184 builder.finish()?;
185 }
186 Ok(buf)
187}
188
189impl ContainerRuntime for DockerRuntime {
190 async fn start(&self, spec: &ContainerSpec) -> Result<ContainerId> {
191 let image_ref = match &spec.image {
192 ImageSource::Pull(image) => {
193 self.ensure_image(image).await?;
194 image.clone()
195 }
196 ImageSource::Build {
197 context,
198 dockerfile,
199 build_args,
200 target,
201 tag,
202 } => {
203 self.build_image(context, dockerfile, build_args, target.as_deref(), tag)
204 .await?;
205 tag.clone()
206 }
207 };
208
209 let host_config = build_host_config(&spec.ports, &spec.volumes);
210 let exposed_ports = build_exposed_ports(&spec.ports);
211 let env = build_env(&spec.env);
212 let healthcheck = spec.healthcheck.as_ref().map(build_healthcheck);
213 let labels = build_labels(&spec.project, &spec.resource);
214
215 let config = ContainerCreateBody {
216 image: Some(image_ref),
217 env: Some(env),
218 cmd: spec.command.clone(),
219 host_config: Some(host_config),
220 exposed_ports: Some(exposed_ports),
221 healthcheck,
222 labels: Some(labels),
223 ..Default::default()
224 };
225
226 let create_options = CreateContainerOptionsBuilder::default()
227 .name(&spec.name)
228 .build();
229
230 let created = self
231 .client
232 .create_container(Some(create_options), config)
233 .await
234 .map_err(RuntimeError::Start)?;
235
236 self.client
237 .start_container(&created.id, None::<StartContainerOptions>)
238 .await
239 .map_err(RuntimeError::Start)?;
240
241 Ok(ContainerId::new(created.id))
242 }
243
244 async fn stop(&self, id: &ContainerId, grace: Duration) -> Result<()> {
245 #[allow(clippy::cast_possible_wrap, clippy::cast_possible_truncation)]
246 let options = StopContainerOptionsBuilder::default()
247 .t(grace.as_secs() as i32)
248 .build();
249 match self.client.stop_container(id.as_str(), Some(options)).await {
250 Ok(())
251 | Err(bollard::errors::Error::DockerResponseServerError {
252 status_code: 304 | 404,
253 ..
254 }) => Ok(()),
255 Err(e) => Err(RuntimeError::Stop {
256 id: id.to_string(),
257 source: e,
258 }),
259 }
260 }
261
262 async fn remove(&self, name: &str) -> Result<()> {
263 let options = RemoveContainerOptionsBuilder::default().force(true).build();
264 match self.client.remove_container(name, Some(options)).await {
265 Ok(())
266 | Err(bollard::errors::Error::DockerResponseServerError {
267 status_code: 404, ..
268 }) => Ok(()),
269 Err(e) => Err(RuntimeError::Remove {
270 name: name.to_owned(),
271 source: e,
272 }),
273 }
274 }
275
276 async fn inspect(&self, id: &ContainerId) -> Result<ContainerStatus> {
277 let info = self
278 .client
279 .inspect_container(id.as_str(), None)
280 .await
281 .map_err(|e| match e {
282 bollard::errors::Error::DockerResponseServerError {
283 status_code: 404, ..
284 } => RuntimeError::NotFound(id.to_string()),
285 other => RuntimeError::Inspect {
286 id: id.to_string(),
287 source: other,
288 },
289 })?;
290
291 let state = info.state.as_ref();
292 let Some(state) = state else {
293 return Ok(ContainerStatus::Starting);
294 };
295
296 if matches!(state.running, Some(true)) {
297 if let Some(health) = &state.health {
298 return Ok(match health.status {
299 Some(bollard::models::HealthStatusEnum::HEALTHY) => ContainerStatus::Healthy,
300 Some(bollard::models::HealthStatusEnum::UNHEALTHY) => {
301 ContainerStatus::Unhealthy
302 }
303 _ => ContainerStatus::Running,
304 });
305 }
306 return Ok(ContainerStatus::Running);
307 }
308
309 if matches!(state.dead, Some(true))
310 || state.status == Some(bollard::models::ContainerStateStatusEnum::EXITED)
311 {
312 #[allow(clippy::cast_possible_truncation)]
313 let exit_code = state.exit_code.map(|c| c as i32);
314 return Ok(ContainerStatus::Stopped { exit_code });
315 }
316
317 Ok(ContainerStatus::Starting)
318 }
319
320 async fn wait_healthy(&self, id: &ContainerId, timeout: Duration) -> Result<()> {
321 let deadline = Instant::now() + timeout;
322 loop {
323 match self.inspect(id).await? {
324 ContainerStatus::Healthy | ContainerStatus::Running => return Ok(()),
325 ContainerStatus::Unhealthy => {
326 if Instant::now() >= deadline {
327 return Err(RuntimeError::Timeout {
328 operation: "wait_healthy",
329 after: timeout,
330 });
331 }
332 }
333 ContainerStatus::Starting => {}
334 ContainerStatus::Stopped { exit_code } => {
335 return Err(RuntimeError::InvalidSpec(format!(
336 "container `{id}` exited with code {exit_code:?} before becoming healthy"
337 )));
338 }
339 }
340 if Instant::now() >= deadline {
341 return Err(RuntimeError::Timeout {
342 operation: "wait_healthy",
343 after: timeout,
344 });
345 }
346 tokio::time::sleep(POLL_INTERVAL).await;
347 }
348 }
349
350 async fn logs(&self, id: &ContainerId, follow: bool) -> Result<LogChunkStream> {
351 let options = LogsOptionsBuilder::default()
352 .follow(follow)
353 .stdout(true)
354 .stderr(true)
355 .timestamps(true)
356 .build();
357 let stream = self.client.logs(id.as_str(), Some(options));
358 let mapped: Pin<Box<dyn Stream<Item = Result<LogChunk>> + Send>> =
359 Box::pin(stream.map(map_log_item));
360 Ok(mapped)
361 }
362}
363
364fn split_image_ref(image: &str) -> (&str, &str) {
365 image.split_once(':').unwrap_or((image, "latest"))
366}
367
368fn build_env(env: &HashMap<String, String>) -> Vec<String> {
369 env.iter().map(|(k, v)| format!("{k}={v}")).collect()
370}
371
372fn build_labels(project: &str, resource: &str) -> HashMap<String, String> {
373 let mut labels = HashMap::with_capacity(2);
374 labels.insert(LABEL_PROJECT.to_owned(), project.to_owned());
375 labels.insert(LABEL_RESOURCE.to_owned(), resource.to_owned());
376 labels
377}
378
379pub const LABEL_PROJECT: &str = "lightshuttle.project";
382
383pub const LABEL_RESOURCE: &str = "lightshuttle.resource";
386
387#[derive(Debug, Clone)]
389pub struct ManagedContainer {
390 pub id: ContainerId,
392 pub resource: String,
394 pub status: ContainerStatus,
396}
397
398fn parse_summary_state(state: Option<&ContainerSummaryStateEnum>) -> ContainerStatus {
399 match state {
400 Some(ContainerSummaryStateEnum::RUNNING) => ContainerStatus::Running,
401 Some(ContainerSummaryStateEnum::EXITED | ContainerSummaryStateEnum::DEAD) => {
402 ContainerStatus::Stopped { exit_code: None }
403 }
404 _ => ContainerStatus::Starting,
405 }
406}
407
408fn build_exposed_ports(ports: &[PortBinding]) -> Vec<String> {
409 ports
410 .iter()
411 .map(|p| format!("{}/tcp", p.container_port))
412 .collect()
413}
414
415const DEFAULT_HOST_BIND_ADDRESS: &str = "127.0.0.1";
422
423fn build_host_config(ports: &[PortBinding], volumes: &[VolumeBinding]) -> HostConfig {
424 let port_bindings = ports
425 .iter()
426 .map(|p| {
427 let host_ip = p
428 .host_address
429 .clone()
430 .unwrap_or_else(|| DEFAULT_HOST_BIND_ADDRESS.to_owned());
431 let bindings = vec![BollardPortBinding {
432 host_ip: Some(host_ip),
433 host_port: Some(p.host_port.to_string()),
434 }];
435 (format!("{}/tcp", p.container_port), Some(bindings))
436 })
437 .collect::<HashMap<_, _>>();
438
439 let binds: Vec<String> = volumes
440 .iter()
441 .filter_map(|v| match &v.source {
442 VolumeSource::HostPath(path) => Some(format!("{path}:{}", v.target)),
443 VolumeSource::Named(name) => Some(format!("{name}:{}", v.target)),
444 VolumeSource::Anonymous => None,
445 })
446 .collect();
447
448 HostConfig {
449 port_bindings: Some(port_bindings),
450 binds: if binds.is_empty() { None } else { Some(binds) },
451 ..Default::default()
452 }
453}
454
455fn build_healthcheck(hc: &HealthcheckSpec) -> HealthConfig {
456 #[allow(clippy::cast_possible_wrap, clippy::cast_possible_truncation)]
457 HealthConfig {
458 test: Some(hc.test.clone()),
459 interval: Some(hc.interval.as_nanos() as i64),
460 timeout: Some(hc.timeout.as_nanos() as i64),
461 retries: Some(i64::from(hc.retries)),
462 start_period: Some(hc.start_period.as_nanos() as i64),
463 ..Default::default()
464 }
465}
466
467fn map_log_item(item: std::result::Result<LogOutput, bollard::errors::Error>) -> Result<LogChunk> {
468 match item {
469 Ok(LogOutput::StdErr { message }) => Ok(log_chunk(LogStream::Stderr, &message)),
470 Ok(
471 LogOutput::StdOut { message }
472 | LogOutput::Console { message }
473 | LogOutput::StdIn { message },
474 ) => Ok(log_chunk(LogStream::Stdout, &message)),
475 Err(e) => Err(RuntimeError::LogStream(e)),
476 }
477}
478
479fn log_chunk(stream: LogStream, message: &[u8]) -> LogChunk {
482 let (timestamp, bytes) = split_docker_timestamp(message);
483 LogChunk {
484 stream,
485 timestamp,
486 bytes,
487 }
488}
489
490fn split_docker_timestamp(message: &[u8]) -> (SystemTime, Vec<u8>) {
498 if let Some(space) = message.iter().position(|&b| b == b' ')
499 && let Ok(prefix) = std::str::from_utf8(&message[..space])
500 && let Ok(ts) = prefix.parse::<jiff::Timestamp>()
501 && let Some(system_time) = timestamp_to_system_time(ts)
502 {
503 let payload = message.get(space + 1..).unwrap_or(&[]).to_vec();
504 return (system_time, payload);
505 }
506 (SystemTime::now(), message.to_vec())
507}
508
509fn timestamp_to_system_time(ts: jiff::Timestamp) -> Option<SystemTime> {
512 let nanos = ts.as_nanosecond();
513 if nanos < 0 {
514 return None;
515 }
516 let secs = u64::try_from(nanos / 1_000_000_000).ok()?;
517 let subsec = u32::try_from(nanos % 1_000_000_000).ok()?;
518 Some(SystemTime::UNIX_EPOCH + Duration::new(secs, subsec))
519}
520
521#[cfg(test)]
522mod tests {
523 use super::{PortBinding, build_host_config};
524
525 fn host_ip_for(ports: &[PortBinding], key: &str) -> Option<String> {
526 let config = build_host_config(ports, &[]);
527 config
528 .port_bindings
529 .and_then(|map| map.get(key).cloned())
530 .flatten()
531 .and_then(|bindings| bindings.into_iter().next())
532 .and_then(|binding| binding.host_ip)
533 }
534
535 #[test]
536 fn unspecified_address_binds_to_loopback() {
537 let ports = vec![PortBinding {
538 container_port: 5432,
539 host_address: None,
540 host_port: 5432,
541 }];
542 assert_eq!(
543 host_ip_for(&ports, "5432/tcp").as_deref(),
544 Some("127.0.0.1")
545 );
546 }
547
548 #[test]
549 fn explicit_address_is_preserved() {
550 let ports = vec![PortBinding {
551 container_port: 80,
552 host_address: Some("0.0.0.0".to_owned()),
553 host_port: 8080,
554 }];
555 assert_eq!(host_ip_for(&ports, "80/tcp").as_deref(), Some("0.0.0.0"));
556 }
557
558 #[test]
559 fn timestamped_line_parses_emission_time_and_strips_prefix() {
560 use std::time::SystemTime;
561
562 let (ts, payload) =
563 super::split_docker_timestamp(b"2024-01-01T12:34:56.789012345Z hello world");
564
565 let elapsed = ts
566 .duration_since(SystemTime::UNIX_EPOCH)
567 .expect("post-epoch");
568 assert_eq!(elapsed.as_secs(), 1_704_112_496);
569 assert_eq!(elapsed.subsec_micros(), 789_012);
572 assert_eq!(payload, b"hello world");
573 }
574
575 #[test]
576 fn timestamped_line_without_payload_yields_empty_bytes() {
577 let (_ts, payload) = super::split_docker_timestamp(b"2024-01-01T00:00:00Z \n");
579 assert_eq!(payload, b"\n");
580 }
581
582 #[test]
583 fn untimestamped_line_is_forwarded_verbatim() {
584 let input = b"not-a-timestamp hello world";
587 let (_ts, payload) = super::split_docker_timestamp(input);
588 assert_eq!(payload, input);
589 }
590
591 #[test]
592 fn line_without_space_is_forwarded_verbatim() {
593 let input = b"singletoken";
594 let (_ts, payload) = super::split_docker_timestamp(input);
595 assert_eq!(payload, input);
596 }
597}