From 16fed2c83bbc228bf8b25c8ef3f54619128e5694 Mon Sep 17 00:00:00 2001 From: Oluwatobi Bamidele Date: Wed, 11 Sep 2024 22:23:17 +0100 Subject: [PATCH 1/3] update: default host added to sending sending child swarm request body --- src/config.rs | 1 + src/defaults.rs | 15 +++++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/config.rs b/src/config.rs index 264502df..72b57132 100644 --- a/src/config.rs +++ b/src/config.rs @@ -91,6 +91,7 @@ pub struct SendSwarmDetailsBody { pub username: String, pub password: String, pub host: String, + pub default_host: String, } #[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)] diff --git a/src/defaults.rs b/src/defaults.rs index 3b08b79d..8138818e 100644 --- a/src/defaults.rs +++ b/src/defaults.rs @@ -225,16 +225,18 @@ pub fn create_super_user() -> User { return; } - //get swarm host - let mut my_domain = getenv("NAV_BOLTWALL_SHARED_HOST").unwrap_or("".to_string()); + let default_host = getenv("HOST").unwrap_or("".to_string()); - if my_domain.is_empty() { - my_domain = getenv("HOST").unwrap_or("".to_string()) + if default_host.is_empty() { + log::error!("HOST {}", &error_msg); + return; } + //get swarm host + let mut my_domain = getenv("NAV_BOLTWALL_SHARED_HOST").unwrap_or("".to_string()); + if my_domain.is_empty() { - log::error!("HOST {}", &error_msg); - return; + my_domain = default_host.clone(); } let client = make_reqwest_client(); @@ -245,6 +247,7 @@ pub fn create_super_user() -> User { username: "super".to_string(), password: password_, host: my_domain, + default_host: default_host, }; match client From a21f9017a28ce1312defead006d4f29b5bb1f645 Mon Sep 17 00:00:00 2001 From: Oluwatobi Bamidele Date: Wed, 11 Sep 2024 22:35:08 +0100 Subject: [PATCH 2/3] update: ensured we have default host --- src/bin/super/cmd.rs | 1 + src/bin/super/mod.rs | 3 +++ src/bin/super/routes.rs | 1 + src/bin/super/state.rs | 2 ++ 4 files changed, 7 insertions(+) diff --git a/src/bin/super/cmd.rs b/src/bin/super/cmd.rs index 6248a37e..108390e4 100644 --- a/src/bin/super/cmd.rs +++ b/src/bin/super/cmd.rs @@ -56,6 +56,7 @@ pub struct ChildSwarm { pub host: String, pub username: String, pub token: String, + pub default_host: String, } #[derive(Serialize, Deserialize, Debug, Clone)] diff --git a/src/bin/super/mod.rs b/src/bin/super/mod.rs index 08a3c2fa..cb8c85f4 100644 --- a/src/bin/super/mod.rs +++ b/src/bin/super/mod.rs @@ -169,6 +169,7 @@ pub async fn super_handle( pass: Some("".to_string()), ec2: Some(swarm.instance), note: Some(swarm.description), + default_host: Some("".to_string()), }; let hm = add_new_swarm_details(&mut state, swarm_detail, &mut must_save_stack); @@ -185,6 +186,7 @@ pub async fn super_handle( note: Some(swarm.description), user: state.stacks[ui].user.clone(), pass: state.stacks[ui].pass.clone(), + default_host: state.stacks[ui].default_host.clone(), }; must_save_stack = true; hm = AddSwarmResponse { @@ -224,6 +226,7 @@ pub async fn super_handle( pass: Some(c.password), user: Some(c.username), ec2: Some("".to_string()), + default_host: Some(c.default_host), }; let hm = add_new_swarm_details(&mut state, swarm_details, &mut must_save_stack); diff --git a/src/bin/super/routes.rs b/src/bin/super/routes.rs index f80ffbb3..a564e42e 100644 --- a/src/bin/super/routes.rs +++ b/src/bin/super/routes.rs @@ -79,6 +79,7 @@ async fn add_new_swarm( username: body.username.clone(), password: body.password.clone(), token: verify_super_token.token.unwrap(), + default_host: body.default_host.clone(), })); let txt = serde_json::to_string(&cmd)?; diff --git a/src/bin/super/state.rs b/src/bin/super/state.rs index 828588cc..e4cd2ad6 100644 --- a/src/bin/super/state.rs +++ b/src/bin/super/state.rs @@ -19,6 +19,7 @@ pub struct RemoteStack { pub ec2: Option, pub user: Option, pub pass: Option, + pub default_host: Option, } #[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Default)] @@ -72,6 +73,7 @@ impl Super { ec2: n.ec2.clone(), user: None, pass: None, + default_host: n.default_host.clone(), }) .collect(); let bots = self From db72c1f3aaf78c2aed7370f5271b1b4601c801f0 Mon Sep 17 00:00:00 2001 From: Oluwatobi Bamidele Date: Wed, 11 Sep 2024 22:57:10 +0100 Subject: [PATCH 3/3] update: ensure we use default host to access child swarm --- src/bin/super/util.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/bin/super/util.rs b/src/bin/super/util.rs index 02b07539..659c4b9a 100644 --- a/src/bin/super/util.rs +++ b/src/bin/super/util.rs @@ -36,7 +36,7 @@ pub fn add_new_swarm_details( pub async fn login_to_child_swarm(swarm_details: &RemoteStack) -> Result { let client = make_reqwest_client(); - let base_route = get_child_base_route(&swarm_details.host); + let base_route = get_child_base_route(swarm_details.default_host.clone())?; let route = format!("{}/login", base_route); if let None = &swarm_details.user { @@ -77,7 +77,7 @@ pub async fn get_child_swarm_config( let token = login_to_child_swarm(swarm_details).await?; // let res = handle_get_child_swarm_config(&swarm_details.host, &token).await?; let cmd = Cmd::Swarm(SwarmCmd::GetConfig); - let res = swarm_cmd(cmd, &swarm_details.host, &token).await?; + let res = swarm_cmd(cmd, swarm_details.default_host.clone(), &token).await?; if res.status().clone() != 200 { return Err(anyhow!(format!( @@ -97,16 +97,20 @@ pub async fn get_child_swarm_config( }) } -async fn swarm_cmd(cmd: Cmd, host: &str, token: &str) -> Result { - let url = get_child_base_route(host); +async fn swarm_cmd(cmd: Cmd, host: Option, token: &str) -> Result { + let url = get_child_base_route(host)?; let cmd_res = send_cmd_request(cmd, "SWARM", &url, Some("x-jwt"), Some(&token)).await?; Ok(cmd_res) } -pub fn get_child_base_route(host: &str) -> String { - return format!("https://app.{}/api", host); +pub fn get_child_base_route(host: Option) -> Result { + if host.is_none() { + return Err(anyhow!("child swarm default host not provided")); + }; + + return Ok(format!("https://app.{}/api", host.unwrap())); - // return format!("http://{}/api", host); + // return Ok(format!("http://{}/api", host.unwrap())); } pub async fn get_child_swarm_containers( @@ -114,7 +118,7 @@ pub async fn get_child_swarm_containers( ) -> Result { let token = login_to_child_swarm(swarm_details).await?; let cmd = Cmd::Swarm(SwarmCmd::ListContainers); - let res = swarm_cmd(cmd, &swarm_details.host, &token).await?; + let res = swarm_cmd(cmd, swarm_details.default_host.clone(), &token).await?; if res.status().clone() != 200 { return Err(anyhow!(format!( @@ -153,7 +157,7 @@ pub async fn access_child_swarm_containers( cmd = Cmd::Swarm(SwarmCmd::StopContainer(node.clone())) } - match swarm_cmd(cmd, &swarm_details.host, &token).await { + match swarm_cmd(cmd, swarm_details.default_host.clone(), &token).await { Ok(res) => { if res.status().clone() != 200 { errors.insert(