diff --git a/.github/workflows/aws-cleanup-check.yml b/.github/workflows/aws-cleanup-check.yml new file mode 100644 index 0000000..d1c5326 --- /dev/null +++ b/.github/workflows/aws-cleanup-check.yml @@ -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 diff --git a/.github/workflows/container-tests.yml b/.github/workflows/container-tests.yml index 7cd0c4e..319f25c 100644 --- a/.github/workflows/container-tests.yml +++ b/.github/workflows/container-tests.yml @@ -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 diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index e17e7a6..0be9ff3 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -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 diff --git a/aws_cleanup/aws_delete.py b/aws_cleanup/aws_delete.py index c2136a1..2a10fe0 100755 --- a/aws_cleanup/aws_delete.py +++ b/aws_cleanup/aws_delete.py @@ -4,7 +4,6 @@ import boto3 from botocore.exceptions import ClientError - EMPTY_LEFTOVERS = { "load_balancer": [], "ec2_instance": [], @@ -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}") @@ -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" @@ -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: @@ -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__":