Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
8bac357dc6 | |||
7154c01f7a |
@@ -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"
|
||||||
|
|
||||||
|
@@ -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>> {
|
||||||
|
Reference in New Issue
Block a user