Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ec2Instance::terminate method #53

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions aws-throwaway/src/backend/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,16 @@ impl Aws {

tracing::info!("Terminating instances");
let instance_ids = Self::get_all_throwaway_tags(tags, "instance").await;
Self::terminate_instances(instance_ids).await;

tokio::join!(
Aws::delete_security_groups(tags),
Aws::delete_placement_groups(tags),
Aws::delete_keypairs(tags),
);
}

async fn terminate_instances(instance_ids: Vec<String>) {
if !instance_ids.is_empty() {
#[derive(Debug, Deserialize)]
enum Response {
Expand Down Expand Up @@ -417,12 +427,10 @@ impl Aws {
);
}
}
}

tokio::join!(
Aws::delete_security_groups(tags),
Aws::delete_placement_groups(tags),
Aws::delete_keypairs(tags),
);
pub(crate) async fn terminate_instance(&self, instance: Ec2Instance) {
Self::terminate_instances(vec![instance.aws_id]).await
}

async fn delete_security_groups(tags: &Tags) {
Expand Down Expand Up @@ -741,6 +749,8 @@ sudo systemctl start ssh
}
}

let aws_id = instance.instance_id.clone();

let private_ip = private_ip.unwrap();
let connect_ip = if self.use_public_addresses {
public_ip.unwrap()
Expand All @@ -750,6 +760,7 @@ sudo systemctl start ssh
tracing::info!("created EC2 instance at public:{public_ip:?} private:{private_ip}");

Ec2Instance::new(
aws_id,
connect_ip,
public_ip,
private_ip,
Expand Down
23 changes: 18 additions & 5 deletions aws-throwaway/src/backend/sdk/aws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,19 @@ impl Aws {

tracing::info!("Terminating instances");
let instance_ids = Self::get_all_throwaway_tags(client, tags, "instance").await;
Self::terminate_instances(client, instance_ids).await;

tokio::join!(
Aws::delete_security_groups(client, tags),
Aws::delete_placement_groups(client, tags),
Aws::delete_keypairs(client, tags),
);
}

pub(crate) async fn terminate_instances(
client: &aws_sdk_ec2::Client,
instance_ids: Vec<String>,
) {
if !instance_ids.is_empty() {
for result in client
.terminate_instances()
Expand All @@ -365,12 +378,10 @@ impl Aws {
);
}
}
}

tokio::join!(
Aws::delete_security_groups(client, tags),
Aws::delete_placement_groups(client, tags),
Aws::delete_keypairs(client, tags),
);
pub(crate) async fn terminate_instance(&self, instance: Ec2Instance) {
Self::terminate_instances(&self.client, vec![instance.aws_id]).await
}

async fn delete_security_groups(client: &aws_sdk_ec2::Client, tags: &Tags) {
Expand Down Expand Up @@ -650,6 +661,7 @@ sudo systemctl start ssh
}
}
}
let aws_id = instance.instance_id().unwrap().to_owned();

let private_ip = private_ip.unwrap();
let connect_ip = if self.use_public_addresses {
Expand All @@ -660,6 +672,7 @@ sudo systemctl start ssh
tracing::info!("created EC2 instance at public:{public_ip:?} private:{private_ip}");

Ec2Instance::new(
aws_id,
connect_ip,
public_ip,
private_ip,
Expand Down
14 changes: 14 additions & 0 deletions aws-throwaway/src/ec2_instance.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::ssh::SshConnection;
use crate::Aws;
use anyhow::{anyhow, Context, Result};
use serde::{Deserialize, Serialize};
use std::net::{IpAddr, Ipv4Addr};
Expand All @@ -11,6 +12,7 @@ use tokio::{net::TcpStream, time::Instant};
/// After restoring Ec2Instance in this way you need to call the [`Ec2Instance::init`] method.
#[derive(Serialize, Deserialize)]
pub struct Ec2Instance {
pub(crate) aws_id: String,
connect_ip: IpAddr,
public_ip: Option<IpAddr>,
private_ip: IpAddr,
Expand Down Expand Up @@ -91,8 +93,19 @@ TERM=xterm ssh -i key ubuntu@{} -o "UserKnownHostsFile known_hosts"
)
}

/// Delete this instance.
/// Prefer using [`Aws::cleanup_resources`] at end of runtime as it will automatically destroy all resources, not just this one instance.
/// However this method can be useful when you have a single instance that you would like to terminate before the rest.
///
/// `Aws` instance must be passed in manually here since `Ec2Instance`s can be deserialized when there is no `Aws` instance.
pub async fn terminate(self, aws: &Aws) {
aws.terminate_instance(self).await;
}

/// It is gauranteed that public_ip will be Some if use_public_address is true
#[allow(clippy::too_many_arguments)]
pub(crate) async fn new(
aws_id: String,
connect_ip: IpAddr,
public_ip: Option<IpAddr>,
private_ip: IpAddr,
Expand Down Expand Up @@ -141,6 +154,7 @@ TERM=xterm ssh -i key ubuntu@{} -o "UserKnownHostsFile known_hosts"
// 4. Then finally we have a working ssh connection.
Ok(ssh) => {
break Ec2Instance {
aws_id,
connect_ip,
ssh: Some(ssh),
public_ip,
Expand Down
Loading