Skip to main content

agentmux_srv_core/backend/container/pull/
pinned.rs

1//! The default agent image, run by the digest the release's signed manifest names
2//! (`backend::agent_image_digest`).
3
4use bollard::image::TagImageOptions;
5
6use super::{ContainerError, ContainerManager, StartObserver};
7
8/// `repo` and `tag` of an image reference `repo:tag`; a registry's `:port` is not a tag.
9fn split_tag(image: &str) -> Option<(&str, &str)> {
10    let (repo, tag) = image.rsplit_once(':')?;
11    (!tag.contains('/') && !repo.is_empty() && !tag.is_empty()).then_some((repo, tag))
12}
13
14impl ContainerManager {
15    /// Make the local tag `image` (`repo:version`) the image at `reference` (`repo@sha256:...`),
16    /// pulling it if it isn't here. Whatever the tag held before, a pushed or stale image, is
17    /// replaced, so the container is created from the signed one. There is no fall back to a
18    /// tag: the point is that no tag is trusted.
19    pub(super) async fn pull_pinned(&self, image: &str, reference: &str, observe: StartObserver<'_>) -> Result<(), ContainerError> {
20        let holds = |digests: Option<Vec<String>>| digests.is_some_and(|d| d.iter().any(|r| r == reference));
21        if let Ok(local) = self.inner.docker.inspect_image(image).await {
22            if holds(local.repo_digests) {
23                tracing::info!(image, reference, "the local agent image is the signed one");
24                return Ok(());
25            }
26        }
27        self.pull_image(reference, observe).await?;
28        let (repo, tag) = split_tag(image).ok_or_else(|| ContainerError::ImagePull {
29            image: image.to_string(),
30            kind: crate::backend::container::image::PullFailure::Other,
31            detail: "not a repository and a tag".into(),
32        })?;
33        self.inner.docker.tag_image(reference, Some(TagImageOptions { repo, tag })).await?;
34        tracing::info!(image, reference, "the agent image is the signed one, tagged locally");
35        Ok(())
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42
43    #[test]
44    fn a_tag_is_split_from_its_repository_and_a_port_is_not_a_tag() {
45        assert_eq!(split_tag("ghcr.io/agentmuxai/agent-base:0.60.3"), Some(("ghcr.io/agentmuxai/agent-base", "0.60.3")));
46        assert_eq!(split_tag("localhost:5000/base:1"), Some(("localhost:5000/base", "1")));
47        assert_eq!(split_tag("localhost:5000/base"), None);
48        assert_eq!(split_tag("base"), None);
49    }
50
51    /// Against a real daemon, with an image it already has (no registry): the tag is moved onto the
52    /// image the reference names.
53    ///   cargo test -p agentmux-srv-core --lib -- --ignored --nocapture itest_pinned
54    #[tokio::test]
55    #[ignore]
56    async fn itest_pinned_tag_is_moved_onto_the_image_the_reference_names() {
57        let cm = ContainerManager::connect().expect("connect to docker (set DOCKER_HOST)");
58        cm.check_available().await.expect("docker daemon must be reachable");
59        let docker = &cm.inner.docker;
60        // Any small local image with a repo digest; the base image's own, which every host here has.
61        let source = "ghcr.io/agentmuxai/agent-base:latest";
62        let local = docker.inspect_image(source).await.expect("the base image is pulled on this machine");
63        let reference = local.repo_digests.and_then(|d| d.into_iter().next()).expect("a pulled image has a repo digest");
64        let tag = "agentmux-itest/pinned:1";
65        let _ = docker.remove_image(tag, None::<bollard::image::RemoveImageOptions>, None).await;
66
67        cm.pull_pinned(tag, &reference, &|_| {}).await.expect("a local reference needs no registry");
68        let tagged = docker.inspect_image(tag).await.expect("the tag exists");
69        assert_eq!(tagged.id, local.id, "the tag names the image the digest does");
70
71        // Asked again, it is already the signed image.
72        cm.pull_pinned(tag, &reference, &|_| {}).await.expect("idempotent");
73        let _ = docker.remove_image(tag, None::<bollard::image::RemoveImageOptions>, None).await;
74    }
75}