feat(sub-component): manage a dedicated configuration file

* pillar.example (template.subcomponent): configurable sub-component
  destination.
  (template.tofs.source_files): override sub-component template

* template/init.sls (include): setup sub-component last.

* template/clean.sls (include): clean sub-component first.

* template/subcomponent/init.sls: include the only setup state
  “config”.

* template/subcomponent/config/init.sls: include the only config state
  “file”.

* template/subcomponent/config/file.sls: manage the sub-component
  configuration file. It requires the main component configuration.

* template/subcomponent/files/default/subcomponent-example.tmpl.jinja:
  jinja template for the sub-component.

* template/subcomponent/files/default/subcomponent-example.tmpl:
  static configuration for the sub-component.

* template/subcomponent/clean.sls: include the only cleanup state “config”.

* template/subcomponent/config/clean.sls: remove the sub-component
  configuration file.
This commit is contained in:
Daniel Dehennin 2019-07-30 16:17:39 +02:00
parent 42a75d99ee
commit c4440d7c55
10 changed files with 76 additions and 0 deletions

View file

@ -24,6 +24,9 @@ template:
{%- endif %}
config: /etc/template-formula.conf
subcomponent:
config: /etc/template-subcomponent-formula.conf
tofs:
# The files_switch key serves as a selector for alternative
# directories under the formula files directory. See TOFS pattern
@ -54,6 +57,8 @@ template:
source_files:
template-config-file-file-managed:
- 'example.tmpl.jinja'
template-subcomponent-config-file-file-managed:
- 'subcomponent-example.tmpl.jinja'
# Just for testing purposes
winner: pillar

View file

@ -2,6 +2,7 @@
# vim: ft=sls
include:
- .subcomponent.clean
- .service.clean
- .config.clean
- .package.clean

View file

@ -5,3 +5,4 @@ include:
- .package
- .config
- .service
- .subcomponent

View file

@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
# vim: ft=sls
include:
- .config.clean

View file

@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-
# vim: ft=sls
{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split('/')[0] %}
{%- set sls_service_running = tplroot ~ '.service.running' %}
{%- from tplroot ~ "/map.jinja" import template with context %}
include:
- {{ sls_service_running }}
template-subcomponent-config-clean-file-absent:
file.absent:
- name: {{ template.subcomponent.config }}
- watch_in:
- sls: {{ sls_service_running }}

View file

@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
# vim: ft=sls
{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split('/')[0] %}
{%- set sls_config_file = tplroot ~ '.config.file' %}
{%- from tplroot ~ "/map.jinja" import template with context %}
{%- from tplroot ~ "/libtofs.jinja" import files_switch with context %}
include:
- {{ sls_config_file }}
template-subcomponent-config-file-file-managed:
file.managed:
- name: {{ template.subcomponent.config }}
- source: {{ files_switch(['subcomponent-example.tmpl'],
lookup='template-subcomponent-config-file-file-managed'
)
}}
- mode: 644
- user: root
- group: {{ template.rootgroup }}
- makedirs: True
- template: jinja
- require_in:
- sls: {{ sls_config_file }}

View file

@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
# vim: ft=sls
include:
- .file

View file

@ -0,0 +1,6 @@
########################################################################
# File managed by Salt at <{{ source }}>.
# Your changes will be overwritten.
########################################################################
This is a subcomponent example file from SaltStack template-formula.

View file

@ -0,0 +1,6 @@
########################################################################
# File managed by Salt at <{{ source }}>.
# Your changes will be overwritten.
########################################################################
This is another subcomponent example file from SaltStack template-formula.

View file

@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
# vim: ft=sls
include:
- .config