scripts: make use of [[ in conditional statements

This commit is contained in:
Sameer Naik
2015-07-12 23:51:56 +05:30
parent 691ca31510
commit 24a109da76
+26 -28
View File
@@ -14,7 +14,7 @@ DB_UNACCENT=${DB_UNACCENT:false}
# by default postgresql will start up as a standalone instance.
# set this environment variable to master, slave or snapshot to use replication features.
# "snapshot" will create a point in time backup of a master instance.
PSQL_MODE=${PSQL_MODE:-"standalone"}
PSQL_MODE=${PSQL_MODE:-standalone}
REPLICATION_USER=${REPLICATION_USER:-}
REPLICATION_PASS=${REPLICATION_PASS:-}
@@ -22,7 +22,7 @@ REPLICATION_HOST=${REPLICATION_HOST:-}
REPLICATION_PORT=${REPLICATION_PORT:-5432}
# set this env variable to "require" to enable encryption and "verify-full" for verification.
PSQL_SSLMODE=${PSQL_SSLMODE:-"disable"}
PSQL_SSLMODE=${PSQL_SSLMODE:-disable}
## Adapt uid and gid for ${PG_USER}:${PG_USER}
USERMAP_ORIG_UID=$(id -u ${PG_USER})
@@ -47,7 +47,7 @@ mkdir -p -m 0755 /run/postgresql /run/postgresql/${PG_VERSION}-main.pg_stat_tmp
chown -R ${PG_USER}:${PG_USER} /run/postgresql
chmod g+s /run/postgresql
if [ "${PSQL_SSLMODE}" == "disable" ]; then
if [[ ${PSQL_SSLMODE} == disable ]]; then
sed 's/ssl = true/#ssl = true/' -i ${PG_CONFDIR}/postgresql.conf
fi
@@ -56,7 +56,7 @@ cat >> ${PG_CONFDIR}/postgresql.conf <<EOF
listen_addresses = '*'
EOF
if [ "${PSQL_TRUST_LOCALNET}" == "true" ]; then
if [[ ${PSQL_TRUST_LOCALNET} == true ]]; then
echo "Enabling trust samenet in pg_hba.conf..."
cat >> ${PG_CONFDIR}/pg_hba.conf <<EOF
host all all samenet trust
@@ -69,8 +69,8 @@ host all all 0.0.0.0/0 md5
EOF
# allow replication connections to the database
if [ -n "${REPLICATION_USER}" ]; then
if [ "${PSQL_SSLMODE}" == "disable" ]; then
if [[ -n ${REPLICATION_USER} ]]; then
if [[ ${PSQL_SSLMODE} == disable ]]; then
cat >> ${PG_CONFDIR}/pg_hba.conf <<EOF
host replication $REPLICATION_USER 0.0.0.0/0 md5
EOF
@@ -81,8 +81,8 @@ EOF
fi
fi
if [ "${PSQL_MODE}" == "master" ]; then
if [ -n "${REPLICATION_USER}" ]; then
if [[ ${PSQL_MODE} == master ]]; then
if [[ -n ${REPLICATION_USER} ]]; then
echo "Supporting hot standby..."
cat >> ${PG_CONFDIR}/postgresql.conf <<EOF
wal_level = hot_standby
@@ -96,18 +96,18 @@ fi
cd ${PG_HOME}
# initialize PostgreSQL data directory
if [ ! -d ${PG_DATADIR} ]; then
if [ "${PSQL_MODE}" == "slave" ] || [ "${PSQL_MODE}" == "snapshot" ]; then
if [[ ! -d ${PG_DATADIR} ]]; then
if [[ ${PSQL_MODE} == slave || ${PSQL_MODE} == snapshot ]]; then
echo "Replicating database..."
if [ "${PSQL_MODE}" == "snapshot" ]; then
if [[ ${PSQL_MODE} == snapshot ]]; then
sudo -Hu ${PG_USER} \
PGPASSWORD=$REPLICATION_PASS "${PG_BINDIR}/pg_basebackup" -D "${PG_DATADIR}" \
-h "${REPLICATION_HOST}" -p "${REPLICATION_PORT}" -U "${REPLICATION_USER}" -w -x -v -P
elif [ "${PSQL_MODE}" == "slave" ]; then
PGPASSWORD=$REPLICATION_PASS ${PG_BINDIR}/pg_basebackup -D ${PG_DATADIR} \
-h ${REPLICATION_HOST} -p ${REPLICATION_PORT} -U ${REPLICATION_USER} -w -x -v -P
elif [[ ${PSQL_MODE} == slave ]]; then
# Setup streaming replication.
sudo -Hu ${PG_USER} \
PGPASSWORD=$REPLICATION_PASS "${PG_BINDIR}/pg_basebackup" -D "${PG_DATADIR}" \
-h "${REPLICATION_HOST}" -p "${REPLICATION_PORT}" -U "${REPLICATION_USER}" -w -v -P
PGPASSWORD=$REPLICATION_PASS ${PG_BINDIR}/pg_basebackup -D ${PG_DATADIR} \
-h ${REPLICATION_HOST} -p ${REPLICATION_PORT} -U ${REPLICATION_USER} -w -v -P
echo "Setting up hot standby configuration..."
cat >> ${PG_CONFDIR}/postgresql.conf <<EOF
hot_standby = on
@@ -125,13 +125,12 @@ EOF
PG_OLD_VERSION=$(find ${PG_HOME}/[0-9].[0-9]/main -maxdepth 1 -name PG_VERSION 2>/dev/null | sort -r | head -n1 | cut -d'/' -f5)
echo "Initializing database..."
sudo -Hu ${PG_USER} "${PG_BINDIR}/initdb" --pgdata="${PG_DATADIR}" \
sudo -Hu ${PG_USER} ${PG_BINDIR}/initdb --pgdata=${PG_DATADIR} \
--username=${PG_USER} --encoding=unicode --auth=trust >/dev/null
fi
fi
if [ -n "${PG_OLD_VERSION}" ]; then
if [[ -n ${PG_OLD_VERSION} ]]; then
echo "Migrating postgresql ${PG_OLD_VERSION} data..."
PG_OLD_CONFDIR="/etc/postgresql/${PG_OLD_VERSION}/main"
PG_OLD_BINDIR="/usr/lib/postgresql/${PG_OLD_VERSION}/bin"
@@ -156,11 +155,10 @@ if [ -n "${PG_OLD_VERSION}" ]; then
-O "-c config_file=${PG_CONFDIR}/postgresql.conf" >/dev/null
fi
# Hot standby (slave and snapshot) servers can ignore the following code.
if [ "${PSQL_MODE}" == "standalone" ] || [ "${PSQL_MODE}" == "master" ]; then
if [ -n "${REPLICATION_USER}" ]; then
if [ -z "${REPLICATION_PASS}" ]; then
if [[ ${PSQL_MODE} == standalone || ${PSQL_MODE} == master ]]; then
if [[ -n ${REPLICATION_USER} ]]; then
if [[ -z ${REPLICATION_PASS} ]]; then
echo ""
echo "WARNING: "
echo " Please specify a password for replication user \"${REPLICATION_USER}\". Skipping user creation..."
@@ -174,8 +172,8 @@ if [ "${PSQL_MODE}" == "standalone" ] || [ "${PSQL_MODE}" == "master" ]; then
fi
fi
if [ -n "${DB_USER}" ]; then
if [ -z "${DB_PASS}" ]; then
if [[ -n ${DB_USER} ]]; then
if [[ -z ${DB_PASS} ]]; then
echo ""
echo "WARNING: "
echo " Please specify a password for \"${DB_USER}\". Skipping user creation..."
@@ -189,21 +187,21 @@ if [ "${PSQL_MODE}" == "standalone" ] || [ "${PSQL_MODE}" == "master" ]; then
fi
fi
if [ -n "${DB_NAME}" ]; then
if [[ -n ${DB_NAME} ]]; then
for db in $(awk -F',' '{for (i = 1 ; i <= NF ; i++) print $i}' <<< "${DB_NAME}"); do
echo "Creating database \"${db}\"..."
echo "CREATE DATABASE ${db};" | \
sudo -Hu ${PG_USER} ${PG_BINDIR}/postgres --single \
-D ${PG_DATADIR} -c config_file=${PG_CONFDIR}/postgresql.conf >/dev/null
if [ "${DB_UNACCENT}" == "true" ]; then
if [[ ${DB_UNACCENT} == true ]]; then
echo "Installing unaccent extension..."
echo "CREATE EXTENSION IF NOT EXISTS unaccent;" | \
sudo -Hu ${PG_USER} ${PG_BINDIR}/postgres --single ${db} \
-D ${PG_DATADIR} -c config_file=${PG_CONFDIR}/postgresql.conf >/dev/null
fi
if [ -n "${DB_USER}" ]; then
if [[ -n ${DB_USER} ]]; then
echo "Granting access to database \"${db}\" for user \"${DB_USER}\"..."
echo "GRANT ALL PRIVILEGES ON DATABASE ${db} to ${DB_USER};" |
sudo -Hu ${PG_USER} ${PG_BINDIR}/postgres --single \