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 <stgraber@ubuntu.com>
This commit is contained in:
Stéphane Graber 2013-07-08 10:41:32 -04:00
parent 18efb001a4
commit b0f9616f62
2 changed files with 27 additions and 7 deletions

View File

@ -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:

View File

@ -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.