Compare commits
14 Commits
0fb3ccd506
...
v0.1.1
Author | SHA1 | Date | |
---|---|---|---|
6fd275802c | |||
9018adf998 | |||
3124697f10 | |||
30382fedef | |||
8910155524 | |||
7a68df41ac | |||
60ce51cd82 | |||
54fca8b1d3 | |||
aa876d9e5d | |||
88625ff986 | |||
428be53fff | |||
83cb815e76 | |||
755617c86f | |||
314bf8c327 |
38
.github/workflows/build.yml
vendored
38
.github/workflows/build.yml
vendored
@@ -9,11 +9,8 @@ on:
|
||||
branches: [ "development", "main" ]
|
||||
|
||||
env:
|
||||
CARGO_TERM_COLOR: always
|
||||
RUST_BACKTRACE: 1
|
||||
REGISTRY: git.triggermeelmo.com
|
||||
IMAGE_NAME: donpat1to/watcher-agent
|
||||
TAG: ${{ github.ref == 'refs/heads/main' && 'latest' || github.ref == 'refs/heads/development' && 'development' || github.ref_type == 'tag' && github.ref_name || 'pr' }}
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref }}
|
||||
@@ -54,24 +51,9 @@ jobs:
|
||||
exit 1
|
||||
fi
|
||||
|
||||
setup-rust:
|
||||
name: Setup Rust Toolchain
|
||||
needs: detect-project
|
||||
if: ${{ !failure() && !cancelled() }}
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Install Rust
|
||||
uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
toolchain: stable
|
||||
targets: x86_64-unknown-linux-gnu, x86_64-pc-windows-gnu
|
||||
components: rustfmt, clippy
|
||||
|
||||
test:
|
||||
name: Run Tests
|
||||
needs: [detect-project, setup-rust]
|
||||
needs: [detect-project]
|
||||
if: ${{ !failure() && !cancelled() }}
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
@@ -134,7 +116,7 @@ jobs:
|
||||
|
||||
# audit:
|
||||
# name: Security Audit
|
||||
# needs: [detect-project, setup-rust]
|
||||
# needs: [detect-project]
|
||||
# if: ${{ !failure() && !cancelled() }}
|
||||
# runs-on: ubuntu-latest
|
||||
# steps:
|
||||
@@ -154,7 +136,7 @@ jobs:
|
||||
|
||||
build:
|
||||
name: Build (${{ matrix.target }})
|
||||
needs: [detect-project, setup-rust, test, audit]
|
||||
needs: [detect-project, test]
|
||||
if: ${{ !failure() && !cancelled() }}
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
@@ -227,14 +209,14 @@ jobs:
|
||||
path: dist/
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v2
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: Login to Docker Registry
|
||||
uses: docker/login-action@v2
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: ${{ env.REGISTRY }}
|
||||
username: ${{ secrets.DOCKER_USERNAME }}
|
||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||
username: ${{ secrets.AUTOMATION_USERNAME }}
|
||||
password: ${{ secrets.AUTOMATION_PASSWORD }}
|
||||
|
||||
- name: Build Docker image
|
||||
uses: docker/build-push-action@v4
|
||||
@@ -250,8 +232,10 @@ jobs:
|
||||
|
||||
tag:
|
||||
name: Create Tag
|
||||
needs: [build, set-tag]
|
||||
if: github.event_name == 'push'
|
||||
needs: [docker-build, build, set-tag]
|
||||
if: |
|
||||
github.event_name == 'push' &&
|
||||
needs.docker-build.result == 'success'
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
@@ -20,8 +20,11 @@ nvml-wrapper-sys = "0.9.0"
|
||||
anyhow = "1.0.98"
|
||||
|
||||
# Docker .env loading
|
||||
config = "0.13"
|
||||
dotenvy = "0.15"
|
||||
# config = "0.13"
|
||||
|
||||
# Docker API access
|
||||
bollard = "0.19"
|
||||
futures-util = "0.3"
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
winapi = { version = "0.3", features = ["winuser", "pdh", "ifmib", "iphlpapi", "winerror" ,"wbemcli", "combaseapi"] }
|
||||
|
@@ -1,3 +1,5 @@
|
||||
use crate::serverclientcomm::handle_server_message;
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::hardware::HardwareInfo;
|
||||
@@ -7,6 +9,9 @@ use reqwest::{Client, StatusCode};
|
||||
use std::error::Error;
|
||||
use tokio::time::sleep;
|
||||
|
||||
use bollard::Docker;
|
||||
use crate::models::ServerMessage;
|
||||
|
||||
pub async fn register_with_server(
|
||||
base_url: &str,
|
||||
) -> Result<(i32, String), Box<dyn Error + Send + Sync>> {
|
||||
@@ -153,3 +158,25 @@ pub async fn send_metrics(
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn listening_to_server(docker: &Docker, base_url: &str) -> Result<(), Box<dyn Error + Send + Sync>>{
|
||||
let url = format!("{}/api/message", base_url);
|
||||
loop {
|
||||
// Replace with your server endpoint
|
||||
let resp = reqwest::get(&url)
|
||||
.await;
|
||||
|
||||
if let Ok(resp) = resp {
|
||||
if let Ok(msg) = resp.json::<ServerMessage>().await {
|
||||
handle_server_message(docker, msg).await;
|
||||
} else {
|
||||
eprintln!("Failed to parse message");
|
||||
}
|
||||
} else {
|
||||
eprintln!("Failed to reach server");
|
||||
}
|
||||
|
||||
// Poll every 5 seconds (or use WebSocket for real-time)
|
||||
sleep(Duration::from_secs(5)).await;
|
||||
}
|
||||
}
|
@@ -5,6 +5,7 @@ pub mod api;
|
||||
pub mod hardware;
|
||||
pub mod metrics;
|
||||
pub mod models;
|
||||
pub mod serverclientcomm;
|
||||
|
||||
use std::env;
|
||||
use std::error::Error;
|
||||
|
@@ -72,3 +72,12 @@ pub struct HardwareDto {
|
||||
pub ram_size: f64,
|
||||
pub ip_address: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(tag = "command", content = "data")]
|
||||
pub enum ServerMessage {
|
||||
Update(String),
|
||||
Restart,
|
||||
#[serde(other)]
|
||||
Unknown,
|
||||
}
|
67
WatcherAgent/src/serverclientcomm.rs
Normal file
67
WatcherAgent/src/serverclientcomm.rs
Normal file
@@ -0,0 +1,67 @@
|
||||
use crate::models::ServerMessage;
|
||||
|
||||
use bollard::Docker;
|
||||
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,
|
||||
msg if msg.starts_with("update:") => ServerMessage::Update(msg[7..].to_string()),
|
||||
_ => ServerMessage::Unknown,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn handle_server_message(docker: &Docker, msg: ServerMessage) {
|
||||
match msg {
|
||||
ServerMessage::Update(version) => update_docker_image(docker, &version).await,
|
||||
ServerMessage::Restart => restart_container(docker).await,
|
||||
ServerMessage::Unknown => eprintln!("Unknown message"),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn update_docker_image(docker: &Docker, image: &str) {
|
||||
println!("Updating to {}", image);
|
||||
|
||||
// 1. Pull new image
|
||||
let mut stream = docker.create_image(
|
||||
Some(CreateImageOptions {
|
||||
from_image: Some(image.to_string()),
|
||||
..Default::default()
|
||||
}),
|
||||
None,
|
||||
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 {
|
||||
println!("Pull status: {}", status);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error pulling image: {}", e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Restart the current container
|
||||
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);
|
||||
if let Err(e) = docker.restart_container(&container_id, Some(RestartContainerOptions { signal: None, t: Some(0) }))
|
||||
.await
|
||||
{
|
||||
eprintln!("Failed to restart container: {}", e);
|
||||
}
|
||||
} else {
|
||||
eprintln!("No container ID found (HOSTNAME not set?)");
|
||||
}
|
||||
}
|
23
docker-compose.yaml
Normal file
23
docker-compose.yaml
Normal file
@@ -0,0 +1,23 @@
|
||||
watcher-agent:
|
||||
image: git.triggermeelmo.com/donpat1to/watcher-agent:development
|
||||
container_name: watcher-agent
|
||||
restart: always
|
||||
privileged: true # Grants full hardware access (use with caution)
|
||||
env_file: .env
|
||||
pid: "host"
|
||||
volumes:
|
||||
# Mount critical system paths for hardware monitoring
|
||||
- /sys:/sys:ro # CPU/GPU temps, sensors
|
||||
- /proc:/proc # Process/CPU stats
|
||||
- /dev:/dev:ro # Disk/GPU device access
|
||||
- /var/run/docker.sock:/var/run/docker.sock # Docker API access
|
||||
- /:/root:ro # Access to for df-command
|
||||
# Application volumes
|
||||
- ./config:/app/config:ro
|
||||
- ./logs:/app/logs
|
||||
network_mode: host # Uses host network (for correct IP/interface detection)
|
||||
healthcheck:
|
||||
test: ["CMD", "/usr/local/bin/WatcherAgent", "healthcheck"]
|
||||
interval: 30s
|
||||
timeout: 3s
|
||||
retries: 3
|
Reference in New Issue
Block a user