Skip to content

Commit 5b87f28

Browse files
author
Nikolai Lambrov
committed
Update VBoxWrapper source reference and enhance VMUpdater functionality
- Updated the source reference for the VBoxWrapper package to a new commit. - Introduced a new method to update the VM directory in the VmUpdater class for better clarity and organization. - Added methods to register the VM in VirtualBox and move it to a group directory, improving VM management capabilities. - Enhanced logging for DVD image removal to provide more context.
1 parent 973a952 commit 5b87f28

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

frameworks/vm_manager/vm_updater.py

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,15 @@ def vm_dir(self) -> Path:
7070
Get VM directory for VM.
7171
"""
7272
if self.__vm_dir is None or not self.__vm_dir.is_dir():
73-
self.__vm_dir = Path(self.vm_config_path).parent if self.vm_config_path else Path(self.vm.info.default_vm_dir) / self.vm.name
73+
self.update_vm_dir()
7474
return self.__vm_dir
7575

76+
def update_vm_dir(self) -> None:
77+
"""
78+
Update VM directory for VM.
79+
"""
80+
self.__vm_dir = Path(self.vm_config_path).parent if self.vm_config_path else Path(self.vm.info.default_vm_dir) / self.vm.name
81+
7682
@property
7783
def vm_config_path(self) -> Optional[str]:
7884
"""
@@ -278,18 +284,42 @@ def unpack(self) -> None:
278284
File.delete(str(self.vm_dir), stdout=False, stderr=False)
279285
File.unpacking(str(self.archive_path), str(self.vm_dir), stdout=False)
280286
self._fix_unpacking_duplication()
287+
self._register_vm()
288+
self._move_to_group_dir()
281289
self._remove_useless_dvd_images()
282290
self._log(f"Unpacked VM [cyan]{self.vm.name}[/cyan] to [cyan]{self.vm_dir}[/cyan]", color='green')
283291
else:
284292
self._log(f"Archive not found or already updated on host [cyan]{self.archive_path}[/cyan]", color='magenta')
285293

294+
def _register_vm(self) -> None:
295+
"""
296+
Register VM in VirtualBox.
297+
"""
298+
if not self.vm.is_registered():
299+
vbox_file = self._find_vbox_file()
300+
if vbox_file:
301+
self.vm.register(str(vbox_file))
302+
else:
303+
self._log(f"VBox file not found on path: [cyan]{self.vm_dir}[/cyan]", color='red')
304+
305+
def _move_to_group_dir(self) -> None:
306+
"""
307+
Move VM to group directory and move remaining files from old directory.
308+
"""
309+
group_name = self.vm.get_group_name()
310+
if group_name and not group_name in str(self.vm_dir):
311+
group_dir = Path(self.vm.info.default_vm_dir) / group_name
312+
group_dir.mkdir(parents=True, exist_ok=True)
313+
self.vm.move_to(str(group_dir), move_remaining_files=True, delete_old_directory=True)
314+
self.update_vm_dir()
315+
286316
def _remove_useless_dvd_images(self) -> None:
287317
"""
288318
Remove useless DVD images from VM. If there are no DVD images, do nothing.
289319
"""
290-
images = self.vm.storage.get_all_images()
320+
images = self.vm.storage.get_dvd_images
291321
if images:
292-
self._log(f"Removing useless DVD images from VM [cyan]{self.vm.name}[/cyan]", color='yellow')
322+
self._log(f"Removing useless DVD images [cyan]{', '.join(images)}[/cyan] from VM [cyan]{self.vm.name}[/cyan]", color='yellow')
293323
self.vm.storage.remove_dvd_images()
294324

295325
def _log(self, msg: str, color: str = 'green', level: str = 'INFO') -> None:
@@ -398,6 +428,14 @@ def _compare_dates(self, s3_date_older: bool) -> bool:
398428
return s3_datetime < current_datetime if s3_date_older else s3_datetime > current_datetime
399429
return False
400430

431+
def _find_vbox_file(self) -> Optional[Path]:
432+
"""
433+
Find .vbox configuration file in VM directory.
434+
"""
435+
if self.vm_dir and self.vm_dir.is_dir():
436+
return next(self.vm_dir.glob('*.vbox'), None)
437+
return None
438+
401439
def _datetime(self, date_string: Optional[str]) -> Optional[datetime]:
402440
"""
403441
Get datetime from string.

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)