github.com/argoproj-labs/argocd-operator@v0.10.0/build/redis/init.sh.tpl (about)

     1  echo "$(date) Start..."
     2  HOSTNAME="$(cat /proc/sys/kernel/hostname)"
     3  INDEX="${HOSTNAME##*-}"
     4  SENTINEL_PORT={{- if eq .UseTLS "false" -}}26379{{- else -}}0{{- end }}
     5  MASTER=''
     6  MASTER_GROUP="argocd"
     7  QUORUM="2"
     8  REDIS_CONF=/data/conf/redis.conf
     9  {{- if eq .UseTLS "false"}}
    10  REDIS_PORT=6379
    11  REDIS_TLS_PORT=
    12  {{- else}}
    13  REDIS_PORT=0
    14  REDIS_TLS_PORT=6379
    15  {{- end}}
    16  SENTINEL_CONF=/data/conf/sentinel.conf
    17  SENTINEL_TLS_PORT={{- if eq .UseTLS "true" -}}26379{{- end }}
    18  SERVICE={{.ServiceName}}
    19  SENTINEL_TLS_REPLICATION_ENABLED={{.UseTLS}}
    20  REDIS_TLS_REPLICATION_ENABLED={{.UseTLS}}
    21  set -eu
    22  
    23  sentinel_get_master() {
    24  set +e
    25      if [ "$SENTINEL_PORT" -eq 0 ]; then
    26          redis-cli -h "${SERVICE}" -p "${SENTINEL_TLS_PORT}"   --tls --cacert /app/config/redis/tls/tls.crt sentinel get-master-addr-by-name "${MASTER_GROUP}" |\
    27          grep -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
    28      else
    29          redis-cli -h "${SERVICE}" -p "${SENTINEL_PORT}"  sentinel get-master-addr-by-name "${MASTER_GROUP}" |\
    30          grep -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
    31      fi
    32  set -e
    33  }
    34  
    35  sentinel_get_master_retry() {
    36      master=''
    37      retry=${1}
    38      sleep=3
    39      for i in $(seq 1 "${retry}"); do
    40          master=$(sentinel_get_master)
    41          if [ -n "${master}" ]; then
    42              break
    43          fi
    44          sleep $((sleep + i))
    45      done
    46      echo "${master}"
    47  }
    48  
    49  identify_master() {
    50      echo "Identifying redis master (get-master-addr-by-name).."
    51      echo "  using sentinel ({{.ServiceName}}), sentinel group name (argocd)"
    52      echo "  $(date).."
    53      MASTER="$(sentinel_get_master_retry 3)"
    54      if [ -n "${MASTER}" ]; then
    55          echo "  $(date) Found redis master (${MASTER})"
    56      else
    57          echo "  $(date) Did not find redis master (${MASTER})"
    58      fi
    59  }
    60  
    61  sentinel_update() {
    62      echo "Updating sentinel config.."
    63      echo "  evaluating sentinel id (\${SENTINEL_ID_${INDEX}})"
    64      eval MY_SENTINEL_ID="\$SENTINEL_ID_${INDEX}"
    65      echo "  sentinel id (${MY_SENTINEL_ID}), sentinel grp (${MASTER_GROUP}), quorum (${QUORUM})"
    66      sed -i "1s/^/sentinel myid ${MY_SENTINEL_ID}\\n/" "${SENTINEL_CONF}"
    67      if [ "$SENTINEL_TLS_REPLICATION_ENABLED" = true ]; then
    68          echo "  redis master (${1}:${REDIS_TLS_PORT})"
    69          sed -i "2s/^/sentinel monitor ${MASTER_GROUP} ${1} ${REDIS_TLS_PORT} ${QUORUM} \\n/" "${SENTINEL_CONF}"
    70      else
    71          echo "  redis master (${1}:${REDIS_PORT})"
    72          sed -i "2s/^/sentinel monitor ${MASTER_GROUP} ${1} ${REDIS_PORT} ${QUORUM} \\n/" "${SENTINEL_CONF}"
    73      fi
    74      echo "sentinel announce-ip ${ANNOUNCE_IP}" >> ${SENTINEL_CONF}
    75      if [ "$SENTINEL_PORT" -eq 0 ]; then
    76          echo "  announce (${ANNOUNCE_IP}:${SENTINEL_TLS_PORT})"
    77          echo "sentinel announce-port ${SENTINEL_TLS_PORT}" >> ${SENTINEL_CONF}
    78      else
    79          echo "  announce (${ANNOUNCE_IP}:${SENTINEL_PORT})"
    80          echo "sentinel announce-port ${SENTINEL_PORT}" >> ${SENTINEL_CONF}
    81      fi
    82  }
    83  
    84  redis_update() {
    85      echo "Updating redis config.."
    86      if [ "$REDIS_TLS_REPLICATION_ENABLED" = true ]; then
    87          echo "  we are slave of redis master (${1}:${REDIS_TLS_PORT})"
    88          echo "slaveof ${1} ${REDIS_TLS_PORT}" >> "${REDIS_CONF}"
    89          echo "slave-announce-port ${REDIS_TLS_PORT}" >> ${REDIS_CONF}
    90      else
    91          echo "  we are slave of redis master (${1}:${REDIS_PORT})"
    92          echo "slaveof ${1} ${REDIS_PORT}" >> "${REDIS_CONF}"
    93          echo "slave-announce-port ${REDIS_PORT}" >> ${REDIS_CONF}
    94      fi
    95      echo "slave-announce-ip ${ANNOUNCE_IP}" >> ${REDIS_CONF}
    96  }
    97  
    98  copy_config() {
    99      echo "Copying default redis config.."
   100      echo "  to '${REDIS_CONF}'"
   101      cp /readonly-config/redis.conf "${REDIS_CONF}"
   102      echo "Copying default sentinel config.."
   103      echo "  to '${SENTINEL_CONF}'"
   104      cp /readonly-config/sentinel.conf "${SENTINEL_CONF}"
   105  }
   106  
   107  setup_defaults() {
   108      echo "Setting up defaults.."
   109      echo "  using statefulset index (${INDEX})"
   110      if [ "${INDEX}" = "0" ]; then
   111          echo "Setting this pod as master for redis and sentinel.."
   112          echo "  using announce (${ANNOUNCE_IP})"
   113          redis_update "${ANNOUNCE_IP}"
   114          sentinel_update "${ANNOUNCE_IP}"
   115          echo "  make sure ${ANNOUNCE_IP} is not a slave (slaveof no one)"
   116          sed -i "s/^.*slaveof.*//" "${REDIS_CONF}"
   117      else
   118          echo "Getting redis master ip.."
   119          echo "  blindly assuming (${SERVICE}-announce-0) or (${SERVICE}-server-0) are master"
   120          DEFAULT_MASTER="$(getent_hosts 0 | awk '{ print $1 }')"
   121          echo "  identified redis (may be redis master) ip (${DEFAULT_MASTER})"
   122          if [ -z "${DEFAULT_MASTER}" ]; then
   123              echo "Error: Unable to resolve redis master (getent hosts)."
   124              exit 1
   125          fi
   126          echo "Setting default slave config for redis and sentinel.."
   127          echo "  using master ip (${DEFAULT_MASTER})"
   128          redis_update "${DEFAULT_MASTER}"
   129          sentinel_update "${DEFAULT_MASTER}"
   130      fi
   131  }
   132  
   133  redis_ping() {
   134  set +e
   135      if [ "$REDIS_PORT" -eq 0 ]; then
   136          redis-cli -h "${MASTER}" -p "${REDIS_TLS_PORT}"  --tls --cacert /app/config/redis/tls/tls.crt ping
   137      else
   138          redis-cli -h "${MASTER}" -p "${REDIS_PORT}" ping
   139      fi
   140  set -e
   141  }
   142  
   143  redis_ping_retry() {
   144      ping=''
   145      retry=${1}
   146      sleep=3
   147      for i in $(seq 1 "${retry}"); do
   148          if [ "$(redis_ping)" = "PONG" ]; then
   149             ping='PONG'
   150             break
   151          fi
   152          sleep $((sleep + i))
   153          MASTER=$(sentinel_get_master)
   154      done
   155      echo "${ping}"
   156  }
   157  
   158  find_master() {
   159      echo "Verifying redis master.."
   160      if [ "$REDIS_PORT" -eq 0 ]; then
   161          echo "  ping (${MASTER}:${REDIS_TLS_PORT})"
   162      else
   163          echo "  ping (${MASTER}:${REDIS_PORT})"
   164      fi
   165      echo "  $(date).."
   166      if [ "$(redis_ping_retry 3)" != "PONG" ]; then
   167          echo "  $(date) Can't ping redis master (${MASTER})"
   168          echo "Attempting to force failover (sentinel failover).."
   169  
   170          if [ "$SENTINEL_PORT" -eq 0 ]; then
   171              echo "  on sentinel (${SERVICE}:${SENTINEL_TLS_PORT}), sentinel grp (${MASTER_GROUP})"
   172              echo "  $(date).."
   173              if redis-cli -h "${SERVICE}" -p "${SENTINEL_TLS_PORT}"   --tls --cacert /app/config/redis/tls/tls.crt sentinel failover "${MASTER_GROUP}" | grep -q 'NOGOODSLAVE' ; then
   174                  echo "  $(date) Failover returned with 'NOGOODSLAVE'"
   175                  echo "Setting defaults for this pod.."
   176                  setup_defaults
   177                  return 0
   178              fi
   179          else
   180              echo "  on sentinel (${SERVICE}:${SENTINEL_PORT}), sentinel grp (${MASTER_GROUP})"
   181              echo "  $(date).."
   182              if redis-cli -h "${SERVICE}" -p "${SENTINEL_PORT}"  sentinel failover "${MASTER_GROUP}" | grep -q 'NOGOODSLAVE' ; then
   183                  echo "  $(date) Failover returned with 'NOGOODSLAVE'"
   184                  echo "Setting defaults for this pod.."
   185                  setup_defaults
   186                  return 0
   187              fi
   188          fi
   189  
   190          echo "Hold on for 10sec"
   191          sleep 10
   192          echo "We should get redis master's ip now. Asking (get-master-addr-by-name).."
   193          if [ "$SENTINEL_PORT" -eq 0 ]; then
   194              echo "  sentinel (${SERVICE}:${SENTINEL_TLS_PORT}), sentinel grp (${MASTER_GROUP})"
   195          else
   196              echo "  sentinel (${SERVICE}:${SENTINEL_PORT}), sentinel grp (${MASTER_GROUP})"
   197          fi
   198          echo "  $(date).."
   199          MASTER="$(sentinel_get_master)"
   200          if [ "${MASTER}" ]; then
   201              echo "  $(date) Found redis master (${MASTER})"
   202              echo "Updating redis and sentinel config.."
   203              sentinel_update "${MASTER}"
   204              redis_update "${MASTER}"
   205          else
   206              echo "$(date) Error: Could not failover, exiting..."
   207              exit 1
   208          fi
   209      else
   210          echo "  $(date) Found reachable redis master (${MASTER})"
   211          echo "Updating redis and sentinel config.."
   212          sentinel_update "${MASTER}"
   213          redis_update "${MASTER}"
   214      fi
   215  }
   216  
   217  redis_ro_update() {
   218      echo "Updating read-only redis config.."
   219      echo "  redis.conf set 'replica-priority 0'"
   220      echo "replica-priority 0" >> ${REDIS_CONF}
   221  }
   222  
   223  getent_hosts() {
   224      index=${1:-${INDEX}}
   225      service="${SERVICE}-announce-${index}"
   226      pod="${SERVICE}-server-${index}"
   227      host=$(getent hosts "${service}")
   228      if [ -z "${host}" ]; then
   229          host=$(getent hosts "${pod}")
   230      fi
   231      echo "${host}"
   232  }
   233  
   234  mkdir -p /data/conf/
   235  
   236  echo "Initializing config.."
   237  copy_config
   238  
   239  # where is redis master
   240  identify_master
   241  
   242  echo "Identify announce ip for this pod.."
   243  echo "  using (${SERVICE}-announce-${INDEX}) or (${SERVICE}-server-${INDEX})"
   244  ANNOUNCE_IP=$(getent_hosts | awk '{ print $1 }')
   245  echo "  identified announce (${ANNOUNCE_IP})"
   246  if [ -z "${ANNOUNCE_IP}" ]; then
   247      "Error: Could not resolve the announce ip for this pod."
   248      exit 1
   249  elif [ "${MASTER}" ]; then
   250      find_master
   251  else
   252      setup_defaults
   253  fi
   254  
   255  if [ "${AUTH:-}" ]; then
   256      echo "Setting redis auth values.."
   257      ESCAPED_AUTH=$(echo "${AUTH}" | sed -e 's/[\/&]/\\&/g');
   258      sed -i "s/replace-default-auth/${ESCAPED_AUTH}/" "${REDIS_CONF}" "${SENTINEL_CONF}"
   259  fi
   260  
   261  if [ "${SENTINELAUTH:-}" ]; then
   262      echo "Setting sentinel auth values"
   263      ESCAPED_AUTH_SENTINEL=$(echo "$SENTINELAUTH" | sed -e 's/[\/&]/\\&/g');
   264      sed -i "s/replace-default-sentinel-auth/${ESCAPED_AUTH_SENTINEL}/" "$SENTINEL_CONF"
   265  fi
   266  
   267  echo "$(date) Ready..."