added image screening for current container
This commit is contained in:
@@ -1,8 +1,7 @@
|
|||||||
use crate::models::ServerMessage;
|
use crate::models::ServerMessage;
|
||||||
|
|
||||||
use bollard::Docker;
|
use bollard::Docker;
|
||||||
use bollard::query_parameters::CreateImageOptions;
|
use bollard::query_parameters::{CreateImageOptions, RestartContainerOptions, InspectContainerOptions};
|
||||||
use bollard::query_parameters::RestartContainerOptions;
|
|
||||||
use futures_util::StreamExt;
|
use futures_util::StreamExt;
|
||||||
|
|
||||||
pub fn parse_message(raw: &str) -> ServerMessage {
|
pub fn parse_message(raw: &str) -> ServerMessage {
|
||||||
@@ -53,6 +52,46 @@ pub async fn update_docker_image(docker: &Docker, image: &str) {
|
|||||||
restart_container(docker).await;
|
restart_container(docker).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_current_image(docker: &Docker) -> Option<String> {
|
||||||
|
// 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 None;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let container_id = match container_id {
|
||||||
|
Some(id) => id,
|
||||||
|
None => {
|
||||||
|
eprintln!("Could not find container ID in cgroup");
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Inspect the current container to get its image
|
||||||
|
match docker.inspect_container(&container_id, None::<InspectContainerOptions>).await {
|
||||||
|
Ok(container_info) => {
|
||||||
|
container_info.config.map(|config| config.image.unwrap_or_else(|| "unknown".to_string()))
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Error inspecting container: {}", e);
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn restart_container(docker: &Docker) {
|
pub async fn restart_container(docker: &Docker) {
|
||||||
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);
|
||||||
|
Reference in New Issue
Block a user