import ConfigParser
import json
import os
+import select
import sys
import traceback
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()
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__':
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.
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):
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)