Use a single definitive way to obtain server version and build

This commit is contained in:
allegroai 2019-12-21 18:13:05 +02:00
parent 1c87ebc900
commit 53296e8891
3 changed files with 30 additions and 37 deletions

View File

@ -1,43 +1,37 @@
from functools import lru_cache from functools import lru_cache
from pathlib import Path
from os import getenv from os import getenv
from pathlib import Path
from version import __version__
root = Path(__file__).parent.parent root = Path(__file__).parent.parent
@lru_cache() def _get(prop_name, env_suffix=None, default=""):
def get_build_number(): value = getenv(f"TRAINS_SERVER_{env_suffix or prop_name}")
try:
return (root / "BUILD").read_text().strip()
except FileNotFoundError:
return ""
@lru_cache()
def get_version():
try:
return (root / "VERSION").read_text().strip()
except FileNotFoundError:
return ""
@lru_cache()
def get_commit_number():
try:
return (root / "COMMIT").read_text().strip()
except FileNotFoundError:
return ""
@lru_cache()
def get_deployment_type() -> str:
value = getenv("TRAINS_SERVER_DEPLOYMENT_TYPE")
if value: if value:
return value return value
try: try:
value = (root / "DEPLOY").read_text().strip() return (root / prop_name).read_text().strip()
except FileNotFoundError: except FileNotFoundError:
pass return default
return value or "manual"
@lru_cache()
def get_build_number():
return _get("BUILD")
@lru_cache()
def get_version():
return _get("VERSION", default=__version__)
@lru_cache()
def get_commit_number():
return _get("COMMIT")
@lru_cache()
def get_deployment_type() -> str:
return _get("DEPLOY", env_suffix="DEPLOYMENT_TYPE", default="manual")

View File

@ -11,7 +11,6 @@ from database.errors import translate_errors_context
from database.model import Company from database.model import Company
from database.model.company import ReportStatsOption from database.model.company import ReportStatsOption
from service_repo import ServiceRepo, APICall, endpoint from service_repo import ServiceRepo, APICall, endpoint
from version import __version__ as current_version
@endpoint("server.get_stats") @endpoint("server.get_stats")
@ -79,7 +78,7 @@ def report_stats(call: APICall, company: str, request: ReportStatsOptionRequest)
stats_option = ReportStatsOption( stats_option = ReportStatsOption(
enabled=enabled, enabled=enabled,
enabled_time=datetime.utcnow(), enabled_time=datetime.utcnow(),
enabled_version=current_version, enabled_version=get_version(),
enabled_user=call.identity.user, enabled_user=call.identity.user,
) )
updated = query.update(defaults__stats_option=stats_option) updated = query.update(defaults__stats_option=stats_option)
@ -88,7 +87,7 @@ def report_stats(call: APICall, company: str, request: ReportStatsOptionRequest)
f"Failed setting report_stats to {enabled}" f"Failed setting report_stats to {enabled}"
) )
data = stats_option.to_mongo() data = stats_option.to_mongo()
data["current_version"] = current_version data["current_version"] = get_version()
result = ReportStatsOptionResponse(**data) result = ReportStatsOptionResponse(**data)
call.result.data_model = result call.result.data_model = result

View File

@ -8,8 +8,8 @@ import requests
from semantic_version import Version from semantic_version import Version
from config import config from config import config
from config.info import get_version
from database.model.settings import Settings from database.model.settings import Settings
from version import __version__ as current_version
log = config.logger(__name__) log = config.logger(__name__)
@ -48,7 +48,7 @@ class CheckUpdatesThread(Thread):
response = requests.get( response = requests.get(
url, url,
json={"versions": {self.component_name: str(current_version)}, "uid": uid}, json={"versions": {self.component_name: str(get_version())}, "uid": uid},
timeout=float( timeout=float(
config.get("apiserver.check_for_updates.request_timeout_sec", 3.0) config.get("apiserver.check_for_updates.request_timeout_sec", 3.0)
), ),
@ -65,7 +65,7 @@ class CheckUpdatesThread(Thread):
if not latest_version: if not latest_version:
return return
cur_version = Version(current_version) cur_version = Version(get_version())
latest_version = Version(latest_version) latest_version = Version(latest_version)
if cur_version >= latest_version: if cur_version >= latest_version:
return return