github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/deploy/postgresql/scripts/setup.sh (about)

     1  #!/bin/bash
     2  set -o errexit
     3  set -e
     4  
     5  # usage: retry <command>
     6  # e.g. retry pg_isready -U postgres -h $primary_fqdn -p 5432
     7  function retry {
     8    local max_attempts=10
     9    local attempt=1
    10    until "$@" || [ $attempt -eq $max_attempts ]; do
    11      echo "Command '$*' failed. Attempt $attempt of $max_attempts. Retrying in 5 seconds..."
    12      attempt=$((attempt + 1))
    13      sleep 5
    14    done
    15    if [ $attempt -eq $max_attempts ]; then
    16      echo "Command '$*' failed after $max_attempts attempts. Exiting..."
    17      exit 1
    18    fi
    19  }
    20  
    21  if [ -f /kb-podinfo/primary-pod ]; then
    22    # Waiting for primary pod information from the DownwardAPI annotation to be available, with a maximum of 5 attempts
    23    attempt=1
    24    max_attempts=10
    25    while [ $attempt -le $max_attempts ] && [ -z "$(cat /kb-podinfo/primary-pod)" ]; do
    26      sleep 3
    27      attempt=$((attempt + 1))
    28    done
    29    primary=$(cat /kb-podinfo/primary-pod)
    30    echo "DownwardAPI get primary=$primary" >> /home/postgres/pgdata/.kb_set_up.log
    31    echo "KB_POD_NAME=$KB_POD_NAME" >> /home/postgres/pgdata/.kb_set_up.log
    32  else
    33     echo "DownwardAPI get /kb-podinfo/primary-pod is empty" >> /home/postgres/pgdata/.kb_set_up.log
    34  fi
    35  
    36  if  [ ! -z "$primary" ] && [ "$primary" != "$KB_POD_NAME" ]; then
    37      primary_fqdn="$primary.$KB_CLUSTER_NAME-$KB_COMP_NAME-headless.$KB_NAMESPACE.svc"
    38      echo "primary_fqdn=$primary_fqdn" >> /home/postgres/pgdata/.kb_set_up.log
    39      # waiting for the primary to be ready, if the wait time exceeds the maximum number of retries, then the script will fail and exit.
    40      retry pg_isready -U "postgres" -h $primary_fqdn -p 5432
    41  fi
    42  
    43  if [ -f ${RESTORE_DATA_DIR}/kb_restore.signal ]; then
    44      chown -R postgres ${RESTORE_DATA_DIR}
    45  fi
    46  python3 /kb-scripts/generate_patroni_yaml.py tmp_patroni.yaml
    47  export SPILO_CONFIGURATION=$(cat tmp_patroni.yaml)
    48  exec /launch.sh init