-
Notifications
You must be signed in to change notification settings - Fork 0
/
pillarstack.cfg
171 lines (159 loc) · 9.35 KB
/
pillarstack.cfg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
{#-
##
##
##
##_META:
##
## <JINJA>
#}
{#- set label locations #}
{%- set label_namespace = __stack__['traverse'](pillar, 'namespace:gema_label', 'label') %}
{%- set label_visitation_namespace_lst = [label_namespace, "__visitation_lst",] %}
{%- set label_visitation_namespace = __stack__['traverse'](pillar, 'namespace:gema_label_visitation', label_visitation_namespace_lst|join(':')) %}
{%- set label_evaluation_namespace_lst = [label_namespace, "__evaluation_dct",] %}
{%- set label_evaluation_namespace = __stack__['traverse'](pillar, 'namespace:gema_label_evaluation', label_evaluation_namespace_lst|join(':')) %}
{#- set lookup locations #}
{%- set uri_stack_rootdir = __salt__['file.dirname'](__stack__.cfg_path|default('/srv/salt/pillar/base.core')) %}
{%- set uri_lookup_prefix_lst = ['_policy_sitelocal/lookup', '_policy_core/lookup'] %}
{%- set uri_lookup_suffix_lst = ['%s.sls', '%s/init.sls'] %}
{%- set lookup_wrap_lst = __stack__['traverse'](pillar, 'gema:lookup_wrap_lst', [__salt__['file.join'](uri_stack_rootdir, uri_lookup_prefix_lst[0], uri_lookup_suffix_lst[0]),
__salt__['file.join'](uri_stack_rootdir, uri_lookup_prefix_lst[0], uri_lookup_suffix_lst[1]),
__salt__['file.join'](uri_stack_rootdir, uri_lookup_prefix_lst[1], uri_lookup_suffix_lst[0]),
__salt__['file.join'](uri_stack_rootdir, uri_lookup_prefix_lst[1], uri_lookup_suffix_lst[1]),
])
%}
{%- if __stack__['traverse'](stack, label_visitation_namespace, none) is none
or __stack__['traverse'](stack, label_evaluation_namespace, none) is none %}
{%- set _discard = stack.update({label_visitation_namespace_lst[0]: {label_visitation_namespace_lst[1]: [],
label_evaluation_namespace_lst[1]: {},
}})
%}
{%- endif %}
{%- set visitation_lst = __stack__['traverse'](stack, label_visitation_namespace, []) %}
{%- set evaluation_dct = __stack__['traverse'](stack, label_evaluation_namespace, {}) %}
{#- the pillarstack.cfg path may be indicated, if desired, particularly when using multiple files #}
{# {%- set _discard = visitation_lst.append('--- # ' ~ __stack__.cfg_path ~ ' # ---') %} #}
{%- set _discard = visitation_lst.append('--- traversal ---') %}
{#- hint: the traversal cursor #}
{%- set cursor_ref_lst = ['label_namespace', 'question', 'question_answer', 'question_answer_detail'] %}
{%- set uri_cursor_lst = [] %}
{#- #}
{%- set pillar_dct = __stack__['traverse'](pillar, label_namespace, {}) %}
{%- set stack_dct = __stack__['traverse'](stack, label_namespace, {}) %}
{%- set merge_dct = {} %}
{#- hint: the pillar provides an OrderedDict which we'll convert to a dict for processing #}
{%- for k, v in __stack__['traverse'](pillar, label_namespace, {}).items() %}
{%- set _discard = merge_dct.__setitem__(k, v) %}
{%- endfor %}
{#- hint: merge stack_dct into pillar_dct #}
{%- set _discard = merge_dct.update(stack_dct) %}
{#- hint: remove evaluation_dct and visitation_lst from merge_dct #}
{#- todo: label_visitation_namespace is somewhat hard-coded (aka inelasticly referenced) here #}
{%- set _discard = merge_dct.pop(label_visitation_namespace.split(':')[-1], []) %}
{#- hint: remove evaluation dictionary from merge_dct #}
{%- set _discard = merge_dct.pop(label_evaluation_namespace.split(':')[-1], []) %}
{# hint: set traversal_dct from merge_dct #}
{%- set traversal_dct = merge_dct %}
{##}
{%- set msg_log_pfx = ":gema-plr(%s):"|format(minion_id) %}
{%- set msg_lookup_declined_as_nonidemp = msg_log_pfx ~ " " * cursor_ref_lst|length ~ ":skipping lookup, previously visited " %}
{%- set msg_label_visitation = msg_log_pfx ~ " " * cursor_ref_lst|length ~ ":visiting label " %}
{%- set msg_lookup_notfound = msg_log_pfx ~ " " * cursor_ref_lst|length ~ ":lookup not available " %}
{%- set msg_lookup_evaluated = msg_log_pfx ~ " " * cursor_ref_lst|length ~ ":evaluating lookup " %}
{%- macro _lookups_macro(uri_cursor_lst) %}
{%- set uri_label = uri_cursor_lst|join(':') %}
{%- if uri_label in visitation_lst %}{%- do __salt__['log.debug'](msg_lookup_declined_as_nonidemp ~ uri_label) %}{% else %}{%- set _discard = visitation_lst.append(uri_label) %}
{%- for lookup_wrap in lookup_wrap_lst if __salt__["file.file_exists"](lookup_wrap|format(uri_cursor_lst[1:]|join('/'))) %}
{%- if uri_label not in evaluation_dct %}{%- set _discard = evaluation_dct.update({uri_label: []}) %}{%- endif %}
{%- set _discard = (evaluation_dct[uri_label]).append(lookup_wrap|format(uri_cursor_lst[1:]|join('/'))) %}
{%- endfor %}
{{ evaluation_dct[uri_label]|first }}
{% endif %}
{%- endmacro %}
{%- macro _log_label(uri_cursor_lst) %}
{%- set depth = uri_cursor_lst|length %}
{%- set max_depth = cursor_ref_lst|length + 1 %}
{%- set leading_indent = " " * (depth - 1) ~ "##" * (max_depth - depth) %}
{%- set vertex_data = uri_cursor_lst|last %}
{%- set vertex_dtype = _dtype_str(vertex_data) %}
{%- do __salt__['log.debug'](msg_log_pfx ~ "%s:%-22s is dtype(%s) with value(%s)"|format(leading_indent, cursor_ref_lst[depth - 1], vertex_dtype, vertex_data)) |default('', True) %}
{%- endmacro %}
{%- macro _dtype_str(data) %}
{%- if data is mapping -%}
mapping
{%- elif data is sequence and data is not string -%}
list
{%- elif data is string -%}
string
{%- elif data is none -%}
none
{%- elif data is undefined -%}
undedined
{%- else -%}
unknown
{%- endif -%}
{%- endmacro %}
{#- hint: view __evaluation_dct to see the list of candidate lookups for each label #}
{#- TODO: filter out 'OrderedDict()' values #}
{#
## </JINJA>
#}
{%- if traversal_dct is mapping %}
{%- for question, question_answer in traversal_dct.items() if question is defined %}
{#- question #}
{{- _log_label([label_namespace, question]) }}
{{- _lookups_macro([label_namespace, question]) }}
{%- if question_answer is defined and question_answer is not none %}
{#- question_answer #}
{{- _log_label([label_namespace, question, question_answer]) }}
{%- if question_answer is string %}
{{- _lookups_macro([label_namespace, question, question_answer]) }}
{%- elif question_answer is mapping %}
{%- for question_answer, question_answer_detail in question_answer.items() if question_answer is defined %}
{{- _lookups_macro([label_namespace, question, question_answer]) }}
{%- if question_answer_detail is defined and question_answer_detail is not none %}
{#- question_answer_detail #}
{{- _log_label([label_namespace, question, question_answer, question_answer_detail]) }}
{%- if question_answer_detail is string %}
{{- _lookups_macro([label_namespace, question, question_answer, question_answer_detail]) }}
{%- elif question_answer_detail is mapping %}
{%- for question_answer_detail_iter, discard_var in question_answer_detail.items() if question_answer_detail_iter is defined and question_answer_detail_iter is not none %}
{#- question_answer_detail_iter #}
{{- _lookups_macro([label_namespace, question, question_answer, question_answer_detail_iter]) }}
{%- endfor %}
{%- elif question_answer_detail is sequence and question_answer_detail is not string %}
{% for question_answer_detail_iter in question_answer_detail if question_answer_detail_iter is defined %}
{#- question_answer_detail_iter #}
{{- _lookups_macro([label_namespace, question, question_answer, question_answer_detail_iter]) }}
{%- endfor %}
{%- endif %}
{%- endif %}
{%- endfor %}
{%- elif question_answer is sequence and question is not string %}
{#- question_answer #}
{%- for question_answer_detail in question_answer if question_answer_detail is defined %}
{%- if question_answer_detail is defined and question_answer_detail is not none %}
{#- question_answer_detail #}
{{- _log_label([label_namespace, question, question_answer, question_answer_detail]) }}
{%- if question_answer_detail is string %}
{{- _lookups_macro([label_namespace, question, question_answer, question_answer_detail]) }}
{%- elif question_answer_detail is mapping %}
{%- for question_answer_detail_iter, discard_var in question_answer_detail.items() if question_answer_detail_iter is defined and question_answer_detail_iter is not none %}
{#- question_answer_detail_iter #}
{{- _lookups_macro([label_namespace, question, question_answer, question_answer_detail, question_answer_detail_iter]) }}
{%- endfor %}
{%- elif question_answer_detail is sequence and question_answer_detail is not string %}
{%- for question_answer_detail_iter in question_answer_detail if question_answer_detail_iter is defined and question_answer_detail_iter is not none %}
{#- question_answer_detail_iter #}
{{- _lookups_macro([label_namespace, question, question_answer, question_answer_detail_iter]) }}
{%- endfor %}
{%- endif %}
{%- endif %}
{%- endfor %}
{%- endif %}
{%- endif %}
{%- endfor %}
{%- endif %}
{#
## EOF
#}