baseline/bin/get_app_history.py

47 lines
1.1 KiB
Python
Executable file

#! /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()