salt-projectsalt-cloud

How to install external python dependency for salt runner module?


I am trying to write a custom salt runner module and was wondering how does salt-stack handle external dependencies.

say I have

import external_library

how do I install or manage this external_library dependency so that my custom module is loaded properly?

Is there something similar to requirements.txt file for salt-stack?


Solution

  • I'm going to assume this is for a supported version of salt. which currently are all onedir based. that will simplify the answer instead of having to worry about which python to install into.

    first to keep your runner from blowing up if it is attempted to be ran while the dependencies are not in salt.

    you want to wrap your import in a try block such as the following

    HAS_LIBS = False
    try:
        import external_library
        HAS_LIBS = True
    except ImportError:
        pass
        
    

    then in the virtual function check if HAS_LIBS and return false so that the module isn't loaded.

    but that covers the basics of how to properly handle third party dependencies in the module.

    for installing the dependencies. you want to use either salt-pip on the cli. it operates much the same as standard pip but installs things into the salt python environment. the other option is to use a state with pip.installed.

    if you are installing a salt-extension then you would use the normal python based methods for having salt-pip install dependencies.