Skip to content

Commit

Permalink
fix: properly disable/enable service, set permissions (#101)
Browse files Browse the repository at this point in the history
* fix: properly disable and enable kafka service, set permissions on all dirs

* chore: update utest patches

* chore: remove old debug message

* chore: recursively update perms

* chore: append files and dirs
  • Loading branch information
marcoppenheimer authored May 3, 2023
1 parent 0dcca26 commit 60e4f0d
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@
from snap import KafkaSnap
from structured_config import CharmConfig
from tls import KafkaTLS
from utils import broker_active, generate_password, safe_get_file
from utils import (
broker_active,
generate_password,
safe_get_file,
set_snap_mode_bits,
set_snap_ownership,
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -206,6 +212,8 @@ def _on_storage_attached(self, event: StorageAttachedEvent) -> None:
# set status only for running services, not on startup
if self.snap.active():
self._set_status(Status.ADDED_STORAGE)
set_snap_ownership(path=self.snap.DATA_PATH)
set_snap_mode_bits(path=self.snap.DATA_PATH)
self._on_config_changed(event)

def _on_storage_detaching(self, event: StorageDetachingEvent) -> None:
Expand Down
1 change: 0 additions & 1 deletion src/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ def peer_relation(self) -> Optional[Relation]:

def _on_relation_created(self, event: RelationCreatedEvent) -> None:
"""Handler for `kafka-client-relation-created` event."""
logger.info("RELATION CREATED - CALLING CONFIG CHANGED")
self.charm._on_config_changed(event)

def _on_relation_broken(self, event: RelationBrokenEvent) -> None:
Expand Down
4 changes: 2 additions & 2 deletions src/snap.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ def disable_enable(self) -> None:
Raises:
subprocess.CalledProcessError if error occurs
"""
subprocess.run(f"snap disable {self.SNAP_SERVICE}", shell=True)
subprocess.run(f"snap enable {self.SNAP_SERVICE}", shell=True)
subprocess.run(f"snap disable {self.SNAP_NAME}", shell=True)
subprocess.run(f"snap enable {self.SNAP_NAME}", shell=True)

@retry(
wait=wait_fixed(1),
Expand Down
8 changes: 8 additions & 0 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,19 @@ def set_snap_ownership(path: str) -> None:
"""Sets a filepath `snap_daemon` ownership."""
shutil.chown(path, user="snap_daemon", group="root")

for root, dirs, files in os.walk(path):
for fp in dirs + files:
shutil.chown(os.path.join(root, fp), user="snap_daemon", group="root")


def set_snap_mode_bits(path: str) -> None:
"""Sets filepath mode bits."""
os.chmod(path, 0o770)

for root, dirs, files in os.walk(path):
for fp in dirs + files:
os.chmod(os.path.join(root, fp), 0o770)


def generate_password() -> str:
"""Creates randomized string for use as app passwords.
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ def test_storage_add_does_nothing_if_snap_not_active(harness, zk_data, passwords
with (
patch("snap.KafkaSnap.active", return_value=False),
patch("charm.KafkaCharm._disable_enable_restart") as patched_restart,
patch("charm.set_snap_ownership"),
patch("charm.set_snap_mode_bits"),
):
harness.add_storage(storage_name="data", count=2)
harness.attach_storage(storage_id="data/1")
Expand All @@ -232,6 +234,8 @@ def test_storage_add_defers_if_service_not_healthy(harness, zk_data, passwords_d
patch("charm.KafkaCharm.healthy", return_value=False),
patch("charm.KafkaCharm._disable_enable_restart") as patched_restart,
patch("ops.framework.EventBase.defer") as patched_defer,
patch("charm.set_snap_ownership"),
patch("charm.set_snap_mode_bits"),
):
harness.add_storage(storage_name="data", count=2)
harness.attach_storage(storage_id="data/1")
Expand All @@ -258,6 +262,8 @@ def test_storage_add_disableenables_and_starts(harness, zk_data, passwords_data)
patch("snap.KafkaSnap.disable_enable") as patched_disable_enable,
patch("snap.KafkaSnap.start_snap_service") as patched_start,
patch("ops.framework.EventBase.defer") as patched_defer,
patch("charm.set_snap_ownership"),
patch("charm.set_snap_mode_bits"),
):
harness.add_storage(storage_name="data", count=2)
harness.attach_storage(storage_id="data/1")
Expand Down
1 change: 1 addition & 0 deletions tests/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ def test_set_environment(harness):
patch("config.safe_write_to_file") as patched_write,
patch("builtins.open", mock_open()),
patch("shutil.chown"),
patch("utils.set_snap_ownership"),
):
harness.charm.kafka_config.set_environment()

Expand Down

0 comments on commit 60e4f0d

Please sign in to comment.