Commit f05cde25 authored by Antoine Millet's avatar Antoine Millet

Rewrote external command execution

parent 62e4e1ce
......@@ -41,7 +41,7 @@ from cloudcontrol.common.client.plugins import Base as BasePlugin, rpc_handler
from cloudcontrol.common.jobs import JobsManager, JobsStore
from cloudcontrol.common.helpers.logger import patch_logging; patch_logging()
from cloudcontrol.node.utils import EvPopen
from cloudcontrol.node.utils import execute
from cloudcontrol.node.exc import RemoteExecutionError
from cloudcontrol.node.host import tags
from cloudcontrol.node.host.jobs import NodeJobsManagerInterface, ScriptJob
......@@ -279,43 +279,19 @@ class Handler(BasePlugin):
@rpc_handler
def execute_command(self, command, stdin=None):
"""Execute an arbitrary shell command on the host.
command = ['/bin/sh', '-c', command]
rcode, output = execute(self.main, command, stdin)
:param string command: shell command to run
:param string stdin: string to use as stdin on command
"""
logger.debug('Executing command %s', command)
try:
remote_command = EvPopen(self.main, command, close_fds=True,
shell=True, stdout=PIPE, stdin=PIPE,
stderr=STDOUT)
except Exception:
logger.exception('Error while starting subprocess for executing '
' command %s', command)
raise
self.commands.add(remote_command)
try:
stdout, _ = remote_command.communicate(stdin)
except Exception:
logger.exception('Error while communicating with subprocess for'
' command %s', command)
raise
self.commands.remove(remote_command)
if remote_command.returncode != 0:
if rcode != 0:
# 127 means command not found, 126 means not executable
if remote_command.returncode == 127:
raise RemoteExecutionError('Command not found: %s' % stdout)
elif remote_command.returncode == 126:
if rcode == 127:
raise RemoteExecutionError('Command not found: %s' % output)
elif rcode == 126:
raise RemoteExecutionError('Command is not executable')
else:
raise RemoteExecutionError('Child exited with non zero status %s' % rcode)
raise RemoteExecutionError('Child exited with non zero status %s' %
remote_command.returncode)
return stdout
return output
@rpc_handler
def node_shutdown(self, reboot=True, gracefull=True):
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment