Debian Codegraph @

ansible_collections/dellemc/enterprise_sonic/plugins/terminal/sonic.py

raw symbols SCIP
1 #
2 # (c) 2020 Red Hat Inc.
3 #
4 # This file is part of Ansible
5 #
6 # Copyright (c) 2020 Dell Inc.
7 #
8 # Ansible is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Ansible is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
20 #
21 from __future__ import (absolute_import, division, print_function)
22 __metaclass__ = type
23
24 import re
25
26 from ansible.errors import AnsibleConnectionFailure
27 from ansible.plugins.terminal import TerminalBase
28
29 DOCUMENTATION = """
30 short_description: Terminal plugin module for sonic CLI modules
31 version_added: 1.0.0
32 """
33
34
35 class TerminalModule(TerminalBase):
36
37 terminal_stdout_re = [
38 re.compile(br"[\r\n]?[\w+\-\.:\/\[\]]+(?:\([^\)]+\)){,3}(?:#) ?$"),
39 re.compile(br"\[\w+\@[\w\-\.]+(?: [^\]])\] ?[>#\$] ?$"),
40 re.compile(br"\$ ?$")
41 ]
42
43 terminal_stderr_re = [
44 re.compile(br"% ?Error"),
45 re.compile(br"% ?Bad secret"),
46 re.compile(br"Syntax error:"),
47 re.compile(br"invalid input", re.I),
48 re.compile(br"(?:incomplete|ambiguous) command", re.I),
49 re.compile(br"connection timed out", re.I),
50 re.compile(br"[^\r\n]+ not found", re.I),
51 re.compile(br"'[^']' +returned error code: ?\d+"),
52 ]
53
54 def on_open_shell(self):
55 try:
56 if self._get_prompt().endswith(b'$ '):
57 self._exec_cli_command(b'sonic-cli')
58 self._exec_cli_command(b'terminal length 0')
59 except AnsibleConnectionFailure:
60 raise AnsibleConnectionFailure('unable to open sonic cli')
61
62 def on_become(self, passwd=None):
63 if self._get_prompt().endswith(b'#'):
64 return
65
66 def on_unbecome(self):
67 prompt = self._get_prompt()
68 if prompt is None:
69 # if prompt is None most likely the terminal is hung up at a prompt
70 return
71
72 if prompt.endswith(b'#'):
73 self._exec_cli_command(b'exit')