github.com/rochacon/deis@v1.0.2-0.20150903015341-6839b592a1ff/database/bin/boot (about)

     1  #!/usr/bin/env bash
     2  #
     3  # This script is designed to be run inside the container
     4  #
     5  
     6  # fail hard and fast even on pipelines
     7  set -eo pipefail
     8  
     9  # set debug based on envvar
    10  [[ $DEBUG ]] && set -x
    11  
    12  # configure etcd
    13  export ETCD_PORT="${ETCD_PORT:-4001}"
    14  export ETCD="$HOST:$ETCD_PORT"
    15  export ETCD_PATH="${ETCD_PATH:-/deis/database}"
    16  export ETCD_TTL="${ETCD_TTL:-20}"
    17  
    18  export BUCKET_NAME=${BUCKET_NAME:-db_wal}
    19  
    20  # wait for etcd to be available
    21  until etcdctl --no-sync -C "$ETCD" ls >/dev/null 2>&1; do
    22    echo "database: waiting for etcd at $ETCD..."
    23    sleep $((ETCD_TTL/2))  # sleep for half the TTL
    24  done
    25  
    26  # wait until etcd has discarded potentially stale values
    27  sleep $((ETCD_TTL+1))
    28  
    29  function etcd_set_default {
    30    set +e
    31    ERROR="$(etcdctl --no-sync -C "$ETCD" mk "$ETCD_PATH/$1" "$2" 2>&1 >/dev/null)"
    32  
    33    if [[ $? -ne 0 ]] && echo "$ERROR" | grep -iqve "key already exists"; then
    34      echo "etcd_set_default: an etcd error occurred ($ERROR)"
    35      echo "aborting..."
    36      exit 1
    37    fi
    38    set -e
    39  }
    40  
    41  etcd_set_default engine postgresql_psycopg2
    42  etcd_set_default adminUser "${PG_ADMIN_USER:-postgres}"
    43  etcd_set_default adminPass "${PG_ADMIN_PASS:-changeme123}"
    44  etcd_set_default user "${PG_USER_NAME:-deis}"
    45  etcd_set_default password "${PG_USER_PASS:-changeme123}"
    46  etcd_set_default name "${PG_USER_DB:-deis}"
    47  etcd_set_default bucketName "${BUCKET_NAME}"
    48  
    49  # stub out the confd reload script before it gets templated
    50  echo '#!/bin/sh' > /usr/local/bin/reload
    51  chmod 0755 /usr/local/bin/reload
    52  
    53  # wait for confd to run once and install initial templates
    54  until confd -onetime -node "$ETCD" -confdir /app --log-level error; do
    55    echo "database: waiting for confd to write initial templates..."
    56    sleep $((ETCD_TTL/2))  # sleep for half the TTL
    57  done
    58  
    59  # initialize database if one doesn't already exist
    60  # for example, in the case of a data container
    61  if [[ ! -d /var/lib/postgresql/9.3/main ]]; then
    62    chown -R postgres:postgres /var/lib/postgresql
    63    sudo -u postgres /usr/bin/initdb -D /var/lib/postgresql/9.3/main
    64  fi
    65  
    66  # ensure WAL log bucket exists
    67  envdir /etc/wal-e.d/env /app/bin/create_bucket "${BUCKET_NAME}"
    68  INIT_ID=$(etcdctl get "$ETCD_PATH/initId" 2> /dev/null || echo none)
    69  echo "database: expecting initialization id: $INIT_ID"
    70  
    71  initial_backup=0
    72  if [[ "$(cat /var/lib/postgresql/9.3/main/initialized 2> /dev/null)" != "$INIT_ID" ]]; then
    73    echo "database: no existing database found or it is outdated."
    74    # check if there are any backups -- if so, let's restore
    75    # we could probably do better than just testing number of lines -- one line is just a heading, meaning no backups
    76    if [[ $(envdir /etc/wal-e.d/env wal-e --terse backup-list | wc -l) -gt "1" ]]; then
    77      echo "database: restoring from backup..."
    78      rm -rf /var/lib/postgresql/9.3/main
    79      sudo -u postgres envdir /etc/wal-e.d/env wal-e backup-fetch /var/lib/postgresql/9.3/main LATEST
    80      chown -R postgres:postgres /var/lib/postgresql/9.3/main
    81      chmod 0700 /var/lib/postgresql/9.3/main
    82      echo "restore_command = 'envdir /etc/wal-e.d/env wal-e wal-fetch \"%f\" \"%p\"'" | sudo -u postgres tee /var/lib/postgresql/9.3/main/recovery.conf >/dev/null
    83    else
    84      echo "database: no backups found. Initializing a new database..."
    85      initial_backup=1
    86    fi
    87    # either way, we mark the database as initialized
    88    INIT_ID=$(cat /proc/sys/kernel/random/uuid)
    89    echo "$INIT_ID" > /var/lib/postgresql/9.3/main/initialized
    90    etcdctl --no-sync -C "$ETCD" set "$ETCD_PATH/initId" "$INIT_ID" >/dev/null
    91  else
    92    echo "database: existing data directory found. Starting postgres..."
    93  fi
    94  
    95  # run the service in the background
    96  sudo -i -u postgres /usr/bin/postgres \
    97                      -c config-file="${PG_CONFIG:-/etc/postgresql/main/postgresql.conf}" \
    98                      -c listen-addresses="${PG_LISTEN:-*}" &
    99  
   100  SERVICE_PID=$!
   101  
   102  # smart shutdown on SIGINT and SIGTERM
   103  function on_exit() {
   104      kill -TERM $SERVICE_PID
   105      wait $SERVICE_PID 2>/dev/null
   106      exit 0
   107  }
   108  trap on_exit INT TERM
   109  
   110  # spawn confd in the background to update services based on etcd changes
   111  confd -node "$ETCD" -confdir /app --log-level error --interval 5 &
   112  
   113  # wait for the service to become available
   114  sleep 1 && while [[ -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".5432\" && \$1 ~ \"tcp.?\"") ]] ; do sleep 1; done
   115  
   116  # perform a one-time reload to populate database entries
   117  /usr/local/bin/reload
   118  
   119  if [[ "${initial_backup}" == "1" ]] ; then
   120    echo "database: performing an initial backup..."
   121    # perform an initial backup
   122    sudo -u postgres envdir /etc/wal-e.d/env wal-e backup-push /var/lib/postgresql/9.3/main
   123  fi
   124  
   125  sudo -Eu postgres /app/bin/backup &
   126  
   127  echo "database: postgres is running..."
   128  
   129  # publish the service to etcd using the injected HOST and EXTERNAL_PORT
   130  if [[ ! -z $EXTERNAL_PORT ]]; then
   131    # configure service discovery
   132    PORT=${PORT:-5432}
   133    PROTO=${PROTO:-tcp}
   134  
   135    set +e
   136  
   137    # wait for the service to become available on PORT
   138    sleep 1 && while [[ -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PORT\" && \$1 ~ \"$PROTO.?\"") ]] ; do sleep 1; done
   139  
   140    # while the port is listening, publish to etcd
   141    while [[ ! -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$PORT\" && \$1 ~ \"$PROTO.?\"") ]] ; do
   142      etcdctl --no-sync -C "$ETCD" set "$ETCD_PATH/host" "$HOST" --ttl "$ETCD_TTL" >/dev/null
   143      etcdctl --no-sync -C "$ETCD" set "$ETCD_PATH/port" "$EXTERNAL_PORT" --ttl "$ETCD_TTL" >/dev/null
   144      sleep $((ETCD_TTL/2)) # sleep for half the TTL
   145    done
   146  
   147    # if the loop quits, something went wrong
   148    exit 1
   149  
   150  fi
   151  
   152  wait