Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time handling #2901

Merged
merged 27 commits into from
Jun 9, 2021
Merged

Time handling #2901

merged 27 commits into from
Jun 9, 2021

Conversation

pvizeli
Copy link
Member

@pvizeli pvizeli commented May 19, 2021

Proposed change

Resolve all issues at the wrong time. It gets all information from the host and can (if needed) adjust the time also over our new whoiam service. It also allows to use of the timezone information from the host on supervisors.

We using the systemd timedate1 backend.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (which adds functionality to the supervisor)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast supervisor tests)
  • Tests have been added to verify that the new code works.

If API endpoints of add-on configuration are added/changed:

@pvizeli pvizeli added the new-feature A new feature label May 19, 2021
@pvizeli pvizeli marked this pull request as ready for review May 20, 2021 13:16
@pvizeli pvizeli added the missing-documentation Added to pull requests that needs a docs, but none is linked label May 20, 2021
Comment on lines +75 to +88
def set_time(self, utc: datetime):
"""Set time & date on host as UTC.

Return a coroutine.
"""
return self.dbus.SetTime(int(utc.timestamp() * 1000000), False, False)

@dbus_connected
def set_ntp(self, use_ntp: bool):
"""Turn NTP on or off.

Return a coroutine.
"""
return self.dbus.SetNTP(use_ntp)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that we used to do this in the past, but it has caused me quite some headaches with debugging. I think it's more readable if we use async methods and await:

Suggested change
def set_time(self, utc: datetime):
"""Set time & date on host as UTC.
Return a coroutine.
"""
return self.dbus.SetTime(int(utc.timestamp() * 1000000), False, False)
@dbus_connected
def set_ntp(self, use_ntp: bool):
"""Turn NTP on or off.
Return a coroutine.
"""
return self.dbus.SetNTP(use_ntp)
async def set_time(self, utc: datetime):
"""Set time & date on host as UTC."""
return await self.dbus.SetTime(int(utc.timestamp() * 1000000), False, False)
@dbus_connected
async def set_ntp(self, use_ntp: bool):
"""Turn NTP on or off."""
return await self.dbus.SetNTP(use_ntp)

Although I realize that @dbus_connected would need to have support for async methods.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using typing would solve the problem without making it slow and eat more filehandle which is a limited resource on the kernel. -> Awaitable[]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't set Time and NTP enough that speed or file handles are a concern.

@@ -241,7 +241,7 @@ async def check_connectivity(self):
timeout = aiohttp.ClientTimeout(total=10)
try:
await self.sys_websession.head(
"http://version.home-assistant.io/online.txt", timeout=timeout
"https://version.home-assistant.io/online.txt", timeout=timeout
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this cause errors when we check connectivity and the time is out of sync ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, but we use SSL everywhere, the whole system is not functional if the time is out of sync too much. This is our main problem for 3 years and makes that system comes never function or hang in an endless loop because we can't install or setup containers etc. This PR going to fix that close as possible for now.

from ..exceptions import WhoamiConnectivityError, WhoamiError, WhoamiSSLError
from .dt import utc_from_timestamp

_LOGGER: logging.Logger = logging.getLogger(__name__)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that typing like this will be infered by MyPy.

Suggested change
_LOGGER: logging.Logger = logging.getLogger(__name__)
_LOGGER = logging.getLogger(__name__)

@pvizeli
Copy link
Member Author

pvizeli commented May 24, 2021

We try to solve a chicken-egg problem:
image

We have 2 endpoints that allow us to not use SSL. The reason is simple, we need the endpoints to know if the setup is correct and adjust the setup to work with SSL. Now we use also TLS on DNS, but that is somethings later to address. It's the same for NetworkManager.

This shows our chicken-egg problem very good. Keep in mind, we use TLS for: docker/web/dns/git and a lot more:
image

The problem itself is a lot complex as this PR will show it and the reason why I needed almost 3 years to have a fix for it which also work without NTP to provide a working system that the user can show a UI and allow to adjust settings.

@balloob
Copy link
Member

balloob commented May 24, 2021

Ah my bad, this is the default redirect. It does work.

❯ curl -L -I http://whoami.home-assistant.io
HTTP/1.1 301 Moved Permanently
Location: https://github.com/home-assistant/whoami.home-assistant.io
❯ curl -L -I http://whoami.home-assistant.io/v1
HTTP/1.1 200 OK

@pvizeli
Copy link
Member Author

pvizeli commented May 25, 2021

Right:
http://whoami.home-assistant.io/v1 would be better instead of http://whoami.home-assistant.io/* I will adjust that. Then it's strict like the other

supervisor/dbus/timedate.py Outdated Show resolved Hide resolved
supervisor/exceptions.py Outdated Show resolved Hide resolved
supervisor/exceptions.py Outdated Show resolved Hide resolved
supervisor/host/control.py Outdated Show resolved Hide resolved

UTC = pytz.utc

GEOIP_URL = "http://ip-api.com/json/"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

Co-authored-by: Joakim Sørensen <joasoe@gmail.com>
@pvizeli pvizeli merged commit bcef340 into main Jun 9, 2021
@pvizeli pvizeli deleted the time-services branch June 9, 2021 07:38
@github-actions github-actions bot locked and limited conversation to collaborators Jun 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla-signed missing-documentation Added to pull requests that needs a docs, but none is linked new-feature A new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

missing all addons on addons store supervisor
4 participants