From 6cc8bd73ac24c0cb4b9cbe3d7961584ea0322d01 Mon Sep 17 00:00:00 2001 From: Simon Pasquier Date: Mon, 16 Dec 2013 13:43:38 +0100 Subject: [PATCH] neutron-rootwrap-xen-dom0 handles data from stdin 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 | 15 ++++++++++++--- .../agent/xenapi/etc/xapi.d/plugins/netwrap | 11 +++++------ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/bin/neutron-rootwrap-xen-dom0 b/bin/neutron-rootwrap-xen-dom0 index 2f7dc872496..1526f17f4e8 100755 --- a/bin/neutron-rootwrap-xen-dom0 +++ b/bin/neutron-rootwrap-xen-dom0 @@ -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__': diff --git a/neutron/plugins/openvswitch/agent/xenapi/etc/xapi.d/plugins/netwrap b/neutron/plugins/openvswitch/agent/xenapi/etc/xapi.d/plugins/netwrap index 6986ea483a7..21909e846c7 100644 --- a/neutron/plugins/openvswitch/agent/xenapi/etc/xapi.d/plugins/netwrap +++ b/neutron/plugins/openvswitch/agent/xenapi/etc/xapi.d/plugins/netwrap @@ -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)