use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::fmt;
use std::time::{Duration, Instant};
use bollard::exec::{CreateExecOptions, StartExecResults};
use std::pin::Pin;
use bollard::models::{
ChangeType, ContainerConfig, ContainerCreateBody, CreateImageInfo, HostConfig, Mount,
NetworkCreateRequest, SystemInfo, VolumeCreateRequest,
};
use bollard::query_parameters::{
AttachContainerOptionsBuilder, CommitContainerOptionsBuilder, CreateContainerOptionsBuilder,
CreateImageOptionsBuilder, DataUsageOptionsBuilder, DownloadFromContainerOptionsBuilder,
EventsOptionsBuilder, InspectContainerOptionsBuilder, ListContainersOptionsBuilder,
ListImagesOptionsBuilder, ListVolumesOptionsBuilder, LogsOptionsBuilder,
RemoveContainerOptionsBuilder, RemoveImageOptionsBuilder, RemoveVolumeOptionsBuilder,
ResizeContainerTTYOptionsBuilder, UploadToContainerOptionsBuilder, WaitContainerOptionsBuilder,
};
use futures_util::stream::BoxStream;
use futures_util::{StreamExt, TryStreamExt};
use tracing::info;
use crate::config::ByteSize;
use crate::isolation::Isolation;
pub use bollard::errors::Error;
#[derive(Debug, Clone)]
pub struct RunnerSpec {
pub name: String,
pub image: String,
pub profile: String,
pub slice: String,
pub cpus: u32,
pub memory: ByteSize,
pub env: Vec<String>,
pub mounts: Vec<Mount>,
pub network: Option<String>,
pub labels: BTreeMap<String, String>,
pub entrypoint: Option<Vec<String>>,
pub jit_config: Option<JitConfig>,
}
pub const JIT_CONFIG_PATH: &str = "/run/runnerd/jitconfig";
pub const SKIP_SYNCS: &str = "RUNNERD_SKIP_SYNCS=1";
pub const SKIP_SYNCS_LABEL: &str = "runnerd.skip-syncs";
pub const VOLATILE_LAYERS_LABEL: &str = "runnerd.volatile-layers";
pub const PROBE_LABEL: &str = "runnerd.runner-probe";
pub fn probe<S: std::hash::BuildHasher>(
labels: &HashMap<String, String, S>,
exit_code: Option<i64>,
) -> Option<crate::api::RunnerProbe> {
use crate::api::RunnerProbe;
if labels.get(PROBE_LABEL).map(String::as_str) != Some("1") {
return None;
}
match exit_code? {
200 => Some(RunnerProbe::Clean),
201 => Some(RunnerProbe::Mount),
202 => Some(RunnerProbe::Procfs),
203 => Some(RunnerProbe::MountAndProcfs),
204 => Some(RunnerProbe::TimedOut),
205 => Some(RunnerProbe::Broken),
_ => None,
}
}
pub fn skips_syncs<S: std::hash::BuildHasher>(labels: &HashMap<String, String, S>) -> bool {
labels
.get(SKIP_SYNCS_LABEL)
.is_some_and(|version| version == "1")
}
pub fn image_syncs<S: std::hash::BuildHasher>(
labels: &HashMap<String, String, S>,
) -> crate::config::ImageSyncs {
use crate::config::ImageSyncs;
if !skips_syncs(labels) {
ImageSyncs::Syncs
} else if labels.contains_key(VOLATILE_LAYERS_LABEL) {
ImageSyncs::Skips
} else {
ImageSyncs::SkipsCalls
}
}
#[derive(Clone)]
pub struct JitConfig(pub String);
impl fmt::Debug for JitConfig {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("JitConfig(<redacted>)")
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RunnerState {
pub running: bool,
pub exit_code: Option<i64>,
pub oom_killed: bool,
pub probe: Option<crate::api::RunnerProbe>,
pub privileged: bool,
pub runtime: Option<String>,
pub pid: Option<u32>,
pub address: Option<String>,
pub started_at: Option<u64>,
pub finished_at: Option<u64>,
pub memory: Option<u64>,
pub cpus: Option<u64>,
}
pub type Output = BoxStream<'static, Result<Vec<u8>, Error>>;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Image {
pub reference: String,
pub labels: HashMap<String, String>,
pub bytes: u64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TaggedImage {
pub id: String,
pub tags: Vec<String>,
pub digests: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct File {
pub name: String,
pub contents: String,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Files {
pub files: Vec<File>,
pub complete: bool,
}
pub(crate) const FILES_LIMIT: usize = 16 << 20;
pub struct Tty {
pub container: String,
pub output: BoxStream<'static, Result<Vec<u8>, Error>>,
pub input: Pin<Box<dyn tokio::io::AsyncWrite + Send>>,
}
impl fmt::Debug for Tty {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Tty")
.field("container", &self.container)
.finish_non_exhaustive()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TtySize {
pub cols: u16,
pub rows: u16,
}
pub fn cpu_shares(cpus: u32) -> i64 {
(i64::from(cpus) * 1024).clamp(2, 262_144)
}
pub fn create_body(spec: &RunnerSpec, isolation: &dyn Isolation) -> ContainerCreateBody {
let memory = i64::try_from(spec.memory.0).unwrap_or(i64::MAX);
let mut host_config = HostConfig {
cgroup_parent: Some(spec.slice.clone()),
memory: Some(memory),
memory_swap: Some(memory),
nano_cpus: Some(i64::from(spec.cpus) * 1_000_000_000),
cpu_shares: Some(cpu_shares(spec.cpus)),
init: Some(true),
mounts: Some(spec.mounts.clone()),
network_mode: spec.network.clone(),
..Default::default()
};
isolation.configure(&mut host_config);
let mut labels: HashMap<String, String> = spec.labels.clone().into_iter().collect();
labels.insert("runnerd.profile".to_owned(), spec.profile.clone());
labels.insert("runnerd.isolation".to_owned(), isolation.name().to_owned());
ContainerCreateBody {
image: Some(spec.image.clone()),
env: Some(spec.env.clone()),
entrypoint: spec.entrypoint.clone(),
labels: Some(labels),
host_config: Some(host_config),
..Default::default()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Container {
pub id: String,
pub name: String,
pub running: bool,
pub labels: HashMap<String, String>,
}
#[derive(Debug)]
pub struct StartError {
pub container: Option<String>,
pub may_have_run: bool,
pub error: Error,
}
impl fmt::Display for StartError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.error.fmt(f)
}
}
impl std::error::Error for StartError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.error)
}
}
impl From<StartError> for Error {
fn from(failed: StartError) -> Error {
failed.error
}
}
fn refused(error: &Error) -> bool {
matches!(
error,
Error::DockerResponseServerError {
status_code: 400..=499,
..
}
)
}
pub fn is_unreachable(error: &Error) -> bool {
matches!(
error,
Error::HyperLegacyError { .. }
| Error::HyperResponseError { .. }
| Error::IOError { .. }
| Error::SocketNotFoundError(_)
)
}
pub fn is_declined(error: &Error) -> bool {
matches!(
error,
Error::DockerResponseServerError {
status_code: 400..=499 | 501,
..
}
)
}
pub fn is_not_found(error: &Error) -> bool {
matches!(
error,
Error::DockerResponseServerError {
status_code: 404,
..
}
)
}
pub fn is_being_removed(error: &Error) -> bool {
matches!(
error,
Error::DockerResponseServerError { status_code: 409, message }
if message.contains("is already in progress") || message.contains("marked for removal")
)
}
pub fn is_not_running(error: &Error) -> bool {
matches!(
error,
Error::DockerResponseServerError { status_code: 409, message } if message.ends_with(" is not running")
)
}
pub fn is_log_gone(error: &Error) -> bool {
is_not_found(error) || is_being_removed(error)
}
pub trait DockerApi: Send + Sync {
fn start(
&self,
spec: &RunnerSpec,
isolation: &dyn Isolation,
) -> impl Future<Output = Result<String, StartError>> + Send;
fn inspect(&self, container: &str) -> impl Future<Output = Result<RunnerState, Error>> + Send;
fn remove(&self, container: &str) -> impl Future<Output = Result<(), Error>> + Send;
fn run(
&self,
name: &str,
body: ContainerCreateBody,
) -> impl Future<Output = Result<String, Error>> + Send;
fn list(&self, label: &str) -> impl Future<Output = Result<Vec<Container>, Error>> + Send;
fn exits(&self) -> BoxStream<'static, Result<String, Error>>;
fn wait(&self, container: &str) -> impl Future<Output = Result<i64, Error>> + Send;
fn logs(&self, container: &str) -> impl Future<Output = Result<String, Error>> + Send;
fn logs_since(
&self,
container: &str,
since: u64,
) -> impl Future<Output = Result<String, Error>> + Send;
fn exec(
&self,
container: &str,
command: Vec<String>,
) -> impl Future<Output = Result<Output, Error>> + Send;
fn image_present(&self, image: &str) -> impl Future<Output = Result<bool, Error>> + Send;
fn image_skips_syncs(&self, image: &str) -> impl Future<Output = Result<bool, Error>> + Send;
fn pull(&self, image: &str) -> impl Future<Output = Result<(), Error>> + Send;
fn ensure_network(&self, name: &str) -> impl Future<Output = Result<(), Error>> + Send;
fn ensure_volume(&self, name: &str) -> impl Future<Output = Result<(), Error>> + Send;
fn remove_volume(&self, name: &str) -> impl Future<Output = Result<(), Error>> + Send;
fn holders(&self, volume: &str) -> impl Future<Output = Result<Vec<Container>, Error>> + Send;
fn volume_sizes(&self) -> impl Future<Output = Result<BTreeMap<String, u64>, Error>> + Send;
fn volumes(&self, prefix: &str) -> impl Future<Output = Result<Vec<String>, Error>> + Send;
fn changes(&self, container: &str) -> impl Future<Output = Result<Vec<String>, Error>> + Send;
fn written(&self, container: &str) -> impl Future<Output = Result<u64, Error>> + Send;
fn files(
&self,
container: &str,
directory: &str,
prefix: &str,
) -> impl Future<Output = Result<Files, Error>> + Send;
fn commit(
&self,
container: &str,
reference: &str,
labels: BTreeMap<String, String>,
) -> impl Future<Output = Result<u64, Error>> + Send;
fn images(&self, label: &str) -> impl Future<Output = Result<Vec<Image>, Error>> + Send;
fn image_id(
&self,
reference: &str,
) -> impl Future<Output = Result<Option<String>, Error>> + Send;
fn tagged_images(&self) -> impl Future<Output = Result<Vec<TaggedImage>, Error>> + Send;
fn container_images(&self) -> impl Future<Output = Result<BTreeSet<String>, Error>> + Send;
fn remove_image(&self, reference: &str) -> impl Future<Output = Result<(), Error>> + Send;
fn free_space(&self) -> impl Future<Output = Result<u64, Error>> + Send;
fn root_dir(&self) -> impl Future<Output = Result<String, Error>> + Send;
fn shell(
&self,
name: &str,
body: ContainerCreateBody,
size: TtySize,
) -> impl Future<Output = Result<Tty, Error>> + Send;
}
#[derive(Debug)]
pub struct Docker {
api: bollard::Docker,
}
impl Docker {
async fn clean_up(&self, made: Option<String>, error: Error, may_have_run: bool) -> StartError {
let container = match made {
Some(container) => match self.remove(&container).await {
Err(failed) if !is_not_found(&failed) => Some(container),
_ => None,
},
None => None,
};
StartError {
container,
may_have_run,
error,
}
}
pub fn connect() -> Result<Docker, Error> {
connect_to(std::env::var("DOCKER_HOST").ok().as_deref())
}
pub async fn info(&self) -> Result<SystemInfo, Error> {
self.api.info().await
}
pub async fn image_labels(
&self,
image: &str,
) -> Result<Option<HashMap<String, String>>, Error> {
match self.api.inspect_image(image).await {
Ok(inspect) => Ok(Some(
inspect
.config
.and_then(|config| config.labels)
.unwrap_or_default(),
)),
Err(error) if is_not_found(&error) => Ok(None),
Err(error) => Err(error),
}
}
async fn containers(&self, filter: &str, value: &str) -> Result<Vec<Container>, Error> {
let options = ListContainersOptionsBuilder::new()
.all(true)
.filters(&HashMap::from([(filter, vec![value])]))
.build();
let containers = self.api.list_containers(Some(options)).await?;
Ok(containers
.into_iter()
.map(|container| Container {
id: container.id.unwrap_or_default(),
name: container
.names
.and_then(|names| names.into_iter().next())
.unwrap_or_default()
.trim_start_matches('/')
.to_owned(),
running: container.state
== Some(bollard::models::ContainerSummaryStateEnum::RUNNING),
labels: container.labels.unwrap_or_default(),
})
.collect())
}
pub async fn registry_platforms(&self, image: &str) -> Result<Vec<String>, Error> {
let inspect = self.api.inspect_registry_image(image, None).await?;
Ok(inspect
.platforms
.into_iter()
.filter_map(|platform| Some(format!("{}/{}", platform.os?, platform.architecture?)))
.filter(|platform| platform != "unknown/unknown")
.collect())
}
}
impl DockerApi for Docker {
async fn start(
&self,
spec: &RunnerSpec,
isolation: &dyn Isolation,
) -> Result<String, StartError> {
let options = CreateContainerOptionsBuilder::new()
.name(&spec.name)
.build();
let id = match self
.api
.create_container(Some(options), create_body(spec, isolation))
.await
{
Ok(created) => created.id,
Err(error) if refused(&error) => {
return Err(StartError {
container: None,
may_have_run: false,
error,
});
}
Err(error) => {
let made = match self.api.inspect_container(&spec.name, None).await {
Ok(found) => Some(found.id.unwrap_or_else(|| spec.name.clone())),
Err(missing) if is_not_found(&missing) => None,
Err(_) => Some(spec.name.clone()),
};
return Err(self.clean_up(made, error, false).await);
}
};
let uploaded = async {
if let Some(jit_config) = &spec.jit_config {
let options = UploadToContainerOptionsBuilder::new().path("/").build();
let tar = jit_config_tar(jit_config)?;
let tar = futures_util::stream::once(std::future::ready(tar.into()));
self.api
.upload_to_container(&id, Some(options), bollard::body_stream(tar))
.await?;
}
Ok::<(), Error>(())
};
if let Err(error) = uploaded.await {
return Err(self.clean_up(Some(id), error, false).await);
}
if let Err(error) = self.api.start_container(&id, None).await {
return Err(self.clean_up(Some(id), error, true).await);
}
Ok(id)
}
async fn inspect(&self, container: &str) -> Result<RunnerState, Error> {
let response = self.api.inspect_container(container, None).await?;
let state = response.state.unwrap_or_default();
let host_config = response.host_config.unwrap_or_default();
let running = state.running.unwrap_or(false);
let labels = response
.config
.and_then(|config| config.labels)
.unwrap_or_default();
let address = response
.network_settings
.and_then(|settings| settings.networks)
.and_then(|networks| {
networks
.into_values()
.find_map(|network| network.ip_address.filter(|address| !address.is_empty()))
});
Ok(RunnerState {
running,
exit_code: state.exit_code,
oom_killed: state.oom_killed.unwrap_or(false),
probe: if running {
None
} else {
probe(&labels, state.exit_code)
},
privileged: host_config.privileged.unwrap_or(false),
runtime: host_config.runtime,
pid: state
.pid
.and_then(|pid| u32::try_from(pid).ok())
.filter(|pid| *pid > 0),
address,
started_at: state.started_at.as_deref().and_then(crate::time::parse_ms),
finished_at: state.finished_at.as_deref().and_then(crate::time::parse_ms),
memory: host_config
.memory
.and_then(|memory| u64::try_from(memory).ok())
.filter(|memory| *memory > 0),
cpus: host_config
.nano_cpus
.and_then(|nano| u64::try_from(nano / 1_000_000).ok())
.filter(|cpus| *cpus > 0),
})
}
async fn remove(&self, container: &str) -> Result<(), Error> {
let options = RemoveContainerOptionsBuilder::new()
.force(true)
.v(true)
.build();
self.api.remove_container(container, Some(options)).await
}
async fn run(&self, name: &str, body: ContainerCreateBody) -> Result<String, Error> {
let options = CreateContainerOptionsBuilder::new().name(name).build();
let id = self.api.create_container(Some(options), body).await?.id;
if let Err(error) = self.api.start_container(&id, None).await {
let _ = self.remove(&id).await;
return Err(error);
}
Ok(id)
}
async fn list(&self, label: &str) -> Result<Vec<Container>, Error> {
self.containers("label", label).await
}
async fn holders(&self, volume: &str) -> Result<Vec<Container>, Error> {
self.containers("volume", volume).await
}
fn exits(&self) -> BoxStream<'static, Result<String, Error>> {
let filters = HashMap::from([
("type", vec!["container"]),
("event", vec!["die"]),
("label", vec!["runnerd.job"]),
]);
let options = EventsOptionsBuilder::new().filters(&filters).build();
self.api
.events(Some(options))
.try_filter_map(|event| std::future::ready(Ok(event.actor.and_then(|actor| actor.id))))
.boxed()
}
async fn wait(&self, container: &str) -> Result<i64, Error> {
let options = WaitContainerOptionsBuilder::new()
.condition("not-running")
.build();
let mut responses = self.api.wait_container(container, Some(options));
match responses.next().await {
Some(Ok(response)) => Ok(response.status_code),
Some(Err(Error::DockerContainerWaitError { code, .. })) => Ok(code),
Some(Err(error)) => Err(error),
None => Ok(0),
}
}
async fn logs(&self, container: &str) -> Result<String, Error> {
let options = LogsOptionsBuilder::new().stdout(true).stderr(true).build();
let chunks: Vec<_> = self
.api
.logs(container, Some(options))
.try_collect()
.await?;
Ok(chunks.iter().map(ToString::to_string).collect())
}
async fn logs_since(&self, container: &str, since: u64) -> Result<String, Error> {
let options = LogsOptionsBuilder::new()
.stdout(true)
.stderr(true)
.timestamps(true)
.since(i32::try_from(since).unwrap_or(i32::MAX))
.build();
let chunks: Vec<_> = self
.api
.logs(container, Some(options))
.try_collect()
.await?;
Ok(chunks.iter().map(ToString::to_string).collect())
}
async fn exec(&self, container: &str, command: Vec<String>) -> Result<Output, Error> {
let options = CreateExecOptions {
cmd: Some(command),
user: Some("root".to_owned()),
attach_stdout: Some(true),
attach_stderr: Some(true),
..Default::default()
};
let exec = self.api.create_exec(container, options).await?;
match self.api.start_exec(&exec.id, None).await? {
StartExecResults::Attached { output, .. } => Ok(output
.try_filter_map(|output| {
std::future::ready(Ok(match output {
bollard::container::LogOutput::StdOut { message } => Some(message.to_vec()),
_ => None,
}))
})
.boxed()),
StartExecResults::Detached => Ok(futures_util::stream::empty().boxed()),
}
}
async fn image_present(&self, image: &str) -> Result<bool, Error> {
Ok(self.image_labels(image).await?.is_some())
}
async fn image_skips_syncs(&self, image: &str) -> Result<bool, Error> {
Ok(self
.image_labels(image)
.await?
.is_some_and(|labels| skips_syncs(&labels)))
}
async fn pull(&self, image: &str) -> Result<(), Error> {
let options = CreateImageOptionsBuilder::new().from_image(image).build();
let mut stream = self.api.create_image(Some(options), None, None);
let mut progress = PullProgress::default();
let mut logged = Instant::now();
info!("pulling {image}");
while let Some(update) = stream.try_next().await? {
progress.update(&update);
if logged.elapsed() >= PULL_LOG_INTERVAL {
logged = Instant::now();
info!("pulling {image}: {progress}");
}
}
info!("pulled {image}");
Ok(())
}
async fn ensure_network(&self, name: &str) -> Result<(), Error> {
match self.api.inspect_network(name, None).await {
Err(error) if is_not_found(&error) => {
let request = NetworkCreateRequest {
name: name.to_owned(),
driver: Some("bridge".to_owned()),
..Default::default()
};
self.api.create_network(request).await.map(|_| ())
}
other => other.map(|_| ()),
}
}
async fn ensure_volume(&self, name: &str) -> Result<(), Error> {
let request = VolumeCreateRequest {
name: Some(name.to_owned()),
..Default::default()
};
self.api.create_volume(request).await.map(|_| ())
}
async fn remove_volume(&self, name: &str) -> Result<(), Error> {
let options = RemoveVolumeOptionsBuilder::new().build();
match self.api.remove_volume(name, Some(options)).await {
Err(error) if is_not_found(&error) => Ok(()),
other => other,
}
}
async fn volumes(&self, prefix: &str) -> Result<Vec<String>, Error> {
let options = ListVolumesOptionsBuilder::new()
.filters(&HashMap::from([("name", vec![prefix])]))
.build();
Ok(self
.api
.list_volumes(Some(options))
.await?
.volumes
.unwrap_or_default()
.into_iter()
.map(|volume| volume.name)
.filter(|name| name.starts_with(prefix))
.collect())
}
async fn volume_sizes(&self) -> Result<BTreeMap<String, u64>, Error> {
let options = DataUsageOptionsBuilder::new().verbose(true).build();
let usage = self.api.df(Some(options)).await?;
Ok(usage
.volume_usage
.and_then(|volumes| volumes.items)
.unwrap_or_default()
.into_iter()
.filter_map(|volume| {
let name = volume.get("Name")?.as_str()?.to_owned();
let size = volume.get("UsageData")?.get("Size")?.as_u64()?;
Some((name, size))
})
.collect())
}
async fn changes(&self, container: &str) -> Result<Vec<String>, Error> {
Ok(self
.api
.container_changes(container)
.await?
.unwrap_or_default()
.into_iter()
.filter(|change| change.kind != ChangeType::_2)
.map(|change| change.path)
.collect())
}
async fn written(&self, container: &str) -> Result<u64, Error> {
let options = InspectContainerOptionsBuilder::new().size(true).build();
let response = self.api.inspect_container(container, Some(options)).await?;
Ok(response
.size_rw
.and_then(|bytes| u64::try_from(bytes).ok())
.unwrap_or(0))
}
async fn files(&self, container: &str, directory: &str, prefix: &str) -> Result<Files, Error> {
let options = DownloadFromContainerOptionsBuilder::new()
.path(directory)
.build();
let mut archive = self.api.download_from_container(container, Some(options));
let (chunks, received) = tokio::sync::mpsc::channel(4);
let prefix = prefix.to_owned();
let reader = tokio::task::spawn_blocking(move || {
files_in(
Chunks {
received,
chunk: Vec::new(),
at: 0,
},
&prefix,
)
});
let mut failed = None;
while let Some(chunk) = archive.next().await {
match chunk {
Ok(chunk) => {
if chunks.send(chunk.to_vec()).await.is_err() {
break;
}
}
Err(error) => {
failed = Some(error);
break;
}
}
}
drop(chunks);
let files = match reader.await {
Ok(files) => files,
Err(error) => match error.try_into_panic() {
Ok(panic) => std::panic::resume_unwind(panic),
Err(_) => Files::default(),
},
};
match failed {
Some(error) => Err(error),
None => Ok(files),
}
}
async fn commit(
&self,
container: &str,
reference: &str,
labels: BTreeMap<String, String>,
) -> Result<u64, Error> {
let (repository, tag) = reference.rsplit_once(':').unwrap_or((reference, "latest"));
let options = CommitContainerOptionsBuilder::new()
.container(container)
.repo(repository)
.tag(tag)
.pause(false)
.build();
let config = ContainerConfig {
labels: Some(labels.into_iter().collect()),
..Default::default()
};
let parent = match self.api.inspect_container(container, None).await?.image {
Some(parent) => self.api.inspect_image(&parent).await?.size.unwrap_or(0),
None => 0,
};
let id = self.api.commit_container(options, config).await?.id;
let size = self.api.inspect_image(&id).await?.size.unwrap_or(0);
Ok(u64::try_from(size - parent).unwrap_or(0))
}
async fn images(&self, label: &str) -> Result<Vec<Image>, Error> {
let options = ListImagesOptionsBuilder::new()
.filters(&HashMap::from([("label", vec![label])]))
.build();
let images = self.api.list_images(Some(options)).await?;
let mut parents: HashMap<String, i64> = HashMap::new();
let mut found = Vec::new();
for image in images {
let parent = if image.parent_id.is_empty() {
0
} else if let Some(size) = parents.get(&image.parent_id) {
*size
} else {
let size = self
.api
.inspect_image(&image.parent_id)
.await
.ok()
.and_then(|parent| parent.size)
.unwrap_or(0);
parents.insert(image.parent_id.clone(), size);
size
};
found.push(Image {
reference: image.repo_tags.into_iter().next().unwrap_or(image.id),
labels: image.labels,
bytes: u64::try_from(image.size - parent).unwrap_or(0),
});
}
Ok(found)
}
async fn image_id(&self, reference: &str) -> Result<Option<String>, Error> {
match self.api.inspect_image(reference).await {
Ok(inspect) => Ok(inspect.id),
Err(error) if is_not_found(&error) => Ok(None),
Err(error) => Err(error),
}
}
async fn tagged_images(&self) -> Result<Vec<TaggedImage>, Error> {
let images = self
.api
.list_images(Some(ListImagesOptionsBuilder::new().build()))
.await?;
Ok(images
.into_iter()
.map(|image| TaggedImage {
id: image.id,
tags: image
.repo_tags
.into_iter()
.filter(|tag| tag != "<none>:<none>")
.collect(),
digests: image
.repo_digests
.into_iter()
.filter(|digest| digest != "<none>@<none>")
.collect(),
})
.filter(|image| !image.tags.is_empty())
.collect())
}
async fn container_images(&self) -> Result<BTreeSet<String>, Error> {
let options = ListContainersOptionsBuilder::new().all(true).build();
let containers = self.api.list_containers(Some(options)).await?;
Ok(containers
.into_iter()
.filter_map(|container| container.image_id)
.collect())
}
async fn remove_image(&self, reference: &str) -> Result<(), Error> {
let options = RemoveImageOptionsBuilder::new().build();
match self.api.remove_image(reference, Some(options), None).await {
Err(error) if is_not_found(&error) => Ok(()),
other => other.map(|_| ()),
}
}
async fn root_dir(&self) -> Result<String, Error> {
Ok(self.api.info().await?.docker_root_dir.unwrap_or_default())
}
async fn free_space(&self) -> Result<u64, Error> {
let root = self.api.info().await?.docker_root_dir.unwrap_or_default();
let statfs = |path: &std::path::Path| {
let stat = rustix::fs::statvfs(path).map_err(std::io::Error::from)?;
Ok(stat.f_bavail.saturating_mul(stat.f_frsize))
};
let paths = crate::observe::metrics::Paths::default();
crate::observe::disk::free_space(&paths, std::path::Path::new(&root), statfs).map_err(
|error| Error::IOError {
err: std::io::Error::other(format!("cannot measure the disk of {root}: {error}")),
},
)
}
async fn shell(
&self,
name: &str,
body: ContainerCreateBody,
size: TtySize,
) -> Result<Tty, Error> {
let options = CreateContainerOptionsBuilder::new().name(name).build();
let id = self.api.create_container(Some(options), body).await?.id;
let attached = async {
let options = AttachContainerOptionsBuilder::new()
.stream(true)
.stdin(true)
.stdout(true)
.stderr(true)
.build();
let attached = self.api.attach_container(&id, Some(options)).await?;
self.api.start_container(&id, None).await?;
let resize = ResizeContainerTTYOptionsBuilder::new()
.w(i32::from(size.cols))
.h(i32::from(size.rows))
.build();
self.api.resize_container_tty(&id, resize).await?;
Ok::<_, Error>(attached)
};
match attached.await {
Ok(attached) => Ok(Tty {
container: id,
output: attached
.output
.map_ok(|output| output.into_bytes().to_vec())
.boxed(),
input: attached.input,
}),
Err(error) => {
let _ = self.remove(&id).await;
Err(error)
}
}
}
}
const PULL_LOG_INTERVAL: Duration = Duration::from_secs(10);
#[derive(Debug, Default)]
struct PullProgress {
layers: BTreeMap<String, Layer>,
}
#[derive(Debug, Default)]
struct Layer {
downloaded: u64,
size: u64,
done: bool,
}
impl PullProgress {
fn update(&mut self, update: &CreateImageInfo) {
let (Some(id), Some(status)) = (&update.id, &update.status) else {
return;
};
let bytes = |value: Option<i64>| value.and_then(|value| u64::try_from(value).ok());
let detail = update.progress_detail.as_ref();
match status.as_str() {
"Pulling fs layer" | "Waiting" => {
self.layers.entry(id.clone()).or_default();
}
"Downloading" => {
let layer = self.layers.entry(id.clone()).or_default();
layer.downloaded =
bytes(detail.and_then(|detail| detail.current)).unwrap_or(layer.downloaded);
layer.size = bytes(detail.and_then(|detail| detail.total)).unwrap_or(layer.size);
}
"Download complete" => {
if let Some(layer) = self.layers.get_mut(id) {
layer.downloaded = layer.size;
}
}
"Pull complete" | "Already exists" => {
let layer = self.layers.entry(id.clone()).or_default();
layer.downloaded = layer.size;
layer.done = true;
}
_ => {}
}
}
}
impl fmt::Display for PullProgress {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let done = self.layers.values().filter(|layer| layer.done).count();
let downloaded: u64 = self.layers.values().map(|layer| layer.downloaded).sum();
let size: u64 = self.layers.values().map(|layer| layer.size).sum();
write!(
f,
"{done} of {} layers done, {} of {} downloaded",
self.layers.len(),
ByteSize(downloaded),
ByteSize(size)
)
}
}
fn connect_to(docker_host: Option<&str>) -> Result<Docker, Error> {
let api = match docker_host.filter(|host| !host.is_empty()) {
None => bollard::Docker::connect_with_socket_defaults()?,
Some(host) if host.starts_with("unix://") => {
bollard::Docker::connect_with_socket(host, 120, bollard::API_DEFAULT_VERSION)?
}
Some(host) => {
return Err(Error::UnsupportedURISchemeError {
uri: host.to_owned(),
});
}
};
Ok(Docker { api })
}
struct Chunks {
received: tokio::sync::mpsc::Receiver<Vec<u8>>,
chunk: Vec<u8>,
at: usize,
}
impl std::io::Read for Chunks {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
while self.at == self.chunk.len() {
match self.received.blocking_recv() {
Some(chunk) => {
self.chunk = chunk;
self.at = 0;
}
None => return Ok(0),
}
}
let n = buf.len().min(self.chunk.len() - self.at);
buf[..n].copy_from_slice(&self.chunk[self.at..self.at + n]);
self.at += n;
Ok(n)
}
}
struct Ends<R> {
inner: R,
ran_out: bool,
}
impl<R: std::io::Read> std::io::Read for Ends<R> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let n = self.inner.read(buf)?;
self.ran_out = n == 0 && !buf.is_empty();
Ok(n)
}
}
pub(crate) fn files_in(tar: impl std::io::Read, prefix: &str) -> Files {
let mut tar = Ends {
inner: tar,
ran_out: false,
};
let mut read = Files::default();
let ended = files_until_end(&mut tar, prefix, &mut read.files);
read.complete = ended && !tar.ran_out;
read
}
fn files_until_end(tar: impl std::io::Read, prefix: &str, files: &mut Vec<File>) -> bool {
let mut archive = tar::Archive::new(tar);
let Ok(entries) = archive.entries() else {
return false;
};
let mut kept = 0;
for entry in entries {
let Ok(mut entry) = entry else {
return false;
};
let name = match entry.path() {
Ok(path) if path.components().count() == 2 => path
.file_name()
.map(|name| name.to_string_lossy().into_owned()),
_ => None,
};
let Some(name) =
name.filter(|name| name.starts_with(prefix) && entry.header().entry_type().is_file())
else {
continue;
};
let room = FILES_LIMIT - kept;
let mut contents = Vec::new();
let whole = std::io::Read::read_to_end(
&mut std::io::Read::take(&mut entry, room as u64 + 1),
&mut contents,
)
.is_ok()
&& contents.len() <= room;
contents.truncate(room);
kept += contents.len();
files.push(File {
name,
contents: String::from_utf8_lossy(&contents).into_owned(),
});
if !whole {
return false;
}
}
true
}
fn jit_config_tar(jit_config: &JitConfig) -> std::io::Result<Vec<u8>> {
let mut tar = tar::Builder::new(Vec::new());
let path = JIT_CONFIG_PATH.trim_start_matches('/');
let mut dir = tar::Header::new_gnu();
dir.set_entry_type(tar::EntryType::Directory);
dir.set_mode(0o700);
dir.set_size(0);
tar.append_data(
&mut dir,
path.rsplit_once('/').map_or("", |(dir, _)| dir),
std::io::empty(),
)?;
let mut file = tar::Header::new_gnu();
file.set_mode(0o400);
file.set_size(jit_config.0.len() as u64);
tar.append_data(&mut file, path, jit_config.0.as_bytes())?;
tar.into_inner()
}
#[cfg(test)]
pub(crate) mod tests;