#!/bin/bash # exit setup set -eo pipefail # [-e] - immediately exit if any command has a non-zero exit status # [-x] - all executed commands are printed to the terminal [not secure] # [-o pipefail] - if any command in a pipeline fails, that return code will be used as the return code of the whole pipeline IS_ROOT="false" # parse args for _ in "$@"; do case ${1} in --root) IS_ROOT="true" shift # past argument with no value ;; -*) echo "unknown arg: ${1}" exit 1 ;; *) ;; esac done if [ -z "${POSTGRES_USER_US}" ] || [ "${IS_ROOT}" == "true" ]; then POSTGRES_USER_US="${POSTGRES_USER}"; fi if [ -z "${POSTGRES_USER_DEMO}" ] || [ "${IS_ROOT}" == "true" ]; then POSTGRES_USER_DEMO="${POSTGRES_USER}"; fi if [ -z "${POSTGRES_PASSWORD_US}" ] || [ "${IS_ROOT}" == "true" ]; then POSTGRES_PASSWORD_US="${POSTGRES_PASSWORD}"; fi if [ -z "${POSTGRES_PASSWORD_DEMO}" ] || [ "${IS_ROOT}" == "true" ]; then POSTGRES_PASSWORD_DEMO="${POSTGRES_PASSWORD}"; fi if [ -z "${POSTGRES_HOST}" ] || [ "${POSTGRES_HOST}" == "localhost" ]; then POSTGRES_HOST="postgres" fi if [ -z "${POSTGRES_PORT}" ]; then POSTGRES_PORT="5432" fi export PGPORT="${POSTGRES_PORT}" if [ -z "${POST_INIT_SLEEP}" ]; then POST_INIT_SLEEP="5" fi if [ -z "${POST_INIT_RETRIES}" ]; then POST_INIT_RETRIES="10" fi echo " [post-init] start e2e data migration..." # shellcheck disable=SC2236 if [ ! -z "${US_ENDPOINT}" ]; then echo " [post-init] us endpoint: ${US_ENDPOINT}" echo " [post-init] sleep ${POST_INIT_SLEEP} seconds..." sleep "${POST_INIT_SLEEP}" RETRIES="${POST_INIT_RETRIES}" echo " [post-init] retries: ${RETRIES}" for RETRY in $(seq 1 "${RETRIES}"); do if curl --output /dev/null --silent --head --fail "${US_ENDPOINT}/ping-db"; then echo " [post-init] us /ping-db success, continue..." break fi echo " [post-init] sleep 3 second..." sleep 3 done echo " [post-init] retry: ${RETRY}" if [ "${RETRY}" == "${RETRIES}" ]; then echo " [post-init] error [${US_ENDPOINT}/ping-db], exit..." exit 1 fi fi export PGPASSWORD="${POSTGRES_PASSWORD_DEMO}" echo " [post-init] import e2e data..." echo " [post-init] postgres host: ${POSTGRES_HOST}" echo " [post-init] postgres port: ${POSTGRES_PORT}" psql -v ON_ERROR_STOP=1 --username "${POSTGRES_USER_DEMO}" --dbname "${POSTGRES_DB_DEMO}"