FEAT(bin): add get_app_history.py

This commit is contained in:
Sebastian Mark 2022-04-18 23:31:43 +02:00
parent d46afb0296
commit a9120f2475

47
bin/get_app_history.py Executable file
View file

@ -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 <application name>", 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()