<commit_before>#!/bin/bash

set -e #fail fast

DATA_DIR=/data
mkdir -p $DATA_DIR

# NAME is automatically passed in from cf-containers-broker
# It is the container name of the running container in the docker-daemon
# it will be cf-<node-id>
if [[ -z "${NAME}" ]]; then
  echo "Requires \$NAME to look up container in registrator"
  exit 1
fi

# PATRONI_SCOPE is passed in from the broker and is == <instance-id>
if [[ -z "${PATRONI_SCOPE}" ]]; then
  echo "Requires \$PATRONI_SCOPE to advertise container and form cluster"
  exit 1
fi
# passed in from broker
if [[ -z "${ETCD_HOST_PORT}" ]]; then
  echo "Requires \$ETCD_HOST_PORT (host:port) for etcd used by registrator & patroni"
  exit 1
fi

# added from cf-containers-broker env_var_dir see jobs/container-env-vars/monit
if [[ -z "${DOCKER_HOSTNAME}" ]]; then
  echo "Requires \$DOCKER_HOSTNAME to discover public host:port from registrator"
  exit 1
fi

# for Backwards compatibility
if [[ ! -z "${NODE_NAME}" ]]; then
  NODE_ID=${NODE_ID:-${NODE_NAME}}
fi

# NODE_ID is the id for this node
if [[ -z "${NODE_ID}" ]]; then
  echo "Requires \$NODE_ID as unique identifier"
  exit 1
fi

indent_startup() {
  c="s/^/${PATRONI_SCOPE:0:6}-startup> /"
  case $(uname) in
    Darwin) sed -l "$c";; # mac/bsd sed: -l buffers on line boundaries
    *)      sed -u "$c";; # unix/gnu sed: -u unbuffered (arbitrary) chunks of data
  esac
}

(
  # look up public host:port binding from registrar entry in etcd
  # this is then advertised via patroni for replicas to connect
  i="0"
  image_name='dingo-postgresql95'
  while [[  $i -lt 4 ]]
  do
    sleep 3
    registrator_5432_uri="${ETCD_HOST_PORT}/v2/keys/${image_name}-5432/${DOCKER_HOSTNAME}:${NAME}:5432"
    echo "looking up public host:5432 from etcd -> ${registrator_5432_uri} ($i)"
    connect_address_5432=$(curl -sL ${registrator_5432_uri} | jq -r .node.value)
    if [[ "${connect_address_5432}" == "null" ]]; then
      echo container not yet registered, waiting...
    else
      break
    fi
    i=$[$i+1]
  done
  if [[ "${connect_address_5432}" == "null" ]]; then
    echo "failed to look up container in etcd"
    exit 1
  else
    echo "public :5432 address ${connect_address_5432}"
  fi

  registrator_8008_uri="${ETCD_HOST_PORT}/v2/keys/${image_name}-8008/${DOCKER_HOSTNAME}:${NAME}:8008"
  echo "looking up public host:8008 from etcd -> ${registrator_8008_uri}"
  connect_address_8008=$(curl -sL ${registrator_8008_uri} | jq -r .node.value)
  echo "public :8008 address ${connect_address_8008}"

  # for backwards compatibility
  if [[ ! -z "${POSTGRES_USERNAME}" ]]; then
    ADMIN_USERNAME=${POSTGRES_USERNAME}
    ADMIN_PASSWORD=${POST<PASSWORD>_PASSWORD}
  fi

  ADMIN_USERNAME=${ADMIN_USERNAME:-pgadmin}
  ADMIN_PASSWORD=${ADMIN_PASSWORD:-$(pwgen -s -1 16)}
  SUPERUSER_USERNAME=${SUPERUSER_USERNAME:-postgres}
  SUPERUSER_PASSWORD=${SUPERUSER_PASSWORD:-<PASSWORD>}
  APPUSER_USERNAME=${APPUSER_USERNAME:-dvw7DJgqzFBJC8}
  APPUSER_PASSWORD=${APPUSER_PASSWORD:-<PASSWORD>}

  WALE_ENV_DIR=${WALE_ENV_DIR:-${DATA_DIR}/wal-e/env}
  mkdir -p $WALE_ENV_DIR

  # pass thru environment variables into an env dir for postgres user's archive/restore commands
  DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
  ${DIR}/create_envdir.sh ${WALE_ENV_DIR}

  PG_DATA_DIR=${PG_DATA_DIR:-${DATA_DIR}/postgres0}
  echo $PG_DATA_DIR > ${WALE_ENV_DIR}/PG_DATA_DIR

  if [[ "${WAL_S3_BUCKET}X" != "X" ]]; then
    if ! curl -s s3-website-us-east-1.amazonaws.com >/dev/null; then
      echo Cannot access AWS S3. Check DNS and Internet access.
      exit 1
    fi
    echo "Enabling wal-e archives to S3 bucket '${WAL_S3_BUCKET}'"
    ENVDIR="envdir ${WALE_ENV_DIR}"
    if [[ "${AWS_INSTANCE_PROFILE}X" != "X" ]]; then
      export WALE_CMD="${ENVDIR} wal-e --aws-instance-profile"
    else
      # see wal-e readme for env variables to configure for S3, Swift, etc
      export WALE_CMD="${ENVDIR} wal-e"
    fi

    export WALE_S3_PREFIX="s3://${WAL_S3_BUCKET}/backups/${PATRONI_SCOPE}/wal/"
    echo $WALE_S3_PREFIX > ${WALE_ENV_DIR}/WALE_S3_PREFIX
    echo $WALE_CMD > ${WALE_ENV_DIR}/WALE_CMD
    if [[ ! -z "${DISABLE_REGULAR_BACKUPS}" ]]; then
      echo "Disabling regular backups"
      echo $DISABLE_REGULAR_BACKUPS > ${WALE_ENV_DIR}/DISABLE_REGULAR_BACKUPS
    fi
<commit_msg>WALE_ENV_DIR is never passed in<commit_after>#!/bin/bash

set -e #fail fast

DATA_DIR=/data
mkdir -p $DATA_DIR

# NAME is automatically passed in from cf-containers-broker
# It is the container name of the running container in the docker-daemon
# it will be cf-<node-id>
if [[ -z "${NAME}" ]]; then
  echo "Requires \$NAME to look up container in registrator"
  exit 1
fi

# PATRONI_SCOPE is passed in from the broker and is == <instance-id>
if [[ -z "${PATRONI_SCOPE}" ]]; then
  echo "Requires \$PATRONI_SCOPE to advertise container and form cluster"
  exit 1
fi
# passed in from broker
if [[ -z "${ETCD_HOST_PORT}" ]]; then
  echo "Requires \$ETCD_HOST_PORT (host:port) for etcd used by registrator & patroni"
  exit 1
fi

# added from cf-containers-broker env_var_dir see jobs/container-env-vars/monit
if [[ -z "${DOCKER_HOSTNAME}" ]]; then
  echo "Requires \$DOCKER_HOSTNAME to discover public host:port from registrator"
  exit 1
fi

# for Backwards compatibility
if [[ ! -z "${NODE_NAME}" ]]; then
  NODE_ID=${NODE_ID:-${NODE_NAME}}
fi

# NODE_ID is the id for this node
if [[ -z "${NODE_ID}" ]]; then
  echo "Requires \$NODE_ID as unique identifier"
  exit 1
fi

indent_startup() {
  c="s/^/${PATRONI_SCOPE:0:6}-startup> /"
  case $(uname) in
    Darwin) sed -l "$c";; # mac/bsd sed: -l buffers on line boundaries
    *)      sed -u "$c";; # unix/gnu sed: -u unbuffered (arbitrary) chunks of data
  esac
}

(
  # look up public host:port binding from registrar entry in etcd
  # this is then advertised via patroni for replicas to connect
  i="0"
  image_name='dingo-postgresql95'
  while [[  $i -lt 4 ]]
  do
    sleep 3
    registrator_5432_uri="${ETCD_HOST_PORT}/v2/keys/${image_name}-5432/${DOCKER_HOSTNAME}:${NAME}:5432"
    echo "looking up public host:5432 from etcd -> ${registrator_5432_uri} ($i)"
    connect_address_5432=$(curl -sL ${registrator_5432_uri} | jq -r .node.value)
    if [[ "${connect_address_5432}" == "null" ]]; then
      echo container not yet registered, waiting...
    else
      break
    fi
    i=$[$i+1]
  done
  if [[ "${connect_address_5432}" == "null" ]]; then
    echo "failed to look up container in etcd"
    exit 1
  else
    echo "public :5432 address ${connect_address_5432}"
  fi

  registrator_8008_uri="${ETCD_HOST_PORT}/v2/keys/${image_name}-8008/${DOCKER_HOSTNAME}:${NAME}:8008"
  echo "looking up public host:8008 from etcd -> ${registrator_8008_uri}"
  connect_address_8008=$(curl -sL ${registrator_8008_uri} | jq -r .node.value)
  echo "public :8008 address ${connect_address_8008}"

  # for backwards compatibility
  if [[ ! -z "${POSTGRES_USERNAME}" ]]; then
    ADMIN_USERNAME=${POSTGRES_USERNAME}
    ADMIN_PASSWORD=${<PASSWORD>}
  fi

  ADMIN_USERNAME=${ADMIN_USERNAME:-pgadmin}
  ADMIN_PASSWORD=${ADMIN_PASSWORD:-$(pwgen -s -1 16)}
  SUPERUSER_USERNAME=${SUPERUSER_USERNAME:-postgres}
  SUPERUSER_PASSWORD=${SUPERUSER_PASSWORD:-<PASSWORD>}
  APPUSER_USERNAME=${APPUSER_USERNAME:-dvw7DJgqzFBJC8}
  APPUSER_PASSWORD=${APPUSER_PASSWORD:-<PASSWORD>}

  WALE_ENV_DIR=${DATA_DIR}/wal-e/env
  mkdir -p $WALE_ENV_DIR

  # pass thru environment variables into an env dir for postgres user's archive/restore commands
  DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
  ${DIR}/create_envdir.sh ${WALE_ENV_DIR}

  PG_DATA_DIR=${PG_DATA_DIR:-${DATA_DIR}/postgres0}
  echo $PG_DATA_DIR > ${WALE_ENV_DIR}/PG_DATA_DIR

  if [[ "${WAL_S3_BUCKET}X" != "X" ]]; then
    if ! curl -s s3-website-us-east-1.amazonaws.com >/dev/null; then
      echo Cannot access AWS S3. Check DNS and Internet access.
      exit 1
    fi
    echo "Enabling wal-e archives to S3 bucket '${WAL_S3_BUCKET}'"
    ENVDIR="envdir ${WALE_ENV_DIR}"
    if [[ "${AWS_INSTANCE_PROFILE}X" != "X" ]]; then
      export WALE_CMD="${ENVDIR} wal-e --aws-instance-profile"
    else
      # see wal-e readme for env variables to configure for S3, Swift, etc
      export WALE_CMD="${ENVDIR} wal-e"
    fi

    export WALE_S3_PREFIX="s3://${WAL_S3_BUCKET}/backups/${PATRONI_SCOPE}/wal/"
    echo $WALE_S3_PREFIX > ${WALE_ENV_DIR}/WALE_S3_PREFIX
    echo $WALE_CMD > ${WALE_ENV_DIR}/WALE_CMD
    if [[ ! -z "${DISABLE_REGULAR_BACKUPS}" ]]; then
      echo "Disabling regular backups"
      echo $DISABLE_REGULAR_BACKUPS > ${WALE_ENV_DIR}/DISABLE_REGULAR_BACKUPS
    fi

{'fraction_non_alphanumeric': 0.13817630597014927, 'fraction_numerical': 0.027052238805970148, 'mean_word_length': 3.9982517482517483, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5702960', 'n_tokens_mistral': 3369, 'n_tokens_neox': 3172, 'n_words': 806}