From ad923eefebec10a64f9943e230dda28cc3241c7d Mon Sep 17 00:00:00 2001 From: Imran Iqbal Date: Wed, 25 Mar 2020 14:26:04 +0000 Subject: [PATCH] refactor(libsaltcli): use `libsaltcli` library to improve readability * Based on https://github.com/saltstack-formulas/template-formula/pull/131 --- libvirt/libsaltcli.jinja | 16 ++++++++++++++++ libvirt/map.jinja | 16 ++++++---------- 2 files changed, 22 insertions(+), 10 deletions(-) create mode 100644 libvirt/libsaltcli.jinja diff --git a/libvirt/libsaltcli.jinja b/libvirt/libsaltcli.jinja new file mode 100644 index 0000000..6bd11ad --- /dev/null +++ b/libvirt/libsaltcli.jinja @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +# vim: ft=jinja + +{#- Determine the type of command being run #} +{%- if salt['config.get']('__cli') == 'salt-minion' %} +{%- set cli = 'minion' %} +{%- elif salt['config.get']('__cli') == 'salt-call' %} +{%- if salt['config.get']('root_dir').endswith('/running_data') %} +{%- set cli = 'ssh' %} +{%- else %} +{%- set cli = 'local' %} +{%- endif %} +{%- else %} +{%- set cli = 'unknown' %} +{%- endif %} +{%- do salt['log.debug']('[libsaltcli] the salt command type has been identified to be: ' ~ cli) %} diff --git a/libvirt/map.jinja b/libvirt/map.jinja index 7eeac1b..a4ad60a 100644 --- a/libvirt/map.jinja +++ b/libvirt/map.jinja @@ -3,6 +3,7 @@ {#- Get the `tplroot` from `tpldir` #} {%- set tplroot = tpldir.split('/')[0] %} +{%- from tplroot ~ "/libsaltcli.jinja" import cli with context %} {#- Where to lookup parameters source files #} {%- set map_sources_dir = tplroot | path_join('parameters') %} @@ -31,22 +32,17 @@ {#- load configuration values used in `config.get` merging strategies #} {%- set _config = {'stack': default_settings.get('values', {}), 'merge_strategy': salt['config.get'](tplroot ~ ':strategy', None), - 'merge_lists': salt['config.get'](tplroot ~ ':merge_lists', False), - 'cli': salt['config.get']('__cli'), - 'root_dir': salt['config.get']('root_dir') + 'merge_lists': salt['config.get'](tplroot ~ ':merge_lists', False) } %} -{#- `config.get` merge option works only for `minion` or local `salt-call` #} -{%- if _config['cli'] == 'salt-minion' - or (_config['cli'] == 'salt-call' and not _config['root_dir'].endswith('/running_data')) %} +{#- the `config.get` merge option only works for `minion` or `local` salt command types #} +{%- if cli in ['minion', 'local'] %} {%- do _config.update({'merge_opt': {'merge': _config['merge_strategy']}, 'merge_msg': ", merge: strategy='" ~ _config['merge_strategy'] ~ "'"}) %} +{#- the `config.get` merge option is not available for `ssh` or `unknown` salt command types #} {%- else %} - {#- no `config.get` merge option for `salt-ssh` or `unknown` cli #} {%- if _config['merge_strategy'] %} - {%- do salt['log.error']("map.jinja: the 'merge' option of 'config.get' is skipped with salt cli '" - ~ _config['cli'] | replace('call', 'ssh') | default('unknown', True) ~ "'") - %} + {%- do salt['log.error']("map.jinja: the 'merge' option of 'config.get' is skipped when the salt command type is '" ~ cli ~ "'") %} {%- endif %} {%- do _config.update({'merge_opt': {}, 'merge_msg': ''}) %}