Repository navigation
Expand file tree
/
Copy pathgitops_bump.py
More file actions
55 lines (39 loc) · 1.56 KB
/
Copy pathgitops_bump.py
File metadata and controls
55 lines (39 loc) · 1.56 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
#!/usr/bin/env python3
"""Update the idle color image tag in staging kustomization.yaml."""
from __future__ import annotations
import re
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
ACTIVE_COLOR_FILE = ROOT / "k8s/overlays/staging/active-color.yaml"
KUSTOMIZATION_FILE = ROOT / "k8s/overlays/staging/kustomization.yaml"
def read_active_color() -> str:
for line in ACTIVE_COLOR_FILE.read_text().splitlines():
if line.strip().startswith("ACTIVE_COLOR:"):
return line.split(":", 1)[1].strip()
raise SystemExit("ACTIVE_COLOR not found in active-color.yaml")
def idle_color(active: str) -> str:
if active == "blue":
return "green"
if active == "green":
return "blue"
raise SystemExit(f"Invalid ACTIVE_COLOR: {active}")
def bump_tag(sha_tag: str) -> tuple[str, str]:
active = read_active_color()
idle = idle_color(active)
image_name = f"backend-{idle}"
content = KUSTOMIZATION_FILE.read_text()
pattern = rf"(- name: {re.escape(image_name)}\n newName: [^\n]+\n newTag: )[^\n]+"
updated, count = re.subn(pattern, rf"\g<1>{sha_tag}", content, count=1)
if count != 1:
raise SystemExit(f"Failed to update newTag for {image_name}")
KUSTOMIZATION_FILE.write_text(updated)
return idle, image_name
def main() -> None:
if len(sys.argv) != 2:
raise SystemExit("Usage: gitops_bump.py <sha-tag>")
idle, image_name = bump_tag(sys.argv[1])
print(f"idle_color={idle}")
print(f"image_name={image_name}")
if __name__ == "__main__":
main()