From f1b1532f78e63ebd8e09735b413ec1ad8b8ad658 Mon Sep 17 00:00:00 2001 From: OpenGnSys Support Team Date: Tue, 1 Oct 2024 21:53:57 +0200 Subject: live: refactor poweroff and reboot - enable WoL - check for poweroff and reboot before checking for busybox. - call new shutdown method --- src/live/ogOperations.py | 54 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/live/ogOperations.py b/src/live/ogOperations.py index 2451c9b..74e4c09 100644 --- a/src/live/ogOperations.py +++ b/src/live/ogOperations.py @@ -342,32 +342,58 @@ class OgLiveOperations: with open(logfile, 'wb', 0) as f: f.truncate(0) - def _poweroff_oglive(self, operation='poweroff'): - interface = get_ethernet_interface() + def _enable_wol(self, interface): cmd_ethtool = shlex.split(f'ethtool -s {interface} wol g') + if subprocess.run(cmd_ethtool).returncode != 0: + logging.error('Error running ethtool subprocess') + + def _shutdown(self, interface, cmd): cmd_browser = shlex.split('pkill -9 browser') - if shutil.which('busybox'): + if shutil.which('browser') and subprocess.run(cmd_browser).returncode != 0: + logging.error('Error terminating ogBrowser process') + + umount_all() + umount_cache() + + ret = subprocess.run(cmd) + if ret.returncode != 0: + logging.error(f'Error running {cmd} subprocess') + + def _poweroff_oglive(self, interface): + self._enable_wol(interface) + if shutil.which('poweroff'): + busybox = '' + elif shutil.which('busybox'): busybox = 'busybox ' else: + logging.warning('No poweroff binary found') busybox = '' - cmd_busybox = shlex.split(f'{busybox}{operation}') - umount_all() - umount_cache() - if subprocess.run(cmd_ethtool).returncode != 0: - logging.error('Error running ethtool subprocess') - if shutil.which('browser') and subprocess.run(cmd_browser).returncode != 0: - logging.error('Error terminating ogBrowser process') - if subprocess.run(cmd_busybox) != 0: - logging.error('Error running "busybox poweroff" subprocess') + cmd_poweroff = shlex.split(f'{busybox}poweroff') + self._shutdown(interface, cmd_poweroff) def poweroff(self): logging.info('Powering off client') - self._poweroff_oglive() + interface = get_ethernet_interface() + self._poweroff_oglive(interface) + + def _reboot_oglive(self, interface): + self._enable_wol(interface) + if shutil.which('reboot'): + busybox = '' + elif shutil.which('busybox'): + busybox = 'busybox ' + else: + logging.warning('No reboot binary found') + busybox = '' + + cmd_reboot = shlex.split(f'{busybox}reboot') + self._shutdown(interface, cmd_reboot) def reboot(self): logging.info('Rebooting client') - self._poweroff_oglive(operation='reboot') + interface = get_ethernet_interface() + self._reboot_oglive(interface) def shellrun(self, request, ogRest): cmd = request.getrun() -- cgit v1.2.3-18-g5258