-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate_cluster_volumes.py
More file actions
259 lines (219 loc) · 8.75 KB
/
update_cluster_volumes.py
File metadata and controls
259 lines (219 loc) · 8.75 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
#!/usr/bin/env python3
"""Update EBS volume IOPS and Throughput to match a target configuration (prod or dev)."""
import json
import subprocess
import sys
from pathlib import Path
SCRIPT_DIR = Path(__file__).resolve().parent
AWS_PROFILE = "dev"
def ensure_sso_login():
result = subprocess.run(
["aws", "sts", "get-caller-identity", "--profile", AWS_PROFILE],
capture_output=True,
)
if result.returncode != 0:
print("SSO session expired or not logged in. Logging in...")
subprocess.run(["aws", "sso", "login", "--profile", "sso-main"], check=True)
ensure_sso_login()
def load_config(profile: str) -> dict:
config_file = SCRIPT_DIR / f"volumes_{profile}.json"
if not config_file.exists():
print(f"Error: config file not found: {config_file}")
sys.exit(1)
with open(config_file) as f:
return json.load(f)
def get_current_volumes(region: str, pattern: str = "javier") -> list[dict]:
result = subprocess.run(
[
"aws", "ec2", "describe-volumes",
"--profile", AWS_PROFILE,
"--region", region,
"--filters", f"Name=tag:Name,Values=*{pattern}*",
"--query",
"Volumes[].{VolumeId:VolumeId,Name:Tags[?Key==`Name`].Value|[0],Size:Size,Iops:Iops,Throughput:Throughput}",
"--output", "json",
],
capture_output=True, text=True, check=True,
)
return json.loads(result.stdout)
def init_config(regions: list[str], pattern: str, env: str):
"""Discover matching volumes and write a new config file."""
config_file = SCRIPT_DIR / f"volumes_{env}.json"
if config_file.exists():
answer = input(f"{config_file.name} already exists. Overwrite? [y/n]: ").strip().lower()
if answer != "y":
print("Aborted.")
return
config = {}
total = 0
for region in regions:
print(f"Discovering volumes in {region} matching '*{pattern}*'...")
volumes = get_current_volumes(region, pattern)
if volumes:
config[region] = volumes
for v in volumes:
print(f" {v['Name']} ({v['VolumeId']}) - {v['Size']} GiB, {v['Iops']} IOPS, {v['Throughput']} MB/s")
total += len(volumes)
else:
print(f" No matching volumes found.")
if not total:
print("\nNo volumes found. Config file not created.")
return
with open(config_file, "w") as f:
json.dump(config, f, indent=2)
f.write("\n")
print(f"\nWrote {total} volume(s) to {config_file.name}")
def get_volumes_by_ids(region: str, volume_ids: list[str]) -> list[dict]:
"""Fetch specific volumes by their IDs."""
result = subprocess.run(
[
"aws", "ec2", "describe-volumes",
"--profile", AWS_PROFILE,
"--region", region,
"--volume-ids", *volume_ids,
"--query",
"Volumes[].{VolumeId:VolumeId,Name:Tags[?Key==`Name`].Value|[0],Size:Size,Iops:Iops,Throughput:Throughput}",
"--output", "json",
],
capture_output=True, text=True, check=True,
)
return json.loads(result.stdout)
def find_diffs(config: dict) -> list[dict]:
"""Return list of volumes whose IOPS or Throughput differ from the target."""
diffs = []
for region in config:
target_by_id = {v["VolumeId"]: v for v in config[region]}
if not target_by_id:
continue
current_volumes = get_volumes_by_ids(region, list(target_by_id.keys()))
for vol in current_volumes:
vid = vol["VolumeId"]
target = target_by_id[vid]
if vol["Iops"] != target["Iops"] or vol["Throughput"] != target["Throughput"]:
diffs.append({
"Region": region,
"VolumeId": vid,
"Name": vol["Name"],
"Size": vol["Size"],
"CurrentIops": vol["Iops"],
"TargetIops": target["Iops"],
"CurrentThroughput": vol["Throughput"],
"TargetThroughput": target["Throughput"],
})
return diffs
def modify_volume(region: str, volume_id: str, iops: int, throughput: int):
subprocess.run(
[
"aws", "ec2", "modify-volume",
"--profile", AWS_PROFILE,
"--region", region,
"--volume-id", volume_id,
"--iops", str(iops),
"--throughput", str(throughput),
],
check=True,
)
def print_diff(d: dict):
print(f" Name: {d['Name']}")
print(f" Volume ID: {d['VolumeId']}")
print(f" Region: {d['Region']}")
print(f" Size: {d['Size']} GiB")
print(f" IOPS: {d['CurrentIops']} -> {d['TargetIops']}")
print(f" Throughput: {d['CurrentThroughput']} -> {d['TargetThroughput']}")
def parse_init_args(argv: list[str]) -> dict:
"""Parse --init arguments."""
result = {"regions": None, "pattern": None, "env": None}
i = 0
while i < len(argv):
if argv[i] == "--regions" and i + 1 < len(argv):
result["regions"] = argv[i + 1].split(",")
i += 2
elif argv[i] == "--pattern" and i + 1 < len(argv):
result["pattern"] = argv[i + 1]
i += 2
elif argv[i] == "--env" and i + 1 < len(argv):
result["env"] = argv[i + 1]
i += 2
else:
i += 1
return result
def main():
if len(sys.argv) < 2:
print(f"Usage:")
print(f" {sys.argv[0]} <env> [--all] [--apply]")
print(f" {sys.argv[0]} --init --regions r1,r2 --pattern NAME --env ENV")
print()
print(" By default runs in dry-run mode. Pass --apply to actually modify volumes.")
sys.exit(1)
if sys.argv[1] == "--init":
init_args = parse_init_args(sys.argv[2:])
if not all([init_args["regions"], init_args["pattern"], init_args["env"]]):
print("--init requires --regions, --pattern, and --env")
print(f" Example: {sys.argv[0]} --init --regions eu-west-1,eu-north-1 --pattern javier --env test")
sys.exit(1)
init_config(init_args["regions"], init_args["pattern"], init_args["env"])
return
profile = sys.argv[1]
args = set(sys.argv[2:])
valid_args = {"--all", "--apply"}
unknown = args - valid_args
if unknown:
print(f"Unknown argument(s): {', '.join(unknown)}")
print(f"Usage: {sys.argv[0]} <env> [--all] [--apply]")
sys.exit(1)
mode = "all" if "--all" in args else None
dry_run = "--apply" not in args
if dry_run:
print("[DRY RUN] No changes will be made. Pass --apply to modify volumes.\n")
config = load_config(profile)
print(f"Checking volumes against '{profile}' config...")
diffs = find_diffs(config)
if not diffs:
print("All volumes already match the target configuration.")
return
print(f"\n{len(diffs)} volume(s) differ from '{profile}' config:\n")
if mode == "all":
# Show all diffs, then ask once
for i, d in enumerate(diffs, 1):
print(f"[{i}] ---")
print_diff(d)
print()
if dry_run:
print("[DRY RUN] Would update all of the above.")
else:
answer = input("Update all of the above? [y/n/c] (y=yes, n=no, c=cancel): ").strip().lower()
if answer == "y":
for d in diffs:
print(f"Updating {d['Name']} ({d['VolumeId']})...")
try:
modify_volume(d["Region"], d["VolumeId"], d["TargetIops"], d["TargetThroughput"])
print(" Done.")
except subprocess.CalledProcessError as e:
print(f" FAILED: {e}")
print("\nAll updates complete.")
else:
print("Cancelled.")
else:
# Ask one by one
for d in diffs:
print("---")
print_diff(d)
if dry_run:
print(" [DRY RUN] Would ask to update this volume.\n")
else:
answer = input("Update this volume? [y/n/c] (y=yes, n=no, c=cancel): ").strip().lower()
if answer == "y":
print(f"Updating {d['Name']} ({d['VolumeId']})...")
try:
modify_volume(d["Region"], d["VolumeId"], d["TargetIops"], d["TargetThroughput"])
print(" Done.\n")
except subprocess.CalledProcessError as e:
print(f" FAILED: {e}\n")
elif answer == "c":
print("Cancelled.")
return
else:
print(" Skipped.\n")
print("Finished.")
if __name__ == "__main__":
main()