From: Stéphane Graber Date: Mon, 8 Jul 2013 14:41:32 +0000 (-0400) Subject: python: Re-introduce timeout in get_ips X-Git-Tag: lxc-1.0.0.alpha1~1^2~138 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b0f9616f6227f56dce8ca2514610f432ba4fab8a;p=thirdparty%2Flxc.git python: Re-introduce timeout in get_ips It turns out that most API users want some kind of timeout option for get_ips, so instead of re-implementing it in every single client software, let's just have it as a python overlay upstream. Signed-off-by: Stéphane Graber --- diff --git a/src/lxc/lxc-start-ephemeral.in b/src/lxc/lxc-start-ephemeral.in index 94cbf3800..904f5ac94 100644 --- a/src/lxc/lxc-start-ephemeral.in +++ b/src/lxc/lxc-start-ephemeral.in @@ -36,7 +36,6 @@ import os import sys import subprocess import tempfile -import time _ = gettext.gettext gettext.textdomain("lxc-start-ephemeral") @@ -260,12 +259,7 @@ if not args.command and not args.daemon: sys.exit(0) # Try to get the IP addresses -ips = None -timeout = 5 -while not ips and timeout != 0: - ips = dest.get_ips() - time.sleep(1) - timeout -= 1 +ips = dest.get_ips(timeout=5) # Deal with the case where we just print info about the container if args.daemon: diff --git a/src/python-lxc/lxc/__init__.py b/src/python-lxc/lxc/__init__.py index 8f108f94e..c15cfad0b 100644 --- a/src/python-lxc/lxc/__init__.py +++ b/src/python-lxc/lxc/__init__.py @@ -26,6 +26,7 @@ import glob import os import subprocess import stat +import time import warnings warnings.warn("The python-lxc API isn't yet stable " @@ -353,6 +354,31 @@ class Container(_lxc.Container): else: return value + def get_ips(self, interface=None, family=None, scope=None, timeout=0): + """ + Get a tuple of IPs for the container. + """ + + kwargs = {} + if interface: + kwargs['interface'] = interface + if family: + kwargs['family'] = family + if scope: + kwargs['scope'] = scope + + ips = None + + while not ips: + ips = _lxc.Container.get_ips(self, **kwargs) + if timeout == 0: + break + + timeout -= 1 + time.sleep(1) + + return ips + def set_config_item(self, key, value): """ Set a config key to a provided value.