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 fn is_being_removed(error: &Error) -> bool {
314 matches!(
315 error,
316 Error::DockerResponseServerError { status_code: 409, message }
317 if message.contains("is already in progress") || message.contains("marked for removal")
318 )
319}
320
321pub fn is_not_running(error: &Error) -> bool {
324 matches!(
325 error,
326 Error::DockerResponseServerError { status_code: 409, message } if message.ends_with(" is not running")
327 )
328}
329
330pub fn is_log_gone(error: &Error) -> bool {
333 is_not_found(error) || is_being_removed(error)
334}
335
336pub trait DockerApi: Send + Sync {
339 fn start(
342 &self,
343 spec: &RunnerSpec,
344 isolation: &dyn Isolation,
345 ) -> impl Future<Output = Result<String, StartError>> + Send;
346 fn inspect(&self, container: &str) -> impl Future<Output = Result<RunnerState, Error>> + Send;
348 fn remove(&self, container: &str) -> impl Future<Output = Result<(), Error>> + Send;
350 fn run(
352 &self,
353 name: &str,
354 body: ContainerCreateBody,
355 ) -> impl Future<Output = Result<String, Error>> + Send;
356 fn list(&self, label: &str) -> impl Future<Output = Result<Vec<Container>, Error>> + Send;
358 fn exits(&self) -> BoxStream<'static, Result<String, Error>>;
362 fn wait(&self, container: &str) -> impl Future<Output = Result<i64, Error>> + Send;
364 fn logs(&self, container: &str) -> impl Future<Output = Result<String, Error>> + Send;
366 fn logs_since(
369 &self,
370 container: &str,
371 since: u64,
372 ) -> impl Future<Output = Result<String, Error>> + Send;
373 fn exec(
376 &self,
377 container: &str,
378 command: Vec<String>,
379 ) -> impl Future<Output = Result<Output, Error>> + Send;
380 fn image_present(&self, image: &str) -> impl Future<Output = Result<bool, Error>> + Send;
382 fn pull(&self, image: &str) -> impl Future<Output = Result<(), Error>> + Send;
384 fn ensure_network(&self, name: &str) -> impl Future<Output = Result<(), Error>> + Send;
386 fn ensure_volume(&self, name: &str) -> impl Future<Output = Result<(), Error>> + Send;
388 fn remove_volume(&self, name: &str) -> impl Future<Output = Result<(), Error>> + Send;
390 fn volume_sizes(&self) -> impl Future<Output = Result<BTreeMap<String, u64>, Error>> + Send;
392 fn changes(&self, container: &str) -> impl Future<Output = Result<Vec<String>, Error>> + Send;
394 fn written(&self, container: &str) -> impl Future<Output = Result<u64, Error>> + Send;
396 fn files(
399 &self,
400 container: &str,
401 directory: &str,
402 prefix: &str,
403 ) -> impl Future<Output = Result<Files, Error>> + Send;
404 fn commit(
407 &self,
408 container: &str,
409 reference: &str,
410 labels: BTreeMap<String, String>,
411 ) -> impl Future<Output = Result<u64, Error>> + Send;
412 fn images(&self, label: &str) -> impl Future<Output = Result<Vec<Image>, Error>> + Send;
414 fn image_id(
416 &self,
417 reference: &str,
418 ) -> impl Future<Output = Result<Option<String>, Error>> + Send;
419 fn tagged_images(&self) -> impl Future<Output = Result<Vec<TaggedImage>, Error>> + Send;
421 fn container_images(&self) -> impl Future<Output = Result<BTreeSet<String>, Error>> + Send;
423 fn remove_image(&self, reference: &str) -> impl Future<Output = Result<(), Error>> + Send;
425 fn free_space(&self) -> impl Future<Output = Result<u64, Error>> + Send;
427 fn shell(
430 &self,
431 name: &str,
432 body: ContainerCreateBody,
433 size: TtySize,
434 ) -> impl Future<Output = Result<Tty, Error>> + Send;
435}
436
437#[derive(Debug)]
439pub struct Docker {
440 api: bollard::Docker,
441}
442
443impl Docker {
444 async fn clean_up(&self, made: Option<String>, error: Error, may_have_run: bool) -> StartError {
447 let container = match made {
448 Some(container) => match self.remove(&container).await {
449 Err(failed) if !is_not_found(&failed) => Some(container),
450 _ => None,
451 },
452 None => None,
453 };
454 StartError {
455 container,
456 may_have_run,
457 error,
458 }
459 }
460
461 pub fn connect() -> Result<Docker, Error> {
463 connect_to(std::env::var("DOCKER_HOST").ok().as_deref())
464 }
465
466 pub async fn info(&self) -> Result<SystemInfo, Error> {
468 self.api.info().await
469 }
470
471 pub async fn image_labels(
473 &self,
474 image: &str,
475 ) -> Result<Option<HashMap<String, String>>, Error> {
476 match self.api.inspect_image(image).await {
477 Ok(inspect) => Ok(Some(
478 inspect
479 .config
480 .and_then(|config| config.labels)
481 .unwrap_or_default(),
482 )),
483 Err(error) if is_not_found(&error) => Ok(None),
484 Err(error) => Err(error),
485 }
486 }
487
488 pub async fn registry_platforms(&self, image: &str) -> Result<Vec<String>, Error> {
491 let inspect = self.api.inspect_registry_image(image, None).await?;
492 Ok(inspect
493 .platforms
494 .into_iter()
495 .filter_map(|platform| Some(format!("{}/{}", platform.os?, platform.architecture?)))
496 .filter(|platform| platform != "unknown/unknown")
498 .collect())
499 }
500}
501
502impl DockerApi for Docker {
503 async fn start(
504 &self,
505 spec: &RunnerSpec,
506 isolation: &dyn Isolation,
507 ) -> Result<String, StartError> {
508 let options = CreateContainerOptionsBuilder::new()
509 .name(&spec.name)
510 .build();
511 let id = match self
512 .api
513 .create_container(Some(options), create_body(spec, isolation))
514 .await
515 {
516 Ok(created) => created.id,
517 Err(error) if refused(&error) => {
518 return Err(StartError {
519 container: None,
520 may_have_run: false,
521 error,
522 });
523 }
524 Err(error) => {
526 let made = match self.api.inspect_container(&spec.name, None).await {
527 Ok(found) => Some(found.id.unwrap_or_else(|| spec.name.clone())),
528 Err(missing) if is_not_found(&missing) => None,
529 Err(_) => Some(spec.name.clone()),
530 };
531 return Err(self.clean_up(made, error, false).await);
533 }
534 };
535 let uploaded = async {
536 if let Some(jit_config) = &spec.jit_config {
537 let options = UploadToContainerOptionsBuilder::new().path("/").build();
538 let tar = jit_config_tar(jit_config)?;
539 let tar = futures_util::stream::once(std::future::ready(tar.into()));
541 self.api
542 .upload_to_container(&id, Some(options), bollard::body_stream(tar))
543 .await?;
544 }
545 Ok::<(), Error>(())
546 };
547 if let Err(error) = uploaded.await {
548 return Err(self.clean_up(Some(id), error, false).await);
549 }
550 if let Err(error) = self.api.start_container(&id, None).await {
552 return Err(self.clean_up(Some(id), error, true).await);
553 }
554 Ok(id)
555 }
556
557 async fn inspect(&self, container: &str) -> Result<RunnerState, Error> {
558 let response = self.api.inspect_container(container, None).await?;
559 let state = response.state.unwrap_or_default();
560 let host_config = response.host_config.unwrap_or_default();
561 let address = response
562 .network_settings
563 .and_then(|settings| settings.networks)
564 .and_then(|networks| {
565 networks
566 .into_values()
567 .find_map(|network| network.ip_address.filter(|address| !address.is_empty()))
568 });
569 Ok(RunnerState {
570 running: state.running.unwrap_or(false),
571 exit_code: state.exit_code,
572 oom_killed: state.oom_killed.unwrap_or(false),
573 privileged: host_config.privileged.unwrap_or(false),
574 runtime: host_config.runtime,
575 pid: state
576 .pid
577 .and_then(|pid| u32::try_from(pid).ok())
578 .filter(|pid| *pid > 0),
579 address,
580 started_at: state.started_at.as_deref().and_then(crate::time::parse_ms),
581 finished_at: state.finished_at.as_deref().and_then(crate::time::parse_ms),
583 memory: host_config
584 .memory
585 .and_then(|memory| u64::try_from(memory).ok())
586 .filter(|memory| *memory > 0),
587 cpus: host_config
588 .nano_cpus
589 .and_then(|nano| u64::try_from(nano / 1_000_000).ok())
590 .filter(|cpus| *cpus > 0),
591 })
592 }
593
594 async fn remove(&self, container: &str) -> Result<(), Error> {
595 let options = RemoveContainerOptionsBuilder::new()
597 .force(true)
598 .v(true)
599 .build();
600 self.api.remove_container(container, Some(options)).await
601 }
602
603 async fn run(&self, name: &str, body: ContainerCreateBody) -> Result<String, Error> {
604 let options = CreateContainerOptionsBuilder::new().name(name).build();
605 let id = self.api.create_container(Some(options), body).await?.id;
606 if let Err(error) = self.api.start_container(&id, None).await {
607 let _ = self.remove(&id).await;
608 return Err(error);
609 }
610 Ok(id)
611 }
612
613 async fn list(&self, label: &str) -> Result<Vec<Container>, Error> {
614 let options = ListContainersOptionsBuilder::new()
615 .all(true)
616 .filters(&HashMap::from([("label", vec![label])]))
617 .build();
618 let containers = self.api.list_containers(Some(options)).await?;
619 Ok(containers
620 .into_iter()
621 .map(|container| Container {
622 id: container.id.unwrap_or_default(),
623 name: container
624 .names
625 .and_then(|names| names.into_iter().next())
626 .unwrap_or_default()
627 .trim_start_matches('/')
628 .to_owned(),
629 running: container.state
630 == Some(bollard::models::ContainerSummaryStateEnum::RUNNING),
631 labels: container.labels.unwrap_or_default(),
632 })
633 .collect())
634 }
635
636 fn exits(&self) -> BoxStream<'static, Result<String, Error>> {
637 let filters = HashMap::from([
638 ("type", vec!["container"]),
639 ("event", vec!["die"]),
640 ("label", vec!["runnerd.job"]),
641 ]);
642 let options = EventsOptionsBuilder::new().filters(&filters).build();
643 self.api
644 .events(Some(options))
645 .try_filter_map(|event| std::future::ready(Ok(event.actor.and_then(|actor| actor.id))))
646 .boxed()
647 }
648
649 async fn wait(&self, container: &str) -> Result<i64, Error> {
650 let options = WaitContainerOptionsBuilder::new()
651 .condition("not-running")
652 .build();
653 let mut responses = self.api.wait_container(container, Some(options));
654 match responses.next().await {
655 Some(Ok(response)) => Ok(response.status_code),
656 Some(Err(Error::DockerContainerWaitError { code, .. })) => Ok(code),
658 Some(Err(error)) => Err(error),
659 None => Ok(0),
660 }
661 }
662
663 async fn logs(&self, container: &str) -> Result<String, Error> {
664 let options = LogsOptionsBuilder::new().stdout(true).stderr(true).build();
665 let chunks: Vec<_> = self
666 .api
667 .logs(container, Some(options))
668 .try_collect()
669 .await?;
670 Ok(chunks.iter().map(ToString::to_string).collect())
671 }
672
673 async fn logs_since(&self, container: &str, since: u64) -> Result<String, Error> {
674 let options = LogsOptionsBuilder::new()
675 .stdout(true)
676 .stderr(true)
677 .timestamps(true)
678 .since(i32::try_from(since).unwrap_or(i32::MAX))
679 .build();
680 let chunks: Vec<_> = self
681 .api
682 .logs(container, Some(options))
683 .try_collect()
684 .await?;
685 Ok(chunks.iter().map(ToString::to_string).collect())
686 }
687
688 async fn exec(&self, container: &str, command: Vec<String>) -> Result<Output, Error> {
689 let options = CreateExecOptions {
690 cmd: Some(command),
691 user: Some("root".to_owned()),
692 attach_stdout: Some(true),
693 attach_stderr: Some(true),
694 ..Default::default()
695 };
696 let exec = self.api.create_exec(container, options).await?;
697 match self.api.start_exec(&exec.id, None).await? {
698 StartExecResults::Attached { output, .. } => Ok(output
699 .try_filter_map(|output| {
700 std::future::ready(Ok(match output {
701 bollard::container::LogOutput::StdOut { message } => Some(message.to_vec()),
702 _ => None,
703 }))
704 })
705 .boxed()),
706 StartExecResults::Detached => Ok(futures_util::stream::empty().boxed()),
707 }
708 }
709
710 async fn image_present(&self, image: &str) -> Result<bool, Error> {
711 Ok(self.image_labels(image).await?.is_some())
712 }
713
714 async fn pull(&self, image: &str) -> Result<(), Error> {
715 let options = CreateImageOptionsBuilder::new().from_image(image).build();
716 let mut stream = self.api.create_image(Some(options), None, None);
717 let mut progress = PullProgress::default();
718 let mut logged = Instant::now();
719 info!("pulling {image}");
720 while let Some(update) = stream.try_next().await? {
721 progress.update(&update);
722 if logged.elapsed() >= PULL_LOG_INTERVAL {
723 logged = Instant::now();
724 info!("pulling {image}: {progress}");
725 }
726 }
727 info!("pulled {image}");
728 Ok(())
729 }
730
731 async fn ensure_network(&self, name: &str) -> Result<(), Error> {
732 match self.api.inspect_network(name, None).await {
733 Err(error) if is_not_found(&error) => {
734 let request = NetworkCreateRequest {
735 name: name.to_owned(),
736 driver: Some("bridge".to_owned()),
737 ..Default::default()
738 };
739 self.api.create_network(request).await.map(|_| ())
740 }
741 other => other.map(|_| ()),
742 }
743 }
744
745 async fn ensure_volume(&self, name: &str) -> Result<(), Error> {
746 let request = VolumeCreateRequest {
748 name: Some(name.to_owned()),
749 ..Default::default()
750 };
751 self.api.create_volume(request).await.map(|_| ())
752 }
753
754 async fn remove_volume(&self, name: &str) -> Result<(), Error> {
755 let options = RemoveVolumeOptionsBuilder::new().build();
756 match self.api.remove_volume(name, Some(options)).await {
757 Err(error) if is_not_found(&error) => Ok(()),
758 other => other,
759 }
760 }
761
762 async fn volume_sizes(&self) -> Result<BTreeMap<String, u64>, Error> {
763 let options = DataUsageOptionsBuilder::new().verbose(true).build();
765 let usage = self.api.df(Some(options)).await?;
766 Ok(usage
768 .volume_usage
769 .and_then(|volumes| volumes.items)
770 .unwrap_or_default()
771 .into_iter()
772 .filter_map(|volume| {
773 let name = volume.get("Name")?.as_str()?.to_owned();
774 let size = volume.get("UsageData")?.get("Size")?.as_u64()?;
775 Some((name, size))
776 })
777 .collect())
778 }
779
780 async fn changes(&self, container: &str) -> Result<Vec<String>, Error> {
781 Ok(self
782 .api
783 .container_changes(container)
784 .await?
785 .unwrap_or_default()
786 .into_iter()
787 .filter(|change| change.kind != ChangeType::_2)
788 .map(|change| change.path)
789 .collect())
790 }
791
792 async fn written(&self, container: &str) -> Result<u64, Error> {
793 let options = InspectContainerOptionsBuilder::new().size(true).build();
794 let response = self.api.inspect_container(container, Some(options)).await?;
795 Ok(response
796 .size_rw
797 .and_then(|bytes| u64::try_from(bytes).ok())
798 .unwrap_or(0))
799 }
800
801 async fn files(&self, container: &str, directory: &str, prefix: &str) -> Result<Files, Error> {
802 let options = DownloadFromContainerOptionsBuilder::new()
803 .path(directory)
804 .build();
805 let mut archive = self.api.download_from_container(container, Some(options));
806 let (chunks, received) = tokio::sync::mpsc::channel(4);
808 let prefix = prefix.to_owned();
809 let reader = tokio::task::spawn_blocking(move || {
810 files_in(
811 Chunks {
812 received,
813 chunk: Vec::new(),
814 at: 0,
815 },
816 &prefix,
817 )
818 });
819 let mut failed = None;
820 while let Some(chunk) = archive.next().await {
821 match chunk {
822 Ok(chunk) => {
824 if chunks.send(chunk.to_vec()).await.is_err() {
825 break;
826 }
827 }
828 Err(error) => {
829 failed = Some(error);
830 break;
831 }
832 }
833 }
834 drop(chunks);
835 let files = match reader.await {
836 Ok(files) => files,
837 Err(error) => match error.try_into_panic() {
838 Ok(panic) => std::panic::resume_unwind(panic),
839 Err(_) => Files::default(),
841 },
842 };
843 match failed {
844 Some(error) => Err(error),
845 None => Ok(files),
846 }
847 }
848
849 async fn commit(
850 &self,
851 container: &str,
852 reference: &str,
853 labels: BTreeMap<String, String>,
854 ) -> Result<u64, Error> {
855 let (repository, tag) = reference.rsplit_once(':').unwrap_or((reference, "latest"));
856 let options = CommitContainerOptionsBuilder::new()
857 .container(container)
858 .repo(repository)
859 .tag(tag)
860 .pause(false)
861 .build();
862 let config = ContainerConfig {
863 labels: Some(labels.into_iter().collect()),
864 ..Default::default()
865 };
866 let parent = match self.api.inspect_container(container, None).await?.image {
868 Some(parent) => self.api.inspect_image(&parent).await?.size.unwrap_or(0),
869 None => 0,
870 };
871 let id = self.api.commit_container(options, config).await?.id;
872 let size = self.api.inspect_image(&id).await?.size.unwrap_or(0);
873 Ok(u64::try_from(size - parent).unwrap_or(0))
874 }
875
876 async fn images(&self, label: &str) -> Result<Vec<Image>, Error> {
877 let options = ListImagesOptionsBuilder::new()
878 .filters(&HashMap::from([("label", vec![label])]))
879 .build();
880 let images = self.api.list_images(Some(options)).await?;
881 let mut parents: HashMap<String, i64> = HashMap::new();
882 let mut found = Vec::new();
883 for image in images {
884 let parent = if image.parent_id.is_empty() {
885 0
886 } else if let Some(size) = parents.get(&image.parent_id) {
887 *size
888 } else {
889 let size = self
890 .api
891 .inspect_image(&image.parent_id)
892 .await
893 .ok()
894 .and_then(|parent| parent.size)
895 .unwrap_or(0);
896 parents.insert(image.parent_id.clone(), size);
897 size
898 };
899 found.push(Image {
900 reference: image.repo_tags.into_iter().next().unwrap_or(image.id),
901 labels: image.labels,
902 bytes: u64::try_from(image.size - parent).unwrap_or(0),
903 });
904 }
905 Ok(found)
906 }
907
908 async fn image_id(&self, reference: &str) -> Result<Option<String>, Error> {
909 match self.api.inspect_image(reference).await {
910 Ok(inspect) => Ok(inspect.id),
911 Err(error) if is_not_found(&error) => Ok(None),
912 Err(error) => Err(error),
913 }
914 }
915
916 async fn tagged_images(&self) -> Result<Vec<TaggedImage>, Error> {
917 let images = self
918 .api
919 .list_images(Some(ListImagesOptionsBuilder::new().build()))
920 .await?;
921 Ok(images
922 .into_iter()
923 .map(|image| TaggedImage {
924 id: image.id,
925 tags: image
926 .repo_tags
927 .into_iter()
928 .filter(|tag| tag != "<none>:<none>")
929 .collect(),
930 digests: image
931 .repo_digests
932 .into_iter()
933 .filter(|digest| digest != "<none>@<none>")
934 .collect(),
935 })
936 .filter(|image| !image.tags.is_empty())
937 .collect())
938 }
939
940 async fn container_images(&self) -> Result<BTreeSet<String>, Error> {
941 let options = ListContainersOptionsBuilder::new().all(true).build();
942 let containers = self.api.list_containers(Some(options)).await?;
943 Ok(containers
944 .into_iter()
945 .filter_map(|container| container.image_id)
946 .collect())
947 }
948
949 async fn remove_image(&self, reference: &str) -> Result<(), Error> {
950 let options = RemoveImageOptionsBuilder::new().build();
951 match self.api.remove_image(reference, Some(options), None).await {
952 Err(error) if is_not_found(&error) => Ok(()),
953 other => other.map(|_| ()),
954 }
955 }
956
957 async fn free_space(&self) -> Result<u64, Error> {
958 let root = self.api.info().await?.docker_root_dir.unwrap_or_default();
959 let stat = rustix::fs::statvfs(root.as_str()).map_err(|error| Error::IOError {
961 err: std::io::Error::other(format!("cannot measure the disk of {root}: {error}")),
962 })?;
963 Ok(stat.f_bavail.saturating_mul(stat.f_frsize))
964 }
965
966 async fn shell(
967 &self,
968 name: &str,
969 body: ContainerCreateBody,
970 size: TtySize,
971 ) -> Result<Tty, Error> {
972 let options = CreateContainerOptionsBuilder::new().name(name).build();
973 let id = self.api.create_container(Some(options), body).await?.id;
974 let attached = async {
975 let options = AttachContainerOptionsBuilder::new()
976 .stream(true)
977 .stdin(true)
978 .stdout(true)
979 .stderr(true)
980 .build();
981 let attached = self.api.attach_container(&id, Some(options)).await?;
982 self.api.start_container(&id, None).await?;
983 let resize = ResizeContainerTTYOptionsBuilder::new()
984 .w(i32::from(size.cols))
985 .h(i32::from(size.rows))
986 .build();
987 self.api.resize_container_tty(&id, resize).await?;
988 Ok::<_, Error>(attached)
989 };
990 match attached.await {
991 Ok(attached) => Ok(Tty {
992 container: id,
993 output: attached
994 .output
995 .map_ok(|output| output.into_bytes().to_vec())
996 .boxed(),
997 input: attached.input,
998 }),
999 Err(error) => {
1000 let _ = self.remove(&id).await;
1001 Err(error)
1002 }
1003 }
1004 }
1005}
1006
1007const PULL_LOG_INTERVAL: Duration = Duration::from_secs(10);
1009
1010#[derive(Debug, Default)]
1012struct PullProgress {
1013 layers: BTreeMap<String, Layer>,
1014}
1015
1016#[derive(Debug, Default)]
1017struct Layer {
1018 downloaded: u64,
1019 size: u64,
1020 done: bool,
1021}
1022
1023impl PullProgress {
1024 fn update(&mut self, update: &CreateImageInfo) {
1025 let (Some(id), Some(status)) = (&update.id, &update.status) else {
1026 return;
1027 };
1028 let bytes = |value: Option<i64>| value.and_then(|value| u64::try_from(value).ok());
1029 let detail = update.progress_detail.as_ref();
1030 match status.as_str() {
1031 "Pulling fs layer" | "Waiting" => {
1032 self.layers.entry(id.clone()).or_default();
1033 }
1034 "Downloading" => {
1035 let layer = self.layers.entry(id.clone()).or_default();
1036 layer.downloaded =
1037 bytes(detail.and_then(|detail| detail.current)).unwrap_or(layer.downloaded);
1038 layer.size = bytes(detail.and_then(|detail| detail.total)).unwrap_or(layer.size);
1039 }
1040 "Download complete" => {
1041 if let Some(layer) = self.layers.get_mut(id) {
1042 layer.downloaded = layer.size;
1043 }
1044 }
1045 "Pull complete" | "Already exists" => {
1046 let layer = self.layers.entry(id.clone()).or_default();
1047 layer.downloaded = layer.size;
1048 layer.done = true;
1049 }
1050 _ => {}
1051 }
1052 }
1053}
1054
1055impl fmt::Display for PullProgress {
1056 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1057 let done = self.layers.values().filter(|layer| layer.done).count();
1058 let downloaded: u64 = self.layers.values().map(|layer| layer.downloaded).sum();
1059 let size: u64 = self.layers.values().map(|layer| layer.size).sum();
1060 write!(
1061 f,
1062 "{done} of {} layers done, {} of {} downloaded",
1063 self.layers.len(),
1064 ByteSize(downloaded),
1065 ByteSize(size)
1066 )
1067 }
1068}
1069
1070fn connect_to(docker_host: Option<&str>) -> Result<Docker, Error> {
1071 let api = match docker_host.filter(|host| !host.is_empty()) {
1072 None => bollard::Docker::connect_with_socket_defaults()?,
1073 Some(host) if host.starts_with("unix://") => {
1074 bollard::Docker::connect_with_socket(host, 120, bollard::API_DEFAULT_VERSION)?
1075 }
1076 Some(host) => {
1078 return Err(Error::UnsupportedURISchemeError {
1079 uri: host.to_owned(),
1080 });
1081 }
1082 };
1083 Ok(Docker { api })
1084}
1085
1086struct Chunks {
1089 received: tokio::sync::mpsc::Receiver<Vec<u8>>,
1090 chunk: Vec<u8>,
1091 at: usize,
1092}
1093
1094impl std::io::Read for Chunks {
1095 fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
1096 while self.at == self.chunk.len() {
1097 match self.received.blocking_recv() {
1098 Some(chunk) => {
1099 self.chunk = chunk;
1100 self.at = 0;
1101 }
1102 None => return Ok(0),
1103 }
1104 }
1105 let n = buf.len().min(self.chunk.len() - self.at);
1106 buf[..n].copy_from_slice(&self.chunk[self.at..self.at + n]);
1107 self.at += n;
1108 Ok(n)
1109 }
1110}
1111
1112struct Ends<R> {
1115 inner: R,
1116 ran_out: bool,
1117}
1118
1119impl<R: std::io::Read> std::io::Read for Ends<R> {
1120 fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
1121 let n = self.inner.read(buf)?;
1122 self.ran_out = n == 0 && !buf.is_empty();
1123 Ok(n)
1124 }
1125}
1126
1127pub(crate) fn files_in(tar: impl std::io::Read, prefix: &str) -> Files {
1132 let mut tar = Ends {
1133 inner: tar,
1134 ran_out: false,
1135 };
1136 let mut read = Files::default();
1137 let ended = files_until_end(&mut tar, prefix, &mut read.files);
1138 read.complete = ended && !tar.ran_out;
1139 read
1140}
1141
1142fn files_until_end(tar: impl std::io::Read, prefix: &str, files: &mut Vec<File>) -> bool {
1145 let mut archive = tar::Archive::new(tar);
1146 let Ok(entries) = archive.entries() else {
1147 return false;
1148 };
1149 let mut kept = 0;
1150 for entry in entries {
1151 let Ok(mut entry) = entry else {
1152 return false;
1153 };
1154 let name = match entry.path() {
1155 Ok(path) if path.components().count() == 2 => path
1156 .file_name()
1157 .map(|name| name.to_string_lossy().into_owned()),
1158 _ => None,
1159 };
1160 let Some(name) =
1161 name.filter(|name| name.starts_with(prefix) && entry.header().entry_type().is_file())
1162 else {
1163 continue;
1164 };
1165 let room = FILES_LIMIT - kept;
1166 let mut contents = Vec::new();
1167 let whole = std::io::Read::read_to_end(
1169 &mut std::io::Read::take(&mut entry, room as u64 + 1),
1170 &mut contents,
1171 )
1172 .is_ok()
1173 && contents.len() <= room;
1174 contents.truncate(room);
1175 kept += contents.len();
1176 files.push(File {
1177 name,
1178 contents: String::from_utf8_lossy(&contents).into_owned(),
1179 });
1180 if !whole {
1181 return false;
1182 }
1183 }
1184 true
1185}
1186
1187fn jit_config_tar(jit_config: &JitConfig) -> std::io::Result<Vec<u8>> {
1188 let mut tar = tar::Builder::new(Vec::new());
1189 let path = JIT_CONFIG_PATH.trim_start_matches('/');
1190 let mut dir = tar::Header::new_gnu();
1191 dir.set_entry_type(tar::EntryType::Directory);
1192 dir.set_mode(0o700);
1193 dir.set_size(0);
1194 tar.append_data(
1195 &mut dir,
1196 path.rsplit_once('/').map_or("", |(dir, _)| dir),
1197 std::io::empty(),
1198 )?;
1199 let mut file = tar::Header::new_gnu();
1200 file.set_mode(0o400);
1201 file.set_size(jit_config.0.len() as u64);
1202 tar.append_data(&mut file, path, jit_config.0.as_bytes())?;
1203 tar.into_inner()
1204}
1205
1206#[cfg(test)]
1207pub(crate) mod tests;