Skip to content

Commit

Permalink
Merge pull request #39 from Holo-Host/uninstal-check/04-17
Browse files Browse the repository at this point in the history
uninstall: check for core-apps first
  • Loading branch information
zo-el committed Apr 17, 2024
2 parents 02422c0 + 0f9d11e commit 04733c9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 21 deletions.
41 changes: 29 additions & 12 deletions src/hbs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use anyhow::Result;
use base64::prelude::*;
use holochain_types::prelude::{holochain_serial, SerializedBytes, Signature, Timestamp};
use hpos_hc_connect::{hpos_agent::get_hpos_config, CoreAppAgent};
use reqwest::Response;
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize)]
Expand All @@ -27,22 +28,45 @@ impl HbsClient {
let client = reqwest::Client::builder().build()?;
Ok(Self { client })
}
pub async fn get_hosting_criteria(&self) -> HostingCriteria {
pub async fn get_hosting_criteria(&self) -> Option<HostingCriteria> {
match self.get_access_token().await {
Ok(v) => v,
Err(e) => {
tracing::warn!("Unable to get kyc & jurisdiction: {:?}", e);
tracing::warn!("returning default kyc level 1");
tracing::warn!("returning default jurisdiction of None");
HostingCriteria {
Some(HostingCriteria {
id: None,
jurisdiction: None,
kyc: KycLevel::Level1,
}
})
}
}
}
async fn get_access_token(&self) -> Result<HostingCriteria> {

async fn get_access_token(&self) -> Result<Option<HostingCriteria>> {
let response = self.inner_get_access_token().await?;
tracing::debug!("response received");
let mut body = response.text().await?;
// 504 Gateway Timeout
// here we either need to retry once more or end the script
if body.contains("error code: 504") {
tracing::warn!("Gateway Timeout. Retrying once more...");
let response = self.inner_get_access_token().await?;
body = response.text().await?;
if body.contains("error code: 504") {
tracing::warn!("Gateway Timeout. Exiting...");
return Ok(None);
}
}
tracing::debug!("Result: {}", body);
let result: serde_json::Value = serde_json::from_str(&body)?;
let h: HostingCriteria = serde_json::from_value(result)?;
tracing::debug!("HostingCriteria: {:?}", h);
Ok(Some(h))
}

async fn inner_get_access_token(&self) -> Result<Response> {
let config: hpos_config_core::Config = get_hpos_config()?;

let email = match config {
Expand Down Expand Up @@ -94,14 +118,7 @@ impl HbsClient {
.headers(headers)
.json(&json);

let response = request.send().await?;
tracing::debug!("response received");
let body = response.text().await?;
tracing::debug!("Result: {}", body);
let result: serde_json::Value = serde_json::from_str(&body)?;
let h: HostingCriteria = serde_json::from_value(result)?;
tracing::debug!("HostingCriteria: {:?}", h);
Ok(h)
Ok(request.send().await?)
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use host_zome_calls::get_all_published_hosted_happs;
mod install_app;
use install_app::install_holo_hosted_happs;
mod uninstall_apps;
use tracing::{debug, info};
use tracing::{debug, error, info};
use uninstall_apps::uninstall_ineligible_happs;
mod suspend_happs;
use suspend_happs::suspend_unpaid_happs;
Expand All @@ -26,7 +26,13 @@ use crate::host_zome_calls::{get_pending_transactions, CoreAppClient};
pub async fn run(core_happ: &config::Happ, config: &config::Config) -> Result<()> {
info!("Activating holo hosted apps");
let hbs_connect = HbsClient::connect()?;
let hosting_criteria = hbs_connect.get_hosting_criteria().await;
let hosting_criteria = match hbs_connect.get_hosting_criteria().await {
Some(v) => v,
None => {
error!("Unable to get hosting criteria from HBS. Exiting...");
return Err(anyhow::anyhow!("Unable to get hosting criteria"));
}
};
let kyc_level = hosting_criteria.kyc;
debug!("Got kyc level {:?}", &kyc_level);
let jurisdiction = hosting_criteria.jurisdiction;
Expand Down
14 changes: 7 additions & 7 deletions src/uninstall_apps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ pub async fn should_be_installed(
suspended_happs: Vec<String>,
jurisdiction: Option<String>,
) -> bool {
trace!("`should_be_installed check` for {}", running_happ_id);
// This should be the first check since the core-app should never be uninstalled currently
if !is_hosted_happ(running_happ_id) {
trace!("Keeping infrastructure happ {}", running_happ_id);
return true;
}

if suspended_happs.contains(running_happ_id) {
trace!("Disabling suspended happ {}", running_happ_id);
return false;
Expand Down Expand Up @@ -121,13 +128,6 @@ pub async fn should_be_installed(
}
}

trace!("`should_be_installed check` for {}", running_happ_id);

if !is_hosted_happ(running_happ_id) {
trace!("Keeping infrastructure happ {}", running_happ_id);
return true;
}

// The running happ is an instance of an expected happ
let expected_happ = published_happs.iter().find(|published_happ| {
is_instance_of_happ(&published_happ.happ_id.to_string(), running_happ_id)
Expand Down

0 comments on commit 04733c9

Please sign in to comment.