bug fixes for shell exec command (#20988)

* fixes up eos_local to call appropriate method
* fixes up ios_cli to call appropriate method
* moves json conversion to eos
This commit is contained in:
Peter Sprygada 2017-02-02 18:32:36 -05:00 committed by GitHub
parent 1df14ed121
commit bce0bd401e
4 changed files with 41 additions and 61 deletions

View file

@ -62,6 +62,7 @@ def run_commands(module, commands):
responses = list()
for cmd in to_list(commands):
cmd = module.jsonify(cmd)
rc, out, err = module.exec_command(cmd)
if rc != 0:

View file

@ -118,13 +118,13 @@ class Cli(CliBase):
def connect(self):
super(Cli, self).connect(self._module.params, kickstart=False)
self.shell.send('terminal length 0')
self.exec_command('terminal length 0')
def authorize(self):
passwd = self._module.params['auth_pass']
if passwd:
prompt = r"[\r\n]?Password: $"
self.execute(dict(command='enable', prompt=prompt, response=passwd))
self.exec_command(dict(command='enable', prompt=prompt, response=passwd))
else:
self.exec_command('enable')
@ -334,23 +334,27 @@ class Eapi:
return response['result']
for item in to_list(commands):
if all((output == 'json', is_text(item))) or all((output =='text', is_json(item))):
if item['output'] == 'json' and not is_json(item['command']):
item['command'] = '%s | json' % item['command']
if item['output'] == 'text' and is_json(item['command']):
item['command'] = str(item['command']).split('|')[0]
if all((output == 'json', is_text(item['command']))) or all((output =='text', is_json(item['command']))):
responses.extend(_send(queue, output))
queue = list()
if is_json(item):
output = 'json'
else:
output = 'text'
queue.append(item)
output = item['output'] or 'json'
queue.append(item['command'])
if queue:
responses.extend(_send(queue, output))
for index, item in enumerate(commands):
if is_text(item):
try:
responses[index] = responses[index]['output'].strip()
except KeyError:
pass
return responses

View file

@ -97,7 +97,7 @@ class Cli(CliBase):
def connect(self):
super(Cli, self).connect(self._module.params, kickstart=False)
self.shell.send('terminal length 0')
self.exec_command('terminal length 0')
def authorize(self):
passwd = self._module.params['auth_pass']
@ -108,7 +108,6 @@ class Cli(CliBase):
self.exec_command('enable')
def connection(module):
global _DEVICE_CONNECTION
if not _DEVICE_CONNECTION:

View file

@ -35,6 +35,7 @@ from ansible.module_utils.network import NetworkError
from ansible.module_utils.six import BytesIO
from ansible.module_utils._text import to_native
from ansible.module_utils.network_common import to_list, ComplexDict
from ansible.module_utils.netcli import Command
ANSI_RE = [
re.compile(r'(\x1b\[\?1h\x1b=)'),
@ -121,15 +122,6 @@ class Shell(object):
data = regex.sub('', data)
return data
def to_command(self, obj):
cast = ComplexDict({
'command': dict(key=True),
'output': dict(),
'prompt': dict(),
'response': dict()
})
return cast(obj)
def alarm_handler(self, signum, frame):
self.shell.close()
raise ShellError('timeout trying to send command: %s' % self._history[-1])
@ -161,10 +153,8 @@ class Shell(object):
exc.command = cmd['command']
raise
def send_command(self, command):
def send(self, obj):
try:
obj = self.to_command(command)
self._history.append(str(obj['command']))
cmd = '%s\r' % str(obj['command'])
@ -182,15 +172,6 @@ class Shell(object):
exc = get_exception()
return (1, '', to_native(exc))
def send(self, commands):
responses = list()
for command in to_list(commands):
rc, out, err = self.send_command(command)
if rc != 0:
raise ShellError(err)
responses.append(out)
return responses
def close(self):
self.shell.close()
@ -234,7 +215,6 @@ class CliBase(object):
self.shell = None
self._connected = False
self.default_output = 'text'
def connect(self, params, kickstart=True):
host = params['host']
@ -267,42 +247,38 @@ class CliBase(object):
self.shell.close()
self._connected = False
def authorize(self, params, **kwargs):
pass
def to_command(self, obj):
if isinstance(command, Command):
if isinstance(obj, Command):
cmdobj = dict()
cmdobj['command'] = obj.command
cmdobj['response'] = obj.response
cmdobj['prompt'] = [p.pattern for p in to_list(obj.prompt)]
return cmdobj
return obj
def execute(self, commands):
try:
for index, item in enumerate(commands):
commands[index] = to_command(item)
return self.shell.send(commands)
except ShellError:
exc = get_exception()
commands = [str(c) for c in commands]
raise NetworkError(to_native(exc), commands=commands)
def exec_command(self, command):
elif not isinstance(obj, dict):
transform = ComplexDict(dict(
command=dict(key=True),
prompt=dict(),
response=dict()
))
return transform(obj)
if not isinstance(command, dict):
command = transform(command)
else:
return obj
rc, out, err = self.shell.send_command(command)
def execute(self, commands):
try:
responses = list()
for item in to_list(commands):
item = self.to_command(item)
rc, out, err = self.shell.send(item)
if rc != 0:
raise ShellError(err)
responses.append(out)
return responses
except ShellError:
exc = get_exception()
raise NetworkError(to_native(exc))
return rc, out, err
def run_commands(self, commands):
return self.execute(to_list(commands))
run_commands = lambda self, x: self.execute(to_list(x))
exec_command = lambda self, x: self.shell.send(self.to_command(x))