diff --git a/check-release.py b/check-release.py new file mode 100755 index 00000000..e3511538 --- /dev/null +++ b/check-release.py @@ -0,0 +1,79 @@ +#!/usr/bin/python3 + +import argparse +import requests +import sys +from datetime import date, datetime +from dateutil.relativedelta import relativedelta + +verbose = False + +def cli_parser(): + parser = argparse.ArgumentParser( + description="Fedora '/etc/os-release' support date validator." + ) + + parser.add_argument( + "--test", "-t", + type=str, + required=True, + help="Test to perform [future, compare]" + ) + + parser.add_argument( + "--verbose", "-v", + action="store_true", + help="Prints detailed info on the screen." + ) + + return parser.parse_args() + +def log(*args, **kwargs): + print(*args, **kwargs) if verbose else None + +def get_file_support() -> date: + """ Returns the support date from the os-release file. """ + with open('/etc/os-release', 'r') as release: + lines = release.readlines() + log("File content read.") + + support_day = datetime.fromtimestamp(0) + for line in lines: + if "SUPPORT_END" in line: + key, value = line.split('=') + value = value.strip() + support_day = date.fromisoformat(value) + return support_day + +def support_date_in_future() -> bool: + """ This function checks the support date from the os-release + file, compares it with the current system date and tests if + the os-release support date lies at least 12 months in the future. """ + + # Get the necessary values from the operating system. + today = datetime.today().date() + log("Current date on tested system =>", today) + tomorrow = today + relativedelta(years=1) + log(f"Minimal future EOL =>", tomorrow) + support = get_file_support() + log(f"/etc/os-release SUPPORT_END =>", support) + + # Test if the support end date is in the future. + result = False + if support >= tomorrow: + log("SUPPORT_END is in the future.") + log("Test passed.") + result = True + else: + log("Test failed.") + result = False + return result + +args = cli_parser() +verbose = args.verbose +result = support_date_in_future() +if result: + sys.exit(12) +else: + sys.exit(25) +