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, ListVolumesOptionsBuilder, LogsOptionsBuilder,
21 RemoveContainerOptionsBuilder, RemoveImageOptionsBuilder, RemoveVolumeOptionsBuilder,
22 ResizeContainerTTYOptionsBuilder, 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
66pub const SKIP_SYNCS: &str = "RUNNERD_SKIP_SYNCS=1";
70
71pub const SKIP_SYNCS_LABEL: &str = "runnerd.skip-syncs";
74
75pub const VOLATILE_LAYERS_LABEL: &str = "runnerd.volatile-layers";
80
81pub fn skips_syncs<S: std::hash::BuildHasher>(labels: &HashMap<String, String, S>) -> bool {
83 labels
84 .get(SKIP_SYNCS_LABEL)
85 .is_some_and(|version| version == "1")
86}
87
88pub fn image_syncs<S: std::hash::BuildHasher>(
91 labels: &HashMap<String, String, S>,
92) -> crate::config::ImageSyncs {
93 use crate::config::ImageSyncs;
94 if !skips_syncs(labels) {
95 ImageSyncs::Syncs
96 } else if labels.contains_key(VOLATILE_LAYERS_LABEL) {
97 ImageSyncs::Skips
98 } else {
99 ImageSyncs::SkipsCalls
100 }
101}
102
103#[derive(Clone)]
105pub struct JitConfig(pub String);
106
107impl fmt::Debug for JitConfig {
108 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109 f.write_str("JitConfig(<redacted>)")
110 }
111}
112
113#[derive(Debug, Clone, PartialEq, Eq)]
115pub struct RunnerState {
116 pub running: bool,
118 pub exit_code: Option<i64>,
120 pub oom_killed: bool,
122 pub privileged: bool,
124 pub runtime: Option<String>,
126 pub pid: Option<u32>,
128 pub address: Option<String>,
130 pub started_at: Option<u64>,
132 pub finished_at: Option<u64>,
134 pub memory: Option<u64>,
136 pub cpus: Option<u64>,
138}
139
140pub type Output = BoxStream<'static, Result<Vec<u8>, Error>>;
142
143#[derive(Debug, Clone, PartialEq, Eq)]
145pub struct Image {
146 pub reference: String,
148 pub labels: HashMap<String, String>,
150 pub bytes: u64,
152}
153
154#[derive(Debug, Clone, PartialEq, Eq)]
156pub struct TaggedImage {
157 pub id: String,
159 pub tags: Vec<String>,
161 pub digests: Vec<String>,
163}
164
165#[derive(Debug, Clone, PartialEq, Eq)]
167pub struct File {
168 pub name: String,
170 pub contents: String,
172}
173
174#[derive(Debug, Clone, Default, PartialEq, Eq)]
176pub struct Files {
177 pub files: Vec<File>,
179 pub complete: bool,
182}
183
184pub(crate) const FILES_LIMIT: usize = 16 << 20;
187
188pub struct Tty {
190 pub container: String,
192 pub output: BoxStream<'static, Result<Vec<u8>, Error>>,
194 pub input: Pin<Box<dyn tokio::io::AsyncWrite + Send>>,
196}
197
198impl fmt::Debug for Tty {
199 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
200 f.debug_struct("Tty")
201 .field("container", &self.container)
202 .finish_non_exhaustive()
203 }
204}
205
206#[derive(Debug, Clone, Copy, PartialEq, Eq)]
208pub struct TtySize {
209 pub cols: u16,
211 pub rows: u16,
213}
214
215pub fn cpu_shares(cpus: u32) -> i64 {
221 (i64::from(cpus) * 1024).clamp(2, 262_144)
222}
223
224pub fn create_body(spec: &RunnerSpec, isolation: &dyn Isolation) -> ContainerCreateBody {
226 let memory = i64::try_from(spec.memory.0).unwrap_or(i64::MAX);
228 let mut host_config = HostConfig {
229 cgroup_parent: Some(spec.slice.clone()),
230 memory: Some(memory),
231 memory_swap: Some(memory),
233 nano_cpus: Some(i64::from(spec.cpus) * 1_000_000_000),
234 cpu_shares: Some(cpu_shares(spec.cpus)),
235 init: Some(true),
236 mounts: Some(spec.mounts.clone()),
237 network_mode: spec.network.clone(),
238 ..Default::default()
239 };
240 isolation.configure(&mut host_config);
241 let mut labels: HashMap<String, String> = spec.labels.clone().into_iter().collect();
242 labels.insert("runnerd.profile".to_owned(), spec.profile.clone());
243 labels.insert("runnerd.isolation".to_owned(), isolation.name().to_owned());
244 ContainerCreateBody {
245 image: Some(spec.image.clone()),
246 env: Some(spec.env.clone()),
247 entrypoint: spec.entrypoint.clone(),
248 labels: Some(labels),
249 host_config: Some(host_config),
250 ..Default::default()
251 }
252}
253
254#[derive(Debug, Clone, PartialEq, Eq)]
256pub struct Container {
257 pub id: String,
259 pub name: String,
261 pub running: bool,
263 pub labels: HashMap<String, String>,
265}
266
267#[derive(Debug)]
270pub struct StartError {
271 pub container: Option<String>,
274 pub may_have_run: bool,
277 pub error: Error,
279}
280
281impl fmt::Display for StartError {
282 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
283 self.error.fmt(f)
284 }
285}
286
287impl std::error::Error for StartError {
288 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
289 Some(&self.error)
290 }
291}
292
293impl From<StartError> for Error {
295 fn from(failed: StartError) -> Error {
296 failed.error
297 }
298}
299
300fn refused(error: &Error) -> bool {
303 matches!(
304 error,
305 Error::DockerResponseServerError {
306 status_code: 400..=499,
307 ..
308 }
309 )
310}
311
312pub fn is_unreachable(error: &Error) -> bool {
316 matches!(
317 error,
318 Error::HyperLegacyError { .. }
319 | Error::HyperResponseError { .. }
320 | Error::IOError { .. }
321 | Error::SocketNotFoundError(_)
322 )
323}
324
325pub fn is_declined(error: &Error) -> bool {
328 matches!(
329 error,
330 Error::DockerResponseServerError {
331 status_code: 400..=499 | 501,
332 ..
333 }
334 )
335}
336
337pub fn is_not_found(error: &Error) -> bool {
339 matches!(
340 error,
341 Error::DockerResponseServerError {
342 status_code: 404,
343 ..
344 }
345 )
346}
347
348pub fn is_being_removed(error: &Error) -> bool {
351 matches!(
352 error,
353 Error::DockerResponseServerError { status_code: 409, message }
354 if message.contains("is already in progress") || message.contains("marked for removal")
355 )
356}
357
358pub fn is_not_running(error: &Error) -> bool {
361 matches!(
362 error,
363 Error::DockerResponseServerError { status_code: 409, message } if message.ends_with(" is not running")
364 )
365}
366
367pub fn is_log_gone(error: &Error) -> bool {
370 is_not_found(error) || is_being_removed(error)
371}
372
373pub trait DockerApi: Send + Sync {
376 fn start(
379 &self,
380 spec: &RunnerSpec,
381 isolation: &dyn Isolation,
382 ) -> impl Future<Output = Result<String, StartError>> + Send;
383 fn inspect(&self, container: &str) -> impl Future<Output = Result<RunnerState, Error>> + Send;
385 fn remove(&self, container: &str) -> impl Future<Output = Result<(), Error>> + Send;
387 fn run(
389 &self,
390 name: &str,
391 body: ContainerCreateBody,
392 ) -> impl Future<Output = Result<String, Error>> + Send;
393 fn list(&self, label: &str) -> impl Future<Output = Result<Vec<Container>, Error>> + Send;
395 fn exits(&self) -> BoxStream<'static, Result<String, Error>>;
399 fn wait(&self, container: &str) -> impl Future<Output = Result<i64, Error>> + Send;
401 fn logs(&self, container: &str) -> impl Future<Output = Result<String, Error>> + Send;
403 fn logs_since(
406 &self,
407 container: &str,
408 since: u64,
409 ) -> impl Future<Output = Result<String, Error>> + Send;
410 fn exec(
413 &self,
414 container: &str,
415 command: Vec<String>,
416 ) -> impl Future<Output = Result<Output, Error>> + Send;
417 fn image_present(&self, image: &str) -> impl Future<Output = Result<bool, Error>> + Send;
419 fn image_skips_syncs(&self, image: &str) -> impl Future<Output = Result<bool, Error>> + Send;
421 fn pull(&self, image: &str) -> impl Future<Output = Result<(), Error>> + Send;
423 fn ensure_network(&self, name: &str) -> impl Future<Output = Result<(), Error>> + Send;
425 fn ensure_volume(&self, name: &str) -> impl Future<Output = Result<(), Error>> + Send;
427 fn remove_volume(&self, name: &str) -> impl Future<Output = Result<(), Error>> + Send;
430 fn holders(&self, volume: &str) -> impl Future<Output = Result<Vec<Container>, Error>> + Send;
432 fn volume_sizes(&self) -> impl Future<Output = Result<BTreeMap<String, u64>, Error>> + Send;
434 fn volumes(&self, prefix: &str) -> impl Future<Output = Result<Vec<String>, Error>> + Send;
436 fn changes(&self, container: &str) -> impl Future<Output = Result<Vec<String>, Error>> + Send;
438 fn written(&self, container: &str) -> impl Future<Output = Result<u64, Error>> + Send;
440 fn files(
443 &self,
444 container: &str,
445 directory: &str,
446 prefix: &str,
447 ) -> impl Future<Output = Result<Files, Error>> + Send;
448 fn commit(
451 &self,
452 container: &str,
453 reference: &str,
454 labels: BTreeMap<String, String>,
455 ) -> impl Future<Output = Result<u64, Error>> + Send;
456 fn images(&self, label: &str) -> impl Future<Output = Result<Vec<Image>, Error>> + Send;
458 fn image_id(
460 &self,
461 reference: &str,
462 ) -> impl Future<Output = Result<Option<String>, Error>> + Send;
463 fn tagged_images(&self) -> impl Future<Output = Result<Vec<TaggedImage>, Error>> + Send;
465 fn container_images(&self) -> impl Future<Output = Result<BTreeSet<String>, Error>> + Send;
467 fn remove_image(&self, reference: &str) -> impl Future<Output = Result<(), Error>> + Send;
469 fn free_space(&self) -> impl Future<Output = Result<u64, Error>> + Send;
472 fn root_dir(&self) -> impl Future<Output = Result<String, Error>> + Send;
474 fn shell(
477 &self,
478 name: &str,
479 body: ContainerCreateBody,
480 size: TtySize,
481 ) -> impl Future<Output = Result<Tty, Error>> + Send;
482}
483
484#[derive(Debug)]
486pub struct Docker {
487 api: bollard::Docker,
488}
489
490impl Docker {
491 async fn clean_up(&self, made: Option<String>, error: Error, may_have_run: bool) -> StartError {
494 let container = match made {
495 Some(container) => match self.remove(&container).await {
496 Err(failed) if !is_not_found(&failed) => Some(container),
497 _ => None,
498 },
499 None => None,
500 };
501 StartError {
502 container,
503 may_have_run,
504 error,
505 }
506 }
507
508 pub fn connect() -> Result<Docker, Error> {
510 connect_to(std::env::var("DOCKER_HOST").ok().as_deref())
511 }
512
513 pub async fn info(&self) -> Result<SystemInfo, Error> {
515 self.api.info().await
516 }
517
518 pub async fn image_labels(
520 &self,
521 image: &str,
522 ) -> Result<Option<HashMap<String, String>>, Error> {
523 match self.api.inspect_image(image).await {
524 Ok(inspect) => Ok(Some(
525 inspect
526 .config
527 .and_then(|config| config.labels)
528 .unwrap_or_default(),
529 )),
530 Err(error) if is_not_found(&error) => Ok(None),
531 Err(error) => Err(error),
532 }
533 }
534
535 async fn containers(&self, filter: &str, value: &str) -> Result<Vec<Container>, Error> {
537 let options = ListContainersOptionsBuilder::new()
538 .all(true)
539 .filters(&HashMap::from([(filter, vec![value])]))
540 .build();
541 let containers = self.api.list_containers(Some(options)).await?;
542
543 Ok(containers
544 .into_iter()
545 .map(|container| Container {
546 id: container.id.unwrap_or_default(),
547 name: container
548 .names
549 .and_then(|names| names.into_iter().next())
550 .unwrap_or_default()
551 .trim_start_matches('/')
552 .to_owned(),
553 running: container.state
554 == Some(bollard::models::ContainerSummaryStateEnum::RUNNING),
555 labels: container.labels.unwrap_or_default(),
556 })
557 .collect())
558 }
559
560 pub async fn registry_platforms(&self, image: &str) -> Result<Vec<String>, Error> {
563 let inspect = self.api.inspect_registry_image(image, None).await?;
564 Ok(inspect
565 .platforms
566 .into_iter()
567 .filter_map(|platform| Some(format!("{}/{}", platform.os?, platform.architecture?)))
568 .filter(|platform| platform != "unknown/unknown")
570 .collect())
571 }
572}
573
574impl DockerApi for Docker {
575 async fn start(
576 &self,
577 spec: &RunnerSpec,
578 isolation: &dyn Isolation,
579 ) -> Result<String, StartError> {
580 let options = CreateContainerOptionsBuilder::new()
581 .name(&spec.name)
582 .build();
583 let id = match self
584 .api
585 .create_container(Some(options), create_body(spec, isolation))
586 .await
587 {
588 Ok(created) => created.id,
589 Err(error) if refused(&error) => {
590 return Err(StartError {
591 container: None,
592 may_have_run: false,
593 error,
594 });
595 }
596 Err(error) => {
598 let made = match self.api.inspect_container(&spec.name, None).await {
599 Ok(found) => Some(found.id.unwrap_or_else(|| spec.name.clone())),
600 Err(missing) if is_not_found(&missing) => None,
601 Err(_) => Some(spec.name.clone()),
602 };
603 return Err(self.clean_up(made, error, false).await);
605 }
606 };
607 let uploaded = async {
608 if let Some(jit_config) = &spec.jit_config {
609 let options = UploadToContainerOptionsBuilder::new().path("/").build();
610 let tar = jit_config_tar(jit_config)?;
611 let tar = futures_util::stream::once(std::future::ready(tar.into()));
613 self.api
614 .upload_to_container(&id, Some(options), bollard::body_stream(tar))
615 .await?;
616 }
617 Ok::<(), Error>(())
618 };
619 if let Err(error) = uploaded.await {
620 return Err(self.clean_up(Some(id), error, false).await);
621 }
622 if let Err(error) = self.api.start_container(&id, None).await {
624 return Err(self.clean_up(Some(id), error, true).await);
625 }
626 Ok(id)
627 }
628
629 async fn inspect(&self, container: &str) -> Result<RunnerState, Error> {
630 let response = self.api.inspect_container(container, None).await?;
631 let state = response.state.unwrap_or_default();
632 let host_config = response.host_config.unwrap_or_default();
633 let address = response
634 .network_settings
635 .and_then(|settings| settings.networks)
636 .and_then(|networks| {
637 networks
638 .into_values()
639 .find_map(|network| network.ip_address.filter(|address| !address.is_empty()))
640 });
641 Ok(RunnerState {
642 running: state.running.unwrap_or(false),
643 exit_code: state.exit_code,
644 oom_killed: state.oom_killed.unwrap_or(false),
645 privileged: host_config.privileged.unwrap_or(false),
646 runtime: host_config.runtime,
647 pid: state
648 .pid
649 .and_then(|pid| u32::try_from(pid).ok())
650 .filter(|pid| *pid > 0),
651 address,
652 started_at: state.started_at.as_deref().and_then(crate::time::parse_ms),
653 finished_at: state.finished_at.as_deref().and_then(crate::time::parse_ms),
655 memory: host_config
656 .memory
657 .and_then(|memory| u64::try_from(memory).ok())
658 .filter(|memory| *memory > 0),
659 cpus: host_config
660 .nano_cpus
661 .and_then(|nano| u64::try_from(nano / 1_000_000).ok())
662 .filter(|cpus| *cpus > 0),
663 })
664 }
665
666 async fn remove(&self, container: &str) -> Result<(), Error> {
667 let options = RemoveContainerOptionsBuilder::new()
669 .force(true)
670 .v(true)
671 .build();
672 self.api.remove_container(container, Some(options)).await
673 }
674
675 async fn run(&self, name: &str, body: ContainerCreateBody) -> Result<String, Error> {
676 let options = CreateContainerOptionsBuilder::new().name(name).build();
677 let id = self.api.create_container(Some(options), body).await?.id;
678 if let Err(error) = self.api.start_container(&id, None).await {
679 let _ = self.remove(&id).await;
680 return Err(error);
681 }
682 Ok(id)
683 }
684
685 async fn list(&self, label: &str) -> Result<Vec<Container>, Error> {
686 self.containers("label", label).await
687 }
688
689 async fn holders(&self, volume: &str) -> Result<Vec<Container>, Error> {
690 self.containers("volume", volume).await
691 }
692
693 fn exits(&self) -> BoxStream<'static, Result<String, Error>> {
694 let filters = HashMap::from([
695 ("type", vec!["container"]),
696 ("event", vec!["die"]),
697 ("label", vec!["runnerd.job"]),
698 ]);
699 let options = EventsOptionsBuilder::new().filters(&filters).build();
700 self.api
701 .events(Some(options))
702 .try_filter_map(|event| std::future::ready(Ok(event.actor.and_then(|actor| actor.id))))
703 .boxed()
704 }
705
706 async fn wait(&self, container: &str) -> Result<i64, Error> {
707 let options = WaitContainerOptionsBuilder::new()
708 .condition("not-running")
709 .build();
710 let mut responses = self.api.wait_container(container, Some(options));
711 match responses.next().await {
712 Some(Ok(response)) => Ok(response.status_code),
713 Some(Err(Error::DockerContainerWaitError { code, .. })) => Ok(code),
715 Some(Err(error)) => Err(error),
716 None => Ok(0),
717 }
718 }
719
720 async fn logs(&self, container: &str) -> Result<String, Error> {
721 let options = LogsOptionsBuilder::new().stdout(true).stderr(true).build();
722 let chunks: Vec<_> = self
723 .api
724 .logs(container, Some(options))
725 .try_collect()
726 .await?;
727 Ok(chunks.iter().map(ToString::to_string).collect())
728 }
729
730 async fn logs_since(&self, container: &str, since: u64) -> Result<String, Error> {
731 let options = LogsOptionsBuilder::new()
732 .stdout(true)
733 .stderr(true)
734 .timestamps(true)
735 .since(i32::try_from(since).unwrap_or(i32::MAX))
736 .build();
737 let chunks: Vec<_> = self
738 .api
739 .logs(container, Some(options))
740 .try_collect()
741 .await?;
742 Ok(chunks.iter().map(ToString::to_string).collect())
743 }
744
745 async fn exec(&self, container: &str, command: Vec<String>) -> Result<Output, Error> {
746 let options = CreateExecOptions {
747 cmd: Some(command),
748 user: Some("root".to_owned()),
749 attach_stdout: Some(true),
750 attach_stderr: Some(true),
751 ..Default::default()
752 };
753 let exec = self.api.create_exec(container, options).await?;
754 match self.api.start_exec(&exec.id, None).await? {
755 StartExecResults::Attached { output, .. } => Ok(output
756 .try_filter_map(|output| {
757 std::future::ready(Ok(match output {
758 bollard::container::LogOutput::StdOut { message } => Some(message.to_vec()),
759 _ => None,
760 }))
761 })
762 .boxed()),
763 StartExecResults::Detached => Ok(futures_util::stream::empty().boxed()),
764 }
765 }
766
767 async fn image_present(&self, image: &str) -> Result<bool, Error> {
768 Ok(self.image_labels(image).await?.is_some())
769 }
770
771 async fn image_skips_syncs(&self, image: &str) -> Result<bool, Error> {
772 Ok(self
773 .image_labels(image)
774 .await?
775 .is_some_and(|labels| skips_syncs(&labels)))
776 }
777
778 async fn pull(&self, image: &str) -> Result<(), Error> {
779 let options = CreateImageOptionsBuilder::new().from_image(image).build();
780 let mut stream = self.api.create_image(Some(options), None, None);
781 let mut progress = PullProgress::default();
782 let mut logged = Instant::now();
783 info!("pulling {image}");
784 while let Some(update) = stream.try_next().await? {
785 progress.update(&update);
786 if logged.elapsed() >= PULL_LOG_INTERVAL {
787 logged = Instant::now();
788 info!("pulling {image}: {progress}");
789 }
790 }
791 info!("pulled {image}");
792 Ok(())
793 }
794
795 async fn ensure_network(&self, name: &str) -> Result<(), Error> {
796 match self.api.inspect_network(name, None).await {
797 Err(error) if is_not_found(&error) => {
798 let request = NetworkCreateRequest {
799 name: name.to_owned(),
800 driver: Some("bridge".to_owned()),
801 ..Default::default()
802 };
803 self.api.create_network(request).await.map(|_| ())
804 }
805 other => other.map(|_| ()),
806 }
807 }
808
809 async fn ensure_volume(&self, name: &str) -> Result<(), Error> {
810 let request = VolumeCreateRequest {
812 name: Some(name.to_owned()),
813 ..Default::default()
814 };
815 self.api.create_volume(request).await.map(|_| ())
816 }
817
818 async fn remove_volume(&self, name: &str) -> Result<(), Error> {
819 let options = RemoveVolumeOptionsBuilder::new().build();
820 match self.api.remove_volume(name, Some(options)).await {
821 Err(error) if is_not_found(&error) => Ok(()),
822 other => other,
823 }
824 }
825
826 async fn volumes(&self, prefix: &str) -> Result<Vec<String>, Error> {
827 let options = ListVolumesOptionsBuilder::new()
829 .filters(&HashMap::from([("name", vec![prefix])]))
830 .build();
831 Ok(self
832 .api
833 .list_volumes(Some(options))
834 .await?
835 .volumes
836 .unwrap_or_default()
837 .into_iter()
838 .map(|volume| volume.name)
839 .filter(|name| name.starts_with(prefix))
840 .collect())
841 }
842
843 async fn volume_sizes(&self) -> Result<BTreeMap<String, u64>, Error> {
844 let options = DataUsageOptionsBuilder::new().verbose(true).build();
846 let usage = self.api.df(Some(options)).await?;
847 Ok(usage
849 .volume_usage
850 .and_then(|volumes| volumes.items)
851 .unwrap_or_default()
852 .into_iter()
853 .filter_map(|volume| {
854 let name = volume.get("Name")?.as_str()?.to_owned();
855 let size = volume.get("UsageData")?.get("Size")?.as_u64()?;
856 Some((name, size))
857 })
858 .collect())
859 }
860
861 async fn changes(&self, container: &str) -> Result<Vec<String>, Error> {
862 Ok(self
863 .api
864 .container_changes(container)
865 .await?
866 .unwrap_or_default()
867 .into_iter()
868 .filter(|change| change.kind != ChangeType::_2)
869 .map(|change| change.path)
870 .collect())
871 }
872
873 async fn written(&self, container: &str) -> Result<u64, Error> {
874 let options = InspectContainerOptionsBuilder::new().size(true).build();
875 let response = self.api.inspect_container(container, Some(options)).await?;
876 Ok(response
877 .size_rw
878 .and_then(|bytes| u64::try_from(bytes).ok())
879 .unwrap_or(0))
880 }
881
882 async fn files(&self, container: &str, directory: &str, prefix: &str) -> Result<Files, Error> {
883 let options = DownloadFromContainerOptionsBuilder::new()
884 .path(directory)
885 .build();
886 let mut archive = self.api.download_from_container(container, Some(options));
887 let (chunks, received) = tokio::sync::mpsc::channel(4);
889 let prefix = prefix.to_owned();
890 let reader = tokio::task::spawn_blocking(move || {
891 files_in(
892 Chunks {
893 received,
894 chunk: Vec::new(),
895 at: 0,
896 },
897 &prefix,
898 )
899 });
900 let mut failed = None;
901 while let Some(chunk) = archive.next().await {
902 match chunk {
903 Ok(chunk) => {
905 if chunks.send(chunk.to_vec()).await.is_err() {
906 break;
907 }
908 }
909 Err(error) => {
910 failed = Some(error);
911 break;
912 }
913 }
914 }
915 drop(chunks);
916 let files = match reader.await {
917 Ok(files) => files,
918 Err(error) => match error.try_into_panic() {
919 Ok(panic) => std::panic::resume_unwind(panic),
920 Err(_) => Files::default(),
922 },
923 };
924 match failed {
925 Some(error) => Err(error),
926 None => Ok(files),
927 }
928 }
929
930 async fn commit(
931 &self,
932 container: &str,
933 reference: &str,
934 labels: BTreeMap<String, String>,
935 ) -> Result<u64, Error> {
936 let (repository, tag) = reference.rsplit_once(':').unwrap_or((reference, "latest"));
937 let options = CommitContainerOptionsBuilder::new()
938 .container(container)
939 .repo(repository)
940 .tag(tag)
941 .pause(false)
942 .build();
943 let config = ContainerConfig {
944 labels: Some(labels.into_iter().collect()),
945 ..Default::default()
946 };
947 let parent = match self.api.inspect_container(container, None).await?.image {
949 Some(parent) => self.api.inspect_image(&parent).await?.size.unwrap_or(0),
950 None => 0,
951 };
952 let id = self.api.commit_container(options, config).await?.id;
953 let size = self.api.inspect_image(&id).await?.size.unwrap_or(0);
954 Ok(u64::try_from(size - parent).unwrap_or(0))
955 }
956
957 async fn images(&self, label: &str) -> Result<Vec<Image>, Error> {
958 let options = ListImagesOptionsBuilder::new()
959 .filters(&HashMap::from([("label", vec![label])]))
960 .build();
961 let images = self.api.list_images(Some(options)).await?;
962 let mut parents: HashMap<String, i64> = HashMap::new();
963 let mut found = Vec::new();
964 for image in images {
965 let parent = if image.parent_id.is_empty() {
966 0
967 } else if let Some(size) = parents.get(&image.parent_id) {
968 *size
969 } else {
970 let size = self
971 .api
972 .inspect_image(&image.parent_id)
973 .await
974 .ok()
975 .and_then(|parent| parent.size)
976 .unwrap_or(0);
977 parents.insert(image.parent_id.clone(), size);
978 size
979 };
980 found.push(Image {
981 reference: image.repo_tags.into_iter().next().unwrap_or(image.id),
982 labels: image.labels,
983 bytes: u64::try_from(image.size - parent).unwrap_or(0),
984 });
985 }
986 Ok(found)
987 }
988
989 async fn image_id(&self, reference: &str) -> Result<Option<String>, Error> {
990 match self.api.inspect_image(reference).await {
991 Ok(inspect) => Ok(inspect.id),
992 Err(error) if is_not_found(&error) => Ok(None),
993 Err(error) => Err(error),
994 }
995 }
996
997 async fn tagged_images(&self) -> Result<Vec<TaggedImage>, Error> {
998 let images = self
999 .api
1000 .list_images(Some(ListImagesOptionsBuilder::new().build()))
1001 .await?;
1002 Ok(images
1003 .into_iter()
1004 .map(|image| TaggedImage {
1005 id: image.id,
1006 tags: image
1007 .repo_tags
1008 .into_iter()
1009 .filter(|tag| tag != "<none>:<none>")
1010 .collect(),
1011 digests: image
1012 .repo_digests
1013 .into_iter()
1014 .filter(|digest| digest != "<none>@<none>")
1015 .collect(),
1016 })
1017 .filter(|image| !image.tags.is_empty())
1018 .collect())
1019 }
1020
1021 async fn container_images(&self) -> Result<BTreeSet<String>, Error> {
1022 let options = ListContainersOptionsBuilder::new().all(true).build();
1023 let containers = self.api.list_containers(Some(options)).await?;
1024 Ok(containers
1025 .into_iter()
1026 .filter_map(|container| container.image_id)
1027 .collect())
1028 }
1029
1030 async fn remove_image(&self, reference: &str) -> Result<(), Error> {
1031 let options = RemoveImageOptionsBuilder::new().build();
1032 match self.api.remove_image(reference, Some(options), None).await {
1033 Err(error) if is_not_found(&error) => Ok(()),
1034 other => other.map(|_| ()),
1035 }
1036 }
1037
1038 async fn root_dir(&self) -> Result<String, Error> {
1039 Ok(self.api.info().await?.docker_root_dir.unwrap_or_default())
1040 }
1041
1042 async fn free_space(&self) -> Result<u64, Error> {
1043 let root = self.api.info().await?.docker_root_dir.unwrap_or_default();
1044 let statfs = |path: &std::path::Path| {
1046 let stat = rustix::fs::statvfs(path).map_err(std::io::Error::from)?;
1047 Ok(stat.f_bavail.saturating_mul(stat.f_frsize))
1048 };
1049 let paths = crate::observe::metrics::Paths::default();
1050 crate::observe::disk::free_space(&paths, std::path::Path::new(&root), statfs).map_err(
1051 |error| Error::IOError {
1052 err: std::io::Error::other(format!("cannot measure the disk of {root}: {error}")),
1053 },
1054 )
1055 }
1056
1057 async fn shell(
1058 &self,
1059 name: &str,
1060 body: ContainerCreateBody,
1061 size: TtySize,
1062 ) -> Result<Tty, Error> {
1063 let options = CreateContainerOptionsBuilder::new().name(name).build();
1064 let id = self.api.create_container(Some(options), body).await?.id;
1065 let attached = async {
1066 let options = AttachContainerOptionsBuilder::new()
1067 .stream(true)
1068 .stdin(true)
1069 .stdout(true)
1070 .stderr(true)
1071 .build();
1072 let attached = self.api.attach_container(&id, Some(options)).await?;
1073 self.api.start_container(&id, None).await?;
1074 let resize = ResizeContainerTTYOptionsBuilder::new()
1075 .w(i32::from(size.cols))
1076 .h(i32::from(size.rows))
1077 .build();
1078 self.api.resize_container_tty(&id, resize).await?;
1079 Ok::<_, Error>(attached)
1080 };
1081 match attached.await {
1082 Ok(attached) => Ok(Tty {
1083 container: id,
1084 output: attached
1085 .output
1086 .map_ok(|output| output.into_bytes().to_vec())
1087 .boxed(),
1088 input: attached.input,
1089 }),
1090 Err(error) => {
1091 let _ = self.remove(&id).await;
1092 Err(error)
1093 }
1094 }
1095 }
1096}
1097
1098const PULL_LOG_INTERVAL: Duration = Duration::from_secs(10);
1100
1101#[derive(Debug, Default)]
1103struct PullProgress {
1104 layers: BTreeMap<String, Layer>,
1105}
1106
1107#[derive(Debug, Default)]
1108struct Layer {
1109 downloaded: u64,
1110 size: u64,
1111 done: bool,
1112}
1113
1114impl PullProgress {
1115 fn update(&mut self, update: &CreateImageInfo) {
1116 let (Some(id), Some(status)) = (&update.id, &update.status) else {
1117 return;
1118 };
1119 let bytes = |value: Option<i64>| value.and_then(|value| u64::try_from(value).ok());
1120 let detail = update.progress_detail.as_ref();
1121 match status.as_str() {
1122 "Pulling fs layer" | "Waiting" => {
1123 self.layers.entry(id.clone()).or_default();
1124 }
1125 "Downloading" => {
1126 let layer = self.layers.entry(id.clone()).or_default();
1127 layer.downloaded =
1128 bytes(detail.and_then(|detail| detail.current)).unwrap_or(layer.downloaded);
1129 layer.size = bytes(detail.and_then(|detail| detail.total)).unwrap_or(layer.size);
1130 }
1131 "Download complete" => {
1132 if let Some(layer) = self.layers.get_mut(id) {
1133 layer.downloaded = layer.size;
1134 }
1135 }
1136 "Pull complete" | "Already exists" => {
1137 let layer = self.layers.entry(id.clone()).or_default();
1138 layer.downloaded = layer.size;
1139 layer.done = true;
1140 }
1141 _ => {}
1142 }
1143 }
1144}
1145
1146impl fmt::Display for PullProgress {
1147 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1148 let done = self.layers.values().filter(|layer| layer.done).count();
1149 let downloaded: u64 = self.layers.values().map(|layer| layer.downloaded).sum();
1150 let size: u64 = self.layers.values().map(|layer| layer.size).sum();
1151 write!(
1152 f,
1153 "{done} of {} layers done, {} of {} downloaded",
1154 self.layers.len(),
1155 ByteSize(downloaded),
1156 ByteSize(size)
1157 )
1158 }
1159}
1160
1161fn connect_to(docker_host: Option<&str>) -> Result<Docker, Error> {
1162 let api = match docker_host.filter(|host| !host.is_empty()) {
1163 None => bollard::Docker::connect_with_socket_defaults()?,
1164 Some(host) if host.starts_with("unix://") => {
1165 bollard::Docker::connect_with_socket(host, 120, bollard::API_DEFAULT_VERSION)?
1166 }
1167 Some(host) => {
1169 return Err(Error::UnsupportedURISchemeError {
1170 uri: host.to_owned(),
1171 });
1172 }
1173 };
1174 Ok(Docker { api })
1175}
1176
1177struct Chunks {
1180 received: tokio::sync::mpsc::Receiver<Vec<u8>>,
1181 chunk: Vec<u8>,
1182 at: usize,
1183}
1184
1185impl std::io::Read for Chunks {
1186 fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
1187 while self.at == self.chunk.len() {
1188 match self.received.blocking_recv() {
1189 Some(chunk) => {
1190 self.chunk = chunk;
1191 self.at = 0;
1192 }
1193 None => return Ok(0),
1194 }
1195 }
1196 let n = buf.len().min(self.chunk.len() - self.at);
1197 buf[..n].copy_from_slice(&self.chunk[self.at..self.at + n]);
1198 self.at += n;
1199 Ok(n)
1200 }
1201}
1202
1203struct Ends<R> {
1206 inner: R,
1207 ran_out: bool,
1208}
1209
1210impl<R: std::io::Read> std::io::Read for Ends<R> {
1211 fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
1212 let n = self.inner.read(buf)?;
1213 self.ran_out = n == 0 && !buf.is_empty();
1214 Ok(n)
1215 }
1216}
1217
1218pub(crate) fn files_in(tar: impl std::io::Read, prefix: &str) -> Files {
1223 let mut tar = Ends {
1224 inner: tar,
1225 ran_out: false,
1226 };
1227 let mut read = Files::default();
1228 let ended = files_until_end(&mut tar, prefix, &mut read.files);
1229 read.complete = ended && !tar.ran_out;
1230 read
1231}
1232
1233fn files_until_end(tar: impl std::io::Read, prefix: &str, files: &mut Vec<File>) -> bool {
1236 let mut archive = tar::Archive::new(tar);
1237 let Ok(entries) = archive.entries() else {
1238 return false;
1239 };
1240 let mut kept = 0;
1241 for entry in entries {
1242 let Ok(mut entry) = entry else {
1243 return false;
1244 };
1245 let name = match entry.path() {
1246 Ok(path) if path.components().count() == 2 => path
1247 .file_name()
1248 .map(|name| name.to_string_lossy().into_owned()),
1249 _ => None,
1250 };
1251 let Some(name) =
1252 name.filter(|name| name.starts_with(prefix) && entry.header().entry_type().is_file())
1253 else {
1254 continue;
1255 };
1256 let room = FILES_LIMIT - kept;
1257 let mut contents = Vec::new();
1258 let whole = std::io::Read::read_to_end(
1260 &mut std::io::Read::take(&mut entry, room as u64 + 1),
1261 &mut contents,
1262 )
1263 .is_ok()
1264 && contents.len() <= room;
1265 contents.truncate(room);
1266 kept += contents.len();
1267 files.push(File {
1268 name,
1269 contents: String::from_utf8_lossy(&contents).into_owned(),
1270 });
1271 if !whole {
1272 return false;
1273 }
1274 }
1275 true
1276}
1277
1278fn jit_config_tar(jit_config: &JitConfig) -> std::io::Result<Vec<u8>> {
1279 let mut tar = tar::Builder::new(Vec::new());
1280 let path = JIT_CONFIG_PATH.trim_start_matches('/');
1281 let mut dir = tar::Header::new_gnu();
1282 dir.set_entry_type(tar::EntryType::Directory);
1283 dir.set_mode(0o700);
1284 dir.set_size(0);
1285 tar.append_data(
1286 &mut dir,
1287 path.rsplit_once('/').map_or("", |(dir, _)| dir),
1288 std::io::empty(),
1289 )?;
1290 let mut file = tar::Header::new_gnu();
1291 file.set_mode(0o400);
1292 file.set_size(jit_config.0.len() as u64);
1293 tar.append_data(&mut file, path, jit_config.0.as_bytes())?;
1294 tar.into_inner()
1295}
1296
1297#[cfg(test)]
1298pub(crate) mod tests;