[lxc-devel] [pylxd/master] option to not store command stdout/stderr

weichweich on Github lxc-bot at linuxcontainers.org
Sun Sep 8 11:33:57 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 328 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20190908/837d1ce8/attachment.bin>
-------------- next part --------------
From 8c72aeee33b8568dd6eca182ee069197944d63b7 Mon Sep 17 00:00:00 2001
From: Archbelt <14820950+weichweich at users.noreply.github.com>
Date: Sun, 8 Sep 2019 12:02:03 +0200
Subject: [PATCH] option to not store command stdout/stderr

Signed-off-by: Archbelt <14820950+weichweich at users.noreply.github.com>
---
 pylxd/models/container.py | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/pylxd/models/container.py b/pylxd/models/container.py
index d13c26a5..e78cc23e 100644
--- a/pylxd/models/container.py
+++ b/pylxd/models/container.py
@@ -351,9 +351,9 @@ def unfreeze(self, timeout=30, force=True, wait=False):
                                wait=wait)
 
     def execute(
-            self, commands, environment={}, encoding=None, decode=True,
+            self, commands, environment=None, encoding=None, decode=True,
             stdin_payload=None, stdin_encoding="utf-8",
-            stdout_handler=None, stderr_handler=None,
+            stdout_handler=None, stderr_handler=None, store=True,
     ):
         """Execute a command on the container.
 
@@ -381,6 +381,8 @@ def execute(
         :param stderr_handler: Callable than receive as first parameter each
             message recived via stderr
         :type stderr_handler: Callable[[str], None]
+        :param store: Whether to store all captured stdout/stderr
+        :type store: bool
         :raises ValueError: if the ws4py library is not installed.
         :returns: A tuple of `(exit_code, stdout, stderr)`
         :rtype: _ContainerExecuteResult() namedtuple
@@ -390,6 +392,9 @@ def execute(
                 'This feature requires the optional ws4py library.')
         if isinstance(commands, six.string_types):
             raise TypeError("First argument must be a list.")
+        if environment is None:
+            environment = {}
+
         response = self.api['exec'].post(json={
             'command': commands,
             'environment': environment,
@@ -412,12 +417,14 @@ def execute(
             stdin.connect()
             stdout = _CommandWebsocketClient(
                 manager, self.client.websocket_url,
-                encoding=encoding, decode=decode, handler=stdout_handler)
+                encoding=encoding, decode=decode, handler=stdout_handler,
+                store=store)
             stdout.resource = '{}?secret={}'.format(parsed.path, fds['1'])
             stdout.connect()
             stderr = _CommandWebsocketClient(
                 manager, self.client.websocket_url,
-                encoding=encoding, decode=decode, handler=stderr_handler)
+                encoding=encoding, decode=decode, handler=stderr_handler,
+                store=store)
             stderr.resource = '{}?secret={}'.format(parsed.path, fds['2'])
             stderr.connect()
 
@@ -626,10 +633,12 @@ def __init__(self, manager, *args, **kwargs):
         self.decode = kwargs.pop('decode', True)
         self.encoding = kwargs.pop('encoding', None)
         self.handler = kwargs.pop('handler', None)
+        self.store = kwargs.pop('store', True)
         self.message_encoding = None
         self.finish_off = False
         self.finished = False
         self.last_message_empty = False
+        self.buffer = []
         super(_CommandWebsocketClient, self).__init__(*args, **kwargs)
 
     def handshake_ok(self):
@@ -648,7 +657,8 @@ def received_message(self, message):
             self.message_encoding = message.encoding
         if self.handler:
             self.handler(self._maybe_decode(message.data))
-        self.buffer.append(message.data)
+        if self.store:
+            self.buffer.append(message.data)
         if self.finish_off and isinstance(message, BinaryMessage):
             self.finished = True
 


More information about the lxc-devel mailing list