Skip to content

Commit

Permalink
Refactor logic to retrieve host paths with their datasets:
Browse files Browse the repository at this point in the history
  • Loading branch information
sonicaj committed Dec 19, 2024
1 parent eefaaa0 commit bea1e6e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
19 changes: 18 additions & 1 deletion src/middlewared/middlewared/plugins/apps/resources.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from middlewared.schema import accepts, Bool, Dict, Int, List, Ref, returns, Str
from middlewared.service import private, Service
from middlewared.service import CallError, private, Service

from middlewared.utils.gpu import get_nvidia_gpus

Expand Down Expand Up @@ -142,3 +142,20 @@ async def gpu_choices_internal(self):
await self.middleware.call('device.get_gpus'),
await self.middleware.run_in_thread(get_nvidia_gpus),
)

@private
async def get_hostpaths_datasets(self, app_name):
app_info = self.middleware.call_sync('app.get_instance', app_name)
host_paths = [
volume['source_path'] for volume in app_info['active_workloads']['volumes']
if volume['source'].startswith(f'{IX_APPS_MOUNT_PATH}/') is False
]

mapping = {}
for host_path in host_paths:
try:
mapping[host_path] = await self.middleware.call('zfs.dataset.path_to_dataset', host_path)
except CallError:
mapping[host_path] = None

return mapping
31 changes: 13 additions & 18 deletions src/middlewared/middlewared/plugins/apps/upgrade.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging
from pkg_resources import parse_version

from middlewared.plugins.docker.state_utils import IX_APPS_MOUNT_PATH
from middlewared.schema import accepts, Bool, Dict, List, Str, Ref, returns
from middlewared.service import CallError, job, private, Service, ValidationErrors

Expand All @@ -24,31 +23,27 @@ class Config:
@private
def take_snapshot_of_hostpath(self, app, snapshot_hostpath):
app_info = self.middleware.call_sync('app.get_instance', app) if isinstance(app, str) else app
host_paths = [
volume['source_path'] for volume in app_info['active_workloads']['volumes']
if volume['source'].startswith(f'{IX_APPS_MOUNT_PATH}/') is False
]
host_path_mapping = self.middleware.call_sync('app.get_host_path_mapping', app_info['name'])
# Stop the app itself before we attempt to take snapshots
self.middleware.call_sync('app.stop', app_info['name']).wait_sync()
if not snapshot_hostpath:
return

if host_paths:
if host_path_mapping:
logger.debug('Taking snapshots of host paths for %r app', app_info['name'])

for host_path in host_paths:
if host_path.startswith('/mnt/') is False:
logger.debug(
'Skipping %r host path for %r app\'s snapshot as it is not under /mnt', host_path, app_info['name']
)
continue
for host_path, dataset in host_path_mapping.items():
if not dataset:
if host_path.startswith('/mnt/') is False:
logger.debug(
'Skipping %r host path for %r app\'s snapshot as it is not under /mnt', host_path, app_info['name']
)
else:
logger.debug(
'Skipping %r host path for %r app\'s snapshot as it is not a dataset', host_path,
app_info['name']
)

try:
dataset = self.middleware.call_sync('zfs.dataset.path_to_dataset', host_path)
except CallError:
logger.debug(
'Skipping %r host path for %r app\'s snapshot as it is not a dataset', host_path, app_info['name']
)
continue

snap_name = f'{dataset}@{app_info["version"]}'
Expand Down

0 comments on commit bea1e6e

Please sign in to comment.