From bcf9185bd3f4bfd5ed9ee90a9e2728e81480906f Mon Sep 17 00:00:00 2001 From: simon Date: Fri, 25 Mar 2022 15:33:09 +0700 Subject: [PATCH] implement es version check at startup --- tubearchivist/home/apps.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tubearchivist/home/apps.py b/tubearchivist/home/apps.py index 13e3986..46a940c 100644 --- a/tubearchivist/home/apps.py +++ b/tubearchivist/home/apps.py @@ -1,8 +1,10 @@ """handle custom startup functions""" import os +import sys from django.apps import AppConfig +from home.src.es.connect import ElasticWrap from home.src.es.index_setup import index_check from home.src.ta.config import AppConfig as ArchivistConfig from home.src.ta.ta_redis import RedisArchivist @@ -11,6 +13,9 @@ from home.src.ta.ta_redis import RedisArchivist class StartupCheck: """checks to run at application startup""" + MIN_MAJOR, MAX_MAJOR = 7, 7 + MIN_MINOR = 17 + def __init__(self): self.config_handler = ArchivistConfig() self.redis_con = RedisArchivist() @@ -19,6 +24,7 @@ class StartupCheck: def run(self): """run all startup checks""" print("run startup checks") + self.es_version_check() self.release_lock() index_check() self.sync_redis_state() @@ -72,6 +78,33 @@ class StartupCheck: if response: print("deleted leftover key from redis: " + lock) + def is_invalid(self, version): + """return true if es version is invalid, false if ok""" + major, minor = [int(i) for i in version.split(".")[:2]] + if not self.MIN_MAJOR <= major <= self.MAX_MAJOR: + return True + + if minor >= self.MIN_MINOR: + return False + + return True + + def es_version_check(self): + """check for minimal elasticsearch version""" + response, _ = ElasticWrap("/").get() + version = response["version"]["number"] + invalid = self.is_invalid(version) + + if invalid: + print( + "minial required elasticsearch version: " + + f"{self.MIN_MAJOR}.{self.MIN_MINOR}, " + + "please update to recommended version." + ) + sys.exit(1) + + print("elasticsearch version check passed") + class HomeConfig(AppConfig): """call startup funcs"""