From b243795eba1b36cadd81c3149e833bdf5c5bea70 Mon Sep 17 00:00:00 2001
From: Christian Poessinger <christian@poessinger.com>
Date: Tue, 21 Sep 2021 20:29:36 +0200
Subject: vrrp: keepalived: T616: move configuration to volatile /run directory

Move keepalived configuration from /etc/keepalived to /run/keepalived.
---
 data/templates/vrrp/keepalived.conf.tmpl                 |  3 +--
 python/vyos/ifconfig/vrrp.py                             |  8 ++++----
 smoketest/scripts/cli/test_ha_vrrp.py                    |  6 ++----
 src/conf_mode/vrrp.py                                    |  5 ++++-
 .../systemd/system/keepalived.service.d/override.conf    | 10 ++++++++++
 src/system/keepalived-fifo.py                            | 16 ++++++++--------
 6 files changed, 29 insertions(+), 19 deletions(-)

diff --git a/data/templates/vrrp/keepalived.conf.tmpl b/data/templates/vrrp/keepalived.conf.tmpl
index 2e2f62ae7..2b53b04af 100644
--- a/data/templates/vrrp/keepalived.conf.tmpl
+++ b/data/templates/vrrp/keepalived.conf.tmpl
@@ -5,7 +5,7 @@
 global_defs {
     dynamic_interfaces
     script_user root
-    notify_fifo /run/keepalived_notify_fifo
+    notify_fifo /run/keepalived/keepalived_notify_fifo
     notify_fifo_script /usr/libexec/vyos/system/keepalived-fifo.py
 }
 
@@ -16,7 +16,6 @@ vrrp_script healthcheck_{{ group.name }} {
     interval {{ group.health_check_interval }}
     fall {{ group.health_check_count }}
     rise 1
-
 }
 {%   endif %}
 
diff --git a/python/vyos/ifconfig/vrrp.py b/python/vyos/ifconfig/vrrp.py
index b522cc1ab..481b0284a 100644
--- a/python/vyos/ifconfig/vrrp.py
+++ b/python/vyos/ifconfig/vrrp.py
@@ -32,14 +32,14 @@ class VRRPNoData(VRRPError):
 class VRRP(object):
     _vrrp_prefix = '00:00:5E:00:01:'
     location = {
-        'pid':      '/run/keepalived.pid',
-        'fifo':     '/run/keepalived_notify_fifo',
+        'pid':      '/run/keepalived/keepalived.pid',
+        'fifo':     '/run/keepalived/keepalived_notify_fifo',
         'state':    '/tmp/keepalived.data',
         'stats':    '/tmp/keepalived.stats',
         'json':     '/tmp/keepalived.json',
         'daemon':   '/etc/default/keepalived',
-        'config':   '/etc/keepalived/keepalived.conf',
-        'vyos':     '/run/keepalived_config.dict',
+        'config':   '/run/keepalived/keepalived.conf',
+        'vyos':     '/run/keepalived/keepalived_config.dict',
     }
 
     _signal = {
diff --git a/smoketest/scripts/cli/test_ha_vrrp.py b/smoketest/scripts/cli/test_ha_vrrp.py
index 03618c7d8..9c8d26699 100755
--- a/smoketest/scripts/cli/test_ha_vrrp.py
+++ b/smoketest/scripts/cli/test_ha_vrrp.py
@@ -14,22 +14,20 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-import os
-import re
 import unittest
 
 from base_vyostest_shim import VyOSUnitTestSHIM
 
 from vyos.configsession import ConfigSession
 from vyos.configsession import ConfigSessionError
+from vyos.ifconfig.vrrp import VRRP
 from vyos.util import cmd
 from vyos.util import process_named_running
 from vyos.util import read_file
-
 from vyos.template import inc_ip
 
 PROCESS_NAME = 'keepalived'
-KEEPALIVED_CONF = '/etc/keepalived/keepalived.conf'
+KEEPALIVED_CONF = VRRP.location['config']
 base_path = ['high-availability', 'vrrp']
 
 vrrp_interface = 'eth1'
diff --git a/src/conf_mode/vrrp.py b/src/conf_mode/vrrp.py
index 2ece792dc..f11dce879 100755
--- a/src/conf_mode/vrrp.py
+++ b/src/conf_mode/vrrp.py
@@ -30,6 +30,7 @@ import vyos.config
 
 from vyos import ConfigError
 from vyos.util import call
+from vyos.util import makedir
 from vyos.template import render
 
 from vyos.ifconfig.vrrp import VRRP
@@ -136,7 +137,9 @@ def get_config(config=None):
         sync_groups.append(sync_group)
 
     # create a file with dict with proposed configuration
-    with open("{}.temp".format(VRRP.location['vyos']), 'w') as dict_file:
+    dirname = os.path.dirname(VRRP.location['vyos'])
+    makedir(dirname)
+    with open(VRRP.location['vyos'] + ".temp", 'w') as dict_file:
         dict_file.write(dumps({'vrrp_groups': vrrp_groups, 'sync_groups': sync_groups}))
 
     return (vrrp_groups, sync_groups)
diff --git a/src/etc/systemd/system/keepalived.service.d/override.conf b/src/etc/systemd/system/keepalived.service.d/override.conf
index 9fcabf652..e338b90a2 100644
--- a/src/etc/systemd/system/keepalived.service.d/override.conf
+++ b/src/etc/systemd/system/keepalived.service.d/override.conf
@@ -1,2 +1,12 @@
+[Unit]
+ConditionPathExists=
+ConditionPathExists=/run/keepalived/keepalived.conf
+After=
+After=vyos-router.service
+
 [Service]
 KillMode=process
+ExecStart=
+ExecStart=/usr/sbin/keepalived --use-file /run/keepalived/keepalived.conf --pid /run/keepalived/keepalived.pid --dont-fork $DAEMON_ARGS
+PIDFile=
+PIDFile=/run/keepalived/keepalived.pid
diff --git a/src/system/keepalived-fifo.py b/src/system/keepalived-fifo.py
index 3b4330e9b..159fd0f54 100755
--- a/src/system/keepalived-fifo.py
+++ b/src/system/keepalived-fifo.py
@@ -1,6 +1,6 @@
 #!/usr/bin/env python3
 #
-# Copyright (C) 2020 VyOS maintainers and contributors
+# Copyright (C) 2020-2021 VyOS maintainers and contributors
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License version 2 or later as
@@ -13,7 +13,6 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
-#
 
 import os
 import time
@@ -22,11 +21,12 @@ import argparse
 import threading
 import re
 import json
-from pathlib import Path
-from queue import Queue
 import logging
+
+from queue import Queue
 from logging.handlers import SysLogHandler
 
+from vyos.ifconfig.vrrp import VRRP
 from vyos.util import cmd
 
 # configure logging
@@ -62,7 +62,7 @@ class KeepalivedFifo:
     def _config_load(self):
         try:
             # read the dictionary file with configuration
-            with open('/run/keepalived_config.dict', 'r') as dict_file:
+            with open(VRRP.location['vyos'], 'r') as dict_file:
                 vrrp_config_dict = json.load(dict_file)
             self.vrrp_config = {'vrrp_groups': {}, 'sync_groups': {}}
             # save VRRP instances to the new dictionary
@@ -95,8 +95,8 @@ class KeepalivedFifo:
 
     # create FIFO pipe
     def pipe_create(self):
-        if Path(self.pipe_path).exists():
-            logger.info("PIPE already exist: {}".format(self.pipe_path))
+        if os.path.exists(self.pipe_path):
+            logger.info(f"PIPE already exist: {self.pipe_path}")
         else:
             os.mkfifo(self.pipe_path)
 
@@ -135,7 +135,7 @@ class KeepalivedFifo:
                         if n_type == 'GROUP':
                             if os.path.exists(mdns_running_file):
                                 cmd(mdns_update_command)
-                            
+
                             if n_name in self.vrrp_config['sync_groups'] and n_state in self.vrrp_config['sync_groups'][n_name]:
                                 n_script = self.vrrp_config['sync_groups'][n_name].get(n_state)
                                 if n_script:
-- 
cgit v1.2.3