Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replaces NetCli in network with CliBase in shell #17168

Merged
merged 1 commit into from
Aug 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 0 additions & 58 deletions lib/ansible/module_utils/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
from ansible.module_utils.basic import env_fallback, get_exception
from ansible.module_utils.netcli import Cli, Command
from ansible.module_utils.netcfg import Config
from ansible.module_utils.shell import Shell, ShellError, HAS_PARAMIKO

NET_TRANSPORT_ARGS = dict(
host=dict(required=True),
Expand Down Expand Up @@ -160,63 +159,6 @@ def disconnect(self):
exc = get_exception()
self.fail_json(msg=exc.message)

class NetCli(object):
"""Basic paramiko-based ssh transport any NetworkModule can use."""

def __init__(self):
if not HAS_PARAMIKO:
raise NetworkError(
msg='paramiko is required but does not appear to be installed. '
'It can be installed using `pip install paramiko`'
)

self.shell = None
self._connected = False
self.default_output = 'text'

def connect(self, params, kickstart=True, **kwargs):
host = params['host']
port = params.get('port') or 22

username = params['username']
password = params.get('password')
key_file = params.get('ssh_keyfile')
timeout = params['timeout']

try:
self.shell = Shell(
kickstart=kickstart,
prompts_re=self.CLI_PROMPTS_RE,
errors_re=self.CLI_ERRORS_RE,
)
self.shell.open(
host, port=port, username=username, password=password,
key_filename=key_file, timeout=timeout,
)
except ShellError:
exc = get_exception()
raise NetworkError(
msg='failed to connect to %s:%s' % (host, port), exc=str(exc)
)

self._connected = True

def disconnect(self, **kwargs):
self.shell.close()
self._connected = False

def authorize(self, params, **kwargs):
passwd = params['auth_pass']
self.execute(Command('enable', prompt=self.NET_PASSWD_RE, response=passwd))

def execute(self, commands, **kwargs):
try:
return self.shell.send(commands)
except ShellError:
exc = get_exception()
raise NetworkError(exc.message, commands=commands)


def register_transport(transport, default=False):
def register(cls):
NET_CONNECTIONS[transport] = cls
Expand Down
121 changes: 81 additions & 40 deletions lib/ansible/module_utils/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
#
import re
import socket

from ansible.module_utils.basic import get_exception
import time

# py2 vs py3; replace with six via ansiballz
try:
Expand All @@ -35,25 +34,11 @@
HAS_PARAMIKO = False

from ansible.module_utils.basic import get_exception
from ansible.module_utils.network import NetworkError

ANSI_RE = re.compile(r'(\x1b\[\?1h\x1b=)')

CLI_PROMPTS_RE = [
re.compile(r'[\r\n]?[a-zA-Z]{1}[a-zA-Z0-9-]*[>|#|%](?:\s*)$'),
re.compile(r'[\r\n]?[a-zA-Z]{1}[a-zA-Z0-9-]*\(.+\)#(?:\s*)$')
]

CLI_ERRORS_RE = [
re.compile(r"% ?Error"),
re.compile(r"^% \w+", re.M),
re.compile(r"% ?Bad secret"),
re.compile(r"invalid input", re.I),
re.compile(r"(?:incomplete|ambiguous) command", re.I),
re.compile(r"connection timed out", re.I),
re.compile(r"[^\r\n]+ not found", re.I),
re.compile(r"'[^']' +returned error code: ?\d+"),
re.compile(r"syntax error"),
re.compile(r"unknown command")
ANSI_RE = [
re.compile(r'(\x1b\[\?1h\x1b=)'),
re.compile(r'\x08.')
]

def to_list(val):
Expand All @@ -64,22 +49,14 @@ def to_list(val):
else:
return list()


class ShellError(Exception):

def __init__(self, msg, command=None):
super(ShellError, self).__init__(msg)
self.message = msg
self.command = command

class Command(object):

def __init__(self, command, prompt=None, response=None):
self.command = command
self.prompt = prompt
self.response = response

def __str__(self):
return self.command

class Shell(object):

Expand All @@ -90,8 +67,8 @@ def __init__(self, prompts_re=None, errors_re=None, kickstart=True):
self.kickstart = kickstart
self._matched_prompt = None

self.prompts = prompts_re or CLI_PROMPTS_RE
self.errors = errors_re or CLI_ERRORS_RE
self.prompts = prompts_re or list()
self.errors = errors_re or list()

def open(self, host, port=22, username=None, password=None,
timeout=10, key_filename=None, pkey=None, look_for_keys=None,
Expand Down Expand Up @@ -123,10 +100,13 @@ def open(self, host, port=22, username=None, password=None,
self.receive()

def strip(self, data):
return ANSI_RE.sub('', data)
for regex in ANSI_RE:
data = regex.sub('', data)
return data

def receive(self, cmd=None):
recv = StringIO()
handled = False

while True:
data = self.shell.recv(200)
Expand All @@ -136,12 +116,15 @@ def receive(self, cmd=None):

window = self.strip(recv.read())

if isinstance(cmd, Command):
self.handle_input(window, prompt=cmd.prompt,
response=cmd.response)
if hasattr(cmd, 'prompt') and not handled:
if self.handle_prompt(window, prompt=cmd.prompt, response=cmd.response):
handled = True
time.sleep(cmd.delay)
if cmd.is_reboot:
return

try:
if self.read(window):
if self.find_prompt(window):
resp = self.strip(recv.getvalue())
return self.sanitize(cmd, resp)
except ShellError:
Expand All @@ -157,7 +140,7 @@ def send(self, commands):
self.shell.sendall(cmd)
responses.append(self.receive(command))
except socket.timeout:
raise ShellError("timeout trying to send command", cmd)
raise ShellError("timeout trying to send command: %s" % cmd)
except socket.error:
exc = get_exception()
raise ShellError("problem sending command to host: %s" % exc.message)
Expand All @@ -166,7 +149,7 @@ def send(self, commands):
def close(self):
self.shell.close()

def handle_input(self, resp, prompt, response):
def handle_prompt(self, resp, prompt, response):
if not prompt or not response:
return

Expand All @@ -178,16 +161,17 @@ def handle_input(self, resp, prompt, response):
if match:
cmd = '%s\r' % ans
self.shell.sendall(cmd)
return True

def sanitize(self, cmd, resp):
cleaned = []
for line in resp.splitlines():
if line.startswith(str(cmd)) or self.read(line):
if line.startswith(str(cmd)) or self.find_prompt(line):
continue
cleaned.append(line)
return "\n".join(cleaned)

def read(self, response):
def find_prompt(self, response):
for regex in self.errors:
if regex.search(response):
raise ShellError('matched error in response: %s' % response)
Expand All @@ -197,3 +181,60 @@ def read(self, response):
if match:
self._matched_prompt = match.group()
return True


class CliBase(object):
"""Basic paramiko-based ssh transport any NetworkModule can use."""

def __init__(self):
if not HAS_PARAMIKO:
raise NetworkError(
msg='paramiko is required but does not appear to be installed. '
'It can be installed using `pip install paramiko`'
)

self.shell = None
self._connected = False
self.default_output = 'text'

def connect(self, params, kickstart=True, **kwargs):
host = params['host']
port = params.get('port') or 22

username = params['username']
password = params.get('password')
key_file = params.get('ssh_keyfile')
timeout = params['timeout']

try:
self.shell = Shell(
kickstart=kickstart,
prompts_re=self.CLI_PROMPTS_RE,
errors_re=self.CLI_ERRORS_RE,
)
self.shell.open(
host, port=port, username=username, password=password,
key_filename=key_file, timeout=timeout,
)
except ShellError:
exc = get_exception()
raise NetworkError(
msg='failed to connect to %s:%s' % (host, port), exc=str(exc)
)

self._connected = True

def disconnect(self, **kwargs):
self.shell.close()
self._connected = False

def authorize(self, params, **kwargs):
pass

def execute(self, commands, **kwargs):
try:
return self.shell.send(commands)
except ShellError:
exc = get_exception()
raise NetworkError(exc.message, commands=commands)