1use std::collections::{BTreeMap, BTreeSet, HashMap};
6use std::fmt;
7use std::time::{Duration, Instant};
8
9use bollard::exec::{CreateExecOptions, StartExecResults};
10use std::pin::Pin;
11
12use bollard::models::{
13 ChangeType, ContainerConfig, ContainerCreateBody, CreateImageInfo, HostConfig, Mount,
14 NetworkCreateRequest, SystemInfo, VolumeCreateRequest,
15};
16use bollard::query_parameters::{
17 AttachContainerOptionsBuilder, CommitContainerOptionsBuilder, CreateContainerOptionsBuilder,
18 CreateImageOptionsBuilder, DataUsageOptionsBuilder, DownloadFromContainerOptionsBuilder,
19 EventsOptionsBuilder, InspectContainerOptionsBuilder, ListContainersOptionsBuilder,
20 ListImagesOptionsBuilder, LogsOptionsBuilder, RemoveContainerOptionsBuilder,
21 RemoveImageOptionsBuilder, RemoveVolumeOptionsBuilder, ResizeContainerTTYOptionsBuilder,
22 UploadToContainerOptionsBuilder, WaitContainerOptionsBuilder,
23};
24use futures_util::stream::BoxStream;
25use futures_util::{StreamExt, TryStreamExt};
26use tracing::info;
27
28use crate::config::ByteSize;
29use crate::isolation::Isolation;
30
31pub use bollard::errors::Error;
32
33#[derive(Debug, Clone)]
35pub struct RunnerSpec {
36 pub name: String,
38 pub image: String,
40 pub profile: String,
42 pub slice: String,
44 pub cpus: u32,
46 pub memory: ByteSize,
48 pub env: Vec<String>,
50 pub mounts: Vec<Mount>,
52 pub network: Option<String>,
54 pub labels: BTreeMap<String, String>,
56 pub entrypoint: Option<Vec<String>>,
58 pub jit_config: Option<JitConfig>,
60}
61
62pub const JIT_CONFIG_PATH: &str = "/run/runnerd/jitconfig";
65
66#[derive(Clone)]
68pub struct JitConfig(pub String);
69
70impl fmt::Debug for JitConfig {
71 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72 f.write_str("JitConfig(<redacted>)")
73 }
74}
75
76#[derive(Debug, Clone, PartialEq, Eq)]
78pub struct RunnerState {
79 pub running: bool,
81 pub exit_code: Option<i64>,
83 pub oom_killed: bool,
85 pub privileged: bool,
87 pub runtime: Option<String>,
89 pub pid: Option<u32>,
91 pub address: Option<String>,
93 pub started_at: Option<u64>,
95 pub finished_at: Option<u64>,
97 pub memory: Option<u64>,
99 pub cpus: Option<u64>,
101}
102
103pub type Output = BoxStream<'static, Result<Vec<u8>, Error>>;
105
106#[derive(Debug, Clone, PartialEq, Eq)]
108pub struct Image {
109 pub reference: String,
111 pub labels: HashMap<String, String>,
113 pub bytes: u64,
115}
116
117#[derive(Debug, Clone, PartialEq, Eq)]
119pub struct TaggedImage {
120 pub id: String,
122 pub tags: Vec<String>,
124 pub digests: Vec<String>,
126}
127
128#[derive(Debug, Clone, PartialEq, Eq)]
130pub struct File {
131 pub name: String,
133 pub contents: String,
135}
136
137#[derive(Debug, Clone, Default, PartialEq, Eq)]
139pub struct Files {
140 pub files: Vec<File>,
142 pub complete: bool,
145}
146
147pub(crate) const FILES_LIMIT: usize = 16 << 20;
150
151pub struct Tty {
153 pub container: String,
155 pub output: BoxStream<'static, Result<Vec<u8>, Error>>,
157 pub input: Pin<Box<dyn tokio::io::AsyncWrite + Send>>,
159}
160
161impl fmt::Debug for Tty {
162 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
163 f.debug_struct("Tty")
164 .field("container", &self.container)
165 .finish_non_exhaustive()
166 }
167}
168
169#[derive(Debug, Clone, Copy, PartialEq, Eq)]
171pub struct TtySize {
172 pub cols: u16,
174 pub rows: u16,
176}
177
178pub fn cpu_shares(cpus: u32) -> i64 {
184 (i64::from(cpus) * 1024).clamp(2, 262_144)
185}
186
187pub fn create_body(spec: &RunnerSpec, isolation: &dyn Isolation) -> ContainerCreateBody {
189 let memory = i64::try_from(spec.memory.0).unwrap_or(i64::MAX);
191 let mut host_config = HostConfig {
192 cgroup_parent: Some(spec.slice.clone()),
193 memory: Some(memory),
194 memory_swap: Some(memory),
196 nano_cpus: Some(i64::from(spec.cpus) * 1_000_000_000),
197 cpu_shares: Some(cpu_shares(spec.cpus)),
198 init: Some(true),
199 mounts: Some(spec.mounts.clone()),
200 network_mode: spec.network.clone(),
201 ..Default::default()
202 };
203 isolation.configure(&mut host_config);
204 let mut labels: HashMap<String, String> = spec.labels.clone().into_iter().collect();
205 labels.insert("runnerd.profile".to_owned(), spec.profile.clone());
206 labels.insert("runnerd.isolation".to_owned(), isolation.name().to_owned());
207 ContainerCreateBody {
208 image: Some(spec.image.clone()),
209 env: Some(spec.env.clone()),
210 entrypoint: spec.entrypoint.clone(),
211 labels: Some(labels),
212 host_config: Some(host_config),
213 ..Default::default()
214 }
215}
216
217#[derive(Debug, Clone, PartialEq, Eq)]
219pub struct Container {
220 pub id: String,
222 pub name: String,
224 pub running: bool,
226 pub labels: HashMap<String, String>,
228}
229
230#[derive(Debug)]
233pub struct StartError {
234 pub container: Option<String>,
237 pub may_have_run: bool,
240 pub error: Error,
242}
243
244impl fmt::Display for StartError {
245 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
246 self.error.fmt(f)
247 }
248}
249
250impl std::error::Error for StartError {
251 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
252 Some(&self.error)
253 }
254}
255
256impl From<StartError> for Error {
258 fn from(failed: StartError) -> Error {
259 failed.error
260 }
261}
262
263fn refused(error: &Error) -> bool {
266 matches!(
267 error,
268 Error::DockerResponseServerError {
269 status_code: 400..=499,
270 ..
271 }
272 )
273}
274
275pub fn is_unreachable(error: &Error) -> bool {
279 matches!(
280 error,
281 Error::HyperLegacyError { .. }
282 | Error::HyperResponseError { .. }
283 | Error::IOError { .. }
284 | Error::SocketNotFoundError(_)
285 )
286}
287
288pub fn is_declined(error: &Error) -> bool {
291 matches!(
292 error,
293 Error::DockerResponseServerError {
294 status_code: 400..=499 | 501,
295 ..
296 }
297 )
298}
299
300pub fn is_not_found(error: &Error) -> bool {
302 matches!(
303 error,
304 Error::DockerResponseServerError {
305 status_code: 404,
306 ..
307 }
308 )
309}
310
311pub trait DockerApi: Send + Sync {
314 fn start(
317 &self,
318 spec: &RunnerSpec,
319 isolation: &dyn Isolation,
320 ) -> impl Future<Output = Result<String, StartError>> + Send;
321 fn inspect(&self, container: &str) -> impl Future<Output = Result<RunnerState, Error>> + Send;
323 fn remove(&self, container: &str) -> impl Future<Output = Result<(), Error>> + Send;
325 fn run(
327 &self,
328 name: &str,
329 body: ContainerCreateBody,
330 ) -> impl Future<Output = Result<String, Error>> + Send;
331 fn list(&self, label: &str) -> impl Future<Output = Result<Vec<Container>, Error>> + Send;
333 fn exits(&self) -> BoxStream<'static, Result<String, Error>>;
337 fn wait(&self, container: &str) -> impl Future<Output = Result<i64, Error>> + Send;
339 fn logs(&self, container: &str) -> impl Future<Output = Result<String, Error>> + Send;
341 fn logs_since(
344 &self,
345 container: &str,
346 since: u64,
347 ) -> impl Future<Output = Result<String, Error>> + Send;
348 fn exec(
351 &self,
352 container: &str,
353 command: Vec<String>,
354 ) -> impl Future<Output = Result<Output, Error>> + Send;
355 fn image_present(&self, image: &str) -> impl Future<Output = Result<bool, Error>> + Send;
357 fn pull(&self, image: &str) -> impl Future<Output = Result<(), Error>> + Send;
359 fn ensure_network(&self, name: &str) -> impl Future<Output = Result<(), Error>> + Send;
361 fn ensure_volume(&self, name: &str) -> impl Future<Output = Result<(), Error>> + Send;
363 fn remove_volume(&self, name: &str) -> impl Future<Output = Result<(), Error>> + Send;
365 fn volume_sizes(&self) -> impl Future<Output = Result<BTreeMap<String, u64>, Error>> + Send;
367 fn changes(&self, container: &str) -> impl Future<Output = Result<Vec<String>, Error>> + Send;
369 fn written(&self, container: &str) -> impl Future<Output = Result<u64, Error>> + Send;
371 fn files(
374 &self,
375 container: &str,
376 directory: &str,
377 prefix: &str,
378 ) -> impl Future<Output = Result<Files, Error>> + Send;
379 fn commit(
382 &self,
383 container: &str,
384 reference: &str,
385 labels: BTreeMap<String, String>,
386 ) -> impl Future<Output = Result<u64, Error>> + Send;
387 fn images(&self, label: &str) -> impl Future<Output = Result<Vec<Image>, Error>> + Send;
389 fn image_id(
391 &self,
392 reference: &str,
393 ) -> impl Future<Output = Result<Option<String>, Error>> + Send;
394 fn tagged_images(&self) -> impl Future<Output = Result<Vec<TaggedImage>, Error>> + Send;
396 fn container_images(&self) -> impl Future<Output = Result<BTreeSet<String>, Error>> + Send;
398 fn remove_image(&self, reference: &str) -> impl Future<Output = Result<(), Error>> + Send;
400 fn free_space(&self) -> impl Future<Output = Result<u64, Error>> + Send;
402 fn shell(
405 &self,
406 name: &str,
407 body: ContainerCreateBody,
408 size: TtySize,
409 ) -> impl Future<Output = Result<Tty, Error>> + Send;
410}
411
412#[derive(Debug)]
414pub struct Docker {
415 api: bollard::Docker,
416}
417
418impl Docker {
419 async fn clean_up(&self, made: Option<String>, error: Error, may_have_run: bool) -> StartError {
422 let container = match made {
423 Some(container) => match self.remove(&container).await {
424 Err(failed) if !is_not_found(&failed) => Some(container),
425 _ => None,
426 },
427 None => None,
428 };
429 StartError {
430 container,
431 may_have_run,
432 error,
433 }
434 }
435
436 pub fn connect() -> Result<Docker, Error> {
438 connect_to(std::env::var("DOCKER_HOST").ok().as_deref())
439 }
440
441 pub async fn info(&self) -> Result<SystemInfo, Error> {
443 self.api.info().await
444 }
445
446 pub async fn image_labels(
448 &self,
449 image: &str,
450 ) -> Result<Option<HashMap<String, String>>, Error> {
451 match self.api.inspect_image(image).await {
452 Ok(inspect) => Ok(Some(
453 inspect
454 .config
455 .and_then(|config| config.labels)
456 .unwrap_or_default(),
457 )),
458 Err(error) if is_not_found(&error) => Ok(None),
459 Err(error) => Err(error),
460 }
461 }
462
463 pub async fn registry_platforms(&self, image: &str) -> Result<Vec<String>, Error> {
466 let inspect = self.api.inspect_registry_image(image, None).await?;
467 Ok(inspect
468 .platforms
469 .into_iter()
470 .filter_map(|platform| Some(format!("{}/{}", platform.os?, platform.architecture?)))
471 .filter(|platform| platform != "unknown/unknown")
473 .collect())
474 }
475}
476
477impl DockerApi for Docker {
478 async fn start(
479 &self,
480 spec: &RunnerSpec,
481 isolation: &dyn Isolation,
482 ) -> Result<String, StartError> {
483 let options = CreateContainerOptionsBuilder::new()
484 .name(&spec.name)
485 .build();
486 let id = match self
487 .api
488 .create_container(Some(options), create_body(spec, isolation))
489 .await
490 {
491 Ok(created) => created.id,
492 Err(error) if refused(&error) => {
493 return Err(StartError {
494 container: None,
495 may_have_run: false,
496 error,
497 });
498 }
499 Err(error) => {
501 let made = match self.api.inspect_container(&spec.name, None).await {
502 Ok(found) => Some(found.id.unwrap_or_else(|| spec.name.clone())),
503 Err(missing) if is_not_found(&missing) => None,
504 Err(_) => Some(spec.name.clone()),
505 };
506 return Err(self.clean_up(made, error, false).await);
508 }
509 };
510 let uploaded = async {
511 if let Some(jit_config) = &spec.jit_config {
512 let options = UploadToContainerOptionsBuilder::new().path("/").build();
513 let tar = jit_config_tar(jit_config)?;
514 let tar = futures_util::stream::once(std::future::ready(tar.into()));
516 self.api
517 .upload_to_container(&id, Some(options), bollard::body_stream(tar))
518 .await?;
519 }
520 Ok::<(), Error>(())
521 };
522 if let Err(error) = uploaded.await {
523 return Err(self.clean_up(Some(id), error, false).await);
524 }
525 if let Err(error) = self.api.start_container(&id, None).await {
527 return Err(self.clean_up(Some(id), error, true).await);
528 }
529 Ok(id)
530 }
531
532 async fn inspect(&self, container: &str) -> Result<RunnerState, Error> {
533 let response = self.api.inspect_container(container, None).await?;
534 let state = response.state.unwrap_or_default();
535 let host_config = response.host_config.unwrap_or_default();
536 let address = response
537 .network_settings
538 .and_then(|settings| settings.networks)
539 .and_then(|networks| {
540 networks
541 .into_values()
542 .find_map(|network| network.ip_address.filter(|address| !address.is_empty()))
543 });
544 Ok(RunnerState {
545 running: state.running.unwrap_or(false),
546 exit_code: state.exit_code,
547 oom_killed: state.oom_killed.unwrap_or(false),
548 privileged: host_config.privileged.unwrap_or(false),
549 runtime: host_config.runtime,
550 pid: state
551 .pid
552 .and_then(|pid| u32::try_from(pid).ok())
553 .filter(|pid| *pid > 0),
554 address,
555 started_at: state.started_at.as_deref().and_then(crate::time::parse_ms),
556 finished_at: state.finished_at.as_deref().and_then(crate::time::parse_ms),
558 memory: host_config
559 .memory
560 .and_then(|memory| u64::try_from(memory).ok())
561 .filter(|memory| *memory > 0),
562 cpus: host_config
563 .nano_cpus
564 .and_then(|nano| u64::try_from(nano / 1_000_000).ok())
565 .filter(|cpus| *cpus > 0),
566 })
567 }
568
569 async fn remove(&self, container: &str) -> Result<(), Error> {
570 let options = RemoveContainerOptionsBuilder::new()
572 .force(true)
573 .v(true)
574 .build();
575 self.api.remove_container(container, Some(options)).await
576 }
577
578 async fn run(&self, name: &str, body: ContainerCreateBody) -> Result<String, Error> {
579 let options = CreateContainerOptionsBuilder::new().name(name).build();
580 let id = self.api.create_container(Some(options), body).await?.id;
581 if let Err(error) = self.api.start_container(&id, None).await {
582 let _ = self.remove(&id).await;
583 return Err(error);
584 }
585 Ok(id)
586 }
587
588 async fn list(&self, label: &str) -> Result<Vec<Container>, Error> {
589 let options = ListContainersOptionsBuilder::new()
590 .all(true)
591 .filters(&HashMap::from([("label", vec![label])]))
592 .build();
593 let containers = self.api.list_containers(Some(options)).await?;
594 Ok(containers
595 .into_iter()
596 .map(|container| Container {
597 id: container.id.unwrap_or_default(),
598 name: container
599 .names
600 .and_then(|names| names.into_iter().next())
601 .unwrap_or_default()
602 .trim_start_matches('/')
603 .to_owned(),
604 running: container.state
605 == Some(bollard::models::ContainerSummaryStateEnum::RUNNING),
606 labels: container.labels.unwrap_or_default(),
607 })
608 .collect())
609 }
610
611 fn exits(&self) -> BoxStream<'static, Result<String, Error>> {
612 let filters = HashMap::from([
613 ("type", vec!["container"]),
614 ("event", vec!["die"]),
615 ("label", vec!["runnerd.job"]),
616 ]);
617 let options = EventsOptionsBuilder::new().filters(&filters).build();
618 self.api
619 .events(Some(options))
620 .try_filter_map(|event| std::future::ready(Ok(event.actor.and_then(|actor| actor.id))))
621 .boxed()
622 }
623
624 async fn wait(&self, container: &str) -> Result<i64, Error> {
625 let options = WaitContainerOptionsBuilder::new()
626 .condition("not-running")
627 .build();
628 let mut responses = self.api.wait_container(container, Some(options));
629 match responses.next().await {
630 Some(Ok(response)) => Ok(response.status_code),
631 Some(Err(Error::DockerContainerWaitError { code, .. })) => Ok(code),
633 Some(Err(error)) => Err(error),
634 None => Ok(0),
635 }
636 }
637
638 async fn logs(&self, container: &str) -> Result<String, Error> {
639 let options = LogsOptionsBuilder::new().stdout(true).stderr(true).build();
640 let chunks: Vec<_> = self
641 .api
642 .logs(container, Some(options))
643 .try_collect()
644 .await?;
645 Ok(chunks.iter().map(ToString::to_string).collect())
646 }
647
648 async fn logs_since(&self, container: &str, since: u64) -> Result<String, Error> {
649 let options = LogsOptionsBuilder::new()
650 .stdout(true)
651 .stderr(true)
652 .timestamps(true)
653 .since(i32::try_from(since).unwrap_or(i32::MAX))
654 .build();
655 let chunks: Vec<_> = self
656 .api
657 .logs(container, Some(options))
658 .try_collect()
659 .await?;
660 Ok(chunks.iter().map(ToString::to_string).collect())
661 }
662
663 async fn exec(&self, container: &str, command: Vec<String>) -> Result<Output, Error> {
664 let options = CreateExecOptions {
665 cmd: Some(command),
666 user: Some("root".to_owned()),
667 attach_stdout: Some(true),
668 attach_stderr: Some(true),
669 ..Default::default()
670 };
671 let exec = self.api.create_exec(container, options).await?;
672 match self.api.start_exec(&exec.id, None).await? {
673 StartExecResults::Attached { output, .. } => Ok(output
674 .try_filter_map(|output| {
675 std::future::ready(Ok(match output {
676 bollard::container::LogOutput::StdOut { message } => Some(message.to_vec()),
677 _ => None,
678 }))
679 })
680 .boxed()),
681 StartExecResults::Detached => Ok(futures_util::stream::empty().boxed()),
682 }
683 }
684
685 async fn image_present(&self, image: &str) -> Result<bool, Error> {
686 Ok(self.image_labels(image).await?.is_some())
687 }
688
689 async fn pull(&self, image: &str) -> Result<(), Error> {
690 let options = CreateImageOptionsBuilder::new().from_image(image).build();
691 let mut stream = self.api.create_image(Some(options), None, None);
692 let mut progress = PullProgress::default();
693 let mut logged = Instant::now();
694 info!("pulling {image}");
695 while let Some(update) = stream.try_next().await? {
696 progress.update(&update);
697 if logged.elapsed() >= PULL_LOG_INTERVAL {
698 logged = Instant::now();
699 info!("pulling {image}: {progress}");
700 }
701 }
702 info!("pulled {image}");
703 Ok(())
704 }
705
706 async fn ensure_network(&self, name: &str) -> Result<(), Error> {
707 match self.api.inspect_network(name, None).await {
708 Err(error) if is_not_found(&error) => {
709 let request = NetworkCreateRequest {
710 name: name.to_owned(),
711 driver: Some("bridge".to_owned()),
712 ..Default::default()
713 };
714 self.api.create_network(request).await.map(|_| ())
715 }
716 other => other.map(|_| ()),
717 }
718 }
719
720 async fn ensure_volume(&self, name: &str) -> Result<(), Error> {
721 let request = VolumeCreateRequest {
723 name: Some(name.to_owned()),
724 ..Default::default()
725 };
726 self.api.create_volume(request).await.map(|_| ())
727 }
728
729 async fn remove_volume(&self, name: &str) -> Result<(), Error> {
730 let options = RemoveVolumeOptionsBuilder::new().build();
731 match self.api.remove_volume(name, Some(options)).await {
732 Err(error) if is_not_found(&error) => Ok(()),
733 other => other,
734 }
735 }
736
737 async fn volume_sizes(&self) -> Result<BTreeMap<String, u64>, Error> {
738 let options = DataUsageOptionsBuilder::new().verbose(true).build();
740 let usage = self.api.df(Some(options)).await?;
741 Ok(usage
743 .volume_usage
744 .and_then(|volumes| volumes.items)
745 .unwrap_or_default()
746 .into_iter()
747 .filter_map(|volume| {
748 let name = volume.get("Name")?.as_str()?.to_owned();
749 let size = volume.get("UsageData")?.get("Size")?.as_u64()?;
750 Some((name, size))
751 })
752 .collect())
753 }
754
755 async fn changes(&self, container: &str) -> Result<Vec<String>, Error> {
756 Ok(self
757 .api
758 .container_changes(container)
759 .await?
760 .unwrap_or_default()
761 .into_iter()
762 .filter(|change| change.kind != ChangeType::_2)
763 .map(|change| change.path)
764 .collect())
765 }
766
767 async fn written(&self, container: &str) -> Result<u64, Error> {
768 let options = InspectContainerOptionsBuilder::new().size(true).build();
769 let response = self.api.inspect_container(container, Some(options)).await?;
770 Ok(response
771 .size_rw
772 .and_then(|bytes| u64::try_from(bytes).ok())
773 .unwrap_or(0))
774 }
775
776 async fn files(&self, container: &str, directory: &str, prefix: &str) -> Result<Files, Error> {
777 let options = DownloadFromContainerOptionsBuilder::new()
778 .path(directory)
779 .build();
780 let mut archive = self.api.download_from_container(container, Some(options));
781 let (chunks, received) = tokio::sync::mpsc::channel(4);
783 let prefix = prefix.to_owned();
784 let reader = tokio::task::spawn_blocking(move || {
785 files_in(
786 Chunks {
787 received,
788 chunk: Vec::new(),
789 at: 0,
790 },
791 &prefix,
792 )
793 });
794 let mut failed = None;
795 while let Some(chunk) = archive.next().await {
796 match chunk {
797 Ok(chunk) => {
799 if chunks.send(chunk.to_vec()).await.is_err() {
800 break;
801 }
802 }
803 Err(error) => {
804 failed = Some(error);
805 break;
806 }
807 }
808 }
809 drop(chunks);
810 let files = match reader.await {
811 Ok(files) => files,
812 Err(error) => match error.try_into_panic() {
813 Ok(panic) => std::panic::resume_unwind(panic),
814 Err(_) => Files::default(),
816 },
817 };
818 match failed {
819 Some(error) => Err(error),
820 None => Ok(files),
821 }
822 }
823
824 async fn commit(
825 &self,
826 container: &str,
827 reference: &str,
828 labels: BTreeMap<String, String>,
829 ) -> Result<u64, Error> {
830 let (repository, tag) = reference.rsplit_once(':').unwrap_or((reference, "latest"));
831 let options = CommitContainerOptionsBuilder::new()
832 .container(container)
833 .repo(repository)
834 .tag(tag)
835 .pause(false)
836 .build();
837 let config = ContainerConfig {
838 labels: Some(labels.into_iter().collect()),
839 ..Default::default()
840 };
841 let parent = match self.api.inspect_container(container, None).await?.image {
843 Some(parent) => self.api.inspect_image(&parent).await?.size.unwrap_or(0),
844 None => 0,
845 };
846 let id = self.api.commit_container(options, config).await?.id;
847 let size = self.api.inspect_image(&id).await?.size.unwrap_or(0);
848 Ok(u64::try_from(size - parent).unwrap_or(0))
849 }
850
851 async fn images(&self, label: &str) -> Result<Vec<Image>, Error> {
852 let options = ListImagesOptionsBuilder::new()
853 .filters(&HashMap::from([("label", vec![label])]))
854 .build();
855 let images = self.api.list_images(Some(options)).await?;
856 let mut parents: HashMap<String, i64> = HashMap::new();
857 let mut found = Vec::new();
858 for image in images {
859 let parent = if image.parent_id.is_empty() {
860 0
861 } else if let Some(size) = parents.get(&image.parent_id) {
862 *size
863 } else {
864 let size = self
865 .api
866 .inspect_image(&image.parent_id)
867 .await
868 .ok()
869 .and_then(|parent| parent.size)
870 .unwrap_or(0);
871 parents.insert(image.parent_id.clone(), size);
872 size
873 };
874 found.push(Image {
875 reference: image.repo_tags.into_iter().next().unwrap_or(image.id),
876 labels: image.labels,
877 bytes: u64::try_from(image.size - parent).unwrap_or(0),
878 });
879 }
880 Ok(found)
881 }
882
883 async fn image_id(&self, reference: &str) -> Result<Option<String>, Error> {
884 match self.api.inspect_image(reference).await {
885 Ok(inspect) => Ok(inspect.id),
886 Err(error) if is_not_found(&error) => Ok(None),
887 Err(error) => Err(error),
888 }
889 }
890
891 async fn tagged_images(&self) -> Result<Vec<TaggedImage>, Error> {
892 let images = self
893 .api
894 .list_images(Some(ListImagesOptionsBuilder::new().build()))
895 .await?;
896 Ok(images
897 .into_iter()
898 .map(|image| TaggedImage {
899 id: image.id,
900 tags: image
901 .repo_tags
902 .into_iter()
903 .filter(|tag| tag != "<none>:<none>")
904 .collect(),
905 digests: image
906 .repo_digests
907 .into_iter()
908 .filter(|digest| digest != "<none>@<none>")
909 .collect(),
910 })
911 .filter(|image| !image.tags.is_empty())
912 .collect())
913 }
914
915 async fn container_images(&self) -> Result<BTreeSet<String>, Error> {
916 let options = ListContainersOptionsBuilder::new().all(true).build();
917 let containers = self.api.list_containers(Some(options)).await?;
918 Ok(containers
919 .into_iter()
920 .filter_map(|container| container.image_id)
921 .collect())
922 }
923
924 async fn remove_image(&self, reference: &str) -> Result<(), Error> {
925 let options = RemoveImageOptionsBuilder::new().build();
926 match self.api.remove_image(reference, Some(options), None).await {
927 Err(error) if is_not_found(&error) => Ok(()),
928 other => other.map(|_| ()),
929 }
930 }
931
932 async fn free_space(&self) -> Result<u64, Error> {
933 let root = self.api.info().await?.docker_root_dir.unwrap_or_default();
934 let stat = rustix::fs::statvfs(root.as_str()).map_err(|error| Error::IOError {
936 err: std::io::Error::other(format!("cannot measure the disk of {root}: {error}")),
937 })?;
938 Ok(stat.f_bavail.saturating_mul(stat.f_frsize))
939 }
940
941 async fn shell(
942 &self,
943 name: &str,
944 body: ContainerCreateBody,
945 size: TtySize,
946 ) -> Result<Tty, Error> {
947 let options = CreateContainerOptionsBuilder::new().name(name).build();
948 let id = self.api.create_container(Some(options), body).await?.id;
949 let attached = async {
950 let options = AttachContainerOptionsBuilder::new()
951 .stream(true)
952 .stdin(true)
953 .stdout(true)
954 .stderr(true)
955 .build();
956 let attached = self.api.attach_container(&id, Some(options)).await?;
957 self.api.start_container(&id, None).await?;
958 let resize = ResizeContainerTTYOptionsBuilder::new()
959 .w(i32::from(size.cols))
960 .h(i32::from(size.rows))
961 .build();
962 self.api.resize_container_tty(&id, resize).await?;
963 Ok::<_, Error>(attached)
964 };
965 match attached.await {
966 Ok(attached) => Ok(Tty {
967 container: id,
968 output: attached
969 .output
970 .map_ok(|output| output.into_bytes().to_vec())
971 .boxed(),
972 input: attached.input,
973 }),
974 Err(error) => {
975 let _ = self.remove(&id).await;
976 Err(error)
977 }
978 }
979 }
980}
981
982const PULL_LOG_INTERVAL: Duration = Duration::from_secs(10);
984
985#[derive(Debug, Default)]
987struct PullProgress {
988 layers: BTreeMap<String, Layer>,
989}
990
991#[derive(Debug, Default)]
992struct Layer {
993 downloaded: u64,
994 size: u64,
995 done: bool,
996}
997
998impl PullProgress {
999 fn update(&mut self, update: &CreateImageInfo) {
1000 let (Some(id), Some(status)) = (&update.id, &update.status) else {
1001 return;
1002 };
1003 let bytes = |value: Option<i64>| value.and_then(|value| u64::try_from(value).ok());
1004 let detail = update.progress_detail.as_ref();
1005 match status.as_str() {
1006 "Pulling fs layer" | "Waiting" => {
1007 self.layers.entry(id.clone()).or_default();
1008 }
1009 "Downloading" => {
1010 let layer = self.layers.entry(id.clone()).or_default();
1011 layer.downloaded =
1012 bytes(detail.and_then(|detail| detail.current)).unwrap_or(layer.downloaded);
1013 layer.size = bytes(detail.and_then(|detail| detail.total)).unwrap_or(layer.size);
1014 }
1015 "Download complete" => {
1016 if let Some(layer) = self.layers.get_mut(id) {
1017 layer.downloaded = layer.size;
1018 }
1019 }
1020 "Pull complete" | "Already exists" => {
1021 let layer = self.layers.entry(id.clone()).or_default();
1022 layer.downloaded = layer.size;
1023 layer.done = true;
1024 }
1025 _ => {}
1026 }
1027 }
1028}
1029
1030impl fmt::Display for PullProgress {
1031 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1032 let done = self.layers.values().filter(|layer| layer.done).count();
1033 let downloaded: u64 = self.layers.values().map(|layer| layer.downloaded).sum();
1034 let size: u64 = self.layers.values().map(|layer| layer.size).sum();
1035 write!(
1036 f,
1037 "{done} of {} layers done, {} of {} downloaded",
1038 self.layers.len(),
1039 ByteSize(downloaded),
1040 ByteSize(size)
1041 )
1042 }
1043}
1044
1045fn connect_to(docker_host: Option<&str>) -> Result<Docker, Error> {
1046 let api = match docker_host.filter(|host| !host.is_empty()) {
1047 None => bollard::Docker::connect_with_socket_defaults()?,
1048 Some(host) if host.starts_with("unix://") => {
1049 bollard::Docker::connect_with_socket(host, 120, bollard::API_DEFAULT_VERSION)?
1050 }
1051 Some(host) => {
1053 return Err(Error::UnsupportedURISchemeError {
1054 uri: host.to_owned(),
1055 });
1056 }
1057 };
1058 Ok(Docker { api })
1059}
1060
1061struct Chunks {
1064 received: tokio::sync::mpsc::Receiver<Vec<u8>>,
1065 chunk: Vec<u8>,
1066 at: usize,
1067}
1068
1069impl std::io::Read for Chunks {
1070 fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
1071 while self.at == self.chunk.len() {
1072 match self.received.blocking_recv() {
1073 Some(chunk) => {
1074 self.chunk = chunk;
1075 self.at = 0;
1076 }
1077 None => return Ok(0),
1078 }
1079 }
1080 let n = buf.len().min(self.chunk.len() - self.at);
1081 buf[..n].copy_from_slice(&self.chunk[self.at..self.at + n]);
1082 self.at += n;
1083 Ok(n)
1084 }
1085}
1086
1087struct Ends<R> {
1090 inner: R,
1091 ran_out: bool,
1092}
1093
1094impl<R: std::io::Read> std::io::Read for Ends<R> {
1095 fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
1096 let n = self.inner.read(buf)?;
1097 self.ran_out = n == 0 && !buf.is_empty();
1098 Ok(n)
1099 }
1100}
1101
1102pub(crate) fn files_in(tar: impl std::io::Read, prefix: &str) -> Files {
1107 let mut tar = Ends {
1108 inner: tar,
1109 ran_out: false,
1110 };
1111 let mut read = Files::default();
1112 let ended = files_until_end(&mut tar, prefix, &mut read.files);
1113 read.complete = ended && !tar.ran_out;
1114 read
1115}
1116
1117fn files_until_end(tar: impl std::io::Read, prefix: &str, files: &mut Vec<File>) -> bool {
1120 let mut archive = tar::Archive::new(tar);
1121 let Ok(entries) = archive.entries() else {
1122 return false;
1123 };
1124 let mut kept = 0;
1125 for entry in entries {
1126 let Ok(mut entry) = entry else {
1127 return false;
1128 };
1129 let name = match entry.path() {
1130 Ok(path) if path.components().count() == 2 => path
1131 .file_name()
1132 .map(|name| name.to_string_lossy().into_owned()),
1133 _ => None,
1134 };
1135 let Some(name) =
1136 name.filter(|name| name.starts_with(prefix) && entry.header().entry_type().is_file())
1137 else {
1138 continue;
1139 };
1140 let room = FILES_LIMIT - kept;
1141 let mut contents = Vec::new();
1142 let whole = std::io::Read::read_to_end(
1144 &mut std::io::Read::take(&mut entry, room as u64 + 1),
1145 &mut contents,
1146 )
1147 .is_ok()
1148 && contents.len() <= room;
1149 contents.truncate(room);
1150 kept += contents.len();
1151 files.push(File {
1152 name,
1153 contents: String::from_utf8_lossy(&contents).into_owned(),
1154 });
1155 if !whole {
1156 return false;
1157 }
1158 }
1159 true
1160}
1161
1162fn jit_config_tar(jit_config: &JitConfig) -> std::io::Result<Vec<u8>> {
1163 let mut tar = tar::Builder::new(Vec::new());
1164 let path = JIT_CONFIG_PATH.trim_start_matches('/');
1165 let mut dir = tar::Header::new_gnu();
1166 dir.set_entry_type(tar::EntryType::Directory);
1167 dir.set_mode(0o700);
1168 dir.set_size(0);
1169 tar.append_data(
1170 &mut dir,
1171 path.rsplit_once('/').map_or("", |(dir, _)| dir),
1172 std::io::empty(),
1173 )?;
1174 let mut file = tar::Header::new_gnu();
1175 file.set_mode(0o400);
1176 file.set_size(jit_config.0.len() as u64);
1177 tar.append_data(&mut file, path, jit_config.0.as_bytes())?;
1178 tar.into_inner()
1179}
1180
1181#[cfg(test)]
1182pub(crate) mod tests;