From 71b40339d23d17ccc649da7b1b45036ff8bfc2d1 Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Fri, 30 Sep 2022 13:31:01 +0200 Subject: [PATCH] Add update progress to firmware stores --- server/src/file.rs | 10 ++++++++++ server/src/hawkbit.rs | 44 +++++++++++++++++++++++++++++++++---------- server/src/oci.rs | 10 ++++++++++ server/src/updater.rs | 12 ++++++++++++ 4 files changed, 66 insertions(+), 10 deletions(-) diff --git a/server/src/file.rs b/server/src/file.rs index bdd31fe..0bbece5 100644 --- a/server/src/file.rs +++ b/server/src/file.rs @@ -46,6 +46,16 @@ impl FirmwareStore for FileClient { Ok(((), Some(f.try_into()?))) } + async fn update_progress( + &mut self, + _: &Self::Params, + _: &Self::Context, + _: u32, + _: u32, + ) -> Result<(), anyhow::Error> { + Ok(()) + } + async fn mark_synced( &mut self, _: &Self::Params, diff --git a/server/src/hawkbit.rs b/server/src/hawkbit.rs index 9370e11..8ef9bb4 100644 --- a/server/src/hawkbit.rs +++ b/server/src/hawkbit.rs @@ -88,7 +88,7 @@ impl HawkbitClient { &self, controller: &str, deployment: &Deployment, - success: bool, + status: serde_json::Value, ) -> Result<(), anyhow::Error> { let client = reqwest::Client::new(); let url = format!( @@ -99,14 +99,7 @@ impl HawkbitClient { let feedback = json! { { "id": deployment.id, - "status": { - "result": { - "finished": if success { "success" } else { "failed" }, - }, - "execution": "closed", - "details": ["Update was successfully installed."], - } - + "status": status, } }; @@ -221,6 +214,30 @@ impl FirmwareStore for HawkbitClient { } } + async fn update_progress( + &mut self, + params: &Self::Params, + context: &Self::Context, + offset: u32, + size: u32, + ) -> Result<(), anyhow::Error> { + if let PollResult::Deployment(d) = context { + let status = json!({ + "result": { + "progress": { + "of": size, + "cnt": offset, + }, + "finished": "none", + }, + "execution": "proceeding", + "details": ["Updating..."], + }); + self.provide_feedback(params, d, status).await?; + } + Ok(()) + } + async fn mark_synced( &mut self, params: &Self::Params, @@ -228,7 +245,14 @@ impl FirmwareStore for HawkbitClient { success: bool, ) -> Result<(), anyhow::Error> { if let PollResult::Deployment(d) = context { - self.provide_feedback(params, d, success).await?; + let status = json!({ + "result": { + "finished": if success { "success" } else { "failed" }, + }, + "execution": "closed", + "details": ["Update was successfully installed."], + }); + self.provide_feedback(params, d, status).await?; } Ok(()) } diff --git a/server/src/oci.rs b/server/src/oci.rs index 739b6af..841f272 100644 --- a/server/src/oci.rs +++ b/server/src/oci.rs @@ -151,6 +151,16 @@ impl FirmwareStore for OciClient { Ok(((), m)) } + async fn update_progress( + &mut self, + _: &Self::Params, + _: &Self::Context, + _: u32, + _: u32, + ) -> Result<(), anyhow::Error> { + Ok(()) + } + type Context = (); async fn fetch_firmware( &mut self, diff --git a/server/src/updater.rs b/server/src/updater.rs index a007889..333f890 100644 --- a/server/src/updater.rs +++ b/server/src/updater.rs @@ -150,6 +150,10 @@ impl Updater { ); } + let _ = store + .update_progress(params, &ctx, offset as u32, metadata.size) + .await; + if offset < metadata.size as usize { let firmware = store.fetch_firmware(params, &ctx, &metadata).await?; @@ -235,6 +239,14 @@ pub trait FirmwareStore { None } + async fn update_progress( + &mut self, + params: &Self::Params, + context: &Self::Context, + offset: u32, + size: u32, + ) -> Result<(), anyhow::Error>; + async fn mark_synced( &mut self, params: &Self::Params,