From 54f34bc7169772e77ebd97612640eb8072b27f4b Mon Sep 17 00:00:00 2001 From: fumingwei Date: Mon, 3 Jul 2023 19:00:45 +0800 Subject: [PATCH] =?UTF-8?q?feature:TSG-15851:=E6=96=B0=E5=A2=9Ecm-cache?= =?UTF-8?q?=E5=88=87=E6=8D=A2=E4=B8=BB=E5=A4=87cm=E7=9A=84=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ansible/HAL_deploy.yml | 1 + .../files/cm-cache-extra-scripts.yaml | 188 ++++++++++++++++++ .../files/cm-cache-extra-secrets.yaml | 9 + ansible/roles/cm-cache/files/redis-6.2.5.tgz | Bin 0 -> 91843 bytes ansible/roles/cm-cache/tasks/main.yml | 21 ++ .../files/cm-cache/redis-6.2.5.tgz | Bin 91696 -> 0 bytes ansible/roles/traffic-engine/tasks/main.yml | 12 -- 7 files changed, 219 insertions(+), 12 deletions(-) create mode 100644 ansible/roles/cm-cache/files/cm-cache-extra-scripts.yaml create mode 100644 ansible/roles/cm-cache/files/cm-cache-extra-secrets.yaml create mode 100644 ansible/roles/cm-cache/files/redis-6.2.5.tgz create mode 100644 ansible/roles/cm-cache/tasks/main.yml delete mode 100644 ansible/roles/traffic-engine/files/cm-cache/redis-6.2.5.tgz diff --git a/ansible/HAL_deploy.yml b/ansible/HAL_deploy.yml index 807029a0..60278e3e 100644 --- a/ansible/HAL_deploy.yml +++ b/ansible/HAL_deploy.yml @@ -173,6 +173,7 @@ - {role: sosreport, tags: sosreport} - {role: nm-config, tags: nm-config} - {role: tsg-os-logo, tags: tsg-os-logo} + - {role: cm-cache, tags: cm-cache} - hosts: TSG-X-NXR620G40-R01-P0906-init remote_user: root diff --git a/ansible/roles/cm-cache/files/cm-cache-extra-scripts.yaml b/ansible/roles/cm-cache/files/cm-cache-extra-scripts.yaml new file mode 100644 index 00000000..b0c6a747 --- /dev/null +++ b/ansible/roles/cm-cache/files/cm-cache-extra-scripts.yaml @@ -0,0 +1,188 @@ +apiVersion: v1 +data: + switch_available_redis.sh: |- + #!/bin/bash + pwd=`pwd` + + function check_replicaof_alive() + { + check_redis_ip=$1 + check_redis_port=$2 + if [ -n "$TLS_REDIS_FILE" ]; then + redis-cli -h $check_redis_ip -p $check_redis_port --tls --cacert $TLS_REDIS_FILE ping > /dev/null + else + redis-cli -h $check_redis_ip -p $check_redis_port ping > /dev/null + fi + if [[ $? -eq 0 ]]; then + return 0 + else + return 1 + fi + } + + function start() + { + current_date=$(date +%Y-%m-%d) + #Get the IP of the current redis synchronization + redis_info=$(redis-cli -h $REDIS_HOST -p $HOST_PORT CONFIG GET replicaof) + + str=(${redis_info//,/}) + db_ip=${str[1]} + db_port=${str[2]} + + #1.Check whether the current redis connection address is CM master + if [ "$db_ip" = "$REDIS_MASTER" -a "$MASTER_PORT" = "$db_port" ];then + check_redis 0 + elif [ "$db_ip" = "$REDIS_SLAVE" -a "$SLAVE_PORT" = "$db_port" ];then + check_redis 1 + else + check_redis 2 + fi + } + + function check_redis() + { + CHECK_TYPE=$1 + # Starting time + start_time=$(date +%s) + + while true; do + # Use redis-cli to detect connections + check_replicaof_alive $REDIS_MASTER $MASTER_PORT + # check return value + if [[ $? -eq 0 ]]; then + #If the current redis slaveof is not the master IP, check whether the master IP is alive and change the redis slaveof + if [ "$CHECK_TYPE" != "0" ]; then + echo "[`date '+%Y-%m-%d %T'`] update redis slaveof: $REDIS_MASTER $MASTER_PORT " + up_redis $REDIS_MASTER $MASTER_PORT + break + fi + sleep 1 + break + else + echo "[`date '+%Y-%m-%d %T'`] unable to connect to $REDIS_MASTER $MASTER_PORT redis server" + #If the current redis slaveof is slave IP, jump out of the loop and no longer continue to change redis slaveof + if [ "$CHECK_TYPE" = "1" ]; then + sleep 1 + break + fi + + fi + + # Check if the duration is exceeded + current_time=$(date +%s) + elapsed_time=$((current_time - start_time)) + if [[ $elapsed_time -ge $CHECK_DURATION ]]; then + check_replicaof_alive $REDIS_SLAVE $SLAVE_PORT + if [[ $? -eq 0 ]]; then + echo "[`date '+%Y-%m-%d %T'`] update redis slaveof: $REDIS_SLAVE $SLAVE_PORT redis server" + up_redis $REDIS_SLAVE $SLAVE_PORT + break + else + echo "[`date '+%Y-%m-%d %T'`] unable to connect to $REDIS_SLAVE $SLAVE_PORT redis server" + fi + fi + sleep 1 + done + } + + function up_redis() + { + UP_IP=$1 + UP_PORT=$2 + + check_replicaof_alive $UP_IP $UP_PORT + if [[ $? -eq 0 ]]; then + redis-cli -h $REDIS_HOST -p $HOST_PORT REPLICAOF $UP_IP $UP_PORT + echo "[`date '+%Y-%m-%d %T'`] successfully changed Redis slaveof : $UP_IP $UP_PORT" + sleep $INTERVAL_TIME + else + echo "[`date '+%Y-%m-%d %T'`] Change failed, unable to connect to $UP_IP $UP_PORT redis server" + fi + } + + REDIS_HOST="127.0.0.1" + HOST_PORT=6379 + #Redis connection timeout (seconds) + CHECK_DURATION=120 + # Redis master-slave switch synchronization interval time + INTERVAL_TIME=5m + REDIS_MASTER="" + MASTER_PORT="" + REDIS_SLAVE="" + SLAVE_PORT="" + TLS_REDIS_FILE="" + + + usage() + { + echo "Usage: $0 [-m ] [-s ] [-T ] [-t