Skip to content

Commit

Permalink
Enable / disable exporter service when start / stop the service. (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
chanchiwai-ray authored Feb 29, 2024
1 parent 3114d59 commit 54bcdad
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,17 @@ def _on_cos_agent_relation_joined(self, event: EventBase) -> None:
)
event.defer()
return
self.exporter.enable()
self.exporter.start()
logger.info("Start exporter service")
logger.info("Start and enable exporter service")
self._on_update_status(event)

def _on_cos_agent_relation_departed(self, event: EventBase) -> None:
"""Remove the exporter when relation departed."""
if self._stored.exporter_installed: # type: ignore[truthy-function]
self.exporter.stop()
logger.info("Stop exporter service")
self.exporter.disable()
logger.info("Stop and disable exporter service")
self._on_update_status(event)

def _get_redfish_creds(self) -> Dict[str, str]:
Expand Down
10 changes: 10 additions & 0 deletions src/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@ def restart(self) -> None:
"""Restart the exporter daemon."""
systemd.service_restart(EXPORTER_NAME)

@check_installed
def enable(self) -> None:
"""Enable the exporter service."""
systemd.service_enable(EXPORTER_NAME)

@check_installed
def disable(self) -> None:
"""Restart the exporter service."""
systemd.service_disable(EXPORTER_NAME)

@check_installed
def check_active(self) -> bool:
"""Check if the exporter is active or not."""
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/test_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def test_20_start_okay(self, mock_service_installed):
self.harness.charm._stored.exporter_installed = True
self.harness.add_relation_unit(rid, "grafana-agent/0")
self.mock_systemd.service_start.assert_called_once()
self.mock_systemd.service_enable.assert_called_once()

@mock.patch.object(pathlib.Path, "exists", return_value=False)
def test_21_start_failed(self, mock_service_not_installed):
Expand All @@ -128,6 +129,7 @@ def test_21_start_failed(self, mock_service_not_installed):
self.harness.begin()
self.harness.add_relation_unit(rid, "grafana-agent/0")
self.mock_systemd.service_start.assert_not_called()
self.mock_systemd.service_enable.assert_not_called()

@mock.patch.object(pathlib.Path, "exists", return_value=True)
def test_22_start_defer_resource_not_ready(self, mock_service_installed):
Expand All @@ -138,6 +140,7 @@ def test_22_start_defer_resource_not_ready(self, mock_service_installed):
self.harness.charm._stored.exporter_installed = True
self.harness.add_relation_unit(rid, "grafana-agent/0")
self.mock_systemd.service_start.assert_not_called()
self.mock_systemd.service_enable.assert_not_called()

@mock.patch.object(pathlib.Path, "exists", return_value=True)
def test_23_start_defer_exporter_not_ready(self, mock_service_installed):
Expand All @@ -148,6 +151,7 @@ def test_23_start_defer_exporter_not_ready(self, mock_service_installed):
self.harness.charm._stored.exporter_installed = False
self.harness.add_relation_unit(rid, "grafana-agent/0")
self.mock_systemd.service_start.assert_not_called()
self.mock_systemd.service_enable.assert_not_called()

@mock.patch.object(pathlib.Path, "exists", return_value=True)
def test_30_stop_okay(self, mock_service_installed):
Expand All @@ -159,6 +163,7 @@ def test_30_stop_okay(self, mock_service_installed):
self.harness.add_relation_unit(rid, "grafana-agent/0")
self.harness.remove_relation_unit(rid, "grafana-agent/0")
self.mock_systemd.service_stop.assert_called_once()
self.mock_systemd.service_disable.assert_called_once()

@mock.patch.object(pathlib.Path, "exists", return_value=False)
def test_31_stop_failed(self, mock_service_not_installed):
Expand All @@ -170,6 +175,7 @@ def test_31_stop_failed(self, mock_service_not_installed):
self.harness.add_relation_unit(rid, "grafana-agent/0")
self.harness.remove_relation_unit(rid, "grafana-agent/0")
self.mock_systemd.service_stop.assert_not_called()
self.mock_systemd.service_disable.assert_not_called()

@parameterized.expand(
[
Expand Down

0 comments on commit 54bcdad

Please sign in to comment.