commonware_deployer/ec2/
clean.rs

1//! `clean` subcommand for `ec2`
2
3use crate::ec2::{
4    s3::{create_s3_client, delete_bucket_and_contents, is_no_such_bucket_error, S3_BUCKET_NAME},
5    Error, MONITORING_REGION,
6};
7use aws_config::Region;
8use tracing::info;
9
10/// Deletes the shared S3 cache bucket and all its contents
11pub async fn clean() -> Result<(), Error> {
12    info!(bucket = S3_BUCKET_NAME, "cleaning S3 bucket");
13
14    // Create S3 client in the monitoring region (where bucket is located)
15    let s3_client = create_s3_client(Region::new(MONITORING_REGION)).await;
16
17    // Delete all objects and the bucket itself
18    match delete_bucket_and_contents(&s3_client, S3_BUCKET_NAME).await {
19        Ok(()) => {
20            info!(bucket = S3_BUCKET_NAME, "cleaned S3 bucket");
21        }
22        Err(e) => {
23            if is_no_such_bucket_error(&e) {
24                info!(
25                    bucket = S3_BUCKET_NAME,
26                    "bucket does not exist, nothing to clean"
27                );
28                return Ok(());
29            }
30            return Err(e);
31        }
32    }
33
34    Ok(())
35}