Skip to content

Commit

Permalink
fix: expose app version to juju
Browse files Browse the repository at this point in the history
Closes #133
  • Loading branch information
nsklikas committed Sep 25, 2023
1 parent 2a1576a commit 363e685
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
12 changes: 7 additions & 5 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,10 @@ def _render_conf_file(self) -> str:
)
return rendered

def _set_version(self) -> None:
version = self._hydra_cli.get_version()
self.unit.set_workload_version(version)

def _get_database_relation_info(self) -> dict:
relation_id = self.database.relations[0].id
relation_data = self.database.fetch_relation_data()[relation_id]
Expand Down Expand Up @@ -516,16 +520,14 @@ def _on_hydra_pebble_ready(self, event: WorkloadEvent) -> None:
self._container.make_dir(path=str(self._log_dir), make_parents=True)
logger.info(f"Created directory {self._log_dir}")

self._set_version()
self._handle_status_update_config(event)

def _migration_is_needed(self):
if not self._peers:
return

return (
self._get_peer_data(DB_MIGRATION_VERSION_KEY)
!= self._hydra_cli.get_version()["version"]
)
return self._get_peer_data(DB_MIGRATION_VERSION_KEY) != self._hydra_cli.get_version()

def _on_database_created(self, event: DatabaseCreatedEvent) -> None:
"""Event Handler for database created event."""
Expand Down Expand Up @@ -575,7 +577,7 @@ def _on_database_created(self, event: DatabaseCreatedEvent) -> None:
logger.error("Automigration job failed, please use the run-migration action")
return

self._set_peer_data(DB_MIGRATION_VERSION_KEY, self._hydra_cli.get_version()["version"])
self._set_peer_data(DB_MIGRATION_VERSION_KEY, self._hydra_cli.get_version())
self._container.start(self._container_name)
self.unit.status = ActiveStatus()

Expand Down
8 changes: 2 additions & 6 deletions src/hydra_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def run_migration(self, timeout: float = 60) -> Optional[str]:
_, stderr = self._run_cmd(cmd, timeout=timeout)
return stderr

def get_version(self) -> Dict[str, str]:
def get_version(self) -> str:
"""Get the version of the hydra binary."""
cmd = ["hydra", "version"]

Expand All @@ -226,11 +226,7 @@ def get_version(self) -> Dict[str, str]:
out_re = r"Version:[ ]*(.+)\nGit Hash:[ ]*(.+)\nBuild Time:[ ]*(.+)"
versions = re.findall(out_re, stdout)[0]

return {
"version": versions[0],
"git_hash": versions[1],
"build_time": versions[2],
}
return versions[0]

def _run_cmd(
self, cmd: List[str], timeout: float = 20
Expand Down
6 changes: 1 addition & 5 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,7 @@ def mocked_get_secrets(mocker: MockerFixture) -> MagicMock:
@pytest.fixture(autouse=True)
def mocked_get_version(mocker: MockerFixture) -> MagicMock:
mock = mocker.patch("charm.HydraCLI.get_version", return_value=None)
mock.return_value = {
"version": "1.0.1",
"git_hash": "fasd23541235123321dfsdgsadg",
"build_time": "2023-06-13 16:42:24.609532580+03:00",
}
mock.return_value = "1.0.1"
return mock


Expand Down

0 comments on commit 363e685

Please sign in to comment.