Fix clippy warnings to pass CI build
- Remove unused SnmpError import - Replace deprecated from_i32 with TryFrom - Add #[allow(dead_code)] to unused SnmpValue methods - Remove unused perform_self_update function - Remove unused token field from AgentClient - Remove unused send_error method - Add #[allow(dead_code)] to protobuf generated module
This commit is contained in:
parent
967d317b69
commit
812ee08ac5
5 changed files with 7 additions and 86 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
// Generated protobuf code
|
// Generated protobuf code
|
||||||
|
#[allow(dead_code)]
|
||||||
pub mod agent {
|
pub mod agent {
|
||||||
include!(concat!(env!("OUT_DIR"), "/towerops.agent.rs"));
|
include!(concat!(env!("OUT_DIR"), "/towerops.agent.rs"));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,4 +2,4 @@ mod client;
|
||||||
mod types;
|
mod types;
|
||||||
|
|
||||||
pub use client::SnmpClient;
|
pub use client::SnmpClient;
|
||||||
pub use types::{SnmpError, SnmpValue};
|
pub use types::SnmpValue;
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,7 @@ pub enum SnmpValue {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SnmpValue {
|
impl SnmpValue {
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn as_i64(&self) -> Option<i64> {
|
pub fn as_i64(&self) -> Option<i64> {
|
||||||
match self {
|
match self {
|
||||||
SnmpValue::Integer(v) => Some(*v),
|
SnmpValue::Integer(v) => Some(*v),
|
||||||
|
|
@ -48,6 +49,7 @@ impl SnmpValue {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn as_f64(&self) -> Option<f64> {
|
pub fn as_f64(&self) -> Option<f64> {
|
||||||
self.as_i64().map(|v| v as f64)
|
self.as_i64().map(|v| v as f64)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
use log::{info, warn};
|
use log::{info, warn};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::process::Command;
|
|
||||||
|
|
||||||
const DOCKER_IMAGE: &str = "gmcintire/towerops-agent";
|
const DOCKER_IMAGE: &str = "gmcintire/towerops-agent";
|
||||||
|
|
||||||
|
|
@ -96,67 +95,6 @@ pub fn check_for_updates() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Perform self-update by pulling latest image and exiting
|
|
||||||
/// Returns Ok(true) if update was initiated, Ok(false) if already up to date
|
|
||||||
pub fn perform_self_update() -> Result<bool, String> {
|
|
||||||
let current_ver = current_version();
|
|
||||||
|
|
||||||
// Check if a newer version is available
|
|
||||||
match get_latest_version() {
|
|
||||||
Ok(latest) => {
|
|
||||||
let current = Version::parse(current_ver);
|
|
||||||
let latest_version = Version::parse(&latest);
|
|
||||||
|
|
||||||
match (current, latest_version) {
|
|
||||||
(Some(curr), Some(lat)) => {
|
|
||||||
if lat <= curr {
|
|
||||||
info!(
|
|
||||||
"Already running latest version ({} <= {})",
|
|
||||||
latest, current_ver
|
|
||||||
);
|
|
||||||
return Ok(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
info!("Update available: {} -> {}", current_ver, latest);
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
warn!("Could not parse versions, proceeding with update check");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
warn!("Could not check Docker Hub for versions: {}", e);
|
|
||||||
// Continue anyway - try to pull and see if anything changed
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pull the latest image
|
|
||||||
info!("Pulling latest Docker image: {}:latest", DOCKER_IMAGE);
|
|
||||||
let output = Command::new("docker")
|
|
||||||
.args(["pull", &format!("{}:latest", DOCKER_IMAGE)])
|
|
||||||
.output()
|
|
||||||
.map_err(|e| format!("Failed to execute docker command: {}", e))?;
|
|
||||||
|
|
||||||
if !output.status.success() {
|
|
||||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
|
||||||
return Err(format!("Failed to pull image: {}", stderr));
|
|
||||||
}
|
|
||||||
|
|
||||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
|
||||||
|
|
||||||
// Check if the image was actually updated
|
|
||||||
if stdout.contains("Image is up to date") || stdout.contains("Already exists") {
|
|
||||||
info!("Image is already up to date, no restart needed");
|
|
||||||
return Ok(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
info!("Successfully pulled new image");
|
|
||||||
info!("Exiting to allow restart with new version...");
|
|
||||||
|
|
||||||
// Exit with success code - orchestrator (docker-compose/k8s) will restart with new image
|
|
||||||
std::process::exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get the latest version from Docker Hub
|
/// Get the latest version from Docker Hub
|
||||||
fn get_latest_version() -> Result<String, Box<dyn std::error::Error>> {
|
fn get_latest_version() -> Result<String, Box<dyn std::error::Error>> {
|
||||||
let url = format!(
|
let url = format!(
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ use tokio_tungstenite::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::proto::agent::{
|
use crate::proto::agent::{
|
||||||
AgentError, AgentHeartbeat, AgentJob, AgentJobList, JobType, QueryType, SnmpResult,
|
AgentHeartbeat, AgentJob, AgentJobList, JobType, QueryType, SnmpResult,
|
||||||
};
|
};
|
||||||
use crate::snmp::{SnmpClient, SnmpValue};
|
use crate::snmp::{SnmpClient, SnmpValue};
|
||||||
|
|
||||||
|
|
@ -34,7 +34,6 @@ struct PhoenixMessage {
|
||||||
/// WebSocket client for agent communication.
|
/// WebSocket client for agent communication.
|
||||||
pub struct AgentClient {
|
pub struct AgentClient {
|
||||||
ws_stream: WebSocketStream<MaybeTlsStream<TcpStream>>,
|
ws_stream: WebSocketStream<MaybeTlsStream<TcpStream>>,
|
||||||
token: String,
|
|
||||||
agent_id: String,
|
agent_id: String,
|
||||||
result_tx: mpsc::UnboundedSender<SnmpResult>,
|
result_tx: mpsc::UnboundedSender<SnmpResult>,
|
||||||
result_rx: mpsc::UnboundedReceiver<SnmpResult>,
|
result_rx: mpsc::UnboundedReceiver<SnmpResult>,
|
||||||
|
|
@ -63,7 +62,6 @@ impl AgentClient {
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
ws_stream,
|
ws_stream,
|
||||||
token: token.to_string(),
|
|
||||||
agent_id: generate_agent_id(),
|
agent_id: generate_agent_id(),
|
||||||
result_tx,
|
result_tx,
|
||||||
result_rx,
|
result_rx,
|
||||||
|
|
@ -160,7 +158,7 @@ impl AgentClient {
|
||||||
log::info!("Received {} jobs from server", job_list.jobs.len());
|
log::info!("Received {} jobs from server", job_list.jobs.len());
|
||||||
|
|
||||||
for job in job_list.jobs {
|
for job in job_list.jobs {
|
||||||
let job_type = JobType::from_i32(job.job_type).unwrap_or(JobType::Poll);
|
let job_type = JobType::try_from(job.job_type).unwrap_or(JobType::Poll);
|
||||||
log::info!("Executing job: {} (type: {:?})", job.job_id, job_type);
|
log::info!("Executing job: {} (type: {:?})", job.job_id, job_type);
|
||||||
|
|
||||||
// Spawn task to execute job
|
// Spawn task to execute job
|
||||||
|
|
@ -218,24 +216,6 @@ impl AgentClient {
|
||||||
log::debug!("Sent SNMP result for equipment {}", result.equipment_id);
|
log::debug!("Sent SNMP result for equipment {}", result.equipment_id);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Send error to server.
|
|
||||||
async fn send_error(&mut self, error: AgentError) -> Result<()> {
|
|
||||||
let binary = error.encode_to_vec();
|
|
||||||
|
|
||||||
let msg = PhoenixMessage {
|
|
||||||
topic: format!("agent:{}", self.agent_id),
|
|
||||||
event: "error".to_string(),
|
|
||||||
payload: serde_json::json!({"binary": BASE64.encode(&binary)}),
|
|
||||||
reference: None,
|
|
||||||
};
|
|
||||||
|
|
||||||
let text = serde_json::to_string(&msg)?;
|
|
||||||
self.ws_stream.send(WsMessage::Text(text)).await?;
|
|
||||||
|
|
||||||
log::warn!("Sent error for equipment {}", error.equipment_id);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Execute an SNMP job and collect results.
|
/// Execute an SNMP job and collect results.
|
||||||
|
|
@ -245,7 +225,7 @@ async fn execute_job(job: AgentJob, result_tx: mpsc::UnboundedSender<SnmpResult>
|
||||||
let snmp_client = SnmpClient::new();
|
let snmp_client = SnmpClient::new();
|
||||||
|
|
||||||
for query in job.queries {
|
for query in job.queries {
|
||||||
let query_type = QueryType::from_i32(query.query_type).unwrap_or(QueryType::Get);
|
let query_type = QueryType::try_from(query.query_type).unwrap_or(QueryType::Get);
|
||||||
|
|
||||||
match query_type {
|
match query_type {
|
||||||
QueryType::Get => {
|
QueryType::Get => {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue