[lxc-devel] [pylxd/master] Add Container.execute_with_result

rockstar on Github lxc-bot at linuxcontainers.org
Wed Nov 16 18:11:44 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 664 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20161116/1937d9fe/attachment.bin>
-------------- next part --------------
From a4eb1eb7127355d60f0f965a9c7ec51c43218280 Mon Sep 17 00:00:00 2001
From: Paul Hummer <paul.hummer at canonical.com>
Date: Wed, 16 Nov 2016 11:09:03 -0700
Subject: [PATCH 1/2] Prevent request 2.12+ for now

---
 requirements.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/requirements.txt b/requirements.txt
index 62882d4..82a132d 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -2,6 +2,6 @@ pbr>=1.6
 python-dateutil>=2.4.2
 six>=1.9.0
 ws4py!=0.3.5,>=0.3.4  # 0.3.5 is broken for websocket support
-requests!=2.8.0,>=2.5.2
+requests!=2.8.0,>=2.5.2,<2.12.0  # 2.12.0+ IDNA support causes breakages
 requests-unixsocket>=0.1.5
 cryptography>=1.4

From dd721cf393137645fbecb4c12686dc858c48cf21 Mon Sep 17 00:00:00 2001
From: Paul Hummer <paul.hummer at canonical.com>
Date: Wed, 16 Nov 2016 11:09:21 -0700
Subject: [PATCH 2/2] Add Container.execute_with_result

Container.execute needs a sane, safe way to return more properties
than just stdout/stderr. Container.execute_with_result will return
a ContainerExecuteResult with the exit code, stdout and stdin, and
provides a mechanism for returning more properties in the future.

Container.execute now raises a DeprecationWarning as its return
value will change in 2.2
---
 pylxd/models/container.py | 21 ++++++++++++++++++++-
 pylxd/tests/mock_lxd.py   |  2 +-
 2 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/pylxd/models/container.py b/pylxd/models/container.py
index 9ef27db..460bc28 100644
--- a/pylxd/models/container.py
+++ b/pylxd/models/container.py
@@ -11,6 +11,7 @@
 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 #    License for the specific language governing permissions and limitations
 #    under the License.
+import collections
 import time
 
 import six
@@ -24,6 +25,7 @@
     _ws4py_installed = False
 
 from pylxd import managers
+from pylxd.deprecation import deprecated
 from pylxd.models import _model as model
 from pylxd.models.operation import Operation
 
@@ -36,6 +38,11 @@ def __init__(self, **kwargs):
             setattr(self, key, value)
 
 
+_ContainerExecuteResult = collections.namedtuple(
+    'ContainerExecuteResult',
+    ['exit_code', 'stdout', 'stderr'])
+
+
 class Container(model.Model):
     """An LXD Container.
 
@@ -186,8 +193,18 @@ def unfreeze(self, timeout=30, force=True, wait=False):
                                force=force,
                                wait=wait)
 
+    @deprecated('execute will return a ContainerExecuteResult in pylxd 2.2')
     def execute(self, commands, environment={}):
         """Execute a command on the container."""
+        result = self.execute_with_result(commands, environment)
+        return result.stdout, result.stderr
+
+    def execute_with_result(self, commands, environment={}):
+        """Execute a command on the container.
+
+        In pylxd 2.2, this method will be renamed `execute` and the existing
+        `execute` method removed.
+        """
         if not _ws4py_installed:
             raise ValueError(
                 'This feature requires the optional ws4py library.')
@@ -222,7 +239,9 @@ def execute(self, commands, environment={}):
         while len(manager.websockets.values()) > 0:
             time.sleep(.1)
 
-        return stdout.data, stderr.data
+        operation = self.client.operations.get(operation_id)
+        return _ContainerExecuteResult(
+            operation.metadata['return'], stdout.data, stderr.data)
 
     def migrate(self, new_client, wait=False):
         """Migrate a container.
diff --git a/pylxd/tests/mock_lxd.py b/pylxd/tests/mock_lxd.py
index b2fd6af..10a3273 100644
--- a/pylxd/tests/mock_lxd.py
+++ b/pylxd/tests/mock_lxd.py
@@ -583,7 +583,7 @@ def profile_GET(request, context):
     {
         'text': json.dumps({
             'type': 'sync',
-            'metadata': {'id': 'operation-abc'},
+            'metadata': {'id': 'operation-abc', 'metadata': {'return': 0}},
             }),
         'method': 'GET',
         'url': r'^http://pylxd.test/1.0/operations/operation-abc$',


More information about the lxc-devel mailing list