-
Notifications
You must be signed in to change notification settings - Fork 0
/
argocd-sync
executable file
·79 lines (63 loc) · 2.15 KB
/
argocd-sync
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
#!/usr/bin/env python3
import argparse
import json
import re
import subprocess
import time
from typing import Any
import yaml
def run_retry(cmd: list[str], nb_times: int = 150, delay=10, **kwargs: Any) -> None:
error = None
for n in range(nb_times):
try:
return subprocess.run(cmd, **kwargs)
except subprocess.CalledProcessError as err:
print(err)
if n < nb_times - 1:
print(f"Will retry in {delay}s.")
time.sleep(delay)
error = err
raise error
def main() -> None:
"""
Sync the application on ArgoCD on each commit on GitHub master branch.
"""
parser = argparse.ArgumentParser(
description="Sync the application on ArgoCD on each commit on GitHub master branch."
)
parser.add_argument("--dry-run", help="Dry run.", action="store_true")
args = parser.parse_args()
app_list = json.loads(
run_retry(
["argocd", "app", "list", "--output=json"], stdout=subprocess.PIPE, check=True
).stdout.decode("utf-8")
)
apps = {app["metadata"]["name"] for app in app_list}
with open("data/argocd.yaml", encoding="utf-8") as no_sync_file:
no_sync_config = yaml.load(no_sync_file, Loader=yaml.SafeLoader)
no_sync_re = [re.compile(pattern) for pattern in no_sync_config["no_sync_apps_re"]]
apps_re = [re.compile(pattern) for pattern in no_sync_config["apps_re"]]
ignored_apps = set()
for app in apps:
for no_sync in no_sync_re:
if no_sync.match(app):
ignored_apps.add(app)
break
accept = False
for app_re in apps_re:
if app_re.match(app):
accept = True
break
if not accept:
ignored_apps.add(app)
sync_apps = apps - ignored_apps
print("Ignored applications:")
print("\n".join(ignored_apps))
print()
print("Application that will be synced:")
print("\n".join(sync_apps))
print()
if not args.dry_run:
run_retry(["argocd", "app", "sync", "--async"] + list(sync_apps), check=True)
if __name__ == "__main__":
main()