From 1dec82d42c6d4246a0cc55bfcf4babee886601fa Mon Sep 17 00:00:00 2001
From: Bo Maryniuk <bo@suse.de>
Date: Tue, 27 Sep 2016 10:59:07 +0200
Subject: [PATCH 19/38] Generate Salt Thin with configured extra modules

* Add thin options to the master config.
* Get the thin Salt with configured extra modules on SSH
* Add a description of the thin/min parameters to the master config
* Add documentation about Salt Thin configuration
* Add extra-mods options to the Salt-Thin via SSH CLI
---
 conf/master                      | 7 +++++++
 doc/ref/configuration/master.rst | 9 +++++++++
 salt/client/ssh/__init__.py      | 2 +-
 salt/config.py                   | 4 ++++
 salt/runners/thin.py             | 4 ++++
 salt/utils/parsers.py            | 6 ++++++
 6 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/conf/master b/conf/master
index fdf5937..68d3bb0 100644
--- a/conf/master
+++ b/conf/master
@@ -977,3 +977,10 @@ hash_type: sha256
 ############################################
 # Default match type for filtering events tags: startswith, endswith, find, regex, fnmatch
 #event_match_type: startswith
+
+# Permanently include any available Python 3rd party modules into Salt Thin
+# when they are generated for Salt-SSH or other purposes.
+# The modules should be named by the names they are actually imported inside the Python.
+# The value of the parameters can be either one module or a comma separated list of them.
+#thin_extra_mods: foo,bar
+
diff --git a/doc/ref/configuration/master.rst b/doc/ref/configuration/master.rst
index f181a66..e1d6508 100644
--- a/doc/ref/configuration/master.rst
+++ b/doc/ref/configuration/master.rst
@@ -696,6 +696,15 @@ overridden on a per-minion basis in the roster (``minion_opts``)
     minion_opts:
       gpg_keydir: /root/gpg
 
+``thin_extra_mods``
+-------------------
+
+Default: None
+
+List of additional modules, needed to be included into the Salt Thin.
+Pass a list of importable Python modules that are typically located in
+the `site-packages` Python directory so they will be also always included
+into the Salt Thin, once generated.
 
 Master Security Settings
 ========================
diff --git a/salt/client/ssh/__init__.py b/salt/client/ssh/__init__.py
index ac01e3c..c4b6934 100644
--- a/salt/client/ssh/__init__.py
+++ b/salt/client/ssh/__init__.py
@@ -252,7 +252,7 @@ class SSH(object):
         self.serial = salt.payload.Serial(opts)
         self.returners = salt.loader.returners(self.opts, {})
         self.fsclient = salt.fileclient.FSClient(self.opts)
-        self.thin = salt.utils.thin.gen_thin(self.opts['cachedir'])
+        self.thin = salt.utils.thin.gen_thin(self.opts['cachedir'], extra_mods=self.opts.get('thin_extra_mods'))
         self.mods = mod_data(self.fsclient)
 
     def get_pubkey(self):
diff --git a/salt/config.py b/salt/config.py
index 9ad1d6a..3b40e6a 100644
--- a/salt/config.py
+++ b/salt/config.py
@@ -784,6 +784,9 @@ VALID_OPTS = {
 
     # Delay in seconds before executing bootstrap (salt cloud)
     'bootstrap_delay': int,
+
+    # Extra modules for Salt Thin
+    'thin_extra_mods': str,
 }
 
 # default configurations
@@ -1240,6 +1243,7 @@ DEFAULT_MASTER_OPTS = {
     'dummy_pub': False,
     'http_request_timeout': 1 * 60 * 60.0,  # 1 hour
     'http_max_body': 100 * 1024 * 1024 * 1024,  # 100GB
+    'thin_extra_mods': '',
 }
 
 
diff --git a/salt/runners/thin.py b/salt/runners/thin.py
index 0a8b398..5a3386f 100644
--- a/salt/runners/thin.py
+++ b/salt/runners/thin.py
@@ -30,4 +30,8 @@ def generate(extra_mods='', overwrite=False, so_mods=''):
         salt-run thin.generate mako,wempy 1
         salt-run thin.generate overwrite=1
     '''
+    conf_mods = __opts__.get('thin_extra_mods')
+    if conf_mods:
+        extra_mods = ','.join([conf_mods, extra_mods])
+
     return salt.utils.thin.gen_thin(__opts__['cachedir'], extra_mods, overwrite, so_mods)
diff --git a/salt/utils/parsers.py b/salt/utils/parsers.py
index 6019883..5a741fb 100644
--- a/salt/utils/parsers.py
+++ b/salt/utils/parsers.py
@@ -2476,6 +2476,12 @@ class SaltSSHOptionParser(six.with_metaclass(OptionParserMeta,
             help='Pass in extra files to include in the state tarball'
         )
         self.add_option(
+            '--thin-extra-modules',
+            dest='thin_extra_mods',
+            default=None,
+            help='One or comma-separated list of extra Python modules'
+                 'to be included into Thin Salt.')
+        self.add_option(
             '-v', '--verbose',
             default=False,
             action='store_true',
-- 
2.10.2

