1 Commits

Author SHA1 Message Date
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

View File

@@ -75,29 +75,16 @@ pub async fn update_docker_image(docker: &Docker, image: &str) -> Result<(), Box
}
pub async fn get_current_image(docker: &Docker) -> Result<Option<String>, Box<dyn Error + Send + Sync>> {
// Get the current container ID from /proc/self/cgroup
let container_id = match std::fs::read_to_string("/proc/self/cgroup") {
Ok(content) => {
content
.lines()
.find_map(|line| {
if line.contains("docker") {
line.split('/').last().map(|s| s.trim().to_string())
} else {
None
}
})
}
Err(e) => {
eprintln!("Error reading cgroup file: {}", e);
return Ok(None);
}
};
// Try multiple methods to get container ID
let container_id = get_container_id().await;
let container_id = match container_id {
Some(id) => id,
Some(id) => {
println!("Found container ID: {}", id);
id
}
None => {
eprintln!("Could not find container ID in cgroup");
eprintln!("Could not determine container ID");
return Ok(None);
}
};
@@ -114,6 +101,74 @@ pub async fn get_current_image(docker: &Docker) -> Result<Option<String>, Box<dy
}
}
async fn get_container_id() -> Option<String> {
// Method 1: Try /proc/self/cgroup with various patterns
if let Ok(content) = std::fs::read_to_string("/proc/self/cgroup") {
for line in content.lines() {
// Try different container runtime identifiers
let patterns = ["docker", "crio", "containerd", "kubepods"];
if patterns.iter().any(|&p| line.contains(p)) {
if let Some(id) = extract_container_id(line) {
return Some(id);
}
}
}
}
// Method 2: Try /proc/self/mountinfo
if let Ok(content) = std::fs::read_to_string("/proc/self/mountinfo") {
for line in content.lines() {
if line.contains("/docker/containers/") {
if let Some(start) = line.find("/docker/containers/") {
let rest = &line[start + 18..]; // 18 = len("/docker/containers/")
if let Some(end) = rest.find('/') {
return Some(rest[..end].to_string());
}
}
}
}
}
// Method 3: Try hostname (works if container ID is used as hostname)
if let Ok(hostname) = std::fs::read_to_string("/etc/hostname") {
let hostname = hostname.trim();
// Container IDs are typically 64-character hex strings
if hostname.len() == 64 && hostname.chars().all(|c| c.is_ascii_hexdigit()) {
return Some(hostname.to_string());
}
}
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();
// Pattern 1: docker-<id>.scope
if last_part.starts_with("docker-") && last_part.ends_with(".scope") {
return Some(last_part
.trim_start_matches("docker-")
.trim_end_matches(".scope")
.to_string());
}
// Pattern 2: <id> (64-character hex)
if last_part.len() == 64 && last_part.chars().all(|c| c.is_ascii_hexdigit()) {
return Some(last_part.to_string());
}
// Pattern 3: Just take the last part as fallback
if !last_part.is_empty() {
return Some(last_part.to_string());
}
}
None
}
pub async fn restart_container(docker: &Docker) -> Result<(), Box<dyn Error + Send + Sync>> {
if let Ok(container_id) = std::env::var("HOSTNAME") {
println!("Restarting container {}", container_id);