]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
neutron-rootwrap-xen-dom0 handles data from stdin
authorSimon Pasquier <simon.pasquier@bull.net>
Mon, 16 Dec 2013 12:43:38 +0000 (13:43 +0100)
committerSimon Pasquier <simon.pasquier@bull.net>
Thu, 19 Dec 2013 08:50:10 +0000 (09:50 +0100)
The neutron-rootwrap-xen-dom0 script and the netwrap plugin have been
modified to pass stdin from one to the other.

Change-Id: Ie97980873ed95f2c96eb68f8de611de1a733b130
Closes-Bug: #1259748

bin/neutron-rootwrap-xen-dom0
neutron/plugins/openvswitch/agent/xenapi/etc/xapi.d/plugins/netwrap

index 2f7dc872496d49621bf3e4d40cf3b11bd212b832..1526f17f4e87a2e4f5732ac9f33aca183235da08 100755 (executable)
@@ -26,6 +26,7 @@ from __future__ import print_function
 import ConfigParser
 import json
 import os
+import select
 import sys
 import traceback
 
@@ -107,13 +108,14 @@ def filter_command(exec_name, filters_path, user_args, exec_dirs):
         sys.exit(RC_UNAUTHORIZED)
 
 
-def run_command(url, username, password, user_args):
+def run_command(url, username, password, user_args, cmd_input):
     try:
         session = XenAPI.Session(url)
         session.login_with_password(username, password)
         host = session.xenapi.session.get_this_host(session.handle)
         result = session.xenapi.host.call_plugin(
-            host, 'netwrap', 'run_command', {'cmd': json.dumps(user_args)})
+            host, 'netwrap', 'run_command',
+            {'cmd': json.dumps(user_args), 'cmd_input': json.dumps(cmd_input)})
         return json.loads(result)
     except Exception as e:
         traceback.print_exc()
@@ -124,8 +126,15 @@ def main():
     exec_name, config_file, user_args = parse_args()
     config = load_configuration(exec_name, config_file)
     filter_command(exec_name, config['filters_path'], user_args, config['exec_dirs'])
+
+    # If data is available on the standard input, we need to pass it to the
+    # command executed in dom0
+    cmd_input = None
+    if select.select([sys.stdin,],[],[],0.0)[0]:
+        cmd_input = "".join(sys.stdin)
+
     return run_command(config['url'], config['username'], config['password'],
-                       user_args)
+                       user_args, cmd_input)
 
 
 if __name__ == '__main__':
index 6986ea483a7291a475c0e6b44cc981deb755e4fc..21909e846c7044915ceb7d6e6ff888c1d6ed22ab 100644 (file)
@@ -44,8 +44,7 @@ class PluginError(Exception):
     def __init__(self, *args):
         Exception.__init__(self, *args)
 
-
-def _run_command(cmd):
+def _run_command(cmd, cmd_input):
     """Abstracts out the basics of issuing system commands. If the command
     returns anything in stderr, a PluginError is raised with that information.
     Otherwise, the output from stdout is returned.
@@ -53,11 +52,11 @@ def _run_command(cmd):
     pipe = subprocess.PIPE
     proc = subprocess.Popen(cmd, shell=False, stdin=pipe, stdout=pipe,
                             stderr=pipe, close_fds=True)
-    proc.wait()
-    err = proc.stderr.read()
+    (out, err) = proc.communicate(cmd_input)
+
     if err:
         raise PluginError(err)
-    return proc.stdout.read()
+    return out
 
 
 def run_command(session, args):
@@ -65,7 +64,7 @@ def run_command(session, args):
     if cmd and cmd[0] not in ALLOWED_CMDS:
         msg = _("Dom0 execution of '%s' is not permitted") % cmd[0]
         raise PluginError(msg)
-    result = _run_command(cmd)
+    result = _run_command(cmd, json.loads(args.get('cmd_input', 'null')))
     return json.dumps(result)