dodo_utils.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import subprocess
  2. def wait_net_service(server, port, timeout=None):
  3. """ Wait for network service to appear
  4. @param timeout: in seconds, if None or 0 wait forever
  5. @return: True of False, if timeout is None may return only True or
  6. throw unhandled network exception
  7. """
  8. import socket
  9. import errno
  10. s = socket.socket()
  11. if timeout:
  12. from time import time as now
  13. # time module is needed to calc timeout shared between two exceptions
  14. end = now() + timeout
  15. while True:
  16. try:
  17. if timeout:
  18. next_timeout = end - now()
  19. if next_timeout < 0:
  20. return False
  21. else:
  22. s.settimeout(next_timeout)
  23. s.connect((server, port))
  24. except socket.timeout as err:
  25. # this exception occurs only if timeout is set
  26. if timeout:
  27. return False
  28. except socket.error as err:
  29. # catch timeout exception from underlying network library
  30. # this one is different from socket.timeout
  31. if type(err.args) != tuple or err[0] != errno.ETIMEDOUT:
  32. raise
  33. else:
  34. s.close()
  35. return True
  36. def up2date_anyimages():
  37. cmd = "docker images -q feedati/*".split()
  38. output = subprocess.check_output(cmd).strip()
  39. if output:
  40. return True
  41. return False