diff --git a/.github/workflows/check_backup.py b/.github/workflows/check_backup.py new file mode 100644 index 0000000..7076580 --- /dev/null +++ b/.github/workflows/check_backup.py @@ -0,0 +1,39 @@ +import boto3 +import os +import requests + +from datetime import datetime + + +# Create the expected file name +today_date = datetime.today().strftime("%Y%m%d") +expected_file = f"{os.getenv('S3_FOLDER')}smartapi_{today_date}.zip" + +# Create the S3 client +s3_client = boto3.client("s3", region_name=os.getenv("AWS_REGION")) + +# Try to fetch the file metadata +try: + s3_client.head_object(Bucket=os.getenv("BACKUP_BUCKET_NAME"), Key=expected_file) + print(f"Backup file {expected_file} exists, no action needed") +except s3_client.exceptions.ClientError: + print(f"Backup file {expected_file} does NOT exist.") + + # Create the payload for Slack + slack_data = { + "channel": os.getenv("SLACK_CHANNEL"), + "username": os.getenv("APPLICATION_NAME"), + "icon_emoji": ":thumbsdown:", + "text": f":alert: Backup file {expected_file} does NOT exist.", + } + + try: + response = requests.post(os.getenv("SLACK_WEBHOOK_URL"), json=slack_data, timeout=10) + if response.status_code == 200: + print(" └─ Slack notification sent successfully.") + else: + print(f" └─ Failed to send message to Slack: {response.status_code}, {response.text}") + except requests.exceptions.Timeout: + print(" └─ Request timed out to Slack WebHook URL.") + except requests.exceptions.RequestException as e: + print(f" └─ Failed to send Slack notification. Error: {str(e)}") diff --git a/.github/workflows/check_backup.yml b/.github/workflows/check_backup.yml index 92316d4..30a5144 100644 --- a/.github/workflows/check_backup.yml +++ b/.github/workflows/check_backup.yml @@ -23,7 +23,7 @@ jobs: pip install boto3 - name: Check if backup exists in S3 - id: check_s3_backup + run: python check_backup.py env: AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} @@ -32,41 +32,3 @@ jobs: S3_FOLDER: "db_backup/" SLACK_CHANNEL: "#observability-test" SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} - run: | - import boto3 - import os - import requests - from datetime import datetime - - # Create the expected file name - today_date = datetime.utcnow().strftime("%Y%m%d") - expected_file = f"{os.getenv('S3_FOLDER')}smartapi_{today_date}.zip" - - # Create the S3 client - s3_client = boto3.client("s3", region_name=os.getenv("AWS_REGION")) - - # Try to fetch the file metadata - try: - s3_client.head_object(Bucket=os.getenv("BACKUP_BUCKET_NAME"), Key=expected_file) - print(f"Backup file {expected_file} exists, no action needed") - except s3_client.exceptions.ClientError: - print(f"Backup file {expected_file} does NOT exist.") - - # Create the payload for Slack - slack_data = { - "channel": os.getenv("SLACK_CHANNEL"), - "username": os.getenv("APPLICATION_NAME"), - "icon_emoji": ":thumbsdown:", - "text": ":alert: Backup file {expected_file} does NOT exist.", - } - - try: - response = requests.post(os.getenv("SLACK_WEBHOOK_URL"), json=slack_data, timeout=10) - if response.status_code == 200: - print(" └─ Slack notification sent successfully.") - else: - print(f" └─ Failed to send message to Slack: {response.status_code}, {response.text}") - except requests.exceptions.Timeout: - print(" └─ Request timed out to Slack WebHook URL.") - except requests.exceptions.RequestException as e: - print(f" └─ Failed to send Slack notification. Error: {str(e)}")