Skip to content
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
42 changes: 42 additions & 0 deletions .github/workflows/aws-cleanup-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Check AWS cleanup script
on:
push:
branches:
- master
paths:
- 'aws_cleanup/**'
pull_request:
paths:
- 'aws_cleanup/**'

jobs:
check-aws-cleanup:
name: Python checks for aws_delete.py
runs-on: ubuntu-latest
steps:

- name: Check out repository
uses: actions/checkout@v3

- name: Set up Python 3.13
uses: actions/setup-python@v4
with:
python-version: "3.13"

- name: Install dependencies
run: pip install boto3 pyflakes flake8

- name: Check syntax
run: python -m py_compile aws_cleanup/aws_delete.py

- name: Run pyflakes
run: python -m pyflakes aws_cleanup/aws_delete.py

- name: Run flake8
run: python -m flake8 aws_cleanup/aws_delete.py --max-line-length 120

- name: Verify imports
run: python -c "import aws_cleanup.aws_delete"

- name: Check CLI --help
run: python aws_cleanup/aws_delete.py --help
4 changes: 4 additions & 0 deletions .github/workflows/container-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ on:
push:
branches:
- master
paths-ignore:
- 'aws_cleanup/**'
pull_request:
paths-ignore:
- 'aws_cleanup/**'
# schedule:
# - cron: 3 0 * * * # Run daily at 0:03 UTC

Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ on:
push:
branches:
- master
paths-ignore:
- 'aws_cleanup/**'
pull_request:
paths-ignore:
- 'aws_cleanup/**'
# schedule:
# - cron: 3 0 * * * # Run daily at 0:03 UTC

Expand Down
19 changes: 13 additions & 6 deletions aws_cleanup/aws_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import boto3
from botocore.exceptions import ClientError


EMPTY_LEFTOVERS = {
"load_balancer": [],
"ec2_instance": [],
Expand Down Expand Up @@ -180,8 +179,13 @@ def run(self):
if not result:
leftovers["s3"].append(bucket)
expired_resources = AWSExpiredResources(
self.ec2_client, self.elb_client, self.elbv2_client, self.s3_client, self.iam_client, self.pricing_client,
self.dry_run
self.ec2_client,
self.elb_client,
self.elbv2_client,
self.s3_client,
self.iam_client,
self.pricing_client,
self.dry_run,
)
expired_resources.eliminate()
print(f"All resources deleted for region {self.ec2_client.meta.region_name}")
Expand Down Expand Up @@ -1183,10 +1187,13 @@ def parse_args():
)
parser.add_argument("--dry-run", action="store_true", help="Dry run mode")
parser.add_argument("--send-email", action="store_true", help="Send email report", default=False)
parser.add_argument(
"--to", default="sshnaidm@redhat.com", help="Email address to send report to (default: sshnaidm@redhat.com)"
)
return parser.parse_args()


def create_report():
def create_report(recipient):
# Create report content first
report = (
f"Hi,\nToday's cleanup run has saved you:\n\n"
Expand All @@ -1209,7 +1216,7 @@ def create_report():
msg = MIMEMultipart()
msg["Subject"] = f"💲 AWS Resource Deletion Report - saved ${TOTAL_SAVED['total'] * 24 * 30:.2f} USD per month"
msg["From"] = "telco5g-ci@redhat.com"
msg["To"] = "sshnaidm@redhat.com"
msg["To"] = recipient
msg.attach(MIMEText(report, "plain"))
# msg.attach(MIMEApplication(open('report.txt', 'rb').read(), Name='report.txt'))
with smtplib.SMTP("smtp.corp.redhat.com", 25) as server:
Expand Down Expand Up @@ -1245,7 +1252,7 @@ def main():
f"${(TOTAL_SAVED['total'] * 24 * 365):.2f} USD per year"
)
if args.send_email:
create_report()
create_report(args.to)


if __name__ == "__main__":
Expand Down
Loading