Skip to content

Commit

Permalink
Merge pull request #297 from stakwork/feat/swarm-url
Browse files Browse the repository at this point in the history
Feat/swarm url
  • Loading branch information
Evanfeenstra authored Sep 12, 2024
2 parents 368f47a + db72c1f commit 3795e77
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/bin/super/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
3 changes: 3 additions & 0 deletions src/bin/super/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 {
Expand Down Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions src/bin/super/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
2 changes: 2 additions & 0 deletions src/bin/super/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct RemoteStack {
pub ec2: Option<String>,
pub user: Option<String>,
pub pass: Option<String>,
pub default_host: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Default)]
Expand Down Expand Up @@ -72,6 +73,7 @@ impl Super {
ec2: n.ec2.clone(),
user: None,
pass: None,
default_host: n.default_host.clone(),
})
.collect();
let bots = self
Expand Down
22 changes: 13 additions & 9 deletions src/bin/super/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn add_new_swarm_details(
pub async fn login_to_child_swarm(swarm_details: &RemoteStack) -> Result<String, Error> {
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 {
Expand Down Expand Up @@ -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!(
Expand All @@ -97,24 +97,28 @@ pub async fn get_child_swarm_config(
})
}

async fn swarm_cmd(cmd: Cmd, host: &str, token: &str) -> Result<Response, Error> {
let url = get_child_base_route(host);
async fn swarm_cmd(cmd: Cmd, host: Option<String>, token: &str) -> Result<Response, Error> {
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<String>) -> Result<String, Error> {
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(
swarm_details: &RemoteStack,
) -> Result<SuperSwarmResponse, Error> {
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!(
Expand Down Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
15 changes: 9 additions & 6 deletions src/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand Down

0 comments on commit 3795e77

Please sign in to comment.