Book Image

Extending SaltStack

Book Image

Extending SaltStack

Overview of this book

Salt already ships with a very powerful set of tools, but that doesn't mean that they all suit your needs perfectly. By adding your own modules and enhancing existing ones, you can bring the functionality that you need to increase your productivity. Extending SaltStack follows a tutorial-based approach to explain different types of modules, from fundamentals to complete and full-functioning modules. Starting with the Loader system that drives Salt, this book will guide you through the most common types of modules. First you will learn how to write execution modules. Then you will extend the configuration using the grain, pillar, and SDB modules. Next up will be state modules and then the renderers that can be used with them. This will be followed with returner and output modules, which increase your options to manage return data. After that, there will be modules for external file servers, clouds, beacons, and finally external authentication and wheel modules to manage the master. With this guide in hand, you will be prepared to create, troubleshoot, and manage the most common types of Salt modules and take your infrastructure to new heights!
Table of Contents (21 chapters)
Extending SaltStack
Credits
Foreword
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Understanding the Salt Style Guide


If you've spent enough time in Python, then you're already familiar with the Style Guide for Python Code, also known as PEP 8. For those who have not seen it, or if you need a refresher, you can take a look at it here:

https://www.python.org/dev/peps/pep-0008/

There is also a guide to the Salt Coding Style, available at:

https://docs.saltstack.com/en/latest/topics/development/conventions/style.html

In general, Salt coding conventions follow PEP 8, but there are some key differences:

  • Quoting: One of the first conventions that new developers come across is that Salt uses single quotes (') instead of double quotes ("). This applies to everything from string formatting to docstrings.

  • Line length: It is very common for code to restrict lines to no longer than 80 characters. This seems to be especially adhered to in Python, but it is based on an older convention where computer screens were exactly 80 characters wide. Because this is no longer the case, it is considered acceptable in Salt to expand to 120 characters, particularly if it helps with readability.

  • Tabs versus spaces: Salt uses four spaces for indentation. No tabs. No exceptions.

Using Pylint

Salt makes extensive use of a program called Pylint to ensure that its code adheres to its style guide. You can find information about installing Pylint at:

http://www.pylint.org/

Keep in mind that Salt currently uses Python 2 (the minimum version being 2.6), so if you're working in a distribution where both Python 2 and 3 versions of Pylint are available, make sure you use the Python 2 version.

The Salt code base ships with a .pylintrc file to be used with Pylint. It doesn't get used by default, so you need to make sure to point it out to Pylint:

$ cd /path/to/salt
$ pylint --rcfile=.pylintrc

Not only will this file allow you to check your code against Salt style guidelines but also to check the entire code base at once. This is important, because the loader inserts variables into modules that wouldn't be picked up otherwise by Pylint.