From 096e83028145582bc9296e31f859058887faa9d6 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Wed, 18 Jan 2017 19:11:23 -0500 Subject: [PATCH 01/19] Get the static items tested. --- keepalived/templates/config.jinja | 7 +++++ keepalived/templates/test_config.jinja | 2 ++ test/__init__.py | 0 test/test_keepalived_config.py | 41 ++++++++++++++++++++++++++ 4 files changed, 50 insertions(+) create mode 100644 keepalived/templates/config.jinja create mode 100644 keepalived/templates/test_config.jinja create mode 100644 test/__init__.py create mode 100755 test/test_keepalived_config.py diff --git a/keepalived/templates/config.jinja b/keepalived/templates/config.jinja new file mode 100644 index 0000000..a8dc487 --- /dev/null +++ b/keepalived/templates/config.jinja @@ -0,0 +1,7 @@ +{% macro keepalived_config(data) %} +{% if data is none %} + +{% else %} +{{ data }} +{% endif %} +{% endmacro %} diff --git a/keepalived/templates/test_config.jinja b/keepalived/templates/test_config.jinja new file mode 100644 index 0000000..baf56b5 --- /dev/null +++ b/keepalived/templates/test_config.jinja @@ -0,0 +1,2 @@ +{% import 'config.jinja' as config %} +{{ config.keepalived_config(testdata) }} diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/test_keepalived_config.py b/test/test_keepalived_config.py new file mode 100755 index 0000000..696fbcd --- /dev/null +++ b/test/test_keepalived_config.py @@ -0,0 +1,41 @@ +#!/usr/bin/python + +import os, unittest +from jinja2 import Environment, FileSystemLoader + +class TestKeepalivedConfiguration(unittest.TestCase): + + def setUp(self): + self.t_dir = os.path.abspath(os.path.join( + os.path.dirname(__file__), + os.pardir, + 'keepalived', + 'templates')) + self.t_conf = Environment(loader=FileSystemLoader(self.t_dir), + trim_blocks=True) + + def _render(self, data): + holder = self.t_conf.get_template('test_config.jinja').render(testdata=data) + print(repr(holder)) + return holder + + def test_string(self): + testdata = 'stuff' + result = 'stuff\n' + self.assertEqual(self._render(testdata), result, + 'A string should be returned with a line feed.') + + def test_number(self): + testdata = 3 + result = '3\n' + self.assertEqual(self._render(testdata), result, + 'A number should be returned with a line feed.') + + def test_null(self): + testdata = None + result = '\n' + self.assertEqual(self._render(testdata), result, + 'A null should just return a line feed.') + +if __name__ == '__main__': + unittest.main() From ff6fc02595bfe339c9c5da9402edd1e1cf2f046c Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Wed, 18 Jan 2017 19:51:16 -0500 Subject: [PATCH 02/19] Refactored the testing scripts. --- keepalived/templates/config.jinja | 18 +++++++++++------- test/test_keepalived_config.py | 20 +++++++++++--------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/keepalived/templates/config.jinja b/keepalived/templates/config.jinja index a8dc487..c7754c3 100644 --- a/keepalived/templates/config.jinja +++ b/keepalived/templates/config.jinja @@ -1,7 +1,11 @@ -{% macro keepalived_config(data) %} -{% if data is none %} - -{% else %} -{{ data }} -{% endif %} -{% endmacro %} +{%- macro keepalived_config(data) -%} + {%- if data is none -%} +{{ '\n' }} + {%- elif data is mapping -%} + {%- for key, value in data.iteritems() -%} +{{ key }} {{ keepalived_config(value) }} + {%- endfor -%} + {%- else -%} +{{ data }}{{ '\n' }} + {%- endif -%} +{%- endmacro -%} diff --git a/test/test_keepalived_config.py b/test/test_keepalived_config.py index 696fbcd..e72ebc3 100755 --- a/test/test_keepalived_config.py +++ b/test/test_keepalived_config.py @@ -14,28 +14,30 @@ class TestKeepalivedConfiguration(unittest.TestCase): self.t_conf = Environment(loader=FileSystemLoader(self.t_dir), trim_blocks=True) - def _render(self, data): + def renderTest(self, data, result): holder = self.t_conf.get_template('test_config.jinja').render(testdata=data) - print(repr(holder)) - return holder + output = repr(holder) + ' did not equal ' + repr(result) + self.assertEqual(holder, result, output) def test_string(self): testdata = 'stuff' result = 'stuff\n' - self.assertEqual(self._render(testdata), result, - 'A string should be returned with a line feed.') + self.renderTest(testdata, result) def test_number(self): testdata = 3 result = '3\n' - self.assertEqual(self._render(testdata), result, - 'A number should be returned with a line feed.') + self.renderTest(testdata, result) def test_null(self): testdata = None result = '\n' - self.assertEqual(self._render(testdata), result, - 'A null should just return a line feed.') + self.renderTest(testdata, result) + + def test_key_value_pair(self): + testdata = {'flintstone': 'fred'} + result = 'flintstone fred\n' + self.renderTest(testdata, result) if __name__ == '__main__': unittest.main() From d66a776ddb2f7067260d09a4c50dd342aa409577 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Wed, 18 Jan 2017 20:06:48 -0500 Subject: [PATCH 03/19] Added key array pairs. --- keepalived/templates/config.jinja | 8 +++++++- test/test_keepalived_config.py | 5 +++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/keepalived/templates/config.jinja b/keepalived/templates/config.jinja index c7754c3..f46e018 100644 --- a/keepalived/templates/config.jinja +++ b/keepalived/templates/config.jinja @@ -5,7 +5,13 @@ {%- for key, value in data.iteritems() -%} {{ key }} {{ keepalived_config(value) }} {%- endfor -%} - {%- else -%} + {%- elif data is string or data is number -%} {{ data }}{{ '\n' }} + {%- else -%} +{{ '{\n' }} + {%- for value in data -%} +{{ keepalived_config(value) }} + {%- endfor -%} +{{ '}\n' }} {%- endif -%} {%- endmacro -%} diff --git a/test/test_keepalived_config.py b/test/test_keepalived_config.py index e72ebc3..d8add52 100755 --- a/test/test_keepalived_config.py +++ b/test/test_keepalived_config.py @@ -39,5 +39,10 @@ class TestKeepalivedConfiguration(unittest.TestCase): result = 'flintstone fred\n' self.renderTest(testdata, result) + def test_key_array_pair(self): + testdata = {'flintstone': ['fred', 'wilma', 'pebbles']} + result = 'flintstone {\nfred\nwilma\npebbles\n}\n' + self.renderTest(testdata, result) + if __name__ == '__main__': unittest.main() From f96a5b9b4b5dd30fb7835a32b764500fab2f6e2f Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Wed, 18 Jan 2017 20:40:19 -0500 Subject: [PATCH 04/19] Added key hash pairs. --- keepalived/templates/config.jinja | 31 +++++++++++++++++++------------ test/test_keepalived_config.py | 5 +++++ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/keepalived/templates/config.jinja b/keepalived/templates/config.jinja index f46e018..832678d 100644 --- a/keepalived/templates/config.jinja +++ b/keepalived/templates/config.jinja @@ -1,17 +1,24 @@ -{%- macro keepalived_config(data) -%} +{%- macro keepalived_config(data, recurse=-1) -%} + {%- set recurse = recurse + 1 -%} {%- if data is none -%} -{{ '\n' }} - {%- elif data is mapping -%} - {%- for key, value in data.iteritems() -%} -{{ key }} {{ keepalived_config(value) }} - {%- endfor -%} + {{- '\n' -}} {%- elif data is string or data is number -%} -{{ data }}{{ '\n' }} - {%- else -%} -{{ '{\n' }} - {%- for value in data -%} -{{ keepalived_config(value) }} + {{- data }}{{ '\n' -}} + {%- elif data is mapping -%} + {%- if recurse > 0 -%} + {{- '{\n' -}} + {%- endif -%} + {%- for key, value in data.iteritems() -%} + {{- key }} {{ keepalived_config(value, recurse) -}} {%- endfor -%} -{{ '}\n' }} + {%- if recurse > 0 -%} + {{- '}\n' -}} + {%- endif -%} + {%- else -%} + {{- '{\n' -}} + {%- for value in data -%} + {{- keepalived_config(value) -}} + {%- endfor -%} + {{- '}\n' -}} {%- endif -%} {%- endmacro -%} diff --git a/test/test_keepalived_config.py b/test/test_keepalived_config.py index d8add52..55b46a1 100755 --- a/test/test_keepalived_config.py +++ b/test/test_keepalived_config.py @@ -44,5 +44,10 @@ class TestKeepalivedConfiguration(unittest.TestCase): result = 'flintstone {\nfred\nwilma\npebbles\n}\n' self.renderTest(testdata, result) + def test_key_hash_pair(self): + testdata = {'friends': {'rubble': 'barney'}} + result = 'friends {\nrubble barney\n}\n' + self.renderTest(testdata, result) + if __name__ == '__main__': unittest.main() From 243ab902abe73254c4bdecc4a510ec800c565183 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Wed, 18 Jan 2017 21:29:15 -0500 Subject: [PATCH 05/19] Sorted the dictionary. --- keepalived/templates/config.jinja | 10 ++++++---- test/test_keepalived_config.py | 7 ++++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/keepalived/templates/config.jinja b/keepalived/templates/config.jinja index 832678d..fa46d41 100644 --- a/keepalived/templates/config.jinja +++ b/keepalived/templates/config.jinja @@ -1,3 +1,5 @@ +{%- set key = 0 -%} +{%- set value = 1 -%} {%- macro keepalived_config(data, recurse=-1) -%} {%- set recurse = recurse + 1 -%} {%- if data is none -%} @@ -8,16 +10,16 @@ {%- if recurse > 0 -%} {{- '{\n' -}} {%- endif -%} - {%- for key, value in data.iteritems() -%} - {{- key }} {{ keepalived_config(value, recurse) -}} + {%- for item in data|dictsort -%} + {{- item[key] }} {{ keepalived_config(item[value], recurse) -}} {%- endfor -%} {%- if recurse > 0 -%} {{- '}\n' -}} {%- endif -%} {%- else -%} {{- '{\n' -}} - {%- for value in data -%} - {{- keepalived_config(value) -}} + {%- for item in data -%} + {{- keepalived_config(item) -}} {%- endfor -%} {{- '}\n' -}} {%- endif -%} diff --git a/test/test_keepalived_config.py b/test/test_keepalived_config.py index 55b46a1..1aed30a 100755 --- a/test/test_keepalived_config.py +++ b/test/test_keepalived_config.py @@ -12,7 +12,7 @@ class TestKeepalivedConfiguration(unittest.TestCase): 'keepalived', 'templates')) self.t_conf = Environment(loader=FileSystemLoader(self.t_dir), - trim_blocks=True) + trim_blocks=True) def renderTest(self, data, result): holder = self.t_conf.get_template('test_config.jinja').render(testdata=data) @@ -49,5 +49,10 @@ class TestKeepalivedConfiguration(unittest.TestCase): result = 'friends {\nrubble barney\n}\n' self.renderTest(testdata, result) + def test_key_ordered_hashs(self): + testdata = {'friends': {'fred': 'flintstone', 'barney': 'rubble', 'wilma': 'flintstone', 'betty': 'rubble'}} + result = 'friends {\nbarney rubble\nbetty rubble\nfred flintstone\nwilma flintstone\n}\n' + self.renderTest(testdata, result) + if __name__ == '__main__': unittest.main() From da070c862b3da5887c41beb499a3b167bbb6b121 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Wed, 18 Jan 2017 21:38:51 -0500 Subject: [PATCH 06/19] Added ordered hashes. --- keepalived/templates/config.jinja | 8 ++++++-- test/test_keepalived_config.py | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/keepalived/templates/config.jinja b/keepalived/templates/config.jinja index fa46d41..e02881f 100644 --- a/keepalived/templates/config.jinja +++ b/keepalived/templates/config.jinja @@ -17,10 +17,14 @@ {{- '}\n' -}} {%- endif -%} {%- else -%} - {{- '{\n' -}} + {%- if recurse > 0 -%} + {{- '{\n' -}} + {%- endif -%} {%- for item in data -%} {{- keepalived_config(item) -}} {%- endfor -%} - {{- '}\n' -}} + {%- if recurse > 0 -%} + {{- '}\n' -}} + {%- endif -%} {%- endif -%} {%- endmacro -%} diff --git a/test/test_keepalived_config.py b/test/test_keepalived_config.py index 1aed30a..30abe51 100755 --- a/test/test_keepalived_config.py +++ b/test/test_keepalived_config.py @@ -54,5 +54,10 @@ class TestKeepalivedConfiguration(unittest.TestCase): result = 'friends {\nbarney rubble\nbetty rubble\nfred flintstone\nwilma flintstone\n}\n' self.renderTest(testdata, result) + def test_ordered_hashes(self): + testdata = [{'fred': 'flintstone'}, {'wilma': 'flintstone'}, {'barney': 'rubble'}, {'betty': 'rubble'}] + result = 'fred flintstone\nwilma flintstone\nbarney rubble\nbetty rubble\n' + self.renderTest(testdata, result) + if __name__ == '__main__': unittest.main() From 4c329515f78beda9dbdd9b3fd523cad4b22fafdf Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Wed, 18 Jan 2017 21:47:53 -0500 Subject: [PATCH 07/19] refactored. --- keepalived/templates/config.jinja | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/keepalived/templates/config.jinja b/keepalived/templates/config.jinja index e02881f..fe0b3e3 100644 --- a/keepalived/templates/config.jinja +++ b/keepalived/templates/config.jinja @@ -6,23 +6,19 @@ {{- '\n' -}} {%- elif data is string or data is number -%} {{- data }}{{ '\n' -}} - {%- elif data is mapping -%} - {%- if recurse > 0 -%} - {{- '{\n' -}} - {%- endif -%} - {%- for item in data|dictsort -%} - {{- item[key] }} {{ keepalived_config(item[value], recurse) -}} - {%- endfor -%} - {%- if recurse > 0 -%} - {{- '}\n' -}} - {%- endif -%} {%- else -%} - {%- if recurse > 0 -%} + {%- if recurse > 0 -%} {{- '{\n' -}} {%- endif -%} - {%- for item in data -%} - {{- keepalived_config(item) -}} - {%- endfor -%} + {%- if data is mapping -%} + {%- for item in data|dictsort -%} + {{- item[key] }} {{ keepalived_config(item[value], recurse) -}} + {%- endfor -%} + {%- else -%} + {%- for item in data -%} + {{- keepalived_config(item) -}} + {%- endfor -%} + {%- endif -%} {%- if recurse > 0 -%} {{- '}\n' -}} {%- endif -%} From 671f64b67284f507aa3fd73166fa683b2af457e5 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Thu, 19 Jan 2017 07:48:26 -0500 Subject: [PATCH 08/19] Added the first of the carryovers. --- keepalived/templates/config.jinja | 14 +++++++++++--- test/test_keepalived_config.py | 5 +++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/keepalived/templates/config.jinja b/keepalived/templates/config.jinja index fe0b3e3..81f3cd9 100644 --- a/keepalived/templates/config.jinja +++ b/keepalived/templates/config.jinja @@ -1,6 +1,7 @@ {%- set key = 0 -%} {%- set value = 1 -%} -{%- macro keepalived_config(data, recurse=-1) -%} +{%- set carryovers = ['vrrp_script'] -%} +{%- macro keepalived_config(data, carryover='', recurse=-1) -%} {%- set recurse = recurse + 1 -%} {%- if data is none -%} {{- '\n' -}} @@ -12,7 +13,14 @@ {%- endif -%} {%- if data is mapping -%} {%- for item in data|dictsort -%} - {{- item[key] }} {{ keepalived_config(item[value], recurse) -}} + {%- if item[key] in carryovers -%} + {{- keepalived_config(item[value], carryover=item[key]) -}} + {%- else -%} + {%- if carryover -%} + {{- carryover }}{{ ' ' -}} + {%- endif -%} + {{- item[key] }} {{ keepalived_config(item[value], recurse=recurse) -}} + {%- endif -%} {%- endfor -%} {%- else -%} {%- for item in data -%} @@ -20,7 +28,7 @@ {%- endfor -%} {%- endif -%} {%- if recurse > 0 -%} - {{- '}\n' -}} + {{- '}\n' -}} {%- endif -%} {%- endif -%} {%- endmacro -%} diff --git a/test/test_keepalived_config.py b/test/test_keepalived_config.py index 30abe51..091e2c7 100755 --- a/test/test_keepalived_config.py +++ b/test/test_keepalived_config.py @@ -59,5 +59,10 @@ class TestKeepalivedConfiguration(unittest.TestCase): result = 'fred flintstone\nwilma flintstone\nbarney rubble\nbetty rubble\n' self.renderTest(testdata, result) + def test_carryover(self): + testdata = {'vrrp_script': {'gizmo': {'fred': 'flintstone', 'barney': 'rubble'}}} + result = 'vrrp_script gizmo {\nbarney rubble\nfred flintstone\n}\n' + self.renderTest(testdata, result) + if __name__ == '__main__': unittest.main() From f419f265e9b2368676b1c7ad1d1850f0ff6775e6 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Thu, 19 Jan 2017 08:09:44 -0500 Subject: [PATCH 09/19] Adding additional vrrp --- keepalived/templates/config.jinja | 2 +- test/test_keepalived_config.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/keepalived/templates/config.jinja b/keepalived/templates/config.jinja index 81f3cd9..95edc30 100644 --- a/keepalived/templates/config.jinja +++ b/keepalived/templates/config.jinja @@ -1,6 +1,6 @@ {%- set key = 0 -%} {%- set value = 1 -%} -{%- set carryovers = ['vrrp_script'] -%} +{%- set carryovers = ['vrrp_instance', 'vrrp_script'] -%} {%- macro keepalived_config(data, carryover='', recurse=-1) -%} {%- set recurse = recurse + 1 -%} {%- if data is none -%} diff --git a/test/test_keepalived_config.py b/test/test_keepalived_config.py index 091e2c7..e8a916c 100755 --- a/test/test_keepalived_config.py +++ b/test/test_keepalived_config.py @@ -64,5 +64,20 @@ class TestKeepalivedConfiguration(unittest.TestCase): result = 'vrrp_script gizmo {\nbarney rubble\nfred flintstone\n}\n' self.renderTest(testdata, result) + def test_carryover_contains_arry(self): + testdata = {'vrrp_script': {'gizmo': [{'fred': 'flintstone'}, {'barney': 'rubble'}]}} + result = 'vrrp_script gizmo {\nfred flintstone\nbarney rubble\n}\n' + self.renderTest(testdata, result) + + def test_carrover_vrrp_instance(self): + testdata = {'vrrp_instance': {'gizmo': {'fred': 'flintstone', 'barney': 'rubble'}}} + result = 'vrrp_instance gizmo {\nbarney rubble\nfred flintstone\n}\n' + self.renderTest(testdata, result) + + def test_carryovers_in_an_array(self): + testdata = [{'vrrp_script': {'gizmo': {'running': 'dumdums'}}}, {'vrrp_instance': {'dumdums': {'fred': 'flintstone'}}}] + result = 'vrrp_script gizmo {\nrunning dumdums\n}\nvrrp_instance dumdums {\nfred flintstone\n}\n' + self.renderTest(testdata, result) + if __name__ == '__main__': unittest.main() From c545484961e63add5c578ceb9fd213ba4616a408 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Thu, 19 Jan 2017 08:25:12 -0500 Subject: [PATCH 10/19] Added additional carryovers. --- keepalived/templates/config.jinja | 3 ++- test/test_keepalived_config.py | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/keepalived/templates/config.jinja b/keepalived/templates/config.jinja index 95edc30..2749f36 100644 --- a/keepalived/templates/config.jinja +++ b/keepalived/templates/config.jinja @@ -1,6 +1,7 @@ {%- set key = 0 -%} {%- set value = 1 -%} -{%- set carryovers = ['vrrp_instance', 'vrrp_script'] -%} +{%- set carryovers = ['real_server', 'virtual_server', 'virtual_server_group', + 'vrrp_instance', 'vrrp_script', 'vrrp_sync_group'] -%} {%- macro keepalived_config(data, carryover='', recurse=-1) -%} {%- set recurse = recurse + 1 -%} {%- if data is none -%} diff --git a/test/test_keepalived_config.py b/test/test_keepalived_config.py index e8a916c..f579236 100755 --- a/test/test_keepalived_config.py +++ b/test/test_keepalived_config.py @@ -79,5 +79,25 @@ class TestKeepalivedConfiguration(unittest.TestCase): result = 'vrrp_script gizmo {\nrunning dumdums\n}\nvrrp_instance dumdums {\nfred flintstone\n}\n' self.renderTest(testdata, result) + def test_carrover_vrrp_sync_group(self): + testdata = {'vrrp_sync_group': {'gizmo': {'fred': 'flintstone', 'barney': 'rubble'}}} + result = 'vrrp_sync_group gizmo {\nbarney rubble\nfred flintstone\n}\n' + self.renderTest(testdata, result) + + def test_carrover_virtual_server_group(self): + testdata = {'virtual_server_group': {'gizmo': {'fred': 'flintstone', 'barney': 'rubble'}}} + result = 'virtual_server_group gizmo {\nbarney rubble\nfred flintstone\n}\n' + self.renderTest(testdata, result) + + def test_carrover_virtual_server(self): + testdata = {'virtual_server': {'gizmo': {'fred': 'flintstone', 'barney': 'rubble'}}} + result = 'virtual_server gizmo {\nbarney rubble\nfred flintstone\n}\n' + self.renderTest(testdata, result) + + def test_carrover_real_server(self): + testdata = {'real_server': {'gizmo': {'fred': 'flintstone', 'barney': 'rubble'}}} + result = 'real_server gizmo {\nbarney rubble\nfred flintstone\n}\n' + self.renderTest(testdata, result) + if __name__ == '__main__': unittest.main() From 3f85ced5254a7b6823d7be0f4aeb75488bb14150 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Thu, 19 Jan 2017 09:06:48 -0500 Subject: [PATCH 11/19] Replaced the old macro. --- keepalived/templates/keepalived.jinja | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/keepalived/templates/keepalived.jinja b/keepalived/templates/keepalived.jinja index 3aaf5e1..44f61cd 100644 --- a/keepalived/templates/keepalived.jinja +++ b/keepalived/templates/keepalived.jinja @@ -6,6 +6,9 @@ # Any changes will be overwritten. {{ '\n' }} +{% import 'config.jinja' as config %} +{{ config.keepalived_config(keepalived_final_values) }} + {# Macro Explanation: @@ -19,7 +22,7 @@ hash is then processed by calling the macro again. Forced carriage returns and use of jinja's indent are only there to make the final file more human readable. They serve no other function. #} - +{# {%- import_yaml 'keepalived/defaults.yaml' as keepalived_defaults -%} {%- set keepalived_final_values = salt.pillar.get( 'keepalived', @@ -61,3 +64,4 @@ the final file more human readable. They serve no other function. {%- endmacro -%} {{ config_entries(keepalived_final_values, 0) }} +#} From 52a9281d42473588d9204fd779311652bcbf5da2 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Thu, 19 Jan 2017 09:15:12 -0500 Subject: [PATCH 12/19] Correcting the import. --- keepalived/templates/keepalived.jinja | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/keepalived/templates/keepalived.jinja b/keepalived/templates/keepalived.jinja index 44f61cd..efcca19 100644 --- a/keepalived/templates/keepalived.jinja +++ b/keepalived/templates/keepalived.jinja @@ -6,7 +6,7 @@ # Any changes will be overwritten. {{ '\n' }} -{% import 'config.jinja' as config %} +{% import 'keepalived/templates/config.jinja' as config %} {{ config.keepalived_config(keepalived_final_values) }} {# From 80b46aca2fac50921ae76f7f552e3a45be7cd13d Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Thu, 19 Jan 2017 09:25:24 -0500 Subject: [PATCH 13/19] Correcting the import. --- keepalived/templates/keepalived.jinja | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/keepalived/templates/keepalived.jinja b/keepalived/templates/keepalived.jinja index efcca19..eb64ed2 100644 --- a/keepalived/templates/keepalived.jinja +++ b/keepalived/templates/keepalived.jinja @@ -7,6 +7,11 @@ {{ '\n' }} {% import 'keepalived/templates/config.jinja' as config %} +{%- import_yaml 'keepalived/defaults.yaml' as keepalived_defaults -%} +{%- set keepalived_final_values = salt.pillar.get( + 'keepalived', + default=keepalived_defaults, + merge=True) -%} {{ config.keepalived_config(keepalived_final_values) }} {# From 1ee609a616a7702b637d97a7b89bd016f9b68385 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Thu, 19 Jan 2017 09:48:28 -0500 Subject: [PATCH 14/19] General cleanup. --- keepalived/templates/keepalived.jinja | 59 +-------------------------- pillar.example | 9 ++++ 2 files changed, 10 insertions(+), 58 deletions(-) diff --git a/keepalived/templates/keepalived.jinja b/keepalived/templates/keepalived.jinja index eb64ed2..b6fa7eb 100644 --- a/keepalived/templates/keepalived.jinja +++ b/keepalived/templates/keepalived.jinja @@ -6,67 +6,10 @@ # Any changes will be overwritten. {{ '\n' }} -{% import 'keepalived/templates/config.jinja' as config %} +{%- import 'keepalived/templates/config.jinja' as config -%} {%- import_yaml 'keepalived/defaults.yaml' as keepalived_defaults -%} {%- set keepalived_final_values = salt.pillar.get( 'keepalived', default=keepalived_defaults, merge=True) -%} {{ config.keepalived_config(keepalived_final_values) }} - -{# -Macro Explanation: - -This is a recursive macro that takes the type of entry and determines how -it is suppose to appear in the configuration file. Strings and numbers -are just written out. Lists and hashes are placed inside if couple of -parenthesis. It also takes into account the special groupings like -vrrp_instance and virtual_server. Any additional data from a list or a -hash is then processed by calling the macro again. - -Forced carriage returns and use of jinja's indent are only there to make -the final file more human readable. They serve no other function. -#} -{# -{%- import_yaml 'keepalived/defaults.yaml' as keepalived_defaults -%} -{%- set keepalived_final_values = salt.pillar.get( - 'keepalived', - default=keepalived_defaults, - merge=True) -%} - -{%- set groupings = ['vrrp_script', 'vrrp_sync_group', 'vrrp_instance', - 'virtual_server_group', 'virtual_server', 'real_server'] -%} -{%- macro config_entries(data, indents, carryover='') -%} - {%- if data is string or data is number -%} - {{- data|string|indent(indents, True) }}{{ '\n' -}} - {%- elif data is none -%} - {{- '\n' -}} - {%- else -%} - {%- if indents != 0 and not carryover -%} - {{- " {\n" -}} - {%- endif -%} - {%- if data is mapping -%} - {%- for entry in data|dictsort -%} - {%- if entry[0] in groupings -%} - {{- config_entries(entry[1], indents, carryover=entry[0]) -}} - {%- else -%} - {%- if carryover -%} - {{- carryover|indent(indents, True) }}{{ ' ' }} - {%- endif -%} - {{- entry[0]|indent(indents, True) }} - {{- config_entries(entry[1], indents + 2) -}} - {%- endif -%} - {%- endfor -%} - {%- else -%} - {%- for entry in data -%} - {{- config_entries(entry, indents) -}} - {%- endfor -%} - {%- endif -%} - {%- if indents != 0 and not carryover -%} - {{- '}'|indent(indents - 2, True) }}{{ '\n' }} - {%- endif -%} - {%- endif -%} -{%- endmacro -%} - -{{ config_entries(keepalived_final_values, 0) }} -#} diff --git a/pillar.example b/pillar.example index 99b8db7..43fdd54 100644 --- a/pillar.example +++ b/pillar.example @@ -1,6 +1,15 @@ # # Example pillar configuration # +# Boolean entries must be placed stored as strings, otherwise it will show +# up as 1 or 0 in the config file. +# +# Anything that needs to be in quotes in the configuration file needs to +# be escaped in the yaml file. Otherwise the quotes will not appear in +# the config file. +# +# When order is important, put the entries into a yaml array or list. This +# could be used to place vrrp_script before vrrp_instance entries. # The following would generate the example file in RedHat based systems. From 9a3410a34cb046de0b0c7823438feae4a5d530f4 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Fri, 20 Jan 2017 22:36:42 -0500 Subject: [PATCH 15/19] Starting the work to add indention back in. --- keepalived/templates/config.jinja | 12 ++++++++---- test/test_keepalived_config.py | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/keepalived/templates/config.jinja b/keepalived/templates/config.jinja index 2749f36..30afbb4 100644 --- a/keepalived/templates/config.jinja +++ b/keepalived/templates/config.jinja @@ -2,15 +2,16 @@ {%- set value = 1 -%} {%- set carryovers = ['real_server', 'virtual_server', 'virtual_server_group', 'vrrp_instance', 'vrrp_script', 'vrrp_sync_group'] -%} -{%- macro keepalived_config(data, carryover='', recurse=-1) -%} +{%- macro keepalived_config(data, carryover='', recurse=-1, indent=0) -%} {%- set recurse = recurse + 1 -%} {%- if data is none -%} {{- '\n' -}} {%- elif data is string or data is number -%} - {{- data }}{{ '\n' -}} + {{- data|string|indent(indent, True) }}{{ '\n' -}} {%- else -%} {%- if recurse > 0 -%} {{- '{\n' -}} + {%- set indent = indent + 2 -%} {%- endif -%} {%- if data is mapping -%} {%- for item in data|dictsort -%} @@ -20,12 +21,15 @@ {%- if carryover -%} {{- carryover }}{{ ' ' -}} {%- endif -%} - {{- item[key] }} {{ keepalived_config(item[value], recurse=recurse) -}} + {%- if item[value] is string or item[value] is not iterable -%} + {%- set indent = 0 -%} + {%- endif -%} + {{- item[key] }} {{ keepalived_config(item[value], recurse=recurse, indent=indent) -}} {%- endif -%} {%- endfor -%} {%- else -%} {%- for item in data -%} - {{- keepalived_config(item) -}} + {{- keepalived_config(item, indent=indent) -}} {%- endfor -%} {%- endif -%} {%- if recurse > 0 -%} diff --git a/test/test_keepalived_config.py b/test/test_keepalived_config.py index f579236..a2149c8 100755 --- a/test/test_keepalived_config.py +++ b/test/test_keepalived_config.py @@ -41,7 +41,7 @@ class TestKeepalivedConfiguration(unittest.TestCase): def test_key_array_pair(self): testdata = {'flintstone': ['fred', 'wilma', 'pebbles']} - result = 'flintstone {\nfred\nwilma\npebbles\n}\n' + result = 'flintstone {\n fred\n wilma\n pebbles\n}\n' self.renderTest(testdata, result) def test_key_hash_pair(self): From 306fd722878f6aec5e1190abc2379377acd8fe5c Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Fri, 20 Jan 2017 22:56:08 -0500 Subject: [PATCH 16/19] Added additional indentions. --- keepalived/templates/config.jinja | 3 ++- test/test_keepalived_config.py | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/keepalived/templates/config.jinja b/keepalived/templates/config.jinja index 30afbb4..d68ec45 100644 --- a/keepalived/templates/config.jinja +++ b/keepalived/templates/config.jinja @@ -14,6 +14,7 @@ {%- set indent = indent + 2 -%} {%- endif -%} {%- if data is mapping -%} + {%- set indentHolder = indent -%} {%- for item in data|dictsort -%} {%- if item[key] in carryovers -%} {{- keepalived_config(item[value], carryover=item[key]) -}} @@ -24,7 +25,7 @@ {%- if item[value] is string or item[value] is not iterable -%} {%- set indent = 0 -%} {%- endif -%} - {{- item[key] }} {{ keepalived_config(item[value], recurse=recurse, indent=indent) -}} + {{- item[key]|indent(indentHolder, True) }} {{ keepalived_config(item[value], recurse=recurse, indent=indent) -}} {%- endif -%} {%- endfor -%} {%- else -%} diff --git a/test/test_keepalived_config.py b/test/test_keepalived_config.py index a2149c8..27c88e7 100755 --- a/test/test_keepalived_config.py +++ b/test/test_keepalived_config.py @@ -46,12 +46,12 @@ class TestKeepalivedConfiguration(unittest.TestCase): def test_key_hash_pair(self): testdata = {'friends': {'rubble': 'barney'}} - result = 'friends {\nrubble barney\n}\n' + result = 'friends {\n rubble barney\n}\n' self.renderTest(testdata, result) def test_key_ordered_hashs(self): testdata = {'friends': {'fred': 'flintstone', 'barney': 'rubble', 'wilma': 'flintstone', 'betty': 'rubble'}} - result = 'friends {\nbarney rubble\nbetty rubble\nfred flintstone\nwilma flintstone\n}\n' + result = 'friends {\n barney rubble\n betty rubble\n fred flintstone\n wilma flintstone\n}\n' self.renderTest(testdata, result) def test_ordered_hashes(self): @@ -61,42 +61,42 @@ class TestKeepalivedConfiguration(unittest.TestCase): def test_carryover(self): testdata = {'vrrp_script': {'gizmo': {'fred': 'flintstone', 'barney': 'rubble'}}} - result = 'vrrp_script gizmo {\nbarney rubble\nfred flintstone\n}\n' + result = 'vrrp_script gizmo {\n barney rubble\n fred flintstone\n}\n' self.renderTest(testdata, result) def test_carryover_contains_arry(self): testdata = {'vrrp_script': {'gizmo': [{'fred': 'flintstone'}, {'barney': 'rubble'}]}} - result = 'vrrp_script gizmo {\nfred flintstone\nbarney rubble\n}\n' + result = 'vrrp_script gizmo {\n fred flintstone\n barney rubble\n}\n' self.renderTest(testdata, result) def test_carrover_vrrp_instance(self): testdata = {'vrrp_instance': {'gizmo': {'fred': 'flintstone', 'barney': 'rubble'}}} - result = 'vrrp_instance gizmo {\nbarney rubble\nfred flintstone\n}\n' + result = 'vrrp_instance gizmo {\n barney rubble\n fred flintstone\n}\n' self.renderTest(testdata, result) def test_carryovers_in_an_array(self): testdata = [{'vrrp_script': {'gizmo': {'running': 'dumdums'}}}, {'vrrp_instance': {'dumdums': {'fred': 'flintstone'}}}] - result = 'vrrp_script gizmo {\nrunning dumdums\n}\nvrrp_instance dumdums {\nfred flintstone\n}\n' + result = 'vrrp_script gizmo {\n running dumdums\n}\nvrrp_instance dumdums {\n fred flintstone\n}\n' self.renderTest(testdata, result) def test_carrover_vrrp_sync_group(self): testdata = {'vrrp_sync_group': {'gizmo': {'fred': 'flintstone', 'barney': 'rubble'}}} - result = 'vrrp_sync_group gizmo {\nbarney rubble\nfred flintstone\n}\n' + result = 'vrrp_sync_group gizmo {\n barney rubble\n fred flintstone\n}\n' self.renderTest(testdata, result) def test_carrover_virtual_server_group(self): testdata = {'virtual_server_group': {'gizmo': {'fred': 'flintstone', 'barney': 'rubble'}}} - result = 'virtual_server_group gizmo {\nbarney rubble\nfred flintstone\n}\n' + result = 'virtual_server_group gizmo {\n barney rubble\n fred flintstone\n}\n' self.renderTest(testdata, result) def test_carrover_virtual_server(self): testdata = {'virtual_server': {'gizmo': {'fred': 'flintstone', 'barney': 'rubble'}}} - result = 'virtual_server gizmo {\nbarney rubble\nfred flintstone\n}\n' + result = 'virtual_server gizmo {\n barney rubble\n fred flintstone\n}\n' self.renderTest(testdata, result) def test_carrover_real_server(self): testdata = {'real_server': {'gizmo': {'fred': 'flintstone', 'barney': 'rubble'}}} - result = 'real_server gizmo {\nbarney rubble\nfred flintstone\n}\n' + result = 'real_server gizmo {\n barney rubble\n fred flintstone\n}\n' self.renderTest(testdata, result) if __name__ == '__main__': From 726f34379dfce5d638bb644de38b479eb42f86c0 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Fri, 20 Jan 2017 23:27:03 -0500 Subject: [PATCH 17/19] Got carryover indention correct. --- keepalived/templates/config.jinja | 10 ++++++---- test/test_keepalived_config.py | 15 ++++++++++----- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/keepalived/templates/config.jinja b/keepalived/templates/config.jinja index d68ec45..5a37e77 100644 --- a/keepalived/templates/config.jinja +++ b/keepalived/templates/config.jinja @@ -17,15 +17,17 @@ {%- set indentHolder = indent -%} {%- for item in data|dictsort -%} {%- if item[key] in carryovers -%} - {{- keepalived_config(item[value], carryover=item[key]) -}} + {{- keepalived_config(item[value], carryover=item[key], indent=indent) -}} {%- else -%} + {%- set carryIndent = indentHolder -%} {%- if carryover -%} - {{- carryover }}{{ ' ' -}} + {{- carryover|indent(indentHolder, True) }}{{ ' ' -}} + {%- set carryIndent = 0 -%} {%- endif -%} {%- if item[value] is string or item[value] is not iterable -%} {%- set indent = 0 -%} {%- endif -%} - {{- item[key]|indent(indentHolder, True) }} {{ keepalived_config(item[value], recurse=recurse, indent=indent) -}} + {{- item[key]|indent(carryIndent, True) }} {{ keepalived_config(item[value], recurse=recurse, indent=indent) -}} {%- endif -%} {%- endfor -%} {%- else -%} @@ -34,7 +36,7 @@ {%- endfor -%} {%- endif -%} {%- if recurse > 0 -%} - {{- '}\n' -}} + {{- '}'|indent(indent - 2, True) -}}{{ '\n' }} {%- endif -%} {%- endif -%} {%- endmacro -%} diff --git a/test/test_keepalived_config.py b/test/test_keepalived_config.py index 27c88e7..bfcbc85 100755 --- a/test/test_keepalived_config.py +++ b/test/test_keepalived_config.py @@ -69,7 +69,7 @@ class TestKeepalivedConfiguration(unittest.TestCase): result = 'vrrp_script gizmo {\n fred flintstone\n barney rubble\n}\n' self.renderTest(testdata, result) - def test_carrover_vrrp_instance(self): + def test_carryover_vrrp_instance(self): testdata = {'vrrp_instance': {'gizmo': {'fred': 'flintstone', 'barney': 'rubble'}}} result = 'vrrp_instance gizmo {\n barney rubble\n fred flintstone\n}\n' self.renderTest(testdata, result) @@ -79,25 +79,30 @@ class TestKeepalivedConfiguration(unittest.TestCase): result = 'vrrp_script gizmo {\n running dumdums\n}\nvrrp_instance dumdums {\n fred flintstone\n}\n' self.renderTest(testdata, result) - def test_carrover_vrrp_sync_group(self): + def test_carryover_vrrp_sync_group(self): testdata = {'vrrp_sync_group': {'gizmo': {'fred': 'flintstone', 'barney': 'rubble'}}} result = 'vrrp_sync_group gizmo {\n barney rubble\n fred flintstone\n}\n' self.renderTest(testdata, result) - def test_carrover_virtual_server_group(self): + def test_carryover_virtual_server_group(self): testdata = {'virtual_server_group': {'gizmo': {'fred': 'flintstone', 'barney': 'rubble'}}} result = 'virtual_server_group gizmo {\n barney rubble\n fred flintstone\n}\n' self.renderTest(testdata, result) - def test_carrover_virtual_server(self): + def test_carryover_virtual_server(self): testdata = {'virtual_server': {'gizmo': {'fred': 'flintstone', 'barney': 'rubble'}}} result = 'virtual_server gizmo {\n barney rubble\n fred flintstone\n}\n' self.renderTest(testdata, result) - def test_carrover_real_server(self): + def test_carryover_real_server(self): testdata = {'real_server': {'gizmo': {'fred': 'flintstone', 'barney': 'rubble'}}} result = 'real_server gizmo {\n barney rubble\n fred flintstone\n}\n' self.renderTest(testdata, result) + def test_carryover_within_carryover(self): + testdata = {'virtual_server': {'gizmo': {'real_server': {'dumdums': {'fred': 'flintstone', 'barney': 'rubble'}}}}} + result = 'virtual_server gizmo {\n real_server dumdums {\n barney rubble\n fred flintstone\n }\n}\n' + self.renderTest(testdata, result) + if __name__ == '__main__': unittest.main() From 2f8ff3ec6bc2e7658ed681d5e8e36cc997f79494 Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Sat, 21 Jan 2017 00:12:44 -0500 Subject: [PATCH 18/19] Corrected carryover indention. --- keepalived/templates/config.jinja | 5 +++-- test/test_keepalived_config.py | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/keepalived/templates/config.jinja b/keepalived/templates/config.jinja index 5a37e77..528d267 100644 --- a/keepalived/templates/config.jinja +++ b/keepalived/templates/config.jinja @@ -20,14 +20,15 @@ {{- keepalived_config(item[value], carryover=item[key], indent=indent) -}} {%- else -%} {%- set carryIndent = indentHolder -%} + {%- set forwardIndent = indentHolder -%} {%- if carryover -%} {{- carryover|indent(indentHolder, True) }}{{ ' ' -}} {%- set carryIndent = 0 -%} {%- endif -%} {%- if item[value] is string or item[value] is not iterable -%} - {%- set indent = 0 -%} + {%- set forwardIndent = 0 -%} {%- endif -%} - {{- item[key]|indent(carryIndent, True) }} {{ keepalived_config(item[value], recurse=recurse, indent=indent) -}} + {{- item[key]|indent(carryIndent, True) }} {{ keepalived_config(item[value], recurse=recurse, indent=forwardIndent) -}} {%- endif -%} {%- endfor -%} {%- else -%} diff --git a/test/test_keepalived_config.py b/test/test_keepalived_config.py index bfcbc85..c275160 100755 --- a/test/test_keepalived_config.py +++ b/test/test_keepalived_config.py @@ -104,5 +104,15 @@ class TestKeepalivedConfiguration(unittest.TestCase): result = 'virtual_server gizmo {\n real_server dumdums {\n barney rubble\n fred flintstone\n }\n}\n' self.renderTest(testdata, result) + def test_indent_nested_hashes(self): + testdata = {'flintstone': {'fred': {'role': {'family': 'father'}}}} + result = 'flintstone {\n fred {\n role {\n family father\n }\n }\n}\n' + self.renderTest(testdata, result) + + def test_indent_carryover_with_nested_hashes(self): + testdata = {'real_server': {'gizmo': {'another': 'day', 'dumdums': {'fred': 'flintstone', 'barney': 'rubble'}}}} + result = 'real_server gizmo {\n another day\n dumdums {\n barney rubble\n fred flintstone\n }\n}\n' + self.renderTest(testdata, result) + if __name__ == '__main__': unittest.main() From 2c97cd79c9a20a157336a89b2c36d14c7b3e759b Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Sat, 21 Jan 2017 00:59:22 -0500 Subject: [PATCH 19/19] Refactored. --- keepalived/templates/config.jinja | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/keepalived/templates/config.jinja b/keepalived/templates/config.jinja index 528d267..8219581 100644 --- a/keepalived/templates/config.jinja +++ b/keepalived/templates/config.jinja @@ -14,15 +14,14 @@ {%- set indent = indent + 2 -%} {%- endif -%} {%- if data is mapping -%} - {%- set indentHolder = indent -%} {%- for item in data|dictsort -%} {%- if item[key] in carryovers -%} {{- keepalived_config(item[value], carryover=item[key], indent=indent) -}} {%- else -%} - {%- set carryIndent = indentHolder -%} - {%- set forwardIndent = indentHolder -%} + {%- set carryIndent = indent -%} + {%- set forwardIndent = indent -%} {%- if carryover -%} - {{- carryover|indent(indentHolder, True) }}{{ ' ' -}} + {{- carryover|indent(indent, True) }}{{ ' ' -}} {%- set carryIndent = 0 -%} {%- endif -%} {%- if item[value] is string or item[value] is not iterable -%}