2 Commits

Author SHA1 Message Date
8bac357dc6 added debugging for docker image
All checks were successful
Rust Cross-Platform Build / Detect Rust Project (push) Successful in 5s
Rust Cross-Platform Build / Set Tag Name (push) Successful in 5s
Rust Cross-Platform Build / Run Tests (push) Successful in 1m10s
Rust Cross-Platform Build / Build (x86_64-unknown-linux-gnu) (push) Successful in 3m9s
Rust Cross-Platform Build / Build (x86_64-pc-windows-gnu) (push) Successful in 3m54s
Rust Cross-Platform Build / Build and Push Docker Image (push) Successful in 2m12s
Rust Cross-Platform Build / Workflow Summary (push) Successful in 1s
Rust Cross-Platform Build / Create Tag (push) Successful in 4s
2025-09-28 00:36:52 +02:00
7154c01f7a added search for docker image in different locations
All checks were successful
Rust Cross-Platform Build / Detect Rust Project (push) Successful in 4s
Rust Cross-Platform Build / Set Tag Name (push) Successful in 5s
Rust Cross-Platform Build / Run Tests (push) Successful in 1m6s
Rust Cross-Platform Build / Build (x86_64-unknown-linux-gnu) (push) Successful in 2m45s
Rust Cross-Platform Build / Build (x86_64-pc-windows-gnu) (push) Successful in 3m30s
Rust Cross-Platform Build / Build and Push Docker Image (push) Successful in 2m19s
Rust Cross-Platform Build / Workflow Summary (push) Successful in 2s
Rust Cross-Platform Build / Create Tag (push) Successful in 5s
2025-09-28 00:24:47 +02:00
2 changed files with 112 additions and 13 deletions

View File

@@ -19,6 +19,8 @@ nvml-wrapper = "0.11"
nvml-wrapper-sys = "0.9.0" nvml-wrapper-sys = "0.9.0"
anyhow = "1.0.98" anyhow = "1.0.98"
regex = "1.11.3"
# Docker .env loading # Docker .env loading
# config = "0.13" # config = "0.13"

View File

@@ -81,8 +81,10 @@ pub async fn get_current_image(docker: &Docker) -> Result<Option<String>, Box<dy
content content
.lines() .lines()
.find_map(|line| { .find_map(|line| {
if line.contains("docker") { // Look for any line that might contain container information
line.split('/').last().map(|s| s.trim().to_string()) if line.contains("docker") || line.contains("crio") || line.contains("containerd") || line.contains("kubepods") {
// Extract the container ID from the line
extract_container_id(line)
} else { } else {
None None
} }
@@ -94,24 +96,119 @@ pub async fn get_current_image(docker: &Docker) -> Result<Option<String>, Box<dy
} }
}; };
// Debug: Print what we found
println!("Container ID search result: {:?}", container_id);
let container_id = match container_id { let container_id = match container_id {
Some(id) => id, Some(id) if !id.is_empty() => {
None => { println!("Found container ID: '{}'", id);
eprintln!("Could not find container ID in cgroup"); id
}
_ => {
// Debug the cgroup file content
if let Ok(content) = std::fs::read_to_string("/proc/self/cgroup") {
eprintln!("Cgroup file content for debugging:");
for (i, line) in content.lines().enumerate() {
eprintln!("Line {}: {}", i, line);
}
}
eprintln!("Could not find valid container ID in cgroup");
return Ok(None); return Ok(None);
} }
}; };
// Inspect the current container to get its image // Clean up the container ID - remove any non-hex characters
match docker.inspect_container(&container_id, None::<InspectContainerOptions>).await { let clean_container_id: String = container_id
Ok(container_info) => { .chars()
Ok(container_info.config.map(|config| config.image.unwrap_or_else(|| "unknown".to_string()))) .filter(|c| c.is_ascii_hexdigit())
} .collect();
Err(e) => {
eprintln!("Error inspecting container: {}", e); // Validate the container ID (should be 64 characters for full ID, but short ID might work too)
Ok(None) if clean_container_id.is_empty() {
eprintln!("Container ID contains no hex characters: '{}'", container_id);
return Ok(None);
}
println!("Using container ID: '{}' (cleaned: '{}')", container_id, clean_container_id);
// Try with both the original and cleaned ID
let ids_to_try = vec![&clean_container_id, &container_id];
for id in ids_to_try {
println!("Attempting to inspect container with ID: '{}'", id);
match docker.inspect_container(id, None::<InspectContainerOptions>).await {
Ok(container_info) => {
if let Some(config) = container_info.config {
if let Some(image) = config.image {
println!("Successfully found image: {}", image);
return Ok(Some(image));
}
}
eprintln!("Container inspected but no image found in config");
return Ok(Some("unknown".to_string()));
}
Err(e) => {
eprintln!("Error inspecting container with ID '{}': {}", id, e);
// Continue to try the next ID
}
} }
} }
eprintln!("All attempts to inspect container failed");
Ok(None)
}
fn extract_container_id(line: &str) -> Option<String> {
let parts: Vec<&str> = line.split('/').collect();
if let Some(last_part) = parts.last() {
let last_part = last_part.trim();
println!("Processing cgroup line part: '{}'", last_part);
// Common patterns for container IDs in cgroups:
// Pattern 1: Full container ID (64-character hex)
if last_part.len() == 64 && last_part.chars().all(|c| c.is_ascii_hexdigit()) {
println!("Found full container ID: {}", last_part);
return Some(last_part.to_string());
}
// Pattern 2: docker-<short_id>.scope
if last_part.starts_with("docker-") && last_part.ends_with(".scope") {
let id = last_part
.trim_start_matches("docker-")
.trim_end_matches(".scope")
.to_string();
println!("Found docker scope ID: {}", id);
return Some(id);
}
// Pattern 3: <short_id> (12-character hex or similar)
if last_part.chars().all(|c| c.is_ascii_hexdigit()) && last_part.len() >= 12 {
println!("Found hex ID: {}", last_part);
return Some(last_part.to_string());
}
// Pattern 4: Try to extract ID from any string that looks like it contains a hex ID
if let Some(caps) = regex::Regex::new(r"[a-fA-F0-9]{12,64}")
.ok()
.and_then(|re| re.find(last_part))
{
let id = caps.as_str().to_string();
println!("Extracted ID via regex: {}", id);
return Some(id);
}
// Pattern 5: If nothing else matches, try the last part as-is
if !last_part.is_empty() {
println!("Using last part as ID: {}", last_part);
return Some(last_part.to_string());
}
}
None
} }
pub async fn restart_container(docker: &Docker) -> Result<(), Box<dyn Error + Send + Sync>> { pub async fn restart_container(docker: &Docker) -> Result<(), Box<dyn Error + Send + Sync>> {