From a9120f2475cc28375c9a9d735061aa9f8a504eeb Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Mon, 18 Apr 2022 23:31:43 +0200 Subject: [PATCH] FEAT(bin): add get_app_history.py --- bin/get_app_history.py | 47 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 bin/get_app_history.py diff --git a/bin/get_app_history.py b/bin/get_app_history.py new file mode 100755 index 0000000..a80c48b --- /dev/null +++ b/bin/get_app_history.py @@ -0,0 +1,47 @@ +#! /usr/bin/env python +# -*- encoding: utf-8; py-indent-offset: 4 -*- + +# Author: Sebastian Mark +# CC-BY-SA (https://creativecommons.org/licenses/by-sa/4.0/deed.de) +# for civil use only + +import os +import sys + +from dateutil.parser import parse + +import yaml + + +def kubectl(cmd: str) -> str: + cmd = f"kubectl -n argocd {cmd}" + return os.popen(cmd).read() + + +def get_history(appname: str) -> dict: + app = kubectl(f"get applications {appname} -o yaml") + app_dict = yaml.safe_load(app) + revisions = {} + if app_dict: + for rev in app_dict["status"]["history"]: + revisions[rev["deployedAt"]] = rev["revision"] + return revisions + + +def main(): + try: + appname = sys.argv[1] + except IndexError: + print("Usage: get_app_history.py ", file=sys.stderr) + sys.exit(1) + + history = get_history(appname) + if len(history): + print(f"History for {appname}") + for k, v in history.items(): + date = parse(k).strftime("%Y-%m-%d %H:%M:%S") + print(f"{date}: {v}") + + +if __name__ == "__main__": + main()