Skip to content

Commit

Permalink
Fix post mount actions execution on system dataset setup
Browse files Browse the repository at this point in the history
(cherry picked from commit 6753351)
  • Loading branch information
Qubad786 authored and bugclerk committed Sep 27, 2024
1 parent f62d65e commit b373a08
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
22 changes: 13 additions & 9 deletions src/middlewared/middlewared/plugins/reporting/netdata_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import subprocess

from middlewared.service import private, Service
from middlewared.utils.filesystem.copy import copytree, CopyTreeConfig

from .utils import get_netdata_state_path

Expand All @@ -24,17 +25,20 @@ def netdata_state_location(self):

@private
def post_dataset_mount_action(self):
if os.path.exists(get_netdata_state_path()):
netdata_state_path = get_netdata_state_path()
# We want to make sure this path exists always regardless of an error so that
# at least netdata can start itself gracefully
try:
os.makedirs(netdata_state_path, exist_ok=False)
except FileExistsError:
return

cp = subprocess.run(
['cp', '-a', '/var/lib/netdata', get_netdata_state_path()], check=False, capture_output=True,
)
if cp.returncode != 0:
self.logger.error('Failed to copy netdata state over from /var/lib/netdata: %r', cp.stderr.decode())
# We want to make sure this path exists always regardless of an error so that
# at least netdata can start itself gracefully
os.makedirs(get_netdata_state_path(), exist_ok=True)
try:
copytree('/var/lib/netdata', netdata_state_path, config=CopyTreeConfig())
except Exception:
self.logger.error('Failed to copy netdata state over from /var/lib/netdata', exc_info=True)
os.chown(netdata_state_path, uid=999, gid=997)
os.chmod(netdata_state_path, mode=0o755)

@private
async def start_service(self):
Expand Down
3 changes: 2 additions & 1 deletion src/middlewared/middlewared/plugins/sysdataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,8 @@ def __mount(self, pool, uuid, path=SYSDATASET_PATH):
os.chmod(mountpoint, mode_perms)

mounted = True
self.__post_mount_actions(ds_config['name'], ds_config.get('post_mount_actions', []))
if path == SYSDATASET_PATH:
self.__post_mount_actions(ds_config['name'], ds_config.get('post_mount_actions', []))

if mounted and path == SYSDATASET_PATH:
fsid = os.statvfs(SYSDATASET_PATH).f_fsid
Expand Down
8 changes: 8 additions & 0 deletions tests/api2/test_system_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,11 @@ def test_system_dataset_mountpoints():
assert ds_stats["uid"] == system_dataset_spec["chown_config"]["uid"]
assert ds_stats["gid"] == system_dataset_spec["chown_config"]["gid"]
assert ds_stats["mode"] & 0o777 == system_dataset_spec["chown_config"]["mode"]


def test_netdata_post_mount_action():
# We rely on this to make sure system dataset post mount actions are working as intended
ds_stats = call("filesystem.stat", "/var/db/system/netdata/ix_state")
assert ds_stats["uid"] == 999, ds_stats
assert ds_stats["gid"] == 997, ds_stats
assert ds_stats["mode"] & 0o777 == 0o755, ds_stats

0 comments on commit b373a08

Please sign in to comment.