Verified Commit 76bd7cb7 authored by Karel Koci's avatar Karel Koci 🤘
Browse files

Replace ping with curl for network check

In reality we need to download files from repo.turris.cz so it makes
sense to try perform real download rather than just ping.

This also changes when we check for network. We check every time that is
enabled. It makes sense as that way we can cover some temporally service
dropouts even when not running from cron.

There are other tiny tweaks such as that we no longer use name of
package in package itself and rather import relatively.
parent c6087fc5
import sys
import argparse
from svupdater import autorun
from svupdater.prerun import random_sleep, wait_for_network
from svupdater._supervisor import run
from svupdater.const import PKGUPDATE_TIMEOUT, PKGUPDATE_TIMEOUT_KILL
from svupdater.const import PING_TIMEOUT
from svupdater.utils import daemonize, report
from . import autorun
from .prerun import random_sleep, wait_for_network
from ._supervisor import run
from .const import PKGUPDATE_TIMEOUT, PKGUPDATE_TIMEOUT_KILL
from .const import TURRIS_REPO_HEALTH_TIMEOUT
from .utils import daemonize, report
HELP_DESCRIPTION = """
Updater-ng supervisor used for system updating.
......@@ -29,11 +29,11 @@ def parse_arguments():
Sleep random amount of the time with maximum of given number of seconds. In default two hours are
used.
""")
prs.add_argument('--wait-for-network', const=PING_TIMEOUT, type=int, default=10,
prs.add_argument('--wait-for-network', const=TURRIS_REPO_HEALTH_TIMEOUT, type=int, default=10,
nargs='?', help="""
Check if Turris repository is accessible (even before going to background). You can specify timeout
in seconds as an argument. 10 seconds is used if no argument is specified. Specify zero to disable
network check.
Check if Turris repository is accessible before running updater. You can specify timeout in seconds
as an argument. 10 seconds is used if no argument is specified. Specify zero to disable network
check.
""")
prs.add_argument('--no-network-fail', action='store_true',
help="""
......@@ -75,9 +75,10 @@ def main():
if args.rand_sleep > 0:
random_sleep(args.rand_sleep)
if not wait_for_network(args.wait_for_network) and not args.no_network_fail:
report("There seems to be no network connection to Turris servers. Please try again later.")
sys.exit(1)
if not wait_for_network(args.wait_for_network) and args.no_network_fail:
report("There seems to be no network connection to Turris servers. Please try again later.")
sys.exit(1)
sys.exit(run(
ensure_run=args.ensure_run,
......
......@@ -23,11 +23,10 @@ PKGUPDATE_TIMEOUT = 3000
# pkgupdate default kill timeout
PKGUPDATE_TIMEOUT_KILL = 60
# Address we ping to check if we have Internet connection
PING_ADDRESS = "repo.turris.cz"
# Maximum number of secomds we wait for network (testing if we can ping
# PING_ADDRESS)
PING_TIMEOUT = 10
# The URL used to check if we can correctly access files on repo.turris.cz
TURRIS_REPO_HEALTH_URL = "https://repo.turris.cz/.health"
# Default timeout used to check if repo is accessible
TURRIS_REPO_HEALTH_TIMEOUT = 10
# Files used for approvals handling.
APPROVALS_ASK_FILE = "/usr/share/updater/need_approval"
......
......@@ -21,17 +21,12 @@ def random_sleep(max_seconds: int):
time.sleep(suspend)
def ping(address: str = const.PING_ADDRESS, count: int = 1, deadline: int = 1) -> bool:
"""Ping address with given amount of pings and deadline.
Returns True on success and False if ping fails.
def turris_repo_health(address: str = const.TURRIS_REPO_HEALTH_URL) -> bool:
"""Tries to receive provided address and checks if result is "ok".
Returns True on success and False if download in any way fails.
"""
with open(os.devnull, 'w') as devnull:
return subprocess.call(
['ping', '-c', str(count), '-w', str(deadline), address],
stdin=devnull,
stdout=devnull,
stderr=devnull
) == 0
res = subprocess.run(['curl', address], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True)
return res.returncode == 0 and res.stdout == "ok\n"
def wait_for_network(max_stall: int) -> typing.Optional[bool]:
......@@ -43,10 +38,17 @@ def wait_for_network(max_stall: int) -> typing.Optional[bool]:
def network_test():
"Run network test (expected to be run as subprocess)"
if not ping():
if not turris_repo_health():
utils.report("Waiting for network connection")
while not ping():
pass
delay = 2
while True:
now = time.time()
if turris_repo_health():
return
sleep_time = delay - time.time() - now
if sleep_time > 0:
time.sleep(sleep_time)
delay *= 2
if max_stall is None:
return None # None means no stall
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment