Skip to content

Commit 8a05d0f

Browse files
kjkeanemyii
authored andcommitted
feat: update chrony formula to new template
BREAKING CHANGE: `chrony.removed` replaced by `.clean` states.
1 parent c491fbd commit 8a05d0f

24 files changed

+505
-173
lines changed

chrony/clean.sls

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# -*- coding: utf-8 -*-
2+
# vim: ft=sls
3+
4+
include:
5+
- .service.clean
6+
- .config.clean
7+
- .package.clean

chrony/config.sls

Lines changed: 0 additions & 17 deletions
This file was deleted.

chrony/config/clean.sls

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# -*- coding: utf-8 -*-
2+
# vim: ft=sls
3+
4+
{#- Get the `tplroot` from `tpldir` #}
5+
{%- set tplroot = tpldir.split('/')[0] %}
6+
{%- set sls_service_clean = tplroot ~ '.service.clean' %}
7+
{%- from tplroot ~ "/map.jinja" import chrony with context %}
8+
9+
include:
10+
- {{ sls_service_clean }}
11+
12+
chrony-config-clean-file-absent:
13+
file.absent:
14+
- name: {{ chrony.config }}
15+
- require:
16+
- sls: {{ sls_service_clean }}

chrony/config/file.sls

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# -*- coding: utf-8 -*-
2+
# vim: ft=sls
3+
4+
{#- Get the `tplroot` from `tpldir` #}
5+
{%- set tplroot = tpldir.split('/')[0] %}
6+
{%- set sls_package_install = tplroot ~ '.package.install' %}
7+
{%- from tplroot ~ "/map.jinja" import chrony with context %}
8+
{%- from tplroot ~ "/libtofs.jinja" import files_switch with context %}
9+
10+
include:
11+
- {{ sls_package_install }}
12+
13+
chrony-config-file-file-managed:
14+
file.managed:
15+
- name: {{ chrony.config }}
16+
- source: {{ files_switch(['chrony.conf', 'chrony.conf.jinja'],
17+
lookup='chrony-config-file-file-managed'
18+
)
19+
}}
20+
- mode: 644
21+
- user: root
22+
- group: root
23+
- template: jinja
24+
- context:
25+
chrony: {{ chrony|json }}
26+
- require:
27+
- sls: {{ sls_package_install }}

chrony/config/init.sls

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# -*- coding: utf-8 -*-
2+
# vim: ft=sls
3+
4+
include:
5+
- .file

chrony/defaults.yaml

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
1-
# Default lookup dictionary
2-
1+
# -*- coding: utf-8 -*-
2+
# vim: ft=yaml
3+
---
34
chrony:
45
package: chrony
5-
service: chronyd
6+
service:
7+
name: chronyd
68
config: /etc/chrony.conf
7-
config_src: salt://{{ slspath }}/files/chrony_config
8-
ntpservers: [ '0.us.pool.ntp.org',
9-
'1.us.pool.ntp.org',
10-
'2.us.pool.ntp.org',
11-
'3.us.pool.ntp.org'
12-
]
9+
ntpservers:
10+
- '0.us.pool.ntp.org'
11+
- '1.us.pool.ntp.org'
12+
- '2.us.pool.ntp.org'
13+
- '3.us.pool.ntp.org'
1314
options: iburst
1415
logdir: /var/log/chrony
1516
keyfile: /etc/chrony.keys
1617
driftfile: /var/lib/chrony/drift
17-
otherparams: [ 'rtcsync',
18-
'makestep 10 3',
19-
'stratumweight 0',
20-
'bindcmdaddress 127.0.0.1',
21-
'bindcmdaddress ::1',
22-
'commandkey 1',
23-
'generatecommandkey',
24-
'noclientlog',
25-
'logchange 0.5',
26-
]
27-
allow: [ '10/8', '192.168/16', '172.16/12' ]
18+
otherparams:
19+
- 'rtcsync'
20+
- 'makestep 10 3'
21+
- 'stratumweight 0'
22+
- 'bindcmdaddress 127.0.0.1'
23+
- 'bindcmdaddress ::1'
24+
- 'noclientlog'
25+
- 'logchange 0.5'
26+
allow:
27+
- '10/8'
28+
- '192.168/16'
29+
- '172.16/12'

chrony/files/chrony_config

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
########################################################################
2+
# File managed by Salt at <{{ source }}>.
3+
# Your changes will be overwritten.
4+
########################################################################
5+
6+
{% for server in chrony.ntpservers -%}
7+
server {{ server }} {{ chrony.options }}
8+
{% endfor %}
9+
10+
keyfile {{ chrony.keyfile }}
11+
12+
driftfile {{ chrony.driftfile }}
13+
14+
{% if chrony.allow is defined %}
15+
{% for allowed in chrony.get('allow', []) -%}
16+
allow {{ allowed }}
17+
{% endfor %}
18+
{%- endif %}
19+
20+
logdir {{ chrony.logdir }}
21+
22+
{% for param in chrony.get('otherparams', []) -%}
23+
{{ param }}
24+
{% endfor %}

chrony/init.sls

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
{% from slspath+"/map.jinja" import chrony with context %}
1+
# -*- coding: utf-8 -*-
2+
# vim: ft=sls
23

3-
chrony:
4-
pkg.installed:
5-
- name: {{ chrony.package }}
6-
service.running:
7-
- enable: True
8-
- name: {{ chrony.service }}
9-
- require:
10-
- pkg: {{ chrony.package }}
4+
include:
5+
- .package
6+
- .config
7+
- .service

chrony/libtofs.jinja

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
{%- macro files_switch(source_files,
2+
lookup=None,
3+
default_files_switch=['id', 'os_family'],
4+
indent_width=6,
5+
v1_path_prefix='') %}
6+
{#-
7+
Returns a valid value for the "source" parameter of a "file.managed"
8+
state function. This makes easier the usage of the Template Override and
9+
Files Switch (TOFS) pattern.
10+
11+
Params:
12+
* source_files: ordered list of files to look for
13+
* lookup: key under '<tplroot>:tofs:source_files' to override
14+
list of source files
15+
* 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+
use as selector switch of the directories under
18+
"<path_prefix>/files"
19+
* indent_witdh: indentation of the result value to conform to YAML
20+
* v1_path_prefix: (deprecated) only used for injecting a path prefix into
21+
the source, to support older TOFS configs
22+
23+
Example (based on a `tplroot` of `xxx`):
24+
25+
If we have a state:
26+
27+
Deploy configuration:
28+
file.managed:
29+
- name: /etc/yyy/zzz.conf
30+
- source: {{ files_switch(['/etc/yyy/zzz.conf', '/etc/yyy/zzz.conf.jinja'],
31+
lookup='Deploy configuration'
32+
) }}
33+
- template: jinja
34+
35+
In a minion with id=theminion and os_family=RedHat, it's going to be
36+
rendered as:
37+
38+
Deploy configuration:
39+
file.managed:
40+
- name: /etc/yyy/zzz.conf
41+
- source:
42+
- salt://xxx/files/theminion/etc/yyy/zzz.conf
43+
- salt://xxx/files/theminion/etc/yyy/zzz.conf.jinja
44+
- salt://xxx/files/RedHat/etc/yyy/zzz.conf
45+
- salt://xxx/files/RedHat/etc/yyy/zzz.conf.jinja
46+
- salt://xxx/files/default/etc/yyy/zzz.conf
47+
- salt://xxx/files/default/etc/yyy/zzz.conf.jinja
48+
- 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 source_files parameter #}
59+
{%- set src_files = salt['config.get'](
60+
tplroot ~ ':tofs:source_files:' ~ lookup,
61+
salt['config.get'](
62+
tplroot ~ ':tofs:files:' ~ lookup,
63+
source_files
64+
)
65+
) %}
66+
{#- Only add to [''] when supporting older TOFS implementations #}
67+
{%- set path_prefix_exts = [''] %}
68+
{%- if v1_path_prefix != '' %}
69+
{%- do path_prefix_exts.append(v1_path_prefix) %}
70+
{%- endif %}
71+
{%- for path_prefix_ext in path_prefix_exts %}
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+
{%- do fsl.append('') %}
82+
{%- endif %}
83+
{%- for fs in fsl %}
84+
{%- for src_file in src_files %}
85+
{%- if fs %}
86+
{%- set fs_dir = salt['config.get'](fs, fs) %}
87+
{%- else %}
88+
{%- set fs_dir = salt['config.get'](tplroot ~ ':tofs:dirs:default', 'default') %}
89+
{%- endif %}
90+
{%- set url = '- salt://' ~ '/'.join([
91+
path_prefix_inc_ext,
92+
files_dir,
93+
fs_dir,
94+
src_file.lstrip('/')
95+
]) %}
96+
{{ url | indent(indent_width, true) }}
97+
{%- endfor %}
98+
{%- endfor %}
99+
{%- endfor %}
100+
{%- endmacro %}

0 commit comments

Comments
 (0)