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
pull/24732/head
Ganesh Nalawade 7 years ago committed by GitHub
parent f4e1dc503b
commit 16d610c1de

@ -25,6 +25,7 @@
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE # 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. # 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.basic import env_fallback, return_values
from ansible.module_utils.network_common import to_list from ansible.module_utils.network_common import to_list
from ansible.module_utils.connection import exec_command from ansible.module_utils.connection import exec_command
@ -43,18 +44,19 @@ vyos_argument_spec = {
'provider': dict(type='dict'), 'provider': dict(type='dict'),
} }
def check_args(module, warnings): def check_args(module, warnings):
provider = module.params['provider'] or {} provider = module.params['provider'] or {}
for key in vyos_argument_spec: for key in vyos_argument_spec:
if key != 'provider' and module.params[key]: if key != 'provider' and module.params[key]:
warnings.append('argument %s has been deprecated and will be ' warnings.append('argument %s has been deprecated and will be removed in a future version' % key)
'removed in a future version' % key)
if provider: if provider:
for param in ('password',): for param in ('password',):
if provider.get(param): if provider.get(param):
module.no_log_values.update(return_values(provider[param])) module.no_log_values.update(return_values(provider[param]))
def get_config(module, target='commands'): def get_config(module, target='commands'):
cmd = ' '.join(['show configuration', target]) cmd = ' '.join(['show configuration', target])
@ -63,24 +65,26 @@ def get_config(module, target='commands'):
except KeyError: except KeyError:
rc, out, err = exec_command(module, cmd) rc, out, err = exec_command(module, cmd)
if rc != 0: if rc != 0:
module.fail_json(msg='unable to retrieve current config', stderr=err) module.fail_json(msg='unable to retrieve current config', stderr=to_text(err, errors='surrogate_or_strict'))
cfg = str(out).strip() cfg = to_text(out, errors='surrogate_or_strict').strip()
_DEVICE_CONFIGS[cmd] = cfg _DEVICE_CONFIGS[cmd] = cfg
return cfg return cfg
def run_commands(module, commands, check_rc=True): def run_commands(module, commands, check_rc=True):
responses = list() responses = list()
for cmd in to_list(commands): for cmd in to_list(commands):
rc, out, err = exec_command(module, cmd) rc, out, err = exec_command(module, cmd)
if check_rc and rc != 0: if check_rc and rc != 0:
module.fail_json(msg=err, rc=rc) module.fail_json(msg=to_text(err, errors='surrogate_or_strict'), rc=rc)
responses.append(out) responses.append(to_text(out, errors='surrogate_or_strict'))
return responses return responses
def load_config(module, commands, commit=False, comment=None): def load_config(module, commands, commit=False, comment=None):
rc, out, err = exec_command(module, 'configure') rc, out, err = exec_command(module, 'configure')
if rc != 0: 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): for cmd in to_list(commands):
rc, out, err = exec_command(module, cmd) rc, out, err = exec_command(module, cmd)
@ -92,9 +96,10 @@ def load_config(module, commands, commit=False, comment=None):
diff = None diff = None
if module._diff: if module._diff:
rc, out, err = exec_command(module, 'compare') rc, out, err = exec_command(module, 'compare')
out = to_text(out, errors='surrogate_or_strict')
if not out.startswith('No changes'): if not out.startswith('No changes'):
rc, out, err = exec_command(module, 'show') rc, out, err = exec_command(module, 'show')
diff = str(out).strip() diff = to_text(out, errors='surrogate_or_strict').strip()
if commit: if commit:
cmd = 'commit' cmd = 'commit'

@ -29,23 +29,22 @@ from ansible.errors import AnsibleConnectionFailure
class TerminalModule(TerminalBase): class TerminalModule(TerminalBase):
terminal_stdout_re = [ terminal_stdout_re = [
re.compile(r"[\r\n]?[\w+\-\.:\/\[\]]+(?:\([^\)]+\)){,3}(?:>|#) ?$"), re.compile(br"[\r\n]?[\w+\-\.:\/\[\]]+(?:\([^\)]+\)){,3}(?:>|#) ?$"),
re.compile(r"\@[\w\-\.]+:\S+?[>#\$] ?$") re.compile(br"\@[\w\-\.]+:\S+?[>#\$] ?$")
] ]
terminal_stderr_re = [ terminal_stderr_re = [
re.compile(r"\n\s*Invalid command:"), re.compile(br"\n\s*Invalid command:"),
re.compile(r"\nCommit failed"), re.compile(br"\nCommit failed"),
re.compile(r"\n\s+Set failed"), re.compile(br"\n\s+Set failed"),
] ]
terminal_length = os.getenv('ANSIBLE_VYOS_TERMINAL_LENGTH', 10000) terminal_length = os.getenv('ANSIBLE_VYOS_TERMINAL_LENGTH', 10000)
def on_open_shell(self): def on_open_shell(self):
try: 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(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: except AnsibleConnectionFailure:
raise AnsibleConnectionFailure('unable to set terminal parameters') raise AnsibleConnectionFailure('unable to set terminal parameters')

@ -94,7 +94,6 @@ lib/ansible/module_utils/univention_umc.py
lib/ansible/module_utils/urls.py lib/ansible/module_utils/urls.py
lib/ansible/module_utils/vca.py lib/ansible/module_utils/vca.py
lib/ansible/module_utils/vmware.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_ami_search.py
lib/ansible/modules/cloud/amazon/_ec2_vpc.py lib/ansible/modules/cloud/amazon/_ec2_vpc.py
lib/ansible/modules/cloud/amazon/aws_kms.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/ios.py
lib/ansible/plugins/terminal/junos.py lib/ansible/plugins/terminal/junos.py
lib/ansible/plugins/terminal/nxos.py lib/ansible/plugins/terminal/nxos.py
lib/ansible/plugins/terminal/vyos.py
lib/ansible/plugins/test/core.py lib/ansible/plugins/test/core.py
lib/ansible/plugins/test/files.py lib/ansible/plugins/test/files.py
lib/ansible/plugins/test/mathstuff.py lib/ansible/plugins/test/mathstuff.py

Loading…
Cancel
Save