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

Question: salt-sproxy support returners ? #259

Open
network-shark opened this issue Mar 10, 2022 · 3 comments
Open

Question: salt-sproxy support returners ? #259

network-shark opened this issue Mar 10, 2022 · 3 comments
Labels
question Further information is requested

Comments

@network-shark
Copy link
Contributor

Hello @mirceaulinic ,

does salt-sproxy support using --return mycustom returner ?

I'm trying to use net.cli inside my returner , but from the logs I think the connection is already lost and then the execution of the command fails.

[DEBUG   ] read_channel:
[DEBUG   ] read_channel:
[DEBUG   ] write_channel: b'exit\n'
[DEBUG   ] LazyLoaded one_liner.returner
[ERROR   ] Cannot execute "cli" on mydevice as myuser. Reason: 'NoneType' object has no attribute 'send_command'!
[ERROR   ] Traceback (most recent call last):
  File "/home/myuser/.pyenv/versions/3.7.6/envs/py376/lib/python3.7/site-packages/salt/utils/napalm.py", line 178, in call
    out = getattr(napalm_device.get('DRIVER'), method)(*args, **kwargs)
  File "/home/myuser/.pyenv/versions/3.7.6/envs/py376/lib/python3.7/site-packages/napalm/ios/ios.py", line 2363, in cli
    output = self._send_command(command)
  File "/home/myuser/.pyenv/versions/3.7.6/envs/py376/lib/python3.7/site-packages/napalm/ios/ios.py", line 205, in _send_command
    output = self.device.send_command(command)
AttributeError: 'NoneType' object has no attribute 'send_command'
@network-shark network-shark added the question Further information is requested label Mar 10, 2022
@mirceaulinic
Copy link
Owner

Hi @network-shark - yes, returners are supported (for instance, see this line LazyLoaded one_liner.returner which shows that the one_liner Returner module has been loaded). The Returns wasn't invoked as the execution failed; the error appears to be from the IOS driver. Are you trying to capture the traceback via the Returner (I need to know, as that'd be an entirely valid use case).

@network-shark
Copy link
Contributor Author

network-shark commented Mar 14, 2022

@mirceaulinic

Are you trying to capture the traceback via the Returner (I need to know, as that'd be an entirely valid use case).

I'm not sure if I understand your question . This is how I do it.

What I did is .

Created a returner in _returners

one_liner.py

What I want to do is using the returner as a last resort to grep some value from the device config.

salt-sproxy --sync-all 'myhost' mycustommodule.myfucntion --return one_liner -ltrace

import salt.utils.json                                    
import os.path                                            
import logging                                            
log = logging.getLogger(__name__)                         
                                                          
                                                          
def returner(ret):  # pylint: disable=unused-argument     
  test = __salt__['net.cli']('show int desc')
  log.debug(test)
  return(ret)

This raises the failure from my first post.

I added some debug lines to ios.py

     def _send_command(self, command):
         """Wrapper for self.device.send.command().

         If command is a list will iterate through commands until valid command.
         """
         try:
             if isinstance(command, list):
                 for cmd in command:
                     output = self.device.send_command(cmd)
                     if "% Invalid" not in output:
                         break
             else:
                 log.debug('ios.py')
                 log.debug(self.device)
                 log.debug(command)
                 log.debug(type(command))
                 output = self.device.send_command(command)
             return self._send_command_postprocess(output)
         except (socket.error, EOFError) as e:
             raise ConnectionClosedException(str(e))

returns


[DEBUG   ] ios.py
[DEBUG   ] None
[DEBUG   ] show int desc
[DEBUG   ] <class 'str'>

@network-shark
Copy link
Contributor Author

network-shark commented Mar 15, 2022

Fix the napalm function , with this approach . returner is working. Thoughts on this ? I could create a PR on the napalm project.

NEW :

    def _send_command(self, command):
        """Wrapper for self.device.send.command().

        If command is a list will iterate through commands until valid command.
        """

        try:
            if isinstance(command, list):
                for cmd in command:
                    output = self.device.send_command(cmd)
                    if "% Invalid" not in output:
                        break
            else:
              if self.device == None:
                 self.open()
              output = self.device.send_command(command)
            return self._send_command_postprocess(output)
        except (socket.error, EOFError) as e:
            raise ConnectionClosedException(str(e))

OLD

    def _send_command(self, command):
        """Wrapper for self.device.send.command().
        If command is a list will iterate through commands until valid command.
        """
        try:
            if isinstance(command, list):
                for cmd in command:
                    output = self.device.send_command(cmd)
                    if "% Invalid" not in output:
                        break
            else:
                output = self.device.send_command(command)
            return self._send_command_postprocess(output)
        except (socket.error, EOFError) as e:
            raise ConnectionClosedException(str(e))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants