[lxc-devel] [PATCH] python: Re-introduce timeout in get_ips

Stéphane Graber stgraber at ubuntu.com
Mon Jul 8 14:42:37 UTC 2013


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 at ubuntu.com>
---
 src/lxc/lxc-start-ephemeral.in |  8 +-------
 src/python-lxc/lxc/__init__.py | 26 ++++++++++++++++++++++++++
 2 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/src/lxc/lxc-start-ephemeral.in b/src/lxc/lxc-start-ephemeral.in
index 94cbf38..904f5ac 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 8f108f9..c15cfad 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.
-- 
1.8.3.2





More information about the lxc-devel mailing list