Optional Salt development

Salt Wheel system

The wheel system is used to manage master side management routines. These routines are primarily intended for the API to enable master configuration.

Salt master debug mode

Start the Salt Master in debug mode to help with troubleshooting, start the Salt master in debug mode:

[email protected]:~# service salt-master stop
[email protected]:~# salt-master -l debug

Writing execution modules

The __virtual__() function

  • Not technically required, but is a best practice
  • Executed when salt-minion is loaded
  • Identifies the module name
  • Commonly used to identify whether a module should be made available

Private functions

  • Name starts with underscores
  • Only used internally by the module
  • Will not show up in sys.doc

Public functions

  • Called directly by salt commands
  • Will show up in sys.doc

Use logging

  • Do not use warn/error when info is correct
  • Do not use info when debug is correct
  • Do not add log messages to __virtual__()

Using the Salt Logging System:

import logging

log = logging.getLogger(__name__)
log.info(Always useful to users)
log.warn(Something isnt right, but I can stillcontinue)
log.error(Something is definitely wrong, and shouldbe fixed)
log.debug(Only developers care about me)

Sample __virtual__() function:

try:
        import urllib2
        HAS_URLLIB2 = True
except:
        HAS_URLLIB2 = False

def __virtual__():
        if HAS_URLLIB2 is True:
                return crawler
        return False

Sample Private Function:

def _query(url):
        result = urllib2.urlopen(url)
        return {
                url: result.url,
                code: result.code,
                msg: result.msg,
                headers: result.headers.dict,
                content: result.read(),
        }

Sample Public Function

def fetch(urls=None):
        ret = {}
        if type(urls) is str:
                ret[urls] = _query(urls)
        elif type(urls) is list:
                for url in urls:
                        ret[url] = _query(url)
        return ret

Using Docstrings

def fetch(urls=None):
"""
Fetch a URL

CLI Example::

        salt myminion crawler.download http://www.mydomain.com/

...SNIP...

Using new module (command line)

Testing/debugging locally:

salt-call --local -l debug crawler.fetch http://tinyurl.com/cnyypk

From the salt-master:

salt myminion crawler.fetch http://tinyurl.com/cnyypk