#!/bin/sh
#***************************************************************#
# ScriptName: k8sha.sh
# Author: xhy
# Create Date: 2025-02-10 21:19
# Modify Author: xhy
# Modify Date: 2025-02-10 21:19
# Version: v1
#***************************************************************#


#######################################
# set variables below to create the config files, all files will create at ./hadir directory
#######################################

# master keepalived virtual ip address
export K8SHA_VIP=172.24.8.100

# master01 ip address
export K8SHA_IP1=172.24.8.111

# master02 ip address
export K8SHA_IP2=172.24.8.112

# master03 ip address
export K8SHA_IP3=172.24.8.113

# master01 hostname
export K8SHA_HOST1=master01

# master02 hostname
export K8SHA_HOST2=master02

# master03 hostname
export K8SHA_HOST3=master03

# master01 network interface name
export K8SHA_NETINF1=eth0

# master02 network interface name
export K8SHA_NETINF2=eth0

# keepalived auth_pass config
export K8SHA_KEEPALIVED_AUTH=ilovek8s

# master03 network interface name
export K8SHA_NETINF3=eth0

##############################
# please do not modify anything below
##############################

mkdir -p hadir/$K8SHA_HOST1/{keepalived,haproxy}
mkdir -p hadir/$K8SHA_HOST2/{keepalived,haproxy}
mkdir -p hadir/$K8SHA_HOST3/{keepalived,haproxy}
mkdir -p hadir/keepalived
mkdir -p hadir/haproxy

echo "create directory files success."

# wget all files
wget -c -P hadir/keepalived/ http://down.linuxsb.com/mydeploy/k8s/common/k8s-keepalived.conf.tpl
wget -c -P hadir/keepalived/ http://down.linuxsb.com/mydeploy/k8s/common/check_apiserver.sh
wget -c -P hadir/haproxy/ http://down.linuxsb.com/mydeploy/k8s/common/k8s-haproxy.cfg.tpl
wget -c -P hadir/haproxy/ http://down.linuxsb.com/mydeploy/k8s/common/k8s-haproxy.service

echo "down files success."

sed \
-e "s/K8SHA_KA_STATE/BACKUP/g" \
-e "s/K8SHA_KA_INTF/${K8SHA_NETINF1}/g" \
-e "s/K8SHA_IPLOCAL/${K8SHA_IP1}/g" \
-e "s/K8SHA_KA_PRIO/102/g" \
-e "s/K8SHA_VIP/${K8SHA_VIP}/g" \
-e "s/K8SHA_KA_AUTH/${K8SHA_KEEPALIVED_AUTH}/g" \
hadir/keepalived/k8s-keepalived.conf.tpl > hadir/$K8SHA_HOST1/keepalived/keepalived.conf

# create all keepalived files
chmod u+x hadir/keepalived/check_apiserver.sh
cp hadir/keepalived/check_apiserver.sh hadir/$K8SHA_HOST1/keepalived
cp hadir/keepalived/check_apiserver.sh hadir/$K8SHA_HOST2/keepalived
cp hadir/keepalived/check_apiserver.sh hadir/$K8SHA_HOST3/keepalived

sed \
-e "s/K8SHA_KA_STATE/BACKUP/g" \
-e "s/K8SHA_KA_INTF/${K8SHA_NETINF2}/g" \
-e "s/K8SHA_IPLOCAL/${K8SHA_IP2}/g" \
-e "s/K8SHA_KA_PRIO/101/g" \
-e "s/K8SHA_VIP/${K8SHA_VIP}/g" \
-e "s/K8SHA_KA_AUTH/${K8SHA_KEEPALIVED_AUTH}/g" \
hadir/keepalived/k8s-keepalived.conf.tpl > hadir/$K8SHA_HOST2/keepalived/keepalived.conf

sed \
-e "s/K8SHA_KA_STATE/BACKUP/g" \
-e "s/K8SHA_KA_INTF/${K8SHA_NETINF3}/g" \
-e "s/K8SHA_IPLOCAL/${K8SHA_IP3}/g" \
-e "s/K8SHA_KA_PRIO/100/g" \
-e "s/K8SHA_VIP/${K8SHA_VIP}/g" \
-e "s/K8SHA_KA_AUTH/${K8SHA_KEEPALIVED_AUTH}/g" \
hadir/keepalived/k8s-keepalived.conf.tpl > hadir/$K8SHA_HOST3/keepalived/keepalived.conf

echo "create keepalived files success. hadir/$K8SHA_HOST1/keepalived/"
echo "create keepalived files success. hadir/$K8SHA_HOST2/keepalived/"
echo "create keepalived files success. hadir/$K8SHA_HOST3/keepalived/"

# create all haproxy files
sed \
-e "s/K8SHA_IP1/$K8SHA_IP1/g" \
-e "s/K8SHA_IP2/$K8SHA_IP2/g" \
-e "s/K8SHA_IP3/$K8SHA_IP3/g" \
-e "s/K8SHA_HOST1/$K8SHA_HOST1/g" \
-e "s/K8SHA_HOST2/$K8SHA_HOST2/g" \
-e "s/K8SHA_HOST3/$K8SHA_HOST3/g" \
hadir/haproxy/k8s-haproxy.cfg.tpl > hadir/haproxy/haproxy.conf

echo "create haproxy files success. hadir/$K8SHA_HOST1/haproxy/"
echo "create haproxy files success. hadir/$K8SHA_HOST2/haproxy/"
echo "create haproxy files success. hadir/$K8SHA_HOST3/haproxy/"

# scp all file
scp -rp hadir/haproxy/haproxy.conf root@$K8SHA_HOST1:/etc/haproxy/haproxy.cfg
scp -rp hadir/haproxy/haproxy.conf root@$K8SHA_HOST2:/etc/haproxy/haproxy.cfg
scp -rp hadir/haproxy/haproxy.conf root@$K8SHA_HOST3:/etc/haproxy/haproxy.cfg
scp -rp hadir/haproxy/k8s-haproxy.service root@$K8SHA_HOST1:/usr/lib/systemd/system/haproxy.service
scp -rp hadir/haproxy/k8s-haproxy.service root@$K8SHA_HOST2:/usr/lib/systemd/system/haproxy.service
scp -rp hadir/haproxy/k8s-haproxy.service root@$K8SHA_HOST3:/usr/lib/systemd/system/haproxy.service

scp -rp hadir/$K8SHA_HOST1/keepalived/* root@$K8SHA_HOST1:/etc/keepalived/
scp -rp hadir/$K8SHA_HOST2/keepalived/* root@$K8SHA_HOST2:/etc/keepalived/
scp -rp hadir/$K8SHA_HOST3/keepalived/* root@$K8SHA_HOST3:/etc/keepalived/

echo "scp haproxy & keepalived file success."
