Skip to content

Commit

Permalink
Expose components versions in API (#1152)
Browse files Browse the repository at this point in the history
* update worker get version

* WIP - Added debugging points to check endpoints

* Add ods + oed version to model versions.

* Include migration with additional version fields

* Add server components version info

* Bug/Fix ktools version

* Extract components version logic to model

* Remove debug tools from Dockerfile

* Update tests for server info

---------

Co-authored-by: Sam Gamble <[email protected]>
  • Loading branch information
vinulw and sambles authored Jan 8, 2025
1 parent a72bd44 commit 60f1b76
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 5 deletions.
31 changes: 29 additions & 2 deletions src/model_execution_worker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
import logging
import json
import os
import re
import tempfile
import shutil
import subprocess
from copy import deepcopy

from pathlib2 import Path
from oasislmf import __version__ as mdk_version
from ods_tools import __version__ as ods_version
from oasislmf.utils.exceptions import OasisException

from ..common.data import ORIGINAL_FILENAME, STORED_FILENAME
Expand Down Expand Up @@ -169,11 +171,34 @@ def get_model_settings(settings):
return settings_data


def get_oed_version():
try:
from ods_tools.oed.oed_schema import OedSchema
OedSchemaData = OedSchema.from_oed_schema_info(oed_schema_info=None)
return OedSchemaData.schema['version']
except Exception as e:
logging.exception("Failed to get OED version info")
return None


def get_ktools_version():
ktool_ver_str = subprocess.getoutput('fmcalc -v')

# Match version x.x.x , x.x or x
reg_pattern = "(\d+\.)?(\d+\.)?(\d+)"
match_ver = re.search(reg_pattern, ktool_ver_str)

if match_ver:
ktool_ver_str = match_ver[0]
return ktool_ver_str


def get_worker_versions():
""" Search and return the versions of Oasis components
"""
ktool_ver_str = subprocess.getoutput('fmcalc -v')
ktool_ver_str = get_ktools_version()
plat_ver_file = '/home/worker/VERSION'
oed_schma_ver = get_oed_version()

if os.path.isfile(plat_ver_file):
with open(plat_ver_file, 'r') as f:
Expand All @@ -184,7 +209,9 @@ def get_worker_versions():
return {
"oasislmf": mdk_version,
"ktools": ktool_ver_str,
"platform": plat_ver_str
"platform": plat_ver_str,
"ods-tools": ods_version,
"oed-schema": oed_schma_ver,
}


Expand Down
4 changes: 3 additions & 1 deletion src/server/oasisapi/analyses/v2_api/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,11 @@ def run_register_worker_v2(m_supplier, m_name, m_id, m_settings, m_version, m_co
model.ver_ktools = m_version['ktools']
model.ver_oasislmf = m_version['oasislmf']
model.ver_platform = m_version['platform']
model.ver_ods = m_version.get("ods-tools", None)
model.ver_oed = m_version.get("oed-schema", None)
logger.info('Updated model versions')
except Exception as e:
logger.info('Failed to set model veriosns:')
logger.info('Failed to set model versions:')
logger.exception(str(e))

# check current value of run_mode -> Set to V2 if null, if 'V1' set to both
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.2.25 on 2024-12-09 16:32

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('analysis_models', '0009_alter_analysismodel_run_mode'),
]

operations = [
migrations.AddField(
model_name='analysismodel',
name='ver_ods',
field=models.CharField(default=None, help_text='The worker ods-tools version.', max_length=255, null=True),
),
migrations.AddField(
model_name='analysismodel',
name='ver_oed',
field=models.CharField(default=None, help_text='The worker oed-schema version.', max_length=255, null=True),
),
]
2 changes: 2 additions & 0 deletions src/server/oasisapi/analysis_models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ class AnalysisModel(TimeStampedModel):
ver_ktools = models.CharField(max_length=255, null=True, default=None, help_text=_('The worker ktools version.'))
ver_oasislmf = models.CharField(max_length=255, null=True, default=None, help_text=_('The worker oasislmf version.'))
ver_platform = models.CharField(max_length=255, null=True, default=None, help_text=_('The worker platform version.'))
ver_ods = models.CharField(max_length=255, null=True, default=None, help_text=_('The worker ods-tools version.'))
ver_oed = models.CharField(max_length=255, null=True, default=None, help_text=_('The worker oed-schema version.'))
oasislmf_config = models.TextField(default='')
deleted = models.BooleanField(default=False, editable=False)
run_mode = models.CharField(max_length=max(len(c) for c in run_mode_choices._db_values),
Expand Down
2 changes: 2 additions & 0 deletions src/server/oasisapi/analysis_models/v2_api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ class Meta:
'ver_ktools',
'ver_oasislmf',
'ver_platform',
'ver_ods',
'ver_oed',
)


Expand Down
17 changes: 17 additions & 0 deletions src/server/oasisapi/info/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from ods_tools import __version__ as ods_version
import logging


def get_components_version():
components_versions = {
'ods_version': ods_version
}

try:
from ods_tools.oed.oed_schema import OedSchema
OedSchemaData = OedSchema.from_oed_schema_info(oed_schema_info=None)
components_versions['oed_version'] = OedSchemaData.schema['version']
except Exception as _:
logging.exception("Failed to get OED version info")

return components_versions
2 changes: 2 additions & 0 deletions src/server/oasisapi/info/tests/test_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,7 @@ def test_user_is_authenticated___response_is_ok(self):
self.assertEqual(200, response.status_code)
self.assertTrue('version' in server_info_json)
self.assertTrue('config' in server_info_json)
self.assertTrue('components' in server_info_json)
self.assertFalse(server_info_json['version'] is str)
self.assertFalse(server_info_json['config'] is {})
self.assertFalse(server_info_json['components'] is {})
7 changes: 6 additions & 1 deletion src/server/oasisapi/info/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from rest_framework.response import Response
from .peril import PERIL_GROUPS, PERILS
from ..schemas.custom_swagger import SERVER_INFO
from .models import get_components_version


class PerilcodesView(views.APIView):
Expand Down Expand Up @@ -75,7 +76,11 @@ def get(self, request):
server_config['ACCESS_TOKEN_LIFETIME'] = settings.SIMPLE_JWT['ACCESS_TOKEN_LIFETIME']
server_config['REFRESH_TOKEN_LIFETIME'] = settings.SIMPLE_JWT['REFRESH_TOKEN_LIFETIME']

# Components information
components_versions = get_components_version()

return Response({
'version': server_version,
'config': server_config
'config': server_config,
'components': components_versions,
})
7 changes: 6 additions & 1 deletion src/server/oasisapi/schemas/custom_swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
SERVER_INFO = Schema(
title='ServerInfo',
type='object',
required=["version", "config"],
required=["version", "config", "components"],
properties={
"version": Schema(
title='Server version',
Expand All @@ -63,6 +63,11 @@
title='Server config',
description="Oasis server public configuration",
type='object',
),
"components": Schema(
title='Components version',
description="Versions of oasis components",
type='object',
)
}
)
Expand Down

0 comments on commit 60f1b76

Please sign in to comment.