Skip to content

Commit

Permalink
datastation-module-versions (#49)
Browse files Browse the repository at this point in the history
* Updated poetry lockfile
* corrected import of Console
* Test for get_dataverse_version
* Test for get_dataverse_version
* Correct calls in main
* get_payara_version
* print as JSON
* Renamed command
  • Loading branch information
janvanmansum authored Nov 28, 2023
1 parent b0fd29f commit 80354d8
Show file tree
Hide file tree
Showing 6 changed files with 467 additions and 280 deletions.
609 changes: 331 additions & 278 deletions poetry.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "dans-datastation-tools"
version = "0.34.0"
version = "0.35.0"
description = "Command line utilities for Data Station application management"
authors = ["DANS-KNAW"]
packages = [
Expand All @@ -14,7 +14,7 @@ lxml = "^4.8.0"
dicttoxml = "^1.7.4"
pyYAML = "^6.0"
psycopg = "^3.1.8"
rich = "^13.3.4"
rich = "^13.7.0"

[tool.poetry.dev-dependencies]
pytest = "^7.3.1"
Expand Down Expand Up @@ -49,3 +49,4 @@ dv-user-import = "datastation.dv_user_import:main"
dv-notifications-cleanup="datastation.dv_notifications_cleanup:main"
ingest-flow = "datastation.ingest_flow:main"
dv-dataverse-root-collect-storage-usage = "datastation.dv_dataverse_root_collect_storage_usage:main"
datastation-get-component-versions = "datastation.datastation_get_component_versions:main"
47 changes: 47 additions & 0 deletions src/datastation/common/version_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import os
from re import match


def rpm_qa():
return os.popen('rpm -qa')


evr_pattern = r'(?P<name>.*?)-(?P<version>\d+\.\d+\.\d+)-(?P<release>\d+)'
payara_version_pattern = r'Thank you for downloading Payara Server (.*).'


def get_rpm_versions(prefix):
"""Get the versions of the RPMs installed on the system."""
rpm_versions = {}
for line in rpm_qa():
if line.startswith(prefix):
evr = match(evr_pattern, line)
version = evr.group('version')
module = evr.group('name')
rpm_versions[module] = version

return rpm_versions


def get_dataverse_version(dataverse_application_path):
with open(os.path.join(dataverse_application_path, 'WEB-INF', 'classes', 'META-INF',
'microprofile-config.properties'), 'r') as f:
for line in f:
if 'dataverse.version' in line:
return (line.split('=')[1]).strip()


def get_dataverse_build_number(dataverse_application_path):
with open(os.path.join(dataverse_application_path, 'WEB-INF', 'classes', 'BuildNumber.properties'), 'r') as f:
for line in f:
if 'build.number' in line:
return (line.split('=')[1]).strip()


def get_payara_version(payara_application_path):
with open(os.path.join(payara_application_path, 'README.txt'), 'r') as f:
# Find first line that matches pattern
line = next((line for line in f if match(payara_version_pattern, line)), None)
# get subgroup 1 of the match
payara_version = match(payara_version_pattern, line).group(1)
return payara_version
41 changes: 41 additions & 0 deletions src/datastation/datastation_get_component_versions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import argparse

import rich
from rich.console import Console
from rich.table import Table

from datastation.common.config import init
from datastation.common.version_info import get_rpm_versions, get_dataverse_version, get_dataverse_build_number, \
get_payara_version


def main():
config = init()

parser = argparse.ArgumentParser(
description='Gets the version of all Data Station components in this installation.')
parser.add_argument('--json', dest='json', action='store_true', help='Output as JSON')
args = parser.parse_args()

components = get_rpm_versions(config['version_info']['dans_rpm_module_prefix'])
dataverse_version = get_dataverse_version(config['version_info']['dataverse_application_path'])
dataverse_build_number = get_dataverse_build_number(config['version_info']['dataverse_application_path'])
components['dataverse'] = f'{dataverse_version} build {dataverse_build_number}'
payara_version = get_payara_version(config['version_info']['payara_install_path'])
components['payara'] = payara_version

if args.json:
rich.print(components)
return
else:
table = Table(title="Data Station Component Versions")
table.add_column("Component")
table.add_column("Version")
for component in components:
table.add_row(component, components[component])
console = Console()
console.print(table)


if __name__ == '__main__':
main()
6 changes: 6 additions & 0 deletions src/datastation/example-dans-datastation-tools.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ reingest_files:
manage_deposit:
service_baseurl: 'http://localhost:20355'

version_info:
dans_rpm_module_prefix: 'dans.knaw.nl-'
dataverse_application_path: '/var/lib/payara5/glassfish/domains/domain1/applications/dataverse/'
payara_install_path: '/usr/local/payara5'


logging:
version: 1
root:
Expand Down
39 changes: 39 additions & 0 deletions src/tests/test_version_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from unittest.mock import patch

from datastation.common.version_info import get_rpm_versions, get_dataverse_version, get_payara_version


def test_some_modules_with_matching_prefix_found():
with patch('datastation.common.version_info.rpm_qa') as mock_qa:
mock_qa.return_value = ['dans.knaw.nl-dd-vault-metadata-2.2.0-1.noarch',
'dans.knaw.nl-dans-schema-0.10.0-1.noarch',
'python3-rpm-generators-5-8.el8.noarch',
'dans.knaw.nl-dd-verify-dataset-0.10.0-1.noarch',
]
versions = get_rpm_versions('dans.knaw.nl-')
assert versions == {
'dans.knaw.nl-dd-vault-metadata': '2.2.0',
'dans.knaw.nl-dans-schema': '0.10.0',
'dans.knaw.nl-dd-verify-dataset': '0.10.0'
}


def test_no_matching_modules_found():
with patch('datastation.common.version_info.rpm_qa') as mock_qa:
mock_qa.return_value = ['python3-rpm-generators-5-8.el8.noarch']
versions = get_rpm_versions('dans.knaw.nl-')
assert versions == {}


def test_get_dataverse_version():
with patch('builtins.open') as mock_open:
mock_open.return_value.__enter__.return_value = ['dataverse.version=5.0.1\n']
version = get_dataverse_version('/opt/dv/application')
assert version == '5.0.1'


def test_get_payara_version():
with patch('builtins.open') as mock_open:
mock_open.return_value.__enter__.return_value = ['Thank you for downloading Payara Server 5.2021.1!\n']
version = get_payara_version('/opt/payara')
assert version == '5.2021.1'

0 comments on commit 80354d8

Please sign in to comment.