|
|
@@ -1,9 +1,10 @@
|
|
|
|
use crate::models::{ServerMessage};
|
|
|
|
use crate::models::{ServerMessage};
|
|
|
|
|
|
|
|
|
|
|
|
use std::error::Error;
|
|
|
|
use std::error::Error;
|
|
|
|
|
|
|
|
use std::num::NonZero;
|
|
|
|
|
|
|
|
|
|
|
|
use bollard::Docker;
|
|
|
|
use bollard::Docker;
|
|
|
|
use bollard::query_parameters::{CreateImageOptions, RestartContainerOptions, InspectContainerOptions};
|
|
|
|
use bollard::query_parameters::{CreateImageOptions, RestartContainerOptions, InspectContainerOptions, ListContainersOptions};
|
|
|
|
use futures_util::StreamExt;
|
|
|
|
use futures_util::StreamExt;
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn handle_server_message(docker: &Docker, msg: ServerMessage) -> Result<(), Box<dyn Error + Send + Sync>> {
|
|
|
|
pub async fn handle_server_message(docker: &Docker, msg: ServerMessage) -> Result<(), Box<dyn Error + Send + Sync>> {
|
|
|
@@ -75,18 +76,28 @@ 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>> {
|
|
|
|
pub async fn get_current_image(docker: &Docker) -> Result<Option<String>, Box<dyn Error + Send + Sync>> {
|
|
|
|
|
|
|
|
// First, let's debug the environment
|
|
|
|
|
|
|
|
debug_docker_environment(docker).await;
|
|
|
|
|
|
|
|
|
|
|
|
// Get the current container ID from /proc/self/cgroup
|
|
|
|
// Get the current container ID from /proc/self/cgroup
|
|
|
|
let container_id = match std::fs::read_to_string("/proc/self/cgroup") {
|
|
|
|
let container_id = match std::fs::read_to_string("/proc/self/cgroup") {
|
|
|
|
Ok(content) => {
|
|
|
|
Ok(content) => {
|
|
|
|
content
|
|
|
|
let mut found_id = None;
|
|
|
|
.lines()
|
|
|
|
println!("Searching cgroup for container ID...");
|
|
|
|
.find_map(|line| {
|
|
|
|
|
|
|
|
if line.contains("docker") {
|
|
|
|
for line in content.lines() {
|
|
|
|
line.split('/').last().map(|s| s.trim().to_string())
|
|
|
|
println!("Checking line: {}", line);
|
|
|
|
} else {
|
|
|
|
|
|
|
|
None
|
|
|
|
// Look for container runtime indicators
|
|
|
|
|
|
|
|
if line.contains("docker") || line.contains("crio") || line.contains("containerd") {
|
|
|
|
|
|
|
|
if let Some(id) = extract_container_id(line) {
|
|
|
|
|
|
|
|
println!("Found potential container ID: {}", id);
|
|
|
|
|
|
|
|
found_id = Some(id);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
found_id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!("Error reading cgroup file: {}", e);
|
|
|
|
eprintln!("Error reading cgroup file: {}", e);
|
|
|
@@ -95,17 +106,29 @@ pub async fn get_current_image(docker: &Docker) -> Result<Option<String>, Box<dy
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let container_id = match container_id {
|
|
|
|
let container_id = match container_id {
|
|
|
|
Some(id) => id,
|
|
|
|
Some(id) if !id.is_empty() => {
|
|
|
|
None => {
|
|
|
|
println!("Using container ID: '{}'", id);
|
|
|
|
eprintln!("Could not find container ID in cgroup");
|
|
|
|
id
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {
|
|
|
|
|
|
|
|
eprintln!("Could not find valid container ID in cgroup");
|
|
|
|
return Ok(None);
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Inspect the current container to get its image
|
|
|
|
// Try to inspect the container
|
|
|
|
|
|
|
|
println!("Attempting to inspect container with ID: '{}'", container_id);
|
|
|
|
|
|
|
|
|
|
|
|
match docker.inspect_container(&container_id, None::<InspectContainerOptions>).await {
|
|
|
|
match docker.inspect_container(&container_id, None::<InspectContainerOptions>).await {
|
|
|
|
Ok(container_info) => {
|
|
|
|
Ok(container_info) => {
|
|
|
|
Ok(container_info.config.map(|config| config.image.unwrap_or_else(|| "unknown".to_string())))
|
|
|
|
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");
|
|
|
|
|
|
|
|
Ok(Some("unknown".to_string()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!("Error inspecting container: {}", e);
|
|
|
|
eprintln!("Error inspecting container: {}", e);
|
|
|
@@ -114,6 +137,90 @@ pub async fn get_current_image(docker: &Docker) -> Result<Option<String>, Box<dy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn extract_container_id(line: &str) -> Option<String> {
|
|
|
|
|
|
|
|
// Split by slashes and take the last part
|
|
|
|
|
|
|
|
if let Some(last_part) = line.split('/').last() {
|
|
|
|
|
|
|
|
let last_part = last_part.trim();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Remove common suffixes
|
|
|
|
|
|
|
|
let clean_id = last_part
|
|
|
|
|
|
|
|
.trim_end_matches(".scope")
|
|
|
|
|
|
|
|
.trim_start_matches("docker-")
|
|
|
|
|
|
|
|
.trim_start_matches("crio-")
|
|
|
|
|
|
|
|
.trim_start_matches("containerd-");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check if it looks like a container ID (hex characters)
|
|
|
|
|
|
|
|
if clean_id.chars().all(|c| c.is_ascii_hexdigit()) && clean_id.len() >= 12 {
|
|
|
|
|
|
|
|
return Some(clean_id.to_string());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If it's not pure hex, try to extract hex sequence
|
|
|
|
|
|
|
|
let hex_part: String = clean_id.chars()
|
|
|
|
|
|
|
|
.take_while(|c| c.is_ascii_hexdigit())
|
|
|
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if hex_part.len() >= 12 {
|
|
|
|
|
|
|
|
return Some(hex_part);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
None
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add this function to debug the Docker connection and environment
|
|
|
|
|
|
|
|
async fn debug_docker_environment(docker: &Docker) {
|
|
|
|
|
|
|
|
println!("=== DOCKER ENVIRONMENT DEBUG ===");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Test basic Docker connection
|
|
|
|
|
|
|
|
match docker.version().await {
|
|
|
|
|
|
|
|
Ok(version) => {
|
|
|
|
|
|
|
|
println!("Docker version: {:?}", version);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(e) => {
|
|
|
|
|
|
|
|
eprintln!("Failed to get Docker version: {}", e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// List containers to see what's available - CORRECTED
|
|
|
|
|
|
|
|
let options = Some(ListContainersOptions {
|
|
|
|
|
|
|
|
all: true, // include stopped containers
|
|
|
|
|
|
|
|
..Default::default()
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
match docker.list_containers(options).await {
|
|
|
|
|
|
|
|
Ok(containers) => {
|
|
|
|
|
|
|
|
println!("Available containers ({}):", containers.len());
|
|
|
|
|
|
|
|
for container in containers {
|
|
|
|
|
|
|
|
if let Some(id) = container.id {
|
|
|
|
|
|
|
|
let short_id = if id.len() > 12 { &id[..12] } else { &id };
|
|
|
|
|
|
|
|
println!(" - ID: {}, Image: {:?}", short_id, container.image);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Also print the names for easier identification
|
|
|
|
|
|
|
|
if let Some(names) = container.names {
|
|
|
|
|
|
|
|
println!(" Names: {:?}", names);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(e) => {
|
|
|
|
|
|
|
|
eprintln!("Failed to list containers: {}", e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check if we're actually running in a container
|
|
|
|
|
|
|
|
if let Ok(content) = std::fs::read_to_string("/proc/self/cgroup") {
|
|
|
|
|
|
|
|
println!("Cgroup contents:");
|
|
|
|
|
|
|
|
println!("{}", content);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check other container indicators
|
|
|
|
|
|
|
|
if std::path::Path::new("/.dockerenv").exists() {
|
|
|
|
|
|
|
|
println!("/.dockerenv exists - running in Docker container");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
println!("/.dockerenv does not exist");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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>> {
|
|
|
|
if let Ok(container_id) = std::env::var("HOSTNAME") {
|
|
|
|
if let Ok(container_id) = std::env::var("HOSTNAME") {
|
|
|
|
println!("Restarting container {}", container_id);
|
|
|
|
println!("Restarting container {}", container_id);
|
|
|
|