Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
4a2f7d15ce | |||
409657a879 | |||
2a1471a380 | |||
c16547628a | |||
![]() |
6dc9972f2f | ||
![]() |
6a2d462ec7 |
40
.github/workflows/build.yml
vendored
40
.github/workflows/build.yml
vendored
@@ -9,8 +9,11 @@ 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 }}
|
||||
@@ -51,9 +54,24 @@ 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]
|
||||
needs: [detect-project, setup-rust]
|
||||
if: ${{ !failure() && !cancelled() }}
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
@@ -82,8 +100,6 @@ jobs:
|
||||
outputs:
|
||||
tag_name: ${{ steps.set_tag.outputs.tag_name }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Determine next semantic version tag
|
||||
id: set_tag
|
||||
run: |
|
||||
@@ -116,7 +132,7 @@ jobs:
|
||||
|
||||
# audit:
|
||||
# name: Security Audit
|
||||
# needs: [detect-project]
|
||||
# needs: [detect-project, setup-rust]
|
||||
# if: ${{ !failure() && !cancelled() }}
|
||||
# runs-on: ubuntu-latest
|
||||
# steps:
|
||||
@@ -136,7 +152,7 @@ jobs:
|
||||
|
||||
build:
|
||||
name: Build (${{ matrix.target }})
|
||||
needs: [detect-project, test]
|
||||
needs: [detect-project, setup-rust, test, audit]
|
||||
if: ${{ !failure() && !cancelled() }}
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
@@ -209,10 +225,10 @@ jobs:
|
||||
path: dist/
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v3
|
||||
uses: docker/setup-buildx-action@v2
|
||||
|
||||
- name: Login to Docker Registry
|
||||
uses: docker/login-action@v3
|
||||
uses: docker/login-action@v2
|
||||
with:
|
||||
registry: ${{ env.REGISTRY }}
|
||||
username: ${{ secrets.AUTOMATION_USERNAME }}
|
||||
@@ -232,10 +248,8 @@ jobs:
|
||||
|
||||
tag:
|
||||
name: Create Tag
|
||||
needs: [docker-build, build, set-tag]
|
||||
if: |
|
||||
github.event_name == 'push' &&
|
||||
needs.docker-build.result == 'success'
|
||||
needs: [build, set-tag]
|
||||
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
@@ -280,4 +294,6 @@ jobs:
|
||||
echo "✅ All checks passed!" >> $GITHUB_STEP_SUMMARY
|
||||
else
|
||||
echo "❌ Some checks failed. Please review the logs." >> $GITHUB_STEP_SUMMARY
|
||||
fi
|
||||
fi
|
||||
|
||||
#test
|
@@ -20,11 +20,8 @@ nvml-wrapper-sys = "0.9.0"
|
||||
anyhow = "1.0.98"
|
||||
|
||||
# Docker .env loading
|
||||
# config = "0.13"
|
||||
|
||||
# Docker API access
|
||||
bollard = "0.19"
|
||||
futures-util = "0.3"
|
||||
config = "0.13"
|
||||
dotenvy = "0.15"
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
winapi = { version = "0.3", features = ["winuser", "pdh", "ifmib", "iphlpapi", "winerror" ,"wbemcli", "combaseapi"] }
|
||||
|
@@ -1,5 +1,3 @@
|
||||
use crate::serverclientcomm::handle_server_message;
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::hardware::HardwareInfo;
|
||||
@@ -9,9 +7,6 @@ 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>> {
|
||||
@@ -159,24 +154,4 @@ 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;
|
||||
}
|
||||
}
|
||||
//test
|
@@ -1,11 +1,10 @@
|
||||
/// WatcherAgent - A Rust-based system monitoring agent
|
||||
/// This agent collects hardware metrics and sends them to a backend server.
|
||||
/// It supports CPU, GPU, RAM, disk, and network metrics.
|
||||
/// It supports CPU, GPU, RAM, disk and network metrics.
|
||||
pub mod api;
|
||||
pub mod hardware;
|
||||
pub mod metrics;
|
||||
pub mod models;
|
||||
pub mod serverclientcomm;
|
||||
|
||||
use std::env;
|
||||
use std::error::Error;
|
||||
|
@@ -72,12 +72,3 @@ 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,
|
||||
}
|
@@ -1,67 +0,0 @@
|
||||
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?)");
|
||||
}
|
||||
}
|
@@ -1,23 +0,0 @@
|
||||
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