Package: jk-invoke
jk_invokeIntroductionThis python module provides a high level interface to provide a clean API for executing commands and reading and writing files transparently: It doesn't matter if you operate locally or remotely or as a regular user or as root via sudo.Information about this module can be found here:github.orgpypi.python.orgWhy this module?If you need to automate system administration tasks it is convenient if you do not need to distinguish between local or remote operation. This library wraps aroundfabric(which in turn wraps aroundparamikoandinvoke) in order to streamline system administration tasks.Limitations of this moduleThis module provides capabilities for:executing commands and process the data returnedread individual filesstore individual filesNothing more. So it's use is quite limited. Nevertheless this exactly fits requirements for system administration.How to use this moduleImport this modulePlease include this module into your application using the following code:importjk_invokeInstantiate a password providerYou require a password provider. This object provides a password whenever it is required.The following password providers are implemented:InteractivePasswordProviderOneSlotPasswordProviderIt is extremely easy to implemnt your own password provider. (For details see section "Extending jk_invoke" below.)Instantiate an invokerThere are two invokers available for use:LocalInvoker- This class should be used of you want to work with the local system.RemoteInvoker- This class should be used of you want to work with a remote system.An instance ofLocalInvokerwill require these arguments:callable passwordProvider- An object that provides the password; requiredint localSSHPort- An integer that tells the system about the local SSH port; optional: ifNonethe port will be autodetected via/etc/ssh/sshd_configThe SSH port is required if you intend to usesudo. (Though technically we could follow different approaches for ease of implementation an SSH connection to the local machine is initiated onsudo.)Example:invoker=jk_invoke.LocalInvoker(jk_invoke.InteractivePasswordProvider(),)An instance ofRemoteInvokerwill require these arguments:str hostName- The host to connect to; requiredint port- The TCP port to use during connect; requiredstr userName- The user name to use for login; requiredcallable passwordProvider- An object that provides the password for login; requiredExample:invoker=jk_invoke.RemoteInvoker("localhost",22,"someuser",jk_invoke.InteractivePasswordProvider(),)Execute a commandThis is how you can execute a command:ret=invoker.runCmd("/usr/bin/id")The data returned is of typeCommandResult. This object provides:the STDOUT datathe STDOUT data splitted into linesthe STDERR datathe STDERR data splitted into linesthe command exit codethe duration of execution in secondsRead a text fileThis is how you can read the contents of a text file:ret=invoker.readTextFile("/etc/hostname")The data returned is of typestr.Write a text fileThis is how you can read the contents of a text file:invoker.writeTextFile("/tmp/someFile","abcXYZ123")This method returns no data.Perform operations as super userIn order to run commands, read or write files as super user, use thesudo()method to obtain another invoker. Example:sudoInvoker=invoker.sudo()ret=sudoInvoker.readTextFile("/etc/hostname")The methodsrunCmd()andwriteTextFile()can be used likewise and transparently.Extending jk_invokeImplement custom password providersIf you want to write an adapter for usingjk_invokein a different context, this is easy by providing a new password provider.A custom password provider must meet the following requirements:It must be a callable object.If called two arguments are passed to the__call__()method:str machineName- The machine to connect tostr loginName- The login name to use during connectOn call you must return a string: The password to use on connect.That's it. If you meet these requirements your password provider will perfectly work.A prototype to start from could be the following code:classMyPasswordProvider(object):def__init__(self,thePassword:str):self.__pwd=thePassworddef__call__(self,machineName:str,loginName:str)->str:returnself.__pwdContact InformationThis work is Open Source. This enables you to use this work for free.Please have in mind this also enables you to contribute. We, the subspecies of software developers, can create great things. But the more collaborate, the more fantastic these things can become. Therefore Feel free to contact the author(s) listed below, either for giving feedback, providing comments, hints, indicate possible collaborations, ideas, improvements. Or maybe for "only" reporting some bugs:Jürgen Knauth:jknauth@uni-goettingen.de,pubsrc@binary-overflow.deLicenseThis software is provided under the following license:Apache Software License 2.0