#!/bin/bash
#This script is used to automatically configure the parameters of a redis cluster using host network mode
#Including middleware-compose-cluster.yml and envfile.env

sed -i '/^#.*entrypoint:/s/^#//g' middleware-master-cluster.yml
sed -i '/^#.*extra_hosts:/s/^#//g' middleware-master-cluster.yml
sed -i -r '/^#.*"redis[1,2,3]:/s/^#//g' middleware-master-cluster.yml
sed -i -r '/^\s{2}+redis?./,/^$/{/networks/ N;s/middleware/host/g}' middleware-master-cluster.yml
echo "RedisPassword=ZgDSvOoY5Q" >> envfile.env
for n in $(docker node ls |grep -v ID |awk '{print $1}');do
  for ((i=1; i<=3 ;i++));do
    if [ ! -z "$(docker inspect ${n} |grep "redis$i")" ];then
      ip=$(docker node inspect --format '{{if eq "0.0.0.0" .Status.Addr}}{{.ManagerStatus.Addr}}{{else}}{{.Status.Addr}}{{end}}' ${n}|awk -F':' '{print $1}')
      sed -i "/"redis${i}:x"/s/x/${ip}/" middleware-master-cluster.yml
      echo "RedisHostID${i}=${ip}" >> envfile.env
    fi
    done
done
