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
This commit is contained in:
Simon Pasquier 2013-12-16 13:43:38 +01:00
parent 22886eb49f
commit 6cc8bd73ac
2 changed files with 17 additions and 9 deletions

View File

@ -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__':

View 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)