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