-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoder_audit_ttl.py
More file actions
556 lines (441 loc) · 15.3 KB
/
Copy pathcoder_audit_ttl.py
File metadata and controls
556 lines (441 loc) · 15.3 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
#!/usr/bin/env python3
"""Combined workspace audit and TTL update tool for Coder.
This script merges the useful parts of `coder_audit.py` and
`get_and_bump_ttl_workspaces.py` into one CLI:
- `report` shows running workspaces with current TTL and deadline data.
- `set-ttl` updates a workspace TTL by workspace ID.
Authentication matches the existing repository behavior:
1. Read `audit-token.txt` from the current working directory or the script
directory.
2. Fall back to the `CODER_TOKEN` environment variable.
3. Read the base URL from `CODER_URL`.
"""
from __future__ import annotations
import argparse
import json
import os
import sys
import uuid
from dataclasses import dataclass
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Iterable
import requests
try:
from tabulate import tabulate
except ImportError:
tabulate = None
REQUEST_TIMEOUT = 30
ZERO_TIME = "0001-01-01T00:00:00Z"
ALL_RESULTS_LIMIT = 0
TTL_MIN_MS = 60_000
TTL_MAX_MS = 30 * 24 * 60 * 60 * 1000
class CoderError(RuntimeError):
"""Raised when the Coder API request fails."""
@dataclass(frozen=True)
class Config:
"""Runtime configuration for the script."""
base_url: str
token: str
class CoderClient:
"""Small wrapper around the Coder API used by this script."""
def __init__(self, config: Config) -> None:
self.base_url = config.base_url.rstrip("/")
self.session = requests.Session()
self.session.headers.update(
{
"Accept": "application/json",
"Coder-Session-Token": config.token,
}
)
def request(
self,
method: str,
path: str,
*,
expected_statuses: Iterable[int] = (200,),
json_body: dict[str, Any] | None = None,
params: dict[str, Any] | None = None,
) -> Any:
url = f"{self.base_url}{path}"
response = self.session.request(
method,
url,
timeout=REQUEST_TIMEOUT,
json=json_body,
params=params,
)
if response.status_code not in tuple(expected_statuses):
raise CoderError(
f"{method} {path} failed with {response.status_code}: "
f"{response.text.strip()}"
)
if not response.content:
return None
try:
return response.json()
except json.JSONDecodeError as exc:
raise CoderError(
f"{method} {path} returned invalid JSON."
) from exc
def get_audit_logs(self) -> list[dict[str, Any]]:
payload = self.request(
"GET", "/api/v2/audit", params={"limit": ALL_RESULTS_LIMIT}
)
if isinstance(payload, dict):
logs = payload.get("audit_logs", [])
return logs if isinstance(logs, list) else []
return []
def get_workspace(self, workspace_id: str) -> dict[str, Any]:
payload = self.request("GET", f"/api/v2/workspaces/{workspace_id}")
return payload if isinstance(payload, dict) else {}
def get_workspaces(self) -> list[dict[str, Any]]:
payload = self.request(
"GET", "/api/v2/workspaces", params={"limit": ALL_RESULTS_LIMIT}
)
if isinstance(payload, dict):
workspaces = payload.get("workspaces", [])
return workspaces if isinstance(workspaces, list) else []
if isinstance(payload, list):
return payload
return []
def get_templates(self) -> dict[str, str]:
payload = self.request("GET", "/api/v2/templates")
if isinstance(payload, dict):
templates = payload.get("templates", [])
else:
templates = payload
if not isinstance(templates, list):
return {}
return {
str(template.get("id")): str(template.get("name"))
for template in templates
if template.get("id") and template.get("name")
}
def update_workspace_ttl(
self, workspace_id: str, ttl_ms: int | None, *, dry_run: bool = False
) -> None:
if dry_run:
return
self.request(
"PUT",
f"/api/v2/workspaces/{workspace_id}/ttl",
expected_statuses=(200, 204),
json_body={"ttl_ms": ttl_ms},
)
def read_token_file() -> str | None:
"""Read the token file from the working directory or script directory."""
search_paths = [
Path.cwd() / "audit-token.txt",
Path(__file__).resolve().with_name("audit-token.txt"),
]
for path in search_paths:
if path.exists():
return path.read_text(encoding="utf-8").strip()
return None
def load_config() -> Config:
token = read_token_file() or os.environ.get("CODER_TOKEN", "").strip()
if not token:
raise CoderError(
"Missing token. Set CODER_TOKEN or create audit-token.txt."
)
base_url = os.environ.get("CODER_URL", "").strip()
if not base_url:
raise CoderError("Missing CODER_URL.")
return Config(base_url=base_url, token=token)
def parse_iso8601(value: Any) -> datetime | None:
if not value or value == ZERO_TIME:
return None
if isinstance(value, datetime):
return value
if not isinstance(value, str):
return None
try:
return datetime.fromisoformat(value.replace("Z", "+00:00"))
except ValueError:
return None
def format_datetime(value: Any) -> str:
parsed = parse_iso8601(value)
if not parsed:
return "N/A"
return parsed.astimezone(timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
def format_ttl(ttl_ms: Any) -> str:
if ttl_ms is None:
return "N/A"
try:
remaining_seconds = int(ttl_ms) // 1000
except (TypeError, ValueError):
return "N/A"
if remaining_seconds < 60:
return f"{remaining_seconds}s"
minutes, seconds = divmod(remaining_seconds, 60)
if minutes < 60:
return f"{minutes}m"
hours, minutes = divmod(minutes, 60)
if hours < 24:
return f"{hours}h"
days, hours = divmod(hours, 24)
if hours:
return f"{days}d {hours}h"
return f"{days}d"
def format_time_remaining(deadline: Any) -> str:
parsed = parse_iso8601(deadline)
if not parsed:
return "N/A"
now = datetime.now(timezone.utc)
if parsed <= now:
return "Expired"
total_seconds = int((parsed - now).total_seconds())
days, rem = divmod(total_seconds, 86400)
hours, rem = divmod(rem, 3600)
minutes, seconds = divmod(rem, 60)
if days:
return f"{days}d {hours}h"
if hours:
return f"{hours}h {minutes}m"
if minutes:
return f"{minutes}m {seconds}s"
return f"{seconds}s"
def normalize_additional_fields(value: Any) -> dict[str, Any]:
if isinstance(value, dict):
return value
if isinstance(value, str):
try:
parsed = json.loads(value)
except json.JSONDecodeError:
return {}
return parsed if isinstance(parsed, dict) else {}
return {}
def latest_start_by_workspace(
audit_logs: list[dict[str, Any]],
) -> dict[str, dict[str, Any]]:
latest: dict[str, dict[str, Any]] = {}
for log in audit_logs:
if log.get("resource_type") != "workspace_build":
continue
if log.get("action") != "start":
continue
fields = normalize_additional_fields(log.get("additional_fields"))
workspace_id = fields.get("workspace_id")
if not workspace_id:
continue
log_time = parse_iso8601(log.get("time"))
if not log_time:
continue
previous = latest.get(str(workspace_id))
if previous and log_time <= previous["dt"]:
continue
user = log.get("user") or {}
latest[str(workspace_id)] = {
"dt": log_time,
"username": user.get("username") or "N/A",
"workspace_name": fields.get("workspace_name") or log.get("resource_target") or "N/A",
}
return latest
def build_report_rows(
workspaces: list[dict[str, Any]],
templates: dict[str, str],
latest_starts: dict[str, dict[str, Any]],
*,
include_stopped: bool,
owner_filter: str | None,
workspace_filter: str | None,
) -> list[list[str]]:
rows: list[list[str]] = []
owner_filter = owner_filter.lower() if owner_filter else None
workspace_filter = workspace_filter.lower() if workspace_filter else None
for workspace in workspaces:
latest_build = workspace.get("latest_build") or {}
status = str(latest_build.get("status") or "unknown")
if not include_stopped and status != "running":
continue
owner_name = str(workspace.get("owner_name") or "N/A")
workspace_name = str(workspace.get("name") or "N/A")
if owner_filter and owner_filter not in owner_name.lower():
continue
if workspace_filter and workspace_filter not in workspace_name.lower():
continue
workspace_id = str(workspace.get("id") or "")
latest_start = latest_starts.get(workspace_id, {})
template_name = (
workspace.get("template_name")
or templates.get(str(workspace.get("template_id")), "Unknown")
)
owner_info = workspace.get("owner") or {}
last_seen = owner_info.get("last_seen_at") or workspace.get("last_used_at")
rows.append(
[
owner_name,
workspace_name,
str(template_name),
status,
format_datetime(latest_start.get("dt")),
format_datetime(last_seen),
format_ttl(workspace.get("ttl_ms")),
format_time_remaining(latest_build.get("deadline")),
format_datetime(latest_build.get("deadline")),
format_datetime(latest_build.get("max_deadline")),
workspace_id or "N/A",
]
)
rows.sort(key=lambda row: (row[0].lower(), row[1].lower()))
return rows
def print_table(headers: list[str], rows: list[list[str]]) -> None:
if tabulate is not None:
print(tabulate(rows, headers=headers, tablefmt="grid"))
return
widths = [len(header) for header in headers]
for row in rows:
for index, value in enumerate(row):
widths[index] = max(widths[index], len(str(value)))
def fmt_row(values: list[str]) -> str:
return " | ".join(
str(value).ljust(widths[index]) for index, value in enumerate(values)
)
separator = "-+-".join("-" * width for width in widths)
print(fmt_row(headers))
print(separator)
for row in rows:
print(fmt_row(row))
def cmd_report(args: argparse.Namespace) -> int:
client = CoderClient(load_config())
workspaces = client.get_workspaces()
templates = client.get_templates()
audit_logs = client.get_audit_logs()
latest_starts = latest_start_by_workspace(audit_logs)
headers = [
"Username",
"Workspace",
"Template",
"Status",
"Last Start",
"Last Seen",
"TTL",
"Until Stop",
"Deadline",
"Max Deadline",
"Workspace ID",
]
rows = build_report_rows(
workspaces,
templates,
latest_starts,
include_stopped=args.include_stopped,
owner_filter=args.owner,
workspace_filter=args.workspace,
)
if args.json:
payload = [dict(zip(headers, row)) for row in rows]
print(json.dumps(payload, indent=2))
return 0
if not rows:
print("No matching workspaces found.")
return 0
print_table(headers, rows)
print(f"\nRows: {len(rows)}")
return 0
def validate_workspace_id(workspace_id: str) -> str:
try:
return str(uuid.UUID(workspace_id))
except ValueError as exc:
raise CoderError(f"Invalid workspace UUID: {workspace_id}") from exc
def validate_ttl_ms(ttl_ms: int) -> int:
if ttl_ms == 0:
return ttl_ms
if ttl_ms < TTL_MIN_MS:
raise CoderError("ttl_ms must be 0 or at least 60000.")
if ttl_ms > TTL_MAX_MS:
raise CoderError("ttl_ms must be less than or equal to 2592000000.")
return ttl_ms
def cmd_set_ttl(args: argparse.Namespace) -> int:
workspace_id = validate_workspace_id(args.workspace_id)
ttl_ms = validate_ttl_ms(args.ttl_ms)
client = CoderClient(load_config())
client.update_workspace_ttl(workspace_id, ttl_ms, dry_run=args.dry_run)
if args.dry_run:
print(
f"Dry run: would update workspace {workspace_id} to "
f"ttl_ms={ttl_ms}."
)
return 0
updated_workspace = client.get_workspace(workspace_id)
effective_ttl_ms = updated_workspace.get("ttl_ms")
effective_ttl = format_ttl(effective_ttl_ms)
if effective_ttl_ms is None:
effective_ttl = "disabled"
print(
f"Updated workspace {workspace_id} to ttl_ms={effective_ttl_ms} "
f"({effective_ttl})."
)
return 0
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description="Audit Coder workspaces and update workspace TTLs."
)
subparsers = parser.add_subparsers(dest="command", required=True)
report_parser = subparsers.add_parser(
"report",
help="Show workspace status, audit-derived last start time, and TTL data.",
)
report_parser.add_argument(
"--include-stopped",
action="store_true",
help="Include non-running workspaces in the report.",
)
report_parser.add_argument(
"--owner",
help="Filter by owner name substring.",
)
report_parser.add_argument(
"--workspace",
help="Filter by workspace name substring.",
)
report_parser.add_argument(
"--json",
action="store_true",
help="Emit JSON instead of a table.",
)
report_parser.set_defaults(func=cmd_report)
set_ttl_parser = subparsers.add_parser(
"set-ttl",
help="Update a workspace TTL by workspace ID.",
)
set_ttl_parser.add_argument("workspace_id", help="Workspace UUID.")
set_ttl_parser.add_argument(
"ttl_ms",
type=int,
help="TTL in milliseconds. Use 0 if your deployment treats that as disabled.",
)
set_ttl_parser.add_argument(
"--dry-run",
action="store_true",
help="Print what would happen without calling the API.",
)
set_ttl_parser.set_defaults(func=cmd_set_ttl)
return parser
def normalize_argv(argv: list[str]) -> list[str]:
if not argv:
return ["report"]
if argv[0] == "--set-ttl":
return ["set-ttl", *argv[1:]]
if argv[0] in {"-h", "--help"}:
return argv
if argv[0].startswith("-"):
return ["report", *argv]
return argv
def main() -> int:
parser = build_parser()
args = parser.parse_args(normalize_argv(sys.argv[1:]))
try:
return int(args.func(args))
except requests.RequestException as exc:
print(f"Network error: {exc}", file=sys.stderr)
return 1
except CoderError as exc:
print(f"Error: {exc}", file=sys.stderr)
return 1
except KeyboardInterrupt:
print("Interrupted.", file=sys.stderr)
return 130
if __name__ == "__main__":
raise SystemExit(main())