Created
October 2, 2014 15:46
-
-
Save bartmeuris/f63eda72ace618a47030 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
function waitfor { | |
SEC=$1 | |
MSG=$2 | |
while [ $SEC -gt 0 ]; do | |
printf "\r$MSG $SEC \r" | |
sleep 1 | |
SEC=$((SEC - 1)) | |
done | |
printf "$MSG Done. \n" | |
} | |
DATABASE_USER="testuser" | |
DATABASE_NAME="testdb" | |
DATABASE_PASSWORD="test123" | |
docker kill postgres-test >/dev/null 2>/dev/null || true | |
docker rm postgres-test 2>/dev/null >/dev/null || true | |
echo "Starting postgres server..." | |
docker run -d --name postgres-test postgres 2>/dev/null >/dev/null | |
waitfor 15 "Waiting for postgres to become ready:" | |
echo "Executing SQL..." | |
ENV="-e DATABASE_USER='$DATABASE_USER' -e DATABASE_NAME='$DATABASE_NAME' -e DATABASE_PASSWORD='$DATABASE_PASSWORD'" | |
BASE_CMD="exec psql -h \"\$POSTGRES_PORT_5432_TCP_ADDR\" -p \"\$POSTGRES_PORT_5432_TCP_PORT\" -U postgres" | |
SQL="DROP USER IF EXISTS \"${DATABASE_USER}\"; CREATE USER \"${DATABASE_USER}\" WITH PASSWORD '\''${DATABASE_PASSWORD}'\'';" | |
CMD="$BASE_CMD -c '$SQL'" | |
docker run -t -i --rm $ENV --link postgres-test:postgres postgres sh -c "$CMD" | |
SQL="CREATE DATABASE \"${DATABASE_NAME}\" WITH OWNER \"${DATABASE_USER}\" ENCODING = '\''UTF8'\'';" | |
CMD="$BASE_CMD -c '$SQL'" | |
docker run -t -i --rm $ENV --link postgres-test:postgres postgres sh -c "$CMD" | |
SQL="GRANT ALL PRIVILEGES ON DATABASE \"${DATABASE_NAME}\" TO \"${DATABASE_USER}\";" | |
CMD="$BASE_CMD -c '$SQL'" | |
docker run -t -i --rm $ENV --link postgres-test:postgres postgres sh -c "$CMD" | |
if [ $? -ne 0 ]; then | |
echo "Executing SQL failed :(" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment