Skip to content

Commit b557cab

Browse files
authored
chore: added ods version compatibility check (#41)
1 parent 3f371bd commit b557cab

File tree

5 files changed

+60
-13
lines changed

5 files changed

+60
-13
lines changed

Cargo.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ license = "MIT OR Apache-2.0"
77
name = "omnect-ui"
88
readme = "README.md"
99
repository = "git@github.com:omnect/omnect-ui.git"
10-
version = "0.13.3"
10+
version = "0.14.0"
1111
build = "src/build.rs"
1212

1313
[dependencies]
@@ -50,6 +50,7 @@ rustls = { version = "0.23", default-features = false, features = [
5050
rustls-pemfile = { version = "2.2", default-features = false, features = [
5151
"std",
5252
] }
53+
semver = { version = "1.0", default-features = false }
5354
serde = { version = "1.0", default-features = false, features = ["derive"] }
5455
serde_json = { version = "1.0", default-features = false, features = [
5556
"raw_value",

src/api.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ pub enum FactoryResetMode {
8181
Mode4 = 4,
8282
}
8383

84+
#[derive(Clone, Debug, Serialize)]
85+
pub struct VersionCheckResult {
86+
pub req_ods_version: String,
87+
pub cur_ods_version: String,
88+
pub version_mismatch: bool,
89+
}
90+
8491
#[derive(Clone)]
8592
pub struct Api {
8693
pub ods_socket_path: String,
@@ -89,6 +96,7 @@ pub struct Api {
8996
pub index_html: PathBuf,
9097
pub keycloak_public_key_url: String,
9198
pub tenant: String,
99+
pub version_check_result: VersionCheckResult,
92100
}
93101

94102
impl Api {
@@ -111,9 +119,14 @@ impl Api {
111119
Ok(NamedFile::open(config_path!("app_config.js"))?)
112120
}
113121

114-
pub async fn healthcheck() -> impl Responder {
122+
pub async fn healthcheck(config: web::Data<Api>) -> impl Responder {
115123
debug!("healthcheck() called");
116-
HttpResponse::Ok().finish()
124+
125+
if config.version_check_result.version_mismatch {
126+
HttpResponse::ServiceUnavailable().json(&config.version_check_result)
127+
} else {
128+
HttpResponse::Ok().json(&config.version_check_result)
129+
}
117130
}
118131

119132
pub async fn factory_reset(

src/common.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
use crate::api::VersionCheckResult;
2+
use crate::REQ_ODS_VERSION;
13
use actix_web::body::MessageBody;
24
use anyhow::{anyhow, bail, Context, Result};
35
use argon2::{Argon2, PasswordHash, PasswordVerifier};
46
use base64::{prelude::BASE64_STANDARD, Engine};
57
use jwt_simple::prelude::{RS256PublicKey, RSAPublicKeyLike};
68
use reqwest::blocking::get;
9+
use semver::{Version, VersionReq};
710
use serde::{Deserialize, Serialize};
811
use std::{fs, io::Write, path::Path};
912

@@ -30,6 +33,7 @@ pub struct StatusResponse {
3033
#[derive(Deserialize)]
3134
pub struct SystemInfo {
3235
pub fleet_id: Option<String>,
36+
omnect_device_service_version: String,
3337
}
3438

3539
#[derive(Deserialize)]
@@ -199,3 +203,25 @@ pub fn create_frontend_config_file(keycloak_url: &str) -> Result<()> {
199203

200204
Ok(())
201205
}
206+
207+
pub async fn check_and_store_ods_version(ods_socket_path: &str) -> Result<VersionCheckResult> {
208+
let status_response = get_status(ods_socket_path)
209+
.await
210+
.context("failed to get status from socket client")?;
211+
212+
let version_req = VersionReq::parse(REQ_ODS_VERSION)
213+
.map_err(|e| anyhow!("failed to parse REQ_ODS_VERSION: {e}"))?;
214+
let current_version =
215+
Version::parse(&status_response.system_info.omnect_device_service_version)
216+
.map_err(|e| anyhow!("failed to parse omnect_device_service_version: {e}"))?;
217+
let version_mismatch = !version_req.matches(&current_version);
218+
219+
Ok(VersionCheckResult {
220+
req_ods_version: REQ_ODS_VERSION.to_string(),
221+
cur_ods_version: status_response
222+
.system_info
223+
.omnect_device_service_version
224+
.clone(),
225+
version_mismatch,
226+
})
227+
}

src/main.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ use tokio::{
2929
};
3030
use uuid::Uuid;
3131

32+
pub const REQ_ODS_VERSION: &str = ">=0.39.0";
33+
3234
const UPLOAD_LIMIT_BYTES: usize = 250 * 1024 * 1024;
3335
const MEMORY_LIMIT_BYTES: usize = 10 * 1024 * 1024;
3436

@@ -120,11 +122,22 @@ async fn main() {
120122
.parse::<u64>()
121123
.expect("UI_PORT format");
122124

125+
let ods_socket_path = std::env::var("SOCKET_PATH").expect("env SOCKET_PATH is missing");
126+
fs::exists(&ods_socket_path).unwrap_or_else(|_| {
127+
panic!(
128+
"omnect device service socket file {} does not exist",
129+
&ods_socket_path
130+
)
131+
});
132+
let version_check_result = common::check_and_store_ods_version(&ods_socket_path)
133+
.await
134+
.expect("failed to check and store ods version");
135+
123136
CryptoProvider::install_default(default_provider()).expect("failed to install crypto provider");
124137

125138
certificate::create_module_certificate(&cert_path!(), &key_path!())
126139
.await
127-
.expect("Failed to create module certificate");
140+
.expect("failed to create module certificate");
128141

129142
let mut tls_certs =
130143
std::io::BufReader::new(std::fs::File::open(cert_path!()).expect("read certs_file"));
@@ -166,19 +179,11 @@ async fn main() {
166179
&centrifugo_http_server_port!(),
167180
);
168181

169-
let ods_socket_path = std::env::var("SOCKET_PATH").expect("env SOCKET_PATH is missing");
170182
let index_html =
171183
std::fs::canonicalize("static/index.html").expect("static/index.html not found");
172184

173185
let tenant = std::env::var("TENANT").expect("env TENANT is missing");
174186

175-
fs::exists(&ods_socket_path).unwrap_or_else(|_| {
176-
panic!(
177-
"omnect device service socket file {} does not exist",
178-
&ods_socket_path
179-
)
180-
});
181-
182187
fs::exists(&update_os_path!())
183188
.unwrap_or_else(|_| panic!("path {} for os update does not exist", &update_os_path!()));
184189

@@ -194,6 +199,7 @@ async fn main() {
194199
index_html,
195200
keycloak_public_key_url: keycloak_url!(),
196201
tenant,
202+
version_check_result,
197203
};
198204

199205
let session_key = Key::generate();

0 commit comments

Comments
 (0)