Test runner: shared base image for acceptance containers, log installed apt packages
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
23fbc6f302
commit
d0e7cea117
9 files changed
+50
-10
No files matched your search
@@ -34,6 +34,8 @@ const CACHE_LABEL: &str = "codecraft.testrunner.cache=1";
|
||||
|
||||
/// What every environment container runs: link the shared directories, restore a matching seed, run
|
||||
/// `install`, store a new seed (only after a successful install, so it is pristine), start the app.
|
||||
const FALLBACK_IMAGE: &str = "node:24-bookworm";
|
||||
|
||||
const ENTRY_SCRIPT: &str = r#"cd /app
|
||||
hit=0
|
||||
if [ -d /cache ]; then
|
||||
@@ -42,9 +44,11 @@ if [ -d /cache ]; then
|
||||
if [ -n "$CC_SEED_KEY" ]; then for p in $CC_SEED_PATHS; do if [ -e "/cache/seed/$CC_SEED_KEY/$p" ]; then mkdir -p "$(dirname "$p")" && cp -a "/cache/seed/$CC_SEED_KEY/$p" "$p" && hit=1 && echo "[runner] restored $p from the cache"; fi; done; fi
|
||||
fi
|
||||
if [ -n "$CC_SEED_KEY" ]; then if [ "$hit" = 1 ]; then echo "[runner] seed $CC_SEED_KEY: hit"; else echo "[runner] seed $CC_SEED_KEY: miss"; fi; fi
|
||||
t0=$(date +%s)
|
||||
t0=$(date +%s); st=$(date -u '+%Y-%m-%d %H:%M:%S')
|
||||
if [ -n "$CC_INSTALL" ]; then sh -c "$CC_INSTALL" || exit 1; fi
|
||||
echo "[runner] install took $(( $(date +%s) - t0 )) s"
|
||||
pk=$(awk -v s="$st" '$3=="install" && ($1" "$2)>=s {sub(/:.*/,"",$4); print $4}' /var/log/dpkg.log 2>/dev/null | sort -u | head -40 | tr '\n' ' ')
|
||||
if [ -n "$pk" ]; then echo "[runner] apt installed: $pk"; fi
|
||||
if [ -d /cache ] && [ -n "$CC_SEED_KEY" ] && [ ! -d "/cache/seed/$CC_SEED_KEY" ]; then
|
||||
t="/cache/seed/.tmp-$$"; rm -rf "$t"; mkdir -p "$t"; ok=1
|
||||
for p in $CC_SEED_PATHS; do if [ -e "$p" ]; then mkdir -p "$t/$(dirname "$p")" && cp -a "$p" "$t/$p" || ok=0; fi; done
|
||||
@@ -355,7 +359,7 @@ pub async fn serve() -> Result<()> {
|
||||
ports: (lo, hi),
|
||||
memory: std::env::var("RUNNER_MEMORY").unwrap_or_else(|_| "2g".into()),
|
||||
cpus: std::env::var("RUNNER_CPUS").unwrap_or_else(|_| "2".into()),
|
||||
default_image: std::env::var("RUNNER_DEFAULT_IMAGE").unwrap_or_else(|_| "node:22-bookworm".into()),
|
||||
default_image: std::env::var("RUNNER_DEFAULT_IMAGE").unwrap_or_else(|_| "git.familie-zink.org/zink/codecraft-testbase:latest".into()),
|
||||
max_age: Duration::from_secs(60 * std::env::var("RUNNER_MAX_MINUTES").ok().and_then(|s| s.parse().ok()).unwrap_or(90)),
|
||||
me: std::env::var("RUNNER_SELF").or_else(|_| std::env::var("HOSTNAME")).unwrap_or_default(),
|
||||
envs: Mutex::new(HashMap::new()),
|
||||
@@ -458,7 +462,7 @@ async fn create(r: &Arc<Runner>, id: &str, req: &StartReq, work: &Path) -> Resul
|
||||
r.envs.lock().unwrap().get_mut(id).unwrap().containers.push(cname);
|
||||
}
|
||||
let image = if req.dockerfile.is_empty() {
|
||||
if req.image.is_empty() { r.default_image.clone() } else { req.image.clone() }
|
||||
if req.image.is_empty() { default_image_or_fallback(&r.default_image).await } else { req.image.clone() }
|
||||
} else {
|
||||
let tag = format!("cc-run-{id}");
|
||||
let df = work.join(&req.dockerfile);
|
||||
@@ -548,7 +552,7 @@ async fn create(r: &Arc<Runner>, id: &str, req: &StartReq, work: &Path) -> Resul
|
||||
tokio::time::sleep(Duration::from_millis(700)).await;
|
||||
}
|
||||
// Report what the cache brought: seed hit/miss, install time, total time until the app answered.
|
||||
let facts: Vec<String> = logs(&app).await.lines().filter(|l| l.starts_with("[runner] seed ") || l.starts_with("[runner] install took")).map(|l| l.trim_start_matches("[runner] ").to_string()).collect();
|
||||
let facts: Vec<String> = logs(&app).await.lines().filter(|l| l.starts_with("[runner] seed ") || l.starts_with("[runner] install took") || l.starts_with("[runner] apt installed")).map(|l| l.trim_start_matches("[runner] ").to_string()).collect();
|
||||
let took = began.elapsed().as_secs();
|
||||
let cache = if cached { "on" } else { "off" };
|
||||
tracing::info!("test env {id} ({}) ready after {took} s; cache {cache}{}{}", req.cache_id, if facts.is_empty() { "" } else { "; " }, facts.join("; "));
|
||||
@@ -572,6 +576,15 @@ async fn create(r: &Arc<Runner>, id: &str, req: &StartReq, work: &Path) -> Resul
|
||||
Ok(Started { id: id.to_string(), port })
|
||||
}
|
||||
|
||||
/// The shared base image; when it can neither be found nor pulled, fall back to plain Node so runs still work.
|
||||
async fn default_image_or_fallback(img: &str) -> String {
|
||||
if docker(&["image", "inspect", img]).await.is_ok() || docker(&["pull", "-q", img]).await.is_ok() {
|
||||
return img.to_string();
|
||||
}
|
||||
tracing::warn!("test runner: base image {img} is not available, using {FALLBACK_IMAGE}");
|
||||
FALLBACK_IMAGE.to_string()
|
||||
}
|
||||
|
||||
async fn logs(container: &str) -> String {
|
||||
match Command::new("docker").args(["logs", "--tail", "400", container]).output().await {
|
||||
Ok(o) => format!("{}{}", String::from_utf8_lossy(&o.stdout), String::from_utf8_lossy(&o.stderr)),
|
||||
|
||||
Reference in new issue
Block a user