From f08bda4256f7c71899c45ea8b5ad73c67f77ae9a Mon Sep 17 00:00:00 2001 From: Georg Pfuetzenreuter Date: Wed, 8 Feb 2023 21:19:37 +0100 Subject: Add netcup_failover profile Profile managing a Netcup IP address failover script for use with keepalived. Signed-off-by: Georg Pfuetzenreuter --- salt/profile/netcup_failover/README.md | 14 +++ salt/profile/netcup_failover/files/failover.sh.j2 | 109 ++++++++++++++++++++++ salt/profile/netcup_failover/init.sls | 10 ++ 3 files changed, 133 insertions(+) create mode 100644 salt/profile/netcup_failover/README.md create mode 100755 salt/profile/netcup_failover/files/failover.sh.j2 create mode 100644 salt/profile/netcup_failover/init.sls (limited to 'salt') diff --git a/salt/profile/netcup_failover/README.md b/salt/profile/netcup_failover/README.md new file mode 100644 index 0000000..fc01bfb --- /dev/null +++ b/salt/profile/netcup_failover/README.md @@ -0,0 +1,14 @@ +This profile installs a script switching failover IP addresses between Netcup hosted VM's. + +Required pillar: + +``` +profile: + netcup_failover: + scp_user: 12345 + scp_pass: xxxx + scp_server: v9876 + mac_address: ff:ff:ff:ff:ff + ip4_address: xx.xx.xx.xx/32 + ip6_address: 'foo:bar::/64' +``` diff --git a/salt/profile/netcup_failover/files/failover.sh.j2 b/salt/profile/netcup_failover/files/failover.sh.j2 new file mode 100755 index 0000000..92ebd31 --- /dev/null +++ b/salt/profile/netcup_failover/files/failover.sh.j2 @@ -0,0 +1,109 @@ +{%- set header = salt['pillar.get']('managed_header_pound') -%} +{%- set mypillar = salt['pillar.get']('profile:netcup_failover') -%} +#!/bin/sh +# Floating IP switching script utilizing the Netcup API + +{{ header }} + +SCP_USER='{{ mypillar['scp_user'] }}' +SCP_PASS='{{ mypillar['scp_pass'] }}' +SCP_SERVER='{{ mypillar['scp_server'] }}' +MAC='{{ mypillar['mac_address'] }}' +IP_v4='{{ mypillar['ip4_address'] }}' +IP_v6='{{ mypillar['ip6_address'] }}' + +URL="https://www.servercontrolpanel.de/WSEndUser?xsd=1" ### ?xsd=1 ?wsdl + +usage () { + echo "$0 [--ipv4 | --ipv6 | --all] [--debug]" + exit 2 +} + +init () { + construct "$1" + run + parse +} + +construct () { + if [ "$1" = "ip4" ]; + then + local IP="$IP_v4" + fi + if [ "$1" = "ip6" ]; + then + local IP="$IP_v6" + fi + local CIDR="${IP#*/}" + local IP="`echo $IP | sed "s?/$CIDR??"`" + if [ "$DEBUG" = "true" ]; + then + echo "[DEBUG] Initiating: $1" + echo "[DEBUG] IP Address: $IP" + echo "[DEBUG] CIDR Mask: $CIDR" + fi + XML_BODY="$SCP_USER$SCP_PASS$IP$CIDR$SCP_SERVER$MAC" + if [ "$DEBUG" = "true" ]; + then + echo "[DEBUG] Payload: $XML_BODY" + fi +} + +request () { + curl -s -H 'Content-Type: text/xml' -H 'SOAPAction:' -d "$XML_BODY" -X POST "$URL" +} + +run () { + RESPONSE=`request` + if [ "$DEBUG" = "true" ]; + then + echo "[DEBUG] Response: $RESPONSE" + fi + +} + +parse () { + local IFS='&' + local check_invalid="validation error&IP already assigned&true" + for check in $check_invalid; + do + if [ "$DEBUG" = "true" ]; + then + echo "[DEBUG] Parsing: $check" + fi + if [ "${RESPONSE#*$check}" = "$RESPONSE" ]; + then + result="Not found" + fi + if [ "${RESPONSE#*$check}" != "$RESPONSE" ]; + then + result="Found" + fi + echo "Check for \"$check\": $result" + done +} + +MODE="$1" + +if [ "$2" = "--debug" ]; +then + DEBUG="true" + echo "[DEBUG] Script invoked at `date`" +fi + +case "$MODE" in + "--ipv4" ) + init ip4 + ;; + "--ipv6" ) + init ip6 + ;; + "--all" ) + init ip6 + init ip4 + ;; + * ) + usage + ;; +esac + diff --git a/salt/profile/netcup_failover/init.sls b/salt/profile/netcup_failover/init.sls new file mode 100644 index 0000000..c4d5679 --- /dev/null +++ b/salt/profile/netcup_failover/init.sls @@ -0,0 +1,10 @@ +include: + - profile.keepalived_script_user + +/usr/local/bin/failover: + file.managed: + - user: keepalived_script + - group: wheel + - mode: 750 + - template: jinja + - source: salt://{{ slspath }}/files/failover.sh.j2 -- cgit v1.2.3