Skip to content

Run Mitogen as a Module #380

Closed
Closed
@jmcgrath207

Description

@jmcgrath207

I am trying to run Mitogen as a module for my environment but I am having an issue that is similiar too @366 where it had a issue with 'Router' object has no attribute 'importer'.

Is there any way not to use if __name__ == "__main__":?

  • Have you tried the latest master version from Git?
    yes
  • Mention your host and target OS and versions
    host: Arch Linux
    target: Red Hat 6
  • Mention your host and target Python versions
    Both Python 3.6.4

CODE

import mitogen.master

import mitogen.core
from MitogenService import MitogenParentContextType
import logging
import mitogen.utils





class MitogenSshClass(object):

    def __init__(self,debug=False):

        self.broker = mitogen.master.Broker()
        self.router = mitogen.master.Router(self.broker)
        if debug:
            logging.basicConfig(level=logging.DEBUG)




    def closeAllConnections(self):
        self.broker.shutdown()



    def createConnection(self,hostname: str, username: str, password: str,
                 python_path: str = 'customPython',via: MitogenParentContextType = None):


        return self.router.ssh(
            hostname=hostname,
            username=username,
            password=password,
            python_path=python_path,
            via=via
        )

    def __enter__(self):
        None

    def __exit__(self,*args):
        if mitogen.core.CallError in args:
            print('Call failed:')
            for x in args:
                print(x)
            self.broker.shutdown()
        elif KeyboardInterrupt in args:
            for x in args:
                print(x)
            self.broker.shutdown()
        elif Exception in args:
            for x in args:
                print(x)
            self.broker.shutdown()
        else:
            None



prodIP = 'prod'
prodUsername = 'username'
prodPassword = 'password'


hostIP='host'
hostUserName='username'
hostPassword='password'

mitogenSshClass = MitogenSshClass(debug=True)

prod = mitogenSshClass.createConnection( hostname=prodIP, username=prodUsername, password=prodPassword,
                 python_path= 'customPython')

host = mitogenSshClass.createConnection( hostname=hostIP, username=hostUserName, password=hostPassword,
                 python_path= 'customPython',via= prod)


def Call_func():
    print("something")


with mitogenSshClass:
    host.call(Call_func)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions