Saltstack Official OpenSSH Formula
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
6.1KB

  1. # -*- coding: utf-8 -*-
  2. # vim: ft=jinja
  3. {#- Get the `tplroot` from `tpldir` #}
  4. {%- set tplroot = tpldir.split("/")[0] %}
  5. {%- from tplroot ~ "/libmatchers.jinja" import parse_matchers, query_map %}
  6. {%- from tplroot ~ "/libmapstack.jinja" import mapstack %}
  7. {#- Where to lookup parameters source files #}
  8. {%- set map_sources_dir = tplroot ~ "/parameters" %}
  9. {#- List of sources to lookup for parameters #}
  10. {#- Fallback to previously used grains plus minion `id` #}
  11. {%- set map_sources = [
  12. "Y:G@osarch",
  13. "Y:G@os_family",
  14. "Y:G@os",
  15. "Y:G@osfinger",
  16. "C@" ~ tplroot ~ ":lookup",
  17. "C@" ~ tplroot,
  18. "Y:G@id",
  19. ] %}
  20. {#- Allow centralised map.jinja configuration #}
  21. {%- set _global_param_filename = "parameters/map_jinja.yaml" %}
  22. {#- Allow per formula map.jinja configuration #}
  23. {%- set _formula_param_filename = map_sources_dir ~ "/map_jinja.yaml" %}
  24. {%- set _map_settings = mapstack(
  25. files=[_global_param_filename, _formula_param_filename],
  26. defaults={
  27. "values": {"sources": map_sources}
  28. },
  29. log_prefix="map.jinja configuration: ",
  30. )
  31. | load_yaml %}
  32. {%- set map_sources = _map_settings | traverse("values:sources") %}
  33. {%- do salt["log.debug"](
  34. "map.jinja: load parameters from sources:\n"
  35. ~ map_sources
  36. | yaml(False)
  37. ) %}
  38. {#- Load formula defaults values #}
  39. {%- set _defaults_filename = map_sources_dir ~ "/defaults.yaml" %}
  40. {%- do salt["log.debug"](
  41. "map.jinja: load per formula default values from "
  42. ~ _defaults_filename
  43. ) %}
  44. {%- load_yaml as default_settings %}
  45. {%- include _defaults_filename ignore missing %}
  46. {%- endload %}
  47. {%- if not default_settings %}
  48. {%- set default_settings = {'values': {} } %}
  49. {%- endif %}
  50. {#- Make sure to track `map.jinja` configuration with `_mapdata` #}
  51. {%- do default_settings["values"].update(
  52. {
  53. "map_jinja": _map_settings["values"]
  54. }
  55. ) %}
  56. {#- Work around assignment inside for loop #}
  57. {#- load configuration values used in `config.get` merging strategies #}
  58. {%- set _config = {
  59. "stack": default_settings.get("values", {}),
  60. "merge_strategy": salt["config.get"](tplroot ~ ":strategy", None),
  61. "merge_lists": salt["config.get"](tplroot ~ ":merge_lists", False),
  62. } %}
  63. {#- `parse_matchers` returns a `list` of `dict` with #}
  64. {#- - type: `F` to load file, `C`, `G`, `I` for lookup #}
  65. {#- - option: specific to the type #}
  66. {#- - query: which key is requested #}
  67. {#- - query_delimiter: the separator between query component #}
  68. {#- - query_method: the salt method doing the query `config.get`, `pillar.get` and `grains.get` #}
  69. {#- - value: the result of the `salt[<query_method>](<query>)` #}
  70. {%- set map_matchers = parse_matchers(
  71. matchers=map_sources,
  72. config_get_strategy=_config["merge_strategy"],
  73. log_prefix="map.jinja: "
  74. )
  75. | load_yaml %}
  76. {%- for matcher in map_matchers %}
  77. {%- if matcher.type in query_map.keys() %}
  78. {#- Merge in `mapdata.<query>` instead of directcly in `mapdata` #}
  79. {%- set is_sub_key = matcher.option | default(False) == "SUB" %}
  80. {#- `slsutil.merge` defaults to `smart` instead of `None` for `config.get` #}
  81. {%- set _strategy = _config["merge_strategy"] | default("smart", boolean=True) %}
  82. {%- do salt["log.debug"](
  83. "map.jinja: merge "
  84. ~ "sub key " * is_sub_key
  85. ~ "'"
  86. ~ matcher.query
  87. ~ "' retrieved with '"
  88. ~ matcher.query_method
  89. ~ "', merge: strategy='"
  90. ~ _strategy
  91. ~ "', lists='"
  92. ~ _config["merge_lists"]
  93. ~ "'"
  94. ) %}
  95. {#- empty value is an empty list, it must be an empty dict for `.update` #}
  96. {%- set value = matcher.value | default({}, boolean=True) %}
  97. {%- if is_sub_key %}
  98. {#- Merge values with `mapdata.<key>`, `<key>` and `<key>:lookup` are merged together #}
  99. {%- set value = { matcher.query | regex_replace("(:lookup)?$", ""): value } %}
  100. {%- endif %}
  101. {%- do _config.update(
  102. {
  103. "stack": salt["slsutil.merge"](
  104. _config["stack"],
  105. value,
  106. strategy=_strategy,
  107. merge_lists=_config["merge_lists"],
  108. )
  109. }
  110. ) %}
  111. {%- else %}
  112. {#- Load YAML file matching the grain/pillar/... #}
  113. {#- Fallback to use the source name as a direct filename #}
  114. {#- Mangle `source_key` to use it as literal path #}
  115. {%- if matcher.value | length == 0 %}
  116. {%- set query_parts = matcher.query.split("/") %}
  117. {%- set map_dirname = query_parts[0:-1] | join("/") %}
  118. {%- set map_values = query_parts[-1] | regex_replace("(\.yaml)?$", ".yaml") %}
  119. {%- else %}
  120. {%- set map_dirname = matcher.query %}
  121. {%- set map_values = matcher.value %}
  122. {%- endif %}
  123. {#- Some configuration return list #}
  124. {%- if map_values is string %}
  125. {%- set map_values = [map_values] %}
  126. {%- endif %}
  127. {#- `map_dirname` can be an empty string with literal path like `myconf.yaml` #}
  128. {%- set yaml_dir = [
  129. map_sources_dir,
  130. map_dirname
  131. ]
  132. | select
  133. | join("/") %}
  134. {%- for map_value in map_values %}
  135. {%- set yamlfile = [
  136. yaml_dir,
  137. map_value ~ ".yaml"
  138. ]
  139. | join("/") %}
  140. {%- do salt["log.debug"]("map.jinja: load parameters from file " ~ yamlfile) %}
  141. {%- load_yaml as loaded_values %}
  142. {%- include yamlfile ignore missing %}
  143. {%- endload %}
  144. {%- if loaded_values %}
  145. {#- Merge loaded values on the stack #}
  146. {%- do salt["log.debug"]("map.jinja: merge parameters from " ~ yamlfile) %}
  147. {%- do _config.update(
  148. {
  149. "stack": salt["slsutil.merge"](
  150. _config["stack"],
  151. loaded_values.get("values", {}),
  152. strategy=loaded_values.get("strategy", "smart"),
  153. merge_lists=loaded_values.get("merge_lists", False)
  154. | to_bool,
  155. )
  156. }
  157. ) %}
  158. {%- endif %}
  159. {%- endfor %}
  160. {%- endif %}
  161. {%- endfor %}
  162. {%- do salt["log.debug"]("map.jinja: save parameters in variable 'mapdata'") %}
  163. {%- set mapdata = _config["stack"] %}