-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #370 from stakwork/feat/swarm-tag
Feat/swarm tag
- Loading branch information
Showing
7 changed files
with
144 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use anyhow::Error; | ||
use aws_config::meta::region::RegionProviderChain; | ||
use aws_config::Region; | ||
use aws_sdk_ec2::Client; | ||
use aws_smithy_types::retry::RetryConfig; | ||
use sphinx_swarm::utils::getenv; | ||
|
||
pub async fn make_aws_client() -> Result<Client, Error> { | ||
let region = getenv("AWS_S3_REGION_NAME")?; | ||
let region_provider = RegionProviderChain::first_try(Some(Region::new(region))); | ||
let config = aws_config::from_env() | ||
.region(region_provider) | ||
.retry_config(RetryConfig::standard().with_max_attempts(10)) | ||
.load() | ||
.await; | ||
|
||
Ok(Client::new(&config)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use crate::{aws_util::make_aws_client, state::InstanceFromAws}; | ||
use anyhow::{anyhow, Error}; | ||
use aws_sdk_ec2::types::Filter; | ||
|
||
pub async fn get_swarms_by_tag(key: &str, value: &str) -> Result<Vec<InstanceFromAws>, Error> { | ||
let client = make_aws_client().await?; | ||
|
||
let mut instances: Vec<InstanceFromAws> = vec![]; | ||
|
||
let tag_filter = Filter::builder() | ||
.name(format!("tag:{}", key)) | ||
.values(format!("{}", value)) | ||
.build(); | ||
|
||
let response = client | ||
.describe_instances() | ||
.filters(tag_filter) | ||
.send() | ||
.await?; | ||
|
||
if response.reservations().is_empty() { | ||
log::error!("No instances found with the given tag."); | ||
return Err(anyhow!("No instances found with the given tag.")); | ||
} | ||
|
||
for reservation in response.reservations.unwrap() { | ||
if !reservation.instances().is_empty() { | ||
for instance in reservation.instances.unwrap() { | ||
if instance.public_ip_address.is_some() | ||
&& instance.instance_id.is_some() | ||
&& instance.instance_type.is_some() | ||
{ | ||
instances.push(InstanceFromAws { | ||
instacne_id: instance.instance_id.unwrap(), | ||
intance_type: instance.instance_type.unwrap().to_string(), | ||
}); | ||
} | ||
} | ||
} else { | ||
log::error!("Instances do not exist") | ||
} | ||
} | ||
|
||
return Ok(instances); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters