added docker api for restart and client updat

This commit is contained in:
2025-09-25 22:02:00 +02:00
parent 83cb815e76
commit 428be53fff
4 changed files with 36 additions and 26 deletions

View File

@@ -1,12 +1,13 @@
use crate::models::ServerMessage;
use bollard::Docker;
use bollard::image::CreateImageOptions;
use bollard::container::{RestartContainerOptions};
use bollard::query_parameters::CreateImageOptions;
use bollard::query_parameters::RestartContainerOptions;
use futures_util::StreamExt;
pub fn parse_message(raw: &str) -> ServerMessage {
match raw {
"restart" => ServerMessage::Restart,
"update" => ServerMessage::Update,
msg if msg.starts_with("log:") => ServerMessage::Log(msg[4..].to_string()),
msg if msg.starts_with("update:") => ServerMessage::Update(msg[7..].to_string()),
_ => ServerMessage::Unknown,
}
@@ -26,35 +27,41 @@ pub async fn update_docker_image(docker: &Docker, image: &str) {
// 1. Pull new image
let mut stream = docker.create_image(
Some(CreateImageOptions {
from_image: image,
from_image: Some(image.to_string()),
..Default::default()
}),
None,
None,
);
while let Some(progress) = stream.try_next().await.unwrap_or(None) {
if let Some(status) = progress.status {
println!("Pull status: {}", status);
// Use the stream with proper trait bounds
while let Some(result) = StreamExt::next(&mut stream).await {
match result {
Ok(progress) => {
if let Some(status) = progress.status {
println!("Pull status: {}", status);
}
}
Err(e) => {
eprintln!("Error pulling image: {}", e);
break;
}
}
}
// 2. Restart the current container
if let Ok(container_id) = std::env::var("HOSTNAME") {
println!("Restarting container: {}", container_id);
docker.restart_container(&container_id, Some(RestartContainerOptions { t: 0 }))
.await
.unwrap();
} else {
eprintln!("No container ID found (HOSTNAME not set?)");
}
restart_container(docker).await;
}
pub async fn restart_container(docker: &Docker) {
if let Ok(container_id) = std::env::var("HOSTNAME") {
println!("Restarting container {}", container_id);
docker.restart_container(&container_id, Some(RestartContainerOptions { t: 0 }))
if let Err(e) = docker.restart_container(&container_id, Some(RestartContainerOptions { signal: None, t: Some(0) }))
.await
.unwrap();
{
eprintln!("Failed to restart container: {}", e);
}
} else {
eprintln!("No container ID found (HOSTNAME not set?)");
}
}
}