added docker api for restart and client updat
This commit is contained in:
@@ -23,7 +23,8 @@ anyhow = "1.0.98"
|
|||||||
# config = "0.13"
|
# config = "0.13"
|
||||||
|
|
||||||
# Docker API access
|
# Docker API access
|
||||||
bollard = "0.16"
|
bollard = "0.19"
|
||||||
|
futures-util = "0.3"
|
||||||
|
|
||||||
[target.'cfg(windows)'.dependencies]
|
[target.'cfg(windows)'.dependencies]
|
||||||
winapi = { version = "0.3", features = ["winuser", "pdh", "ifmib", "iphlpapi", "winerror" ,"wbemcli", "combaseapi"] }
|
winapi = { version = "0.3", features = ["winuser", "pdh", "ifmib", "iphlpapi", "winerror" ,"wbemcli", "combaseapi"] }
|
||||||
|
@@ -1,5 +1,4 @@
|
|||||||
use crate::serverclientmessage::handle_server_message;
|
use crate::serverclientcomm::handle_server_message;
|
||||||
|
|
||||||
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
@@ -10,7 +9,8 @@ use reqwest::{Client, StatusCode};
|
|||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use tokio::time::sleep;
|
use tokio::time::sleep;
|
||||||
|
|
||||||
|
use bollard::Docker;
|
||||||
|
use crate::models::ServerMessage;
|
||||||
|
|
||||||
pub async fn register_with_server(
|
pub async fn register_with_server(
|
||||||
base_url: &str,
|
base_url: &str,
|
||||||
@@ -163,7 +163,7 @@ pub async fn listening_to_server(docker: &Docker, base_url: &str) -> Result<(),
|
|||||||
let url = format!("{}/api/message", base_url);
|
let url = format!("{}/api/message", base_url);
|
||||||
loop {
|
loop {
|
||||||
// Replace with your server endpoint
|
// Replace with your server endpoint
|
||||||
let resp = reqwest::get(url)
|
let resp = reqwest::get(&url)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
if let Ok(resp) = resp {
|
if let Ok(resp) = resp {
|
||||||
|
@@ -73,9 +73,11 @@ pub struct HardwareDto {
|
|||||||
pub ip_address: String,
|
pub ip_address: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
#[serde(tag = "command", content = "data")]
|
||||||
pub enum ServerMessage {
|
pub enum ServerMessage {
|
||||||
Restart,
|
|
||||||
Log(String),
|
|
||||||
Update(String),
|
Update(String),
|
||||||
|
Restart,
|
||||||
|
#[serde(other)]
|
||||||
Unknown,
|
Unknown,
|
||||||
}
|
}
|
@@ -1,12 +1,13 @@
|
|||||||
|
use crate::models::ServerMessage;
|
||||||
|
|
||||||
use bollard::Docker;
|
use bollard::Docker;
|
||||||
use bollard::image::CreateImageOptions;
|
use bollard::query_parameters::CreateImageOptions;
|
||||||
use bollard::container::{RestartContainerOptions};
|
use bollard::query_parameters::RestartContainerOptions;
|
||||||
|
use futures_util::StreamExt;
|
||||||
|
|
||||||
pub fn parse_message(raw: &str) -> ServerMessage {
|
pub fn parse_message(raw: &str) -> ServerMessage {
|
||||||
match raw {
|
match raw {
|
||||||
"restart" => ServerMessage::Restart,
|
"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()),
|
msg if msg.starts_with("update:") => ServerMessage::Update(msg[7..].to_string()),
|
||||||
_ => ServerMessage::Unknown,
|
_ => ServerMessage::Unknown,
|
||||||
}
|
}
|
||||||
@@ -26,35 +27,41 @@ pub async fn update_docker_image(docker: &Docker, image: &str) {
|
|||||||
// 1. Pull new image
|
// 1. Pull new image
|
||||||
let mut stream = docker.create_image(
|
let mut stream = docker.create_image(
|
||||||
Some(CreateImageOptions {
|
Some(CreateImageOptions {
|
||||||
from_image: image,
|
from_image: Some(image.to_string()),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}),
|
}),
|
||||||
None,
|
None,
|
||||||
None,
|
None,
|
||||||
);
|
);
|
||||||
|
|
||||||
while let Some(progress) = stream.try_next().await.unwrap_or(None) {
|
// 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 {
|
if let Some(status) = progress.status {
|
||||||
println!("Pull status: {}", status);
|
println!("Pull status: {}", status);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Error pulling image: {}", e);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 2. Restart the current container
|
// 2. Restart the current container
|
||||||
if let Ok(container_id) = std::env::var("HOSTNAME") {
|
restart_container(docker).await;
|
||||||
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?)");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
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
|
.await
|
||||||
.unwrap();
|
{
|
||||||
|
eprintln!("Failed to restart container: {}", e);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
eprintln!("No container ID found (HOSTNAME not set?)");
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user