fix: auto-resolve stuck distribution deletions

This commit is contained in:
Iain Learmonth 2025-04-18 17:03:26 +01:00
parent 1797c4a826
commit 2bf4282416
3 changed files with 33 additions and 4 deletions

View file

@ -1,5 +1,10 @@
import json
import re
from datetime import datetime, timezone
from typing import Any
from typing import Any, Optional
import boto3
from flask import current_app
from app.extensions import db
from app.models.mirrors import Proxy
@ -108,6 +113,29 @@ class ProxyCloudfrontAutomation(ProxyAutomation):
{% endfor %}
"""
def tf_posthook(self, *, prehook_result: Any = None, logs: Optional[str] = None) -> None:
self.import_state(self.tf_show())
failed_ids = []
for line in logs.strip().split('\n'):
try:
log_entry = json.loads(line)
if log_entry.get("@level") == "error" and "CloudFront Distribution" in log_entry.get("@message", ""):
match = re.search(r'CloudFront Distribution (\w+) cannot be deleted', log_entry["@message"])
if match:
failed_ids.append(match.group(1))
except json.JSONDecodeError:
continue
client = boto3.client(
'cloudfront',
aws_access_key_id=current_app.config["AWS_ACCESS_KEY"],
aws_secret_access_key=current_app.config["AWS_SECRET_KEY"],
region_name="us-east-1"
)
for failed_id in failed_ids:
response = client.get_distribution_config(Id=failed_id)
etag = response['ETag']
client.delete_distribution(Id=failed_id, IfMatch=etag)
def import_state(self, state: Any) -> None:
if not isinstance(state, dict):
raise RuntimeError("The Terraform state object returned was not a dict.")