Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tofs): allow TOFS for master+minion configuration #398

Merged
merged 3 commits into from
Jun 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions pillar.example
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,32 @@ salt:
salt_minion_pkg_source: 'https://repo.saltstack.com/osx/salt-2017.7.4-py3-x86_64.pkg'
salt_minion_pkg_hash: 'https://repo.saltstack.com/osx/salt-2017.7.4-py3-x86_64.pkg.md5'

# tofs:
# The files_switch key serves as a selector for alternative
# directories under the formula files directory. See TOFS pattern
# doc for more info.
# Note: Any value not evaluated by `config.get` will be used literally.
# This can be used to set custom paths, as many levels deep as required.
# files_switch:
# - any/path/can/be/used/here
# - id
# - osfinger
# - os
# - os_family
# All aspects of path/file resolution are customisable using the options below.
# This is unnecessary in most cases; there are sensible defaults.
# path_prefix: template_alt
# dirs:
# files: files_alt
# default: default_alt
# source_files:
# salt-master:
# - 'alt_master.d'
# salt-minion:
# - 'alt_minion.d'

# salt master config
master_config_use_TOFS: True
master:
myii marked this conversation as resolved.
Show resolved Hide resolved
fileserver_backend:
- git
Expand Down Expand Up @@ -127,6 +152,7 @@ salt:
- /srv/salt/reactors/deploy.sls

# salt minion config:
minion_config_use_TOFS: True
minion:

# single master setup
Expand Down
2 changes: 2 additions & 0 deletions salt/defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ salt:

minion_remove_config: False
master_remove_config: False
minion_config_use_TOFS: False
master_config_use_TOFS: False

minion_service: salt-minion
master_service: salt-master
Expand Down
4 changes: 4 additions & 0 deletions salt/files/default/master.d/engine.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
########################################################################
## File managed by Salt.
## Your changes will be overwritten.
########################################################################
4 changes: 4 additions & 0 deletions salt/files/default/master.d/f_defaults.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
########################################################################
## File managed by Salt.
## Your changes will be overwritten.
########################################################################
4 changes: 4 additions & 0 deletions salt/files/default/master.d/lxc_profiles.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
########################################################################
## File managed by Salt.
## Your changes will be overwritten.
########################################################################
4 changes: 4 additions & 0 deletions salt/files/default/master.d/reactor.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
########################################################################
## File managed by Salt.
## Your changes will be overwritten.
########################################################################
4 changes: 4 additions & 0 deletions salt/files/default/minion.d/beacons.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
########################################################################
## File managed by Salt.
## Your changes will be overwritten.
########################################################################
4 changes: 4 additions & 0 deletions salt/files/default/minion.d/engine.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
########################################################################
## File managed by Salt.
## Your changes will be overwritten.
########################################################################
4 changes: 4 additions & 0 deletions salt/files/default/minion.d/f_defaults.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
########################################################################
## File managed by Salt.
## Your changes will be overwritten.
########################################################################
4 changes: 4 additions & 0 deletions salt/files/default/minion.d/reactor.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
########################################################################
## File managed by Salt.
## Your changes will be overwritten.
########################################################################
101 changes: 101 additions & 0 deletions salt/libtofs.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{%- macro files_switch(source_files,
lookup=None,
default_files_switch=['id', 'os_family'],
indent_width=6,
v1_path_prefix='') %}
{#-
Returns a valid value for the "source" parameter of a "file.managed"
state function. This makes easier the usage of the Template Override and
Files Switch (TOFS) pattern.

Params:
* source_files: ordered list of files to look for
* lookup: key under '<tplroot>:tofs:source_files' to override
list of source files
* default_files_switch: if there's no config (e.g. pillar)
'<tplroot>:tofs:files_switch' this is the ordered list of grains to
use as selector switch of the directories under
"<path_prefix>/files"
* indent_witdh: indentation of the result value to conform to YAML
* v1_path_prefix: (deprecated) only used for injecting a path prefix into
the source, to support older TOFS configs

Example (based on a `tplroot` of `xxx`):

If we have a state:

Deploy configuration:
file.managed:
- name: /etc/yyy/zzz.conf
- source: {{ files_switch(['/etc/yyy/zzz.conf', '/etc/yyy/zzz.conf.jinja'],
lookup='Deploy configuration'
) }}
- template: jinja

In a minion with id=theminion and os_family=RedHat, it's going to be
rendered as:

Deploy configuration:
file.managed:
- name: /etc/yyy/zzz.conf
- source:
- salt://xxx/files/theminion/etc/yyy/zzz.conf
- salt://xxx/files/theminion/etc/yyy/zzz.conf.jinja
- salt://xxx/files/RedHat/etc/yyy/zzz.conf
- salt://xxx/files/RedHat/etc/yyy/zzz.conf.jinja
- salt://xxx/files/default/etc/yyy/zzz.conf
- salt://xxx/files/default/etc/yyy/zzz.conf.jinja
- template: jinja
#}
{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split('/')[0] %}
{%- set path_prefix = salt['config.get'](tplroot ~ ':tofs:path_prefix', tplroot) %}
{%- set files_dir = salt['config.get'](tplroot ~ ':tofs:dirs:files', 'files') %}
{%- set files_switch_list = salt['config.get'](
tplroot ~ ':tofs:files_switch',
default_files_switch
) %}
{#- Lookup source_files (v2), files (v1), or fallback to source_files parameter #}
{%- set src_files = salt['config.get'](
tplroot ~ ':tofs:source_files:' ~ lookup,
salt['config.get'](
tplroot ~ ':tofs:files:' ~ lookup,
source_files
)
) %}
{#- Only add to [''] when supporting older TOFS implementations #}
{%- set path_prefix_exts = [''] %}
{%- if v1_path_prefix != '' %}
{%- do path_prefix_exts.append(v1_path_prefix) %}
{%- endif %}
{%- for path_prefix_ext in path_prefix_exts %}
{%- set path_prefix_inc_ext = path_prefix ~ path_prefix_ext %}
{#- For older TOFS implementation, use `files_switch` from the config #}
{#- Use the default, new method otherwise #}
{%- set fsl = salt['config.get'](
tplroot ~ path_prefix_ext|replace('/', ':') ~ ':files_switch',
files_switch_list
) %}
{#- Append an empty value to evaluate as `default` in the loop below #}
{%- if '' not in fsl %}
{%- do fsl.append('') %}
{%- endif %}
{%- for fs in fsl %}
{%- for src_file in src_files %}
{%- if fs %}
{%- set fs_dir = salt['config.get'](fs, fs) %}
{%- else %}
{%- set fs_dir = salt['config.get'](tplroot ~ ':tofs:dirs:default', 'default') %}
{%- endif %}
{%- set url = [
'- salt:/',
path_prefix_inc_ext.strip('/'),
files_dir.strip('/'),
fs_dir.strip('/'),
src_file.strip('/'),
] | select | join('/') %}
{{ url | indent(indent_width, true) }}
{%- endfor %}
{%- endfor %}
{%- endfor %}
{%- endmacro %}
12 changes: 11 additions & 1 deletion salt/master.sls
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{% from "salt/map.jinja" import salt_settings with context %}
{%- set tplroot = tpldir.split('/')[0] %}
{%- from tplroot ~ "/map.jinja" import salt_settings with context %}
{%- from tplroot ~ "/libtofs.jinja" import files_switch with context %}

salt-master:
{% if salt_settings.install_packages %}
Expand All @@ -14,8 +16,16 @@ salt-master:
{% endif %}
file.recurse:
- name: {{ salt_settings.config_path }}/master.d
{%- if salt_settings.master_config_use_TOFS %}
- template: ''
- source: {{ files_switch(['master.d'],
lookup='salt-master'
)
}}
{%- else %}
- template: jinja
- source: salt://{{ slspath }}/files/master.d
{%- endif %}
- clean: {{ salt_settings.clean_config_d_dir }}
- exclude_pat: _*
service.running:
Expand Down
16 changes: 13 additions & 3 deletions salt/minion.sls
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{% from "salt/map.jinja" import salt_settings with context %}
{%- set tplroot = tpldir.split('/')[0] %}
{%- from tplroot ~ "/map.jinja" import salt_settings with context %}
{%- from tplroot ~ "/libtofs.jinja" import files_switch with context %}

{% if salt_settings.install_packages and grains.os == 'MacOS' and salt_settings.salt_minion_pkg_source != '' and salt_settings.version != '' %}
{# only download IF we know where to get the pkg from and if we know what version to check the current install (if installed) against #}
Expand Down Expand Up @@ -45,12 +47,20 @@ salt-minion:
{% endif %}
file.recurse:
- name: {{ salt_settings.config_path }}/minion.d
{%- if salt_settings.minion_config_use_TOFS %}
- template: ''
- source: {{ files_switch(['minion.d'],
lookup='salt-minion'
)
}}
{%- else %}
- template: jinja
- source: salt://{{ slspath }}/files/minion.d
- clean: {{ salt_settings.clean_config_d_dir }}
- exclude_pat: _*
- context:
standalone: False
{%- endif %}
- clean: {{ salt_settings.clean_config_d_dir }}
- exclude_pat: _*
service.running:
- enable: True
- name: {{ salt_settings.minion_service }}
Expand Down