1#!/usr/bin/env python3

2#

3# This file is part of Checkbox.

4#

5# Copyright 2024 Canonical Ltd.

6# Written by:

7# Pierre Equoy <pierre.equoy@canonical.com>

8#

9# Checkbox is free software: you can redistribute it and/or modify

10# it under the terms of the GNU General Public License version 3,

11# as published by the Free Software Foundation.

12#

13# Checkbox is distributed in the hope that it will be useful,

14# but WITHOUT ANY WARRANTY; without even the implied warranty of

15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

16# GNU General Public License for more details.

17#

18# You should have received a copy of the GNU General Public License

19# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.

20#

21

22 import argparse

23 import subprocess

24 import sys

25

26 from checkbox_support.helpers.retry import retry

27

28

29@retry(max_attempts=5, delay=60)

30 def http_connect(url):

31 """

32 Use `wget` to try to connect to `url`. If attempt fails, the next one is

33 made after adding a random delay calculated using a backoff and a jitter.

34 """

35 subprocess.run(

36 [

37 "wget",

38 "-SO",

39 "/dev/null",

40 url,

41 ],

42 check=True,

43 )

44

45

46 def main(args):

47 parser = argparse.ArgumentParser()

48 parser.add_argument("url", help="URL to try to connect to")

49 args = parser.parse_args(args)

50 try:

51 http_connect(args.url)

52 except subprocess.CalledProcessError as e:

53 raise SystemExit(e)

54

55

56 if __name__ == "__main__":

57 sys.exit(main(sys.argv[1:]))