keepalived: add ipvs uci support
authorFlorian Eckert <fe@dev.tdt.de>
Thu, 25 Oct 2018 07:55:15 +0000 (09:55 +0200)
committerFlorian Eckert <fe@dev.tdt.de>
Mon, 12 Nov 2018 14:20:20 +0000 (15:20 +0100)
Add uci ipvs support to keepalived.

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
net/keepalived/Makefile
net/keepalived/files/keepalived.config
net/keepalived/files/keepalived.init

index dfbed1b41bac54f9fd9f2ff0ebf1b8df66ed0c05..4f5e25119916954051b306995ca470f3adbed8e4 100644 (file)
@@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk
 
 PKG_NAME:=keepalived
 PKG_VERSION:=2.0.7
-PKG_RELEASE:=1
+PKG_RELEASE:=2
 
 PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
 PKG_SOURCE_URL:=http://www.keepalived.org/software
index 7279381a81f7f74089bd29462135fe87a1d8acd1..5fde1f89d5d2b521480f3e06b0e635782bfa1b5f 100644 (file)
@@ -145,3 +145,57 @@ config global_defs
 #      option fall             "2"
 #      option rise             "3"
 
+#config virtual_server
+#      option enabled                  "1"
+#      option ipaddr                   "192.168.200.2"
+#      option port                     "80"
+#      option fwmark                   "0x42"
+#      option delay_loop               "30"
+# valid values for lb_algo rr|wrr|lc|wlc
+#      option lb_algo                  "rr"
+# valid values for lb_kind NAT|DR|TUN
+#      option lb_kind                  "NAT"
+#      option persistence_timeout      "50"
+#      option persistence_granularity  "192.168.110.2"
+#      option virtualhost              "www.domain1.com"
+# valid values for protocol TCP|UDP
+#      option protocol                 "TCP"
+#      option sorry_server_ip          "192.168.100.100"
+#      option sorry_server_port        "80"
+#      list real_server                "Server1"
+#      list real_server                "Server2"
+
+#config real_server
+#      option enabled                  "1"
+#      option name                     "Server1"
+#      option weight                   "2"
+#      option check                    "HTTP_GET"
+# only for check TCP_CHECK|HTTP_GET|SSL_GET
+#      option connect_port             "80"
+#      option connect_timeout          "3"
+# only for check MISC_CHECK
+#      option misc_path                "<user-defined-check-script>"
+# only  for check HTTP_GET | SSL_GET
+#      list url                        "url1"
+#      list url                        "url2"
+#      option nb_get_retry             "3"
+#      option delay_before_retry       "2"
+
+#config real_server
+#      option enabled                  "1"
+#      option name                     "Server2"
+#      option weight                   "2"
+#      option check                    "TCP_CHECK"
+#      option connect_port             "80"
+#      option connect_timeout          "3"
+
+#config url
+#      option name     "url1"
+#      option path     "/testurl/test1.jsp"
+#      option digest   "11"
+
+#config url
+#      option name     "url2"
+#      option path     "/testurl/test2.jsp"
+#      option digest   "22"
+
index 49c64f0160eb88b6e74bd76a1ff395f9677d265e..70e0b7310b979f96e1c68d3c82052393c2346455 100644 (file)
@@ -10,6 +10,8 @@ KEEPALIVED_CONF=/tmp/keepalived.conf
 
 INDENT_1=\\t
 INDENT_2=$INDENT_1$INDENT_1
+INDENT_3=$INDENT_1$INDENT_1$INDENT_1
+INDENT_4=$INDENT_1$INDENT_1$INDENT_1$INDENT_1
 
 config_section_open() {
        local tag=$1
@@ -366,6 +368,113 @@ vrrp_script() {
        config_section_close
 }
 
+url() {
+       local url="$2"
+
+       local name path digest
+
+       config_get name $1 name
+       [ "$url" = "$name" ] || return 0
+
+       config_get path $1 path
+       config_get digest $1 digest
+
+       [ -n "$digest" -a -n "$path" ] && {
+               printf "${INDENT_3}url {\n" >> $KEEPALIVED_CONF
+               printf "${INDENT_4}path "$path"\n" >> $KEEPALIVED_CONF
+               printf "${INDENT_4}digest $digest\n" >> $KEEPALIVED_CONF
+               printf "${INDENT_3}}\n" >> $KEEPALIVED_CONF
+       }
+}
+
+url_list() {
+       config_foreach url url "$1"
+}
+
+real_server() {
+       local server="$2"
+
+       local enabled name weight ipaddr port check
+
+       config_get_bool enabled $1 enabled 1
+       [ "$enabled" -eq 1 ] || return 0
+
+       config_get name $1 name
+       [ "$server" = "$name" ] || return 0
+
+       config_get weight $1 weight
+       [ -n "$weight" ] || return 0
+
+       config_get ipaddr $1 ipaddr
+       config_get port $1 port
+       config_get check $1 check
+
+       [ -n "$ipaddr" -a -n "$port" ] && {
+               printf "${INDENT_1}real_server $ipaddr $port {\n" >> $KEEPALIVED_CONF
+               printf "${INDENT_2}weight $weight\n" >> $KEEPALIVED_CONF
+               case "$check" in
+                       TCP_CHECK)
+                               printf "${INDENT_2}${check} {\n" >> $KEEPALIVED_CONF
+                               print_elems_indent $1 $INDENT_3 connect_timeout \
+                                       connect_port
+                               printf "${INDENT_2}}\n" >> $KEEPALIVED_CONF
+                               ;;
+                       MISC_CHECK)
+                               printf "${INDENT_2}${check} {\n" >> $KEEPALIVED_CONF
+                               print_elems_indent $1 $INDENT_3 misc_path
+                               printf "${INDENT_2}}\n" >> $KEEPALIVED_CONF
+                               ;;
+                       HTTP_GET | SSL_GET)
+                               printf "${INDENT_2}${check} {\n" >> $KEEPALIVED_CONF
+                               print_elems_indent $1 $INDENT_3 connect_timeout \
+                                       connect_port nb_get_retry delay_before_retry
+                               # Handle url list
+                               config_list_foreach $1 url url_list
+                               printf "${INDENT_2}}\n" >> $KEEPALIVED_CONF
+                               ;;
+               esac
+               printf "${INDENT_1}}\n" >> $KEEPALIVED_CONF
+       }
+}
+
+real_server_list() {
+       config_foreach real_server real_server "$1"
+}
+
+virtual_server() {
+       local enabled ipaddr port lb_algo sorry_server_ip sorry_server_port
+
+       config_get_bool enabled $1 enabled 1
+       [ "$enabled" -eq 1 ] || return 0
+
+       config_get ipaddr $1 ipaddr
+       [ -z "$ipaddr" ] && return 0
+       config_get port $1 port
+       [ -z "$port" ] && return 0
+
+       config_section_open "virtual_server" "$ipaddr $port"
+
+       print_elems_indent $1 $INDENT_1 fwmark delay_loop \
+               lb_kind persistence_timeout persistence_granularity \
+               virtualhost protocol
+
+       config_get lb_algo $1 lb_algo
+       [ -z "$lb_algo" ] && lb_algo="rr"
+       modprobe ip_vs_${lb_algo} 2>&1 1>/dev/null
+       printf "${INDENT_1}lb_algo ${lb_algo}\n" >> $KEEPALIVED_CONF
+
+       config_get sorry_server_ip $1 sorry_server_ip
+       config_get sorry_server_port $1 sorry_server_port
+       [ -n "$sorry_server_ip" -a -n "$sorry_server_port" ] && {
+               printf "${INDENT_1}sorry_server $sorry_server_ip $sorry_server_port\n" >> $KEEPALIVED_CONF
+       }
+
+       # Handle real_server list
+       config_list_foreach $1 real_server real_server_list
+
+       config_section_close
+}
+
 process_config() {
        local alt_config_file
 
@@ -401,6 +510,7 @@ process_config() {
        config_foreach_wrapper vrrp_script
        config_foreach_wrapper vrrp_sync_group
        config_foreach_wrapper vrrp_instance
+       config_foreach_wrapper virtual_server
        return 0
 }