|
1 |
| -{%- macro files_switch(source_files, |
2 |
| - lookup=None, |
3 |
| - default_files_switch=['id', 'os_family'], |
4 |
| - indent_width=6, |
5 |
| - use_subpath=False) %} |
6 |
| - {#- |
| 1 | +{%- macro files_switch( |
| 2 | + source_files, |
| 3 | + lookup=None, |
| 4 | + default_files_switch=["id", "os_family"], |
| 5 | + indent_width=6, |
| 6 | + use_subpath=False |
| 7 | + ) %} |
| 8 | +{#- |
7 | 9 | Returns a valid value for the "source" parameter of a "file.managed"
|
8 | 10 | state function. This makes easier the usage of the Template Override and
|
9 | 11 | Files Switch (TOFS) pattern.
|
10 |
| -
|
11 | 12 | Params:
|
12 | 13 | * source_files: ordered list of files to look for
|
13 |
| - * lookup: key under '<tplroot>:tofs:source_files' to prepend to the |
| 14 | + * lookup: key under "<tplroot>:tofs:source_files" to prepend to the |
14 | 15 | list of source files
|
15 | 16 | * default_files_switch: if there's no config (e.g. pillar)
|
16 |
| - '<tplroot>:tofs:files_switch' this is the ordered list of grains to |
| 17 | + "<tplroot>:tofs:files_switch" this is the ordered list of grains to |
17 | 18 | use as selector switch of the directories under
|
18 | 19 | "<path_prefix>/files"
|
19 | 20 | * indent_width: indentation of the result value to conform to YAML
|
20 | 21 | * use_subpath: defaults to `False` but if set, lookup the source file
|
21 | 22 | recursively from the current state directory up to `tplroot`
|
22 |
| -
|
23 | 23 | Example (based on a `tplroot` of `xxx`):
|
24 |
| -
|
25 | 24 | If we have a state:
|
26 |
| -
|
27 | 25 | Deploy configuration:
|
28 | 26 | file.managed:
|
29 | 27 | - name: /etc/yyy/zzz.conf
|
30 |
| - - source: {{ files_switch(['/etc/yyy/zzz.conf', '/etc/yyy/zzz.conf.jinja'], |
31 |
| - lookup='Deploy configuration' |
32 |
| - ) }} |
| 28 | + - source: {{ files_switch( |
| 29 | + ["/etc/yyy/zzz.conf", "/etc/yyy/zzz.conf.jinja"], |
| 30 | + lookup="Deploy configuration", |
| 31 | + ) }} |
33 | 32 | - template: jinja
|
34 |
| -
|
35 | 33 | In a minion with id=theminion and os_family=RedHat, it's going to be
|
36 | 34 | rendered as:
|
37 |
| -
|
38 | 35 | Deploy configuration:
|
39 | 36 | file.managed:
|
40 | 37 | - name: /etc/yyy/zzz.conf
|
|
46 | 43 | - salt://xxx/files/default/etc/yyy/zzz.conf
|
47 | 44 | - salt://xxx/files/default/etc/yyy/zzz.conf.jinja
|
48 | 45 | - template: jinja
|
49 |
| - #} |
50 |
| - {#- Get the `tplroot` from `tpldir` #} |
51 |
| - {%- set tplroot = tpldir.split('/')[0] %} |
52 |
| - {%- set path_prefix = salt['config.get'](tplroot ~ ':tofs:path_prefix', tplroot) %} |
53 |
| - {%- set files_dir = salt['config.get'](tplroot ~ ':tofs:dirs:files', 'files') %} |
54 |
| - {%- set files_switch_list = salt['config.get']( |
55 |
| - tplroot ~ ':tofs:files_switch', |
56 |
| - default_files_switch |
57 |
| - ) %} |
58 |
| - {#- Lookup source_files (v2), files (v1), or fallback to an empty list #} |
59 |
| - {%- set src_files = salt['config.get']( |
60 |
| - tplroot ~ ':tofs:source_files:' ~ lookup, |
61 |
| - salt['config.get'](tplroot ~ ':tofs:files:' ~ lookup, []) |
62 |
| - ) %} |
63 |
| - {#- Append the default source_files #} |
64 |
| - {%- set src_files = src_files + source_files %} |
65 |
| - {#- Only add to [''] when supporting older TOFS implementations #} |
66 |
| - {%- set path_prefix_exts = [''] %} |
67 |
| - {%- if use_subpath and tplroot != tpldir %} |
68 |
| - {#- Walk directory tree to find {{ files_dir }} #} |
69 |
| - {%- set subpath_parts = tpldir.lstrip(tplroot).lstrip('/').split('/') %} |
70 |
| - {%- for path in subpath_parts %} |
71 |
| - {%- set subpath = subpath_parts[0:loop.index] | join('/') %} |
72 |
| - {%- do path_prefix_exts.append('/' ~ subpath) %} |
73 |
| - {%- endfor %} |
74 |
| - {%- endif %} |
75 |
| - {%- for path_prefix_ext in path_prefix_exts|reverse %} |
76 |
| - {%- set path_prefix_inc_ext = path_prefix ~ path_prefix_ext %} |
77 |
| - {#- For older TOFS implementation, use `files_switch` from the config #} |
78 |
| - {#- Use the default, new method otherwise #} |
79 |
| - {%- set fsl = salt['config.get']( |
80 |
| - tplroot ~ path_prefix_ext|replace('/', ':') ~ ':files_switch', |
81 |
| - files_switch_list |
82 |
| - ) %} |
83 |
| - {#- Append an empty value to evaluate as `default` in the loop below #} |
84 |
| - {%- if '' not in fsl %} |
85 |
| - {%- set fsl = fsl + [''] %} |
86 |
| - {%- endif %} |
87 |
| - {%- for fs in fsl %} |
88 |
| - {%- for src_file in src_files %} |
89 |
| - {%- if fs %} |
90 |
| - {%- set fs_dirs = salt['config.get'](fs, fs) %} |
91 |
| - {%- else %} |
92 |
| - {%- set fs_dirs = salt['config.get'](tplroot ~ ':tofs:dirs:default', 'default') %} |
93 |
| - {%- endif %} |
94 |
| - {#- Force the `config.get` lookup result as a list where necessary #} |
95 |
| - {#- since we need to also handle grains that are lists #} |
96 |
| - {%- if fs_dirs is string %} |
97 |
| - {%- set fs_dirs = [fs_dirs] %} |
98 |
| - {%- endif %} |
99 |
| - {%- for fs_dir in fs_dirs %} |
100 |
| - {%- set url = [ |
101 |
| - '- salt:/', |
102 |
| - path_prefix_inc_ext.strip('/'), |
103 |
| - files_dir.strip('/'), |
104 |
| - fs_dir.strip('/'), |
105 |
| - src_file.strip('/'), |
106 |
| - ] | select | join('/') %} |
| 46 | +#} |
| 47 | +{#- Get the `tplroot` from `tpldir` #} |
| 48 | +{%- set tplroot = tpldir.split("/")[0] %} |
| 49 | +{%- set path_prefix = salt["config.get"](tplroot ~ ":tofs:path_prefix", tplroot) %} |
| 50 | +{%- set files_dir = salt["config.get"](tplroot ~ ":tofs:dirs:files", "files") %} |
| 51 | +{%- set files_switch_list = salt["config.get"]( |
| 52 | + tplroot ~ ":tofs:files_switch", default_files_switch |
| 53 | + ) %} |
| 54 | +{#- Lookup source_files (v2), files (v1), or fallback to an empty list #} |
| 55 | +{%- set src_files = salt["config.get"]( |
| 56 | + tplroot ~ ":tofs:source_files:" ~ lookup, |
| 57 | + salt["config.get"](tplroot ~ ":tofs:files:" ~ lookup, []), |
| 58 | + ) %} |
| 59 | +{#- Append the default source_files #} |
| 60 | +{%- set src_files = src_files + source_files %} |
| 61 | +{#- Only add to [""] when supporting older TOFS implementations #} |
| 62 | +{%- set path_prefix_exts = [""] %} |
| 63 | +{%- if use_subpath and tplroot != tpldir %} |
| 64 | +{#- Walk directory tree to find {{ files_dir }} #} |
| 65 | +{%- set subpath_parts = tpldir.lstrip(tplroot).lstrip("/").split("/") %} |
| 66 | +{%- for path in subpath_parts %} |
| 67 | +{%- set subpath = subpath_parts[0 : loop.index] | join("/") %} |
| 68 | +{%- do path_prefix_exts.append("/" ~ subpath) %} |
| 69 | +{%- endfor %} |
| 70 | +{%- endif %} |
| 71 | +{%- for path_prefix_ext in path_prefix_exts | reverse %} |
| 72 | +{%- set path_prefix_inc_ext = path_prefix ~ path_prefix_ext %} |
| 73 | +{#- For older TOFS implementation, use `files_switch` from the config #} |
| 74 | +{#- Use the default, new method otherwise #} |
| 75 | +{%- set fsl = salt["config.get"]( |
| 76 | + tplroot ~ path_prefix_ext | replace("/", ":") ~ ":files_switch", |
| 77 | + files_switch_list, |
| 78 | + ) %} |
| 79 | +{#- Append an empty value to evaluate as `default` in the loop below #} |
| 80 | +{%- if "" not in fsl %} |
| 81 | +{%- set fsl = fsl + [""] %} |
| 82 | +{%- endif %} |
| 83 | +{%- for fs in fsl %} |
| 84 | +{%- for src_file in src_files %} |
| 85 | +{%- if fs %} |
| 86 | +{%- set fs_dirs = salt["config.get"](fs, fs) %} |
| 87 | +{%- else %} |
| 88 | +{%- set fs_dirs = salt["config.get"]( |
| 89 | + tplroot ~ ":tofs:dirs:default", "default" |
| 90 | + ) %} |
| 91 | +{%- endif %} |
| 92 | +{#- Force the `config.get` lookup result as a list where necessary #} |
| 93 | +{#- since we need to also handle grains that are lists #} |
| 94 | +{%- if fs_dirs is string %} |
| 95 | +{%- set fs_dirs = [fs_dirs] %} |
| 96 | +{%- endif %} |
| 97 | +{%- for fs_dir in fs_dirs %} |
| 98 | +{#- strip empty elements by using a select #} |
| 99 | +{%- set url = ( |
| 100 | + [ |
| 101 | + "- salt:/", |
| 102 | + path_prefix_inc_ext.strip("/"), |
| 103 | + files_dir.strip("/"), |
| 104 | + fs_dir.strip("/"), |
| 105 | + src_file.strip("/"), |
| 106 | + ] |
| 107 | + | select |
| 108 | + | join("/") |
| 109 | + ) %} |
107 | 110 | {{ url | indent(indent_width, true) }}
|
108 |
| - {%- endfor %} |
109 |
| - {%- endfor %} |
110 |
| - {%- endfor %} |
111 |
| - {%- endfor %} |
| 111 | +{%- endfor %} |
| 112 | +{%- endfor %} |
| 113 | +{%- endfor %} |
| 114 | +{%- endfor %} |
112 | 115 | {%- endmacro %}
|
0 commit comments