summaryrefslogtreecommitdiff
path: root/src/system/vrrp-script-wrapper.py
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@baturin.org>2018-07-30 00:33:13 +0700
committerGitHub <noreply@github.com>2018-07-30 00:33:13 +0700
commit08016dc01b83dd3a3a1be3350a8e5669fee9db14 (patch)
tree9367200cde712f8b2167ca1f3c4c91426474cc63 /src/system/vrrp-script-wrapper.py
parent0fdfe533cd2bad844b1c0cd98c62a7e1866c481c (diff)
parent1c0b7ede177e97c4b4e6ad5e9e13bec56f233bb5 (diff)
downloadvyos-1x-08016dc01b83dd3a3a1be3350a8e5669fee9db14.tar.gz
vyos-1x-08016dc01b83dd3a3a1be3350a8e5669fee9db14.zip
Merge branch 'current' into current
Diffstat (limited to 'src/system/vrrp-script-wrapper.py')
-rwxr-xr-xsrc/system/vrrp-script-wrapper.py77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/system/vrrp-script-wrapper.py b/src/system/vrrp-script-wrapper.py
new file mode 100755
index 000000000..5d6aa6c55
--- /dev/null
+++ b/src/system/vrrp-script-wrapper.py
@@ -0,0 +1,77 @@
+#!/usr/bin/env python3
+#
+# Copyright (C) 2018 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
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# 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 sys
+import subprocess
+import argparse
+import syslog
+
+import vyos.util
+import vyos.keepalived
+
+
+parser = argparse.ArgumentParser()
+parser.add_argument("-s", "--script", type=str, help="Script to run")
+parser.add_argument("-t", "--state", type=str, help="VRRP state")
+parser.add_argument("-g", "--group", type=str, help="VRRP group")
+parser.add_argument("-i", "--interface", type=str, help="Network interface")
+
+syslog.openlog('vyos-vrrp-wrapper')
+
+args = parser.parse_args()
+if not args.script or not args.state or not args.group \
+ or not args.interface:
+ parser.print_usage()
+ sys.exit(1)
+
+# Get the old state if it exists and compare it to the current state received
+# in command line options to avoid executing scripts if no real transition occured.
+# This is necessary because keepalived does not keep persistent state data even between
+# config reloads and will cheerfully execute everything whether it's required or not.
+
+old_state = vyos.keepalived.get_old_state(args.group)
+
+if (old_state is None) or (old_state != args.state):
+ exitcode = 0
+
+ # Run the script and save the new state
+
+ # Change the process GID to the config owners group to avoid screwing up
+ # running config permissions
+ os.setgid(vyos.util.get_cfg_group_id())
+
+ syslog.syslog(syslog.LOG_NOTICE, 'Running transition script {0} for VRRP group {1}'.format(args.script, args.group))
+ try:
+ ret = subprocess.call([args.script, args.state, args.interface, args.group])
+ if ret != 0:
+ syslog.syslog(syslog.LOG_ERR, "Transition script {0} failed, exit status: {1}".format(args.script, ret))
+ exitcode = ret
+ except Exception as e:
+ syslog.syslog(syslog.LOG_ERR, "Failed to execute transition script {0}: {1}".format(args.script, e))
+ exitcode = 1
+
+ if exitcode == 0:
+ syslog.syslog(syslog.LOG_NOTICE, "Transition script {0} executed successfully".format(args.script))
+
+ vyos.keepalived.save_state(args.group, args.state)
+else:
+ syslog.syslog(syslog.LOG_NOTICE, "State of the group {0} has not changed, not running transition script".format(args.group))
+
+syslog.closelog()
+sys.exit(exitcode)