forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze-access.sh
More file actions
executable file
路305 lines (261 loc) 路 9.25 KB
/
Copy pathanalyze-access.sh
File metadata and controls
executable file
路305 lines (261 loc) 路 9.25 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env bash
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
cd "${REPO_ROOT}"
INPUT_FILE=""
OUTPUT_FILE="${OUTPUT_FILE:-}"
BUCKET_NAME="${BUCKET_NAME:-gistpin-backups}"
RESTORE_KEY="${RESTORE_KEY:-}"
REPORT_DIR="${REPORT_DIR:-infrastructure/ci/reports/storage-tiering}"
usage() {
cat <<'EOF'
Usage: analyze-access.sh [--input FILE] [--output FILE] [--bucket NAME] [--restore-key KEY]
Reads a JSON inventory of S3 objects and produces a storage-tiering report.
If no input is provided, the script writes an empty report and exits cleanly.
Expected JSON shape:
[
{
"key": "backups/2026-06-01.sql.gz",
"bucket": "gistpin-backups",
"storage_class": "STANDARD",
"size_bytes": 12345,
"last_accessed_days": 42,
"request_count_30d": 8
}
]
EOF
}
while [[ $# -gt 0 ]]; do
case "$1" in
--input)
INPUT_FILE="${2:-}"
shift 2
;;
--output)
OUTPUT_FILE="${2:-}"
shift 2
;;
--bucket)
BUCKET_NAME="${2:-}"
shift 2
;;
--restore-key)
RESTORE_KEY="${2:-}"
shift 2
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown argument: $1" >&2
usage >&2
exit 1
;;
esac
done
if [[ -z "${OUTPUT_FILE}" ]]; then
TIMESTAMP="$(date -u +%Y%m%dT%H%M%SZ)"
OUTPUT_FILE="${REPORT_DIR}/storage-tiering-${TIMESTAMP}.json"
fi
mkdir -p "$(dirname "${OUTPUT_FILE}")"
if ! command -v python3 >/dev/null 2>&1; then
echo "python3 is required to run access analysis." >&2
exit 1
fi
python3 - "${INPUT_FILE}" "${OUTPUT_FILE}" "${BUCKET_NAME}" "${RESTORE_KEY}" <<'PY'
from __future__ import annotations
import json
import pathlib
import shlex
import sys
from datetime import datetime, timezone
input_file, output_file, default_bucket, restore_key = sys.argv[1:5]
WEIGHTS = {
"STANDARD": 1.0,
"STANDARD_IA": 0.7,
"GLACIER_IR": 0.25,
"DEEP_ARCHIVE": 0.05,
}
DEFAULT_LIFECYCLE_RECOMMENDATION = [
{"days": 30, "storage_class": "STANDARD_IA"},
{"days": 90, "storage_class": "GLACIER_IR"},
{"days": 180, "storage_class": "DEEP_ARCHIVE"},
]
def to_int(value: object, default: int = 0) -> int:
if value in (None, ""):
return default
try:
return int(float(value))
except (TypeError, ValueError):
return default
def first_present(*values: object, default: object = "") -> object:
for value in values:
if value not in (None, ""):
return value
return default
def classify(days: int, requests: int) -> tuple[str, str, str]:
if requests >= 100 or days <= 7:
return "hot", "STANDARD", "frequently accessed"
if days <= 30 or requests >= 20:
return "warm", "STANDARD_IA", "moderate access frequency"
if days <= 90 or requests >= 1:
return "cold", "GLACIER_IR", "infrequent access"
return "archive", "DEEP_ARCHIVE", "cold archive candidate"
def weight(storage_class: str) -> float:
return WEIGHTS.get(storage_class.upper(), 1.0)
def load_inventory() -> list[dict[str, object]]:
raw = ""
input_path = pathlib.Path(input_file) if input_file else None
if input_path and input_path.exists():
raw = input_path.read_text()
elif not sys.stdin.isatty():
raw = sys.stdin.read()
if not raw.strip():
return []
payload = json.loads(raw)
if isinstance(payload, list):
return [item for item in payload if isinstance(item, dict)]
if isinstance(payload, dict):
for key in ("objects", "items", "records", "data"):
value = payload.get(key)
if isinstance(value, list):
return [item for item in value if isinstance(item, dict)]
return []
def normalize(inventory: list[dict[str, object]]) -> list[dict[str, object]]:
normalized: list[dict[str, object]] = []
for entry in inventory:
key = str(
first_present(
entry.get("key"),
entry.get("Key"),
entry.get("object_key"),
entry.get("name"),
default="",
)
)
if not key:
continue
bucket = str(
first_present(entry.get("bucket"), entry.get("Bucket"), default=default_bucket)
)
storage_class = str(first_present(entry.get("storage_class"), entry.get("StorageClass"), default="STANDARD")).upper()
size_bytes = to_int(first_present(entry.get("size_bytes"), entry.get("Size"), entry.get("size"), default=0))
last_accessed_days = to_int(
first_present(
entry.get("last_accessed_days"),
entry.get("days_since_last_access"),
entry.get("age_days"),
default=0,
)
)
request_count_30d = to_int(
first_present(
entry.get("request_count_30d"),
entry.get("requests_30d"),
entry.get("access_count_30d"),
default=0,
)
)
tier, recommended_storage_class, reason = classify(last_accessed_days, request_count_30d)
current_weight = weight(storage_class)
recommended_weight = weight(recommended_storage_class)
restore_command = None
if recommended_storage_class in {"GLACIER_IR", "DEEP_ARCHIVE"}:
restore_request = json.dumps(
{"Days": 7, "GlacierJobParameters": {"Tier": "Standard"}},
separators=(",", ":"),
)
restore_command = (
f"aws s3api restore-object --bucket {shlex.quote(bucket)} "
f"--key {shlex.quote(key)} --restore-request {shlex.quote(restore_request)}"
)
normalized.append(
{
"bucket": bucket,
"key": key,
"storage_class": storage_class,
"size_bytes": size_bytes,
"last_accessed_days": last_accessed_days,
"request_count_30d": request_count_30d,
"tier": tier,
"recommended_storage_class": recommended_storage_class,
"reason": reason,
"storage_class_changed": storage_class != recommended_storage_class,
"current_relative_cost_units": round(size_bytes * current_weight, 2),
"optimized_relative_cost_units": round(size_bytes * recommended_weight, 2),
"restore_command": restore_command,
}
)
return normalized
def summarize(objects: list[dict[str, object]]) -> dict[str, object]:
summary = {
"objects": len(objects),
"bytes": 0,
"current_relative_cost_units": 0.0,
"optimized_relative_cost_units": 0.0,
}
tier_breakdown = {
"hot": {"objects": 0, "bytes": 0},
"warm": {"objects": 0, "bytes": 0},
"cold": {"objects": 0, "bytes": 0},
"archive": {"objects": 0, "bytes": 0},
}
recommendations = []
restore_candidates = []
for obj in objects:
summary["bytes"] += obj["size_bytes"]
summary["current_relative_cost_units"] += obj["current_relative_cost_units"]
summary["optimized_relative_cost_units"] += obj["optimized_relative_cost_units"]
tier_breakdown[obj["tier"]]["objects"] += 1
tier_breakdown[obj["tier"]]["bytes"] += obj["size_bytes"]
if obj["storage_class_changed"]:
recommendations.append(
{
"bucket": obj["bucket"],
"key": obj["key"],
"current_storage_class": obj["storage_class"],
"recommended_storage_class": obj["recommended_storage_class"],
"reason": obj["reason"],
}
)
if obj["restore_command"]:
restore_candidates.append(
{
"bucket": obj["bucket"],
"key": obj["key"],
"recommended_storage_class": obj["recommended_storage_class"],
"restore_command": obj["restore_command"],
}
)
savings = 0.0
if summary["current_relative_cost_units"] > 0:
savings = round(
(1 - summary["optimized_relative_cost_units"] / summary["current_relative_cost_units"]) * 100,
2,
)
lifecycle_recommendation = DEFAULT_LIFECYCLE_RECOMMENDATION
if restore_key:
restore_candidates = [item for item in restore_candidates if item["key"] == restore_key]
return {
"generated_at": datetime.now(timezone.utc).isoformat().replace("+00:00", "Z"),
"source": input_file or "stdin",
"bucket": default_bucket,
"summary": {
**summary,
"estimated_savings_percent": savings,
},
"tier_breakdown": tier_breakdown,
"recommendations": recommendations,
"restore_candidates": restore_candidates,
"lifecycle_recommendation": lifecycle_recommendation,
}
report = summarize(normalize(load_inventory()))
pathlib.Path(output_file).write_text(json.dumps(report, indent=2, sort_keys=True) + "\n")
print(json.dumps({
"report_file": output_file,
"objects": report["summary"]["objects"],
"estimated_savings_percent": report["summary"]["estimated_savings_percent"],
"restore_candidates": len(report["restore_candidates"]),
}, sort_keys=True))
PY