From c1ac0630cfe0ee65569fbe435cc006ade20fed22 Mon Sep 17 00:00:00 2001
From: Christian Poessinger <christian@poessinger.com>
Date: Tue, 21 Sep 2021 20:05:52 +0200
Subject: vrrp: keepalived: T2720: adjust to Jinja2 trim_blocks feature

This is a successor to commit a2ac9fac16e ("vyos.template: T2720: always enable
Jinja2 trim_blocks feature"). It only shifts the whitespaces / indents inside
the keepalived configuration file.
---
 data/templates/vrrp/keepalived.conf.tmpl | 111 ++++++++++++++-----------------
 src/conf_mode/vrrp.py                    |   7 +-
 2 files changed, 56 insertions(+), 62 deletions(-)

diff --git a/data/templates/vrrp/keepalived.conf.tmpl b/data/templates/vrrp/keepalived.conf.tmpl
index c01101d85..2e2f62ae7 100644
--- a/data/templates/vrrp/keepalived.conf.tmpl
+++ b/data/templates/vrrp/keepalived.conf.tmpl
@@ -10,8 +10,7 @@ global_defs {
 }
 
 {% for group in groups %}
-
-{% if group.health_check_script %}
+{%   if group.health_check_script is defined and group.health_check_script is not none %}
 vrrp_script healthcheck_{{ group.name }} {
     script "{{ group.health_check_script }}"
     interval {{ group.health_check_interval }}
@@ -19,88 +18,78 @@ vrrp_script healthcheck_{{ group.name }} {
     rise 1
 
 }
-{% endif %}
+{%   endif %}
 
 vrrp_instance {{ group.name }} {
-    {% if group.description %}
-    # {{ group.description }}
-    {% endif %}
-
+    {{ '# ' ~ group.description if group.description is defined }}
     state BACKUP
     interface {{ group.interface }}
     virtual_router_id {{ group.vrid }}
     priority {{ group.priority }}
     advert_int {{ group.advertise_interval }}
-
-    {% if group.preempt %}
+{%   if group.preempt is defined and group.preempt is not none %}
     preempt_delay {{ group.preempt_delay }}
-    {% else %}
+{%   else %}
     nopreempt
-    {% endif %}
-
-    {% if group.peer_address %}
+{%   endif %}
+{%   if group.peer_address is defined and group.peer_address is not none %}
     unicast_peer { {{ group.peer_address }} }
-    {% endif %}
-
-    {% if group.hello_source %}
-      {% if group.peer_address %}
-      unicast_src_ip {{ group.hello_source }}
-      {% else %}
-      mcast_src_ip {{ group.hello_source }}
-      {% endif %}
-    {% endif %}
-
-    {% if group.use_vmac and group.peer_address %}
-      use_vmac {{group.interface}}v{{group.vrid}}
-      vmac_xmit_base
-    {% elif group.use_vmac %}
-      use_vmac {{group.interface}}v{{group.vrid}}
-    {% endif %}
-
-    {% if group.auth_password %}
-      authentication {
+{%   endif %}
+{%   if group.hello_source is defined and group.hello_source is not none %}
+{%     if group.peer_address is defined and group.peer_address is not none %}
+    unicast_src_ip {{ group.hello_source }}
+{%     else %}
+    mcast_src_ip {{ group.hello_source }}
+{%     endif %}
+{%   endif %}
+{%   if group.use_vmac is defined and group.peer_address is defined %}
+    use_vmac {{ group.interface }}v{{ group.vrid }}
+    vmac_xmit_base
+{%   elif group.use_vmac is defined %}
+    use_vmac {{ group.interface }}v{{ group.vrid }}
+{%   endif %}
+{%   if group.auth_password is defined and group.auth_password is not none %}
+    authentication {
         auth_pass "{{ group.auth_password }}"
         auth_type {{ group.auth_type }}
-      }
-    {% endif %}
-
+    }
+{%   endif %}
+{%   if group.virtual_addresses is defined and group.virtual_addresses is not none %}
     virtual_ipaddress {
-    {% for addr in group.virtual_addresses %}
+{%     for addr in group.virtual_addresses %}
         {{ addr }}
-    {% endfor %}
+{%     endfor %}
     }
-
-    {% if group.virtual_addresses_excluded %}
+{%   endif %}
+{%   if group.virtual_addresses_excluded is defined and group.virtual_addresses_excluded is not none %}
     virtual_ipaddress_excluded {
-    {% for addr in group.virtual_addresses_excluded %}
+{%     for addr in group.virtual_addresses_excluded %}
         {{ addr }}
-    {% endfor %}
+{%     endfor %}
     }
-    {% endif %}
-
-    {% if group.health_check_script %}
+{%   endif %}
+{%   if group.health_check_script is defined and group.health_check_script is not none %}
     track_script {
         healthcheck_{{ group.name }}
     }
-    {% endif %}
+{%   endif %}
 }
-
 {% endfor %}
 
-{% for sync_group in sync_groups %}
+{% if sync_groups is defined and sync_groups is not none %}
+{%   for sync_group in sync_groups %}
 vrrp_sync_group {{ sync_group.name }} {
-       group {
-            {% for member in sync_group.members %}
-                {{ member }}
-            {% endfor %}
-        }
-
-        {% if sync_group.conntrack_sync %}
-        {%  set vyos_helper = "/usr/libexec/vyos/vyos-vrrp-conntracksync.sh" %}
-            notify_master "{{ vyos_helper }} master {{ sync_group.name }}"
-            notify_backup "{{ vyos_helper }} backup {{ sync_group.name }}"
-            notify_fault "{{ vyos_helper }} fault {{ sync_group.name }}"
-        {% endif %}
+    group {
+{%     for member in sync_group.members %}
+        {{ member }}
+{%     endfor %}
+    }
+{%     if sync_group.conntrack_sync %}
+{%     set vyos_helper = "/usr/libexec/vyos/vyos-vrrp-conntracksync.sh" %}
+    notify_master "{{ vyos_helper }} master {{ sync_group.name }}"
+    notify_backup "{{ vyos_helper }} backup {{ sync_group.name }}"
+    notify_fault "{{ vyos_helper }} fault {{ sync_group.name }}"
+{%     endif %}
 }
-
-{% endfor %}
+{%   endfor %}
+{% endif %}
diff --git a/src/conf_mode/vrrp.py b/src/conf_mode/vrrp.py
index 680a80859..2ece792dc 100755
--- a/src/conf_mode/vrrp.py
+++ b/src/conf_mode/vrrp.py
@@ -17,7 +17,12 @@
 import os
 
 from sys import exit
-from ipaddress import ip_address, ip_interface, IPv4Interface, IPv6Interface, IPv4Address, IPv6Address
+from ipaddress import ip_address
+from ipaddress import ip_interface
+from ipaddress import IPv4Interface
+from ipaddress import IPv6Interface
+from ipaddress import IPv4Address
+from ipaddress import IPv6Address
 from json import dumps
 from pathlib import Path
 
-- 
cgit v1.2.3