-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapply-helm-chart
executable file
·86 lines (71 loc) · 2.51 KB
/
apply-helm-chart
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
#!/usr/bin/env python3
import argparse
import glob
from pathlib import Path
import subprocess
import sys
from typing import Any
import yaml
class GitHubLogGroup:
def __init__(self, message: str) -> None:
self.message = message
def __enter__(self) -> None:
print(f"::group::{self.message}")
sys.stdout.flush()
def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None:
del traceback
print("::endgroup::")
if exc_type is not None:
print(f"With {exc_value} error.")
def _thread_helmfile_project(filename: Path) -> None:
with open(filename, encoding="utf-8") as f:
helmfile = yaml.safe_load(f.read())
for nb, release in enumerate(helmfile["releases"]):
with GitHubLogGroup(f"Process release {release['name']}"):
_thread_project([f"--index={nb}", filename])
def _thread_project(args: list[str|Path]) -> None:
try:
subprocess.run(
[
"scripts/template-gen",
"--debug",
"--no-sops",
"--output=output.yaml",
*args,
],
check=True,
)
except subprocess.CalledProcessError:
print("Error during templating.")
sys.exit(1)
try:
subprocess.run(
["kubectl", "apply", "--filename=output.yaml"],
check=True,
)
subprocess.run(["kubectl", "delete", "--filename=output.yaml"])
except subprocess.CalledProcessError:
print("Error during apply.")
with open("output.yaml", encoding="utf-8") as f:
print(f.read())
sys.exit(1)
def main() -> None:
parser = argparse.ArgumentParser(description="Test to apply the HELM charts.")
parser.parse_args()
for directory_name in [
*glob.glob("apps/int/*/*/", recursive=True),
*glob.glob("apps/prod/*/*/", recursive=True),
]:
directory_path = Path(directory_name)
helmfile_path = directory_path / "helmfile.yaml"
values_path = directory_path / "values.yaml"
if helmfile_path.exists():
print(f"Process project {directory_path} as helmfile project.")
_thread_helmfile_project(helmfile_path)
elif values_path.exists():
with GitHubLogGroup(f"Process project {directory_path} ad HELM chart project."):
_thread_project([values_path])
else:
print(f"Directory {directory_path} is not a project.")
if __name__ == "__main__":
main()