From 4bf945352585ea9440b9b141c571a1f77cc075b3 Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Wed, 20 Mar 2019 23:35:00 +0000 Subject: [PATCH] =?UTF-8?q?refactor(tofs):=20avoid=20using=20=E2=80=9Csalt?= =?UTF-8?q?['config.get']=E2=80=9D=20for=20formula=20writers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * https://github.com/saltstack-formulas/template-formula/pull/77 --- TOFS_pattern.rst | 10 ++++------ systemd/macros.jinja | 19 ++++++++++++------- systemd/networkd/init.sls | 10 ++++------ systemd/resolved/init.sls | 10 ++++------ systemd/timesyncd/init.sls | 10 ++++------ 5 files changed, 28 insertions(+), 31 deletions(-) diff --git a/TOFS_pattern.rst b/TOFS_pattern.rst index 2fd1a12..e5b1c3d 100644 --- a/TOFS_pattern.rst +++ b/TOFS_pattern.rst @@ -314,12 +314,10 @@ We can simplify the ``conf.sls`` with the new ``files_switch`` macro to use in t file.managed: - name: {{ ntp.config }} - template: jinja - - source: {{ files_switch( - salt['config.get']( - tplroot ~ ':tofs:source_files:Configure NTP', - ['/etc/ntp.conf.jinja'] - ) - ) }} + - source: {{ files_switch(['/etc/ntp.conf.jinja'], + lookup='Configure NTP' + ) + }} - watch_in: - service: Enable and start NTP service - require: diff --git a/systemd/macros.jinja b/systemd/macros.jinja index 11590be..4faf5c6 100644 --- a/systemd/macros.jinja +++ b/systemd/macros.jinja @@ -1,4 +1,5 @@ {%- macro files_switch(source_files, + lookup=None, default_files_switch=['id', 'os_family'], indent_width=6) %} {#- @@ -8,6 +9,8 @@ Params: * source_files: ordered list of files to look for + * lookup: key under ':tofs:source_files' to override + list of source files * default_files_switch: if there's no pillar ':tofs:files_switch' this is the ordered list of grains to use as selector switch of the directories under @@ -21,11 +24,8 @@ Deploy configuration: file.managed: - name: /etc/yyy/zzz.conf - - source: {{ files_switch( - salt['config.get']( - tplroot ~ ':tofs:source_files:Deploy configuration', - ['/etc/yyy/zzz.conf', '/etc/yyy/zzz.conf.jinja'] - ) + - source: {{ files_switch(['/etc/yyy/zzz.conf', '/etc/yyy/zzz.conf.jinja'], + lookup='Deploy configuration' ) }} - template: jinja @@ -52,6 +52,11 @@ tplroot ~ ':tofs:files_switch', default_files_switch ) %} + {#- Lookup files or fallback to source_files parameter #} + {%- set src_files = salt['config.get']( + tplroot ~ ':tofs:source_files:' ~ lookup, + source_files + ) %} {#- Only add to [''] when supporting older TOFS implementations #} {%- for path_prefix_ext in ['', '/networkd', '/resolved', '/timesyncd'] %} {%- set path_prefix_inc_ext = path_prefix ~ path_prefix_ext %} @@ -66,7 +71,7 @@ {%- do fsl.append('') %} {%- endif %} {%- for fs in fsl %} - {%- for source_file in source_files %} + {%- for src_file in src_files %} {%- if fs %} {%- set fs_dir = salt['config.get'](fs, fs) %} {%- else %} @@ -76,7 +81,7 @@ path_prefix_inc_ext, files_dir, fs_dir, - source_file.lstrip('/') + src_file.lstrip('/') ]) %} {{ url | indent(indent_width, true) }} {%- endfor %} diff --git a/systemd/networkd/init.sls b/systemd/networkd/init.sls index eeb94da..b5294aa 100644 --- a/systemd/networkd/init.sls +++ b/systemd/networkd/init.sls @@ -13,12 +13,10 @@ networkd: - user: root - group: root - template: jinja - - source: {{ files_switch( - salt['config.get']( - 'systemd:tofs:source_files:networkd', - ['network'] - ) - ) }} + - source: {{ files_switch(['network'], + lookup='networkd' + ) + }} - clean: True - dir_mode: 755 - file_mode: 644 diff --git a/systemd/resolved/init.sls b/systemd/resolved/init.sls index 466f3e3..94e8af9 100644 --- a/systemd/resolved/init.sls +++ b/systemd/resolved/init.sls @@ -14,12 +14,10 @@ resolved: - group: root - mode: 644 - template: jinja - - source: {{ files_switch( - salt['config.get']( - 'systemd:tofs:source_files:resolved', - ['resolved.conf'] - ) - ) }} + - source: {{ files_switch(['resolved.conf'], + lookup='resolved' + ) + }} - listen_in: - service: resolved service.running: diff --git a/systemd/timesyncd/init.sls b/systemd/timesyncd/init.sls index d19a535..1ff516b 100644 --- a/systemd/timesyncd/init.sls +++ b/systemd/timesyncd/init.sls @@ -17,12 +17,10 @@ timesyncd: {% endif %} file.managed: - name: /etc/systemd/timesyncd.conf - - source: {{ files_switch( - salt['config.get']( - 'systemd:tofs:source_files:timesyncd', - ['timesyncd.conf'] - ) - ) }} + - source: {{ files_switch(['timesyncd.conf'], + lookup='timesyncd' + ) + }} cmd.wait: - name: timedatectl set-ntp true - runas: root