From 16d610c1dedaca8f15b87df8323d084ee34bcf1b Mon Sep 17 00:00:00 2001 From: Ganesh Nalawade Date: Wed, 17 May 2017 00:32:59 +0530 Subject: [PATCH] Add vyos changes for Python3 (#24603) * Add vyos changes for Python3 Make `execute_command` arguments and its return value complaint to PY3 changes made in PR #24431 pep8 fixes * Fix CI issues --- lib/ansible/module_utils/vyos.py | 21 +++++++++++++-------- lib/ansible/plugins/terminal/vyos.py | 15 +++++++-------- test/sanity/pep8/legacy-files.txt | 2 -- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/lib/ansible/module_utils/vyos.py b/lib/ansible/module_utils/vyos.py index 54ee4a3985..9d4ef682b3 100644 --- a/lib/ansible/module_utils/vyos.py +++ b/lib/ansible/module_utils/vyos.py @@ -25,6 +25,7 @@ # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # +from ansible.module_utils._text import to_text from ansible.module_utils.basic import env_fallback, return_values from ansible.module_utils.network_common import to_list from ansible.module_utils.connection import exec_command @@ -43,18 +44,19 @@ vyos_argument_spec = { 'provider': dict(type='dict'), } + def check_args(module, warnings): provider = module.params['provider'] or {} for key in vyos_argument_spec: if key != 'provider' and module.params[key]: - warnings.append('argument %s has been deprecated and will be ' - 'removed in a future version' % key) + warnings.append('argument %s has been deprecated and will be removed in a future version' % key) if provider: for param in ('password',): if provider.get(param): module.no_log_values.update(return_values(provider[param])) + def get_config(module, target='commands'): cmd = ' '.join(['show configuration', target]) @@ -63,24 +65,26 @@ def get_config(module, target='commands'): except KeyError: rc, out, err = exec_command(module, cmd) if rc != 0: - module.fail_json(msg='unable to retrieve current config', stderr=err) - cfg = str(out).strip() + module.fail_json(msg='unable to retrieve current config', stderr=to_text(err, errors='surrogate_or_strict')) + cfg = to_text(out, errors='surrogate_or_strict').strip() _DEVICE_CONFIGS[cmd] = cfg return cfg + def run_commands(module, commands, check_rc=True): responses = list() for cmd in to_list(commands): rc, out, err = exec_command(module, cmd) if check_rc and rc != 0: - module.fail_json(msg=err, rc=rc) - responses.append(out) + module.fail_json(msg=to_text(err, errors='surrogate_or_strict'), rc=rc) + responses.append(to_text(out, errors='surrogate_or_strict')) return responses + def load_config(module, commands, commit=False, comment=None): rc, out, err = exec_command(module, 'configure') if rc != 0: - module.fail_json(msg='unable to enter configuration mode', output=err) + module.fail_json(msg='unable to enter configuration mode', output=to_text(err, errors='surrogate_or_strict')) for cmd in to_list(commands): rc, out, err = exec_command(module, cmd) @@ -92,9 +96,10 @@ def load_config(module, commands, commit=False, comment=None): diff = None if module._diff: rc, out, err = exec_command(module, 'compare') + out = to_text(out, errors='surrogate_or_strict') if not out.startswith('No changes'): rc, out, err = exec_command(module, 'show') - diff = str(out).strip() + diff = to_text(out, errors='surrogate_or_strict').strip() if commit: cmd = 'commit' diff --git a/lib/ansible/plugins/terminal/vyos.py b/lib/ansible/plugins/terminal/vyos.py index 1a78a4695c..833808dc1f 100644 --- a/lib/ansible/plugins/terminal/vyos.py +++ b/lib/ansible/plugins/terminal/vyos.py @@ -29,23 +29,22 @@ from ansible.errors import AnsibleConnectionFailure class TerminalModule(TerminalBase): terminal_stdout_re = [ - re.compile(r"[\r\n]?[\w+\-\.:\/\[\]]+(?:\([^\)]+\)){,3}(?:>|#) ?$"), - re.compile(r"\@[\w\-\.]+:\S+?[>#\$] ?$") + re.compile(br"[\r\n]?[\w+\-\.:\/\[\]]+(?:\([^\)]+\)){,3}(?:>|#) ?$"), + re.compile(br"\@[\w\-\.]+:\S+?[>#\$] ?$") ] terminal_stderr_re = [ - re.compile(r"\n\s*Invalid command:"), - re.compile(r"\nCommit failed"), - re.compile(r"\n\s+Set failed"), + re.compile(br"\n\s*Invalid command:"), + re.compile(br"\nCommit failed"), + re.compile(br"\n\s+Set failed"), ] terminal_length = os.getenv('ANSIBLE_VYOS_TERMINAL_LENGTH', 10000) def on_open_shell(self): try: - for cmd in ['set terminal length 0', 'set terminal width 512']: + for cmd in (b'set terminal length 0', b'set terminal width 512'): self._exec_cli_command(cmd) - self._exec_cli_command('set terminal length %s' % self.terminal_length) + self._exec_cli_command(b'set terminal length %s' % self.terminal_length) except AnsibleConnectionFailure: raise AnsibleConnectionFailure('unable to set terminal parameters') - diff --git a/test/sanity/pep8/legacy-files.txt b/test/sanity/pep8/legacy-files.txt index 957ad15bd5..ac9aaf4ee5 100644 --- a/test/sanity/pep8/legacy-files.txt +++ b/test/sanity/pep8/legacy-files.txt @@ -94,7 +94,6 @@ lib/ansible/module_utils/univention_umc.py lib/ansible/module_utils/urls.py lib/ansible/module_utils/vca.py lib/ansible/module_utils/vmware.py -lib/ansible/module_utils/vyos.py lib/ansible/modules/cloud/amazon/_ec2_ami_search.py lib/ansible/modules/cloud/amazon/_ec2_vpc.py lib/ansible/modules/cloud/amazon/aws_kms.py @@ -848,7 +847,6 @@ lib/ansible/plugins/terminal/eos.py lib/ansible/plugins/terminal/ios.py lib/ansible/plugins/terminal/junos.py lib/ansible/plugins/terminal/nxos.py -lib/ansible/plugins/terminal/vyos.py lib/ansible/plugins/test/core.py lib/ansible/plugins/test/files.py lib/ansible/plugins/test/mathstuff.py