Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
1f23c303c1 | |||
1cc85bfa14 | |||
8bac357dc6 |
@@ -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"
|
||||||
|
|
||||||
|
@@ -37,28 +37,12 @@ pub async fn get_disk_info() -> Result<DiskInfo, Box<dyn std::error::Error + Sen
|
|||||||
component_disk_label: String::new(),
|
component_disk_label: String::new(),
|
||||||
component_disk_temperature: 0.0,
|
component_disk_temperature: 0.0,
|
||||||
});
|
});
|
||||||
|
|
||||||
println!(
|
|
||||||
"Disk_Name: {:?}:\n---- Disk_Kind: {},\n---- Total: {},\n---- Available: {},\n---- Used: {}, \n---- Mount_Point: {:?}",
|
|
||||||
disk.name(),
|
|
||||||
disk.kind(),
|
|
||||||
disk.total_space(),
|
|
||||||
disk.available_space(),
|
|
||||||
disk_used,
|
|
||||||
disk.mount_point()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get component temperatures
|
// Get component temperatures
|
||||||
let components = Components::new_with_refreshed_list();
|
let components = Components::new_with_refreshed_list();
|
||||||
for component in &components {
|
for component in &components {
|
||||||
if let Some(temperature) = component.temperature() {
|
if let Some(temperature) = component.temperature() {
|
||||||
println!(
|
|
||||||
"Component_Label: {}, Temperature: {}°C",
|
|
||||||
component.label(),
|
|
||||||
temperature
|
|
||||||
);
|
|
||||||
|
|
||||||
// Update detailed info with temperature data if it matches a disk component
|
// Update detailed info with temperature data if it matches a disk component
|
||||||
for disk_info in &mut detailed_info {
|
for disk_info in &mut detailed_info {
|
||||||
if component.label().contains(&disk_info.disk_name) {
|
if component.label().contains(&disk_info.disk_name) {
|
||||||
|
@@ -1,9 +1,8 @@
|
|||||||
use crate::models::{ServerMessage};
|
use crate::models::{ServerMessage};
|
||||||
|
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
||||||
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,24 +74,59 @@ 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>> {
|
||||||
// Try multiple methods to get container ID
|
// First, let's debug the environment
|
||||||
let container_id = get_container_id().await;
|
debug_docker_environment(docker).await;
|
||||||
|
|
||||||
let container_id = match container_id {
|
// Get the current container ID from /proc/self/cgroup
|
||||||
Some(id) => {
|
let container_id = match std::fs::read_to_string("/proc/self/cgroup") {
|
||||||
println!("Found container ID: {}", id);
|
Ok(content) => {
|
||||||
id
|
let mut found_id = None;
|
||||||
|
println!("Searching cgroup for container ID...");
|
||||||
|
|
||||||
|
for line in content.lines() {
|
||||||
|
println!("Checking line: {}", line);
|
||||||
|
|
||||||
|
// 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
|
||||||
}
|
}
|
||||||
None => {
|
Err(e) => {
|
||||||
eprintln!("Could not determine container ID");
|
eprintln!("Error reading cgroup file: {}", e);
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Inspect the current container to get its image
|
let container_id = match container_id {
|
||||||
|
Some(id) if !id.is_empty() => {
|
||||||
|
println!("Using container ID: '{}'", id);
|
||||||
|
id
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
eprintln!("Could not find valid container ID in cgroup");
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 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);
|
||||||
@@ -101,72 +135,78 @@ pub async fn get_current_image(docker: &Docker) -> Result<Option<String>, Box<dy
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_container_id() -> Option<String> {
|
fn extract_container_id(line: &str) -> Option<String> {
|
||||||
// Method 1: Try /proc/self/cgroup with various patterns
|
// Split by slashes and take the last part
|
||||||
if let Ok(content) = std::fs::read_to_string("/proc/self/cgroup") {
|
if let Some(last_part) = line.split('/').last() {
|
||||||
for line in content.lines() {
|
let last_part = last_part.trim();
|
||||||
// Try different container runtime identifiers
|
|
||||||
let patterns = ["docker", "crio", "containerd", "kubepods"];
|
// Remove common suffixes
|
||||||
if patterns.iter().any(|&p| line.contains(p)) {
|
let clean_id = last_part
|
||||||
if let Some(id) = extract_container_id(line) {
|
.trim_end_matches(".scope")
|
||||||
return Some(id);
|
.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
|
||||||
// Method 2: Try /proc/self/mountinfo
|
let hex_part: String = clean_id.chars()
|
||||||
if let Ok(content) = std::fs::read_to_string("/proc/self/mountinfo") {
|
.take_while(|c| c.is_ascii_hexdigit())
|
||||||
for line in content.lines() {
|
.collect();
|
||||||
if line.contains("/docker/containers/") {
|
|
||||||
if let Some(start) = line.find("/docker/containers/") {
|
if hex_part.len() >= 12 {
|
||||||
let rest = &line[start + 18..]; // 18 = len("/docker/containers/")
|
return Some(hex_part);
|
||||||
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
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
fn extract_container_id(line: &str) -> Option<String> {
|
// Add this function to debug the Docker connection and environment
|
||||||
let parts: Vec<&str> = line.split('/').collect();
|
async fn debug_docker_environment(docker: &Docker) {
|
||||||
|
println!("=== DOCKER ENVIRONMENT DEBUG ===");
|
||||||
|
|
||||||
if let Some(last_part) = parts.last() {
|
// List containers to see what's available - CORRECTED
|
||||||
let last_part = last_part.trim();
|
let options = Some(ListContainersOptions {
|
||||||
|
all: true, // include stopped containers
|
||||||
// Pattern 1: docker-<id>.scope
|
..Default::default()
|
||||||
if last_part.starts_with("docker-") && last_part.ends_with(".scope") {
|
});
|
||||||
return Some(last_part
|
|
||||||
.trim_start_matches("docker-")
|
match docker.list_containers(options).await {
|
||||||
.trim_end_matches(".scope")
|
Ok(containers) => {
|
||||||
.to_string());
|
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) => {
|
||||||
// Pattern 2: <id> (64-character hex)
|
eprintln!("Failed to list containers: {}", e);
|
||||||
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
|
// 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>> {
|
||||||
|
Reference in New Issue
Block a user