"Fossies" - the Fresh Open Source Software Archive

Member "salt-3002.2/salt/states/idem.py" (18 Nov 2020, 4004 Bytes) of package /linux/misc/salt-3002.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "idem.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3002.1_vs_3002.2.

    1 #
    2 # Author: Tyler Johnson <tjohnson@saltstack.com>
    3 #
    4 
    5 """
    6 Idem Support
    7 ============
    8 
    9 This state provides access to idem states
   10 
   11 .. versionadded:: 3002
   12 """
   13 import pathlib
   14 import re
   15 
   16 __virtualname__ = "idem"
   17 
   18 
   19 def __virtual__():
   20     if "idem.hub" in __utils__:
   21         return __virtualname__
   22     else:
   23         return False, "idem is not available"
   24 
   25 
   26 def _get_refs(sources, tree):
   27     """
   28     Determine where the sls sources are
   29     """
   30     sls_sources = []
   31     SLSs = []
   32     if tree:
   33         sls_sources.append("file://{}".format(tree))
   34     for sls in sources:
   35         path = pathlib.Path(sls)
   36         if path.is_file():
   37             ref = str(path.stem if path.suffix == ".sls" else path.name)
   38             SLSs.append(ref)
   39             implied = "file://{}".format(path.parent)
   40             if implied not in sls_sources:
   41                 sls_sources.append(implied)
   42         else:
   43             SLSs.append(sls)
   44     return sls_sources, SLSs
   45 
   46 
   47 def _get_low_data(low_data):
   48     """
   49     Get salt-style low data from an idem state name
   50     """
   51     # state_|-id_|-name_|-function
   52     match = re.match(r"(.+)_\|-(.+)_\|-(.+)_\|-(.+)", low_data)
   53     return {
   54         "state": match.group(1),
   55         "__id__": match.group(2),
   56         "name": match.group(3),
   57         "fun": match.group(4),
   58     }
   59 
   60 
   61 def state(
   62     name,
   63     sls,
   64     acct_file=None,
   65     acct_key=None,
   66     acct_profile=None,
   67     cache_dir=None,
   68     render=None,
   69     runtime=None,
   70     source_dir=None,
   71     test=False,
   72 ):
   73     """
   74     Execute an idem sls file through a salt state
   75 
   76     sls
   77         A list of idem sls files or sources
   78 
   79     acct_file
   80         Path to the acct file used in generating idem ctx parameters.
   81         Defaults to the value in the ACCT_FILE environment variable.
   82 
   83     acct_key
   84         Key used to decrypt the acct file.
   85         Defaults to the value in the ACCT_KEY environment variable.
   86 
   87     acct_profile
   88         Name of the profile to add to idem's ctx.acct parameter
   89         Defaults to the value in the ACCT_PROFILE environment variable.
   90 
   91     cache_dir
   92         The location to use for the cache directory
   93 
   94     render
   95         The render pipe to use, this allows for the language to be specified (jinja|yaml)
   96 
   97     runtime
   98         Select which execution runtime to use (serial|parallel)
   99 
  100     source_dir
  101         The directory containing sls files
  102 
  103     .. code-block:: yaml
  104 
  105         cheese:
  106             idem.state:
  107                 - runtime: parallel
  108                 - sls:
  109                     - idem_state.sls
  110                     - sls_source
  111 
  112     :maturity:      new
  113     :depends:       acct, pop, pop-config, idem
  114     :platform:      all
  115     """
  116     hub = __utils__["idem.hub"]()
  117 
  118     if isinstance(sls, str):
  119         sls = [sls]
  120 
  121     sls_sources, SLSs = _get_refs(sls, source_dir or hub.OPT.idem.tree)
  122 
  123     coro = hub.idem.state.apply(
  124         name=name,
  125         sls_sources=sls_sources,
  126         render=render or hub.OPT.idem.render,
  127         runtime=runtime or hub.OPT.idem.runtime,
  128         subs=["states"],
  129         cache_dir=cache_dir or hub.OPT.idem.cache_dir,
  130         sls=SLSs,
  131         test=test,
  132         acct_file=acct_file or hub.OPT.acct.acct_file,
  133         acct_key=acct_key or hub.OPT.acct.acct_key,
  134         acct_profile=acct_profile or hub.OPT.acct.acct_profile or "default",
  135     )
  136     hub.pop.Loop.run_until_complete(coro)
  137 
  138     errors = hub.idem.RUNS[name]["errors"]
  139     success = not errors
  140 
  141     running = []
  142     for idem_name, idem_return in hub.idem.RUNS[name]["running"].items():
  143         standardized_idem_return = {
  144             "name": idem_return["name"],
  145             "changes": idem_return["changes"],
  146             "result": idem_return["result"],
  147             "comment": idem_return.get("comment"),
  148             "low": _get_low_data(idem_name),
  149         }
  150         running.append(standardized_idem_return)
  151 
  152     return {
  153         "name": name,
  154         "result": success,
  155         "comment": "Ran {} idem states".format(len(running)) if success else errors,
  156         "changes": {},
  157         "sub_state_run": running,
  158     }