-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
138 lines (101 loc) · 3.7 KB
/
Copy pathcode.py
File metadata and controls
138 lines (101 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import os
from datetime import datetime, timezone
from obs import ObsClient
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdksmn.v2 import SmnClient, PublishMessageRequest, PublishMessageRequestBody
from huaweicloudsdksmn.v2.region.smn_region import SmnRegion
try:
from zoneinfo import ZoneInfo
except Exception:
ZoneInfo = None
def env(name):
value = os.getenv(name)
if not value:
raise RuntimeError(f"Falta variable de entorno: {name}")
return value
def parse_date(value):
if isinstance(value, datetime):
dt = value
else:
raw = str(value).strip()
formats = [
"%Y/%m/%d %H:%M:%S",
"%Y-%m-%d %H:%M:%S",
"%Y-%m-%dT%H:%M:%S.%fZ",
"%Y-%m-%dT%H:%M:%SZ",
"%a, %d %b %Y %H:%M:%S GMT",
]
for fmt in formats:
try:
dt = datetime.strptime(raw, fmt)
break
except ValueError:
dt = None
if dt is None:
dt = datetime.fromisoformat(raw.replace("Z", "+00:00"))
return dt if dt.tzinfo else dt.replace(tzinfo=timezone.utc)
def has_backup_today():
ak = env("CLOUD_SDK_AK")
sk = env("CLOUD_SDK_SK")
bucket = env("BUCKET_NAME")
region = os.getenv("REGION", "la-south-2")
endpoint = os.getenv("OBS_ENDPOINT", f"https://obs.{region}.myhuaweicloud.com")
prefix = os.getenv("PREFIX", "")
local_tz = ZoneInfo(os.getenv("LOCAL_TZ", "America/Santiago")) if ZoneInfo else timezone.utc
today = datetime.now(local_tz).date()
obs = ObsClient(access_key_id=ak, secret_access_key=sk, server=endpoint)
marker = None
checked = 0
while True:
resp = obs.listObjects(bucket, prefix or None, marker, 1000)
if resp.status >= 300:
raise RuntimeError(f"OBS error: {resp.status} - {resp.errorCode} - {resp.errorMessage}")
objects = resp.body.contents or []
for obj in objects:
checked += 1
if obj.key.endswith("/") and getattr(obj, "size", 0) == 0:
continue
obj_date = parse_date(obj.lastModified).astimezone(local_tz).date()
if obj_date == today:
return True, checked, obj.key
if not getattr(resp.body, "is_truncated", False):
return False, checked, None
marker = getattr(resp.body, "next_marker", None) or (objects[-1].key if objects else None)
def publish_alert():
ak = env("CLOUD_SDK_AK")
sk = env("CLOUD_SDK_SK")
project_id = env("PROJECT_ID")
topic_urn = env("TOPIC_URN")
region = os.getenv("REGION", "la-south-2")
smn_endpoint = os.getenv("SMN_ENDPOINT")
credentials = BasicCredentials(ak, sk, project_id)
builder = SmnClient.new_builder().with_credentials(credentials)
if smn_endpoint:
builder = builder.with_endpoint(smn_endpoint)
else:
builder = builder.with_region(SmnRegion.value_of(region))
client = builder.build()
request = PublishMessageRequest()
request.topic_urn = topic_urn
request.body = PublishMessageRequestBody(
subject="Alerta backup OBS",
message="No se hizo backup",
time_to_live="3600"
)
return client.publish_message(request).to_dict()
def handler(event, context):
found, checked, object_key = has_backup_today()
if found:
return {
"status": "OK",
"message": "Se encontró backup de hoy.",
"checked_count": checked,
"object": object_key
}
response = publish_alert()
return {
"status": "ALERT_SENT",
"message": "No se hizo backup",
"checked_count": checked,
"smn_response": response
}