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 <module 'plinth.modules.datetime' from '/builds/sunilmohan/plinth/plinth/modules/datetime/__init__.py'>: 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 <sunil@medhas.org>
CC: Veiko Aasa <veiko17@disroot.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2020-05-02 18:12:36 -07:00 committed by James Valleroy
parent b1780f5e09
commit 57daf338c8
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808

View File

@ -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()