From 57daf338c835cb9e9969b9b307a967ea3597f33f Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Sat, 2 May 2020 18:12:36 -0700 Subject: [PATCH] datetime: Refactor handling systemd-timesyncd not running in VMs - Merged the two DateTimeApp classes. In future, we will remove all module.init() methods in favor of automatically performing the operations from module_loader. - Also fix an error running './run --list-dependencies' when running without systemd support inside a test case container: ERROR plinth.module_loader Exception while running init for : Command '['systemctl', 'show', '--property=ConditionResult', '--value', 'systemd-timesyncd']' returned non-zero exit status 1. Traceback (most recent call last): File "/builds/sunilmohan/plinth/plinth/module_loader.py", line 123, in _initialize_module init() File "/builds/sunilmohan/plinth/plinth/modules/datetime/__init__.py", line 77, in init if _is_time_managed(): File "/builds/sunilmohan/plinth/plinth/modules/datetime/__init__.py", line 112, in _is_time_managed output = subprocess.check_output([ File "/usr/lib/python3.8/subprocess.py", line 411, in check_output return run(*popenargs, stdout=PIPE, timeout=timeout, check=True, File "/usr/lib/python3.8/subprocess.py", line 512, in run raise CalledProcessError(retcode, process.args, subprocess.CalledProcessError: Command '['systemctl', 'show', '--property=ConditionResult', '--value', 'systemd-timesyncd']' returned non-zero exit status 1. Signed-off-by: Sunil Mohan Adapa CC: Veiko Aasa Reviewed-by: James Valleroy --- plinth/modules/datetime/__init__.py | 64 ++++++++++++++--------------- 1 file changed, 31 insertions(+), 33 deletions(-) diff --git a/plinth/modules/datetime/__init__.py b/plinth/modules/datetime/__init__.py index 1832b3de8..e92a6c00d 100644 --- a/plinth/modules/datetime/__init__.py +++ b/plinth/modules/datetime/__init__.py @@ -29,12 +29,37 @@ _description = [ app = None -class UnmanagedDateTimeApp(app_module.App): +class DateTimeApp(app_module.App): """FreedomBox app for date and time if time syncronization is unmanaged.""" app_id = 'datetime' - can_be_disabled = False + _time_managed = None + + @property + def can_be_disabled(self): + """Return whether the app can be disabled.""" + return self._is_time_managed() + + def _is_time_managed(self): + """Check whether time should be syncronized by the systemd-timesyncd. + + systemd-timesyncd does not run if we have another NTP daemon installed + or FreedomBox runs inside a container where the host manages the time. + + """ + if self._time_managed is None: + try: + output = subprocess.check_output([ + 'systemctl', 'show', '--property=ConditionResult', + '--value', 'systemd-timesyncd' + ]) + self._time_managed = 'yes' in output.decode() + except subprocess.CalledProcessError: + # When systemd is not running. + self._time_managed = False + + return self._time_managed def __init__(self): """Create components for the app.""" @@ -50,18 +75,9 @@ class UnmanagedDateTimeApp(app_module.App): 'datetime:index', parent_url_name='system') self.add(menu_item) - -class ManagedDateTimeApp(UnmanagedDateTimeApp): - """FreedomBox app for date and time if time syncronization is managed.""" - - can_be_disabled = True - - def __init__(self): - """Create components for the app.""" - super().__init__() - - daemon = Daemon('daemon-datetime', managed_services[0]) - self.add(daemon) + if self._is_time_managed(): + daemon = Daemon('daemon-datetime', managed_services[0]) + self.add(daemon) def diagnose(self): """Run diagnostics and return the results.""" @@ -73,11 +89,7 @@ class ManagedDateTimeApp(UnmanagedDateTimeApp): def init(): """Initialize the date/time module.""" global app - - if _is_time_managed(): - app = ManagedDateTimeApp() - else: - app = UnmanagedDateTimeApp() + app = DateTimeApp() if app.is_enabled(): app.set_enabled(True) @@ -100,17 +112,3 @@ def _diagnose_time_synchronized(): pass return [_('Time synchronized to NTP server'), result] - - -def _is_time_managed(): - """Check whether time should be syncronized by the systemd-timesyncd. - - systemd-timesyncd does not run if we have another NTP daemon installed or - FreedomBox runs inside a container where the host manages the time. - - """ - output = subprocess.check_output([ - 'systemctl', 'show', '--property=ConditionResult', '--value', - 'systemd-timesyncd' - ]) - return 'yes' in output.decode()