diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..ab5aa77 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,27 @@ +sudo: required + +language: python + +services: + - docker + +virtualenv: + system_site_packages: true + +python: + - '2.7' + +script: + - ./build.sh + - pushd sample/replication + - make up + - make status + # Check for database status + - until make check-master-running; do echo "Retrying"; sleep 5; done + - until make check-slave-running; do echo "Retrying"; sleep 5; done + + # Check replications + - until make check-master-replication; do echo "Retrying"; make master-log-tail; sleep 5; done + - sleep 60 # Wait replication finished + - until make check-slave-replication; do echo "Retrying"; make slave-log-tail; sleep 5; done + - popd diff --git a/sample/replication/Makefile b/sample/replication/Makefile index f0f902b..7086aa8 100644 --- a/sample/replication/Makefile +++ b/sample/replication/Makefile @@ -24,3 +24,27 @@ master-log: slave-log: docker-compose logs -f --tail=30 pg-slave + + +# These commands were used for travis + +master-log-tail : + docker-compose logs --tail=30 pg-master + +slave-log-tail: + docker-compose logs --tail=30 pg-slave + +check-master-running: + @echo "Check master is running" + @docker-compose logs --tail=1 pg-master | grep 'database system is ready to accept connections' &> /dev/null + +check-master-replication: + @docker-compose exec pg-master /bin/sh -c "su - postgres -c \"/tests/replication_test_master.sh\"" + +check-slave-running: + @echo "Check slave is running" + @docker-compose logs pg-slave | grep 'database system is ready to accept read only connections' &> /dev/null + @docker-compose logs pg-slave | grep 'started streaming WAL from primary' &> /dev/null + +check-slave-replication: + @docker-compose exec pg-slave /bin/sh -c "su - postgres -c \"/tests/replication_test_slave.sh\"" diff --git a/sample/replication/docker-compose.yml b/sample/replication/docker-compose.yml index 9c6ec04..f7b6c64 100644 --- a/sample/replication/docker-compose.yml +++ b/sample/replication/docker-compose.yml @@ -9,6 +9,7 @@ services: # observe how the slave will behave after restarts. volumes: - './pg-master:/var/lib/postgresql' + - './tests:/tests' environment: # ALLOW_IP_RANGE option is used to specify additionals allowed domains # in pg_hba. @@ -31,6 +32,7 @@ services: # runtime volumes: - './pg-slave:/var/lib/postgresql' + - './tests:/tests' environment: # ALLOW_IP_RANGE option is used to specify additionals allowed domains # in pg_hba. diff --git a/sample/replication/tests/replication_test_master.sh b/sample/replication/tests/replication_test_master.sh new file mode 100755 index 0000000..da24671 --- /dev/null +++ b/sample/replication/tests/replication_test_master.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +. /env-data.sh + +set -e + +echo "Check master replication" + +# Create a new table + +echo "Create new table" +psql -d ${POSTGRES_DBNAME} -c "CREATE TABLE test_replication_table ();" + +# Check table exists in master + +echo "Check table exists" +psql -d ${POSTGRES_DBNAME} -c "\dt" | grep test_replication_table + +exit $? diff --git a/sample/replication/tests/replication_test_slave.sh b/sample/replication/tests/replication_test_slave.sh new file mode 100755 index 0000000..57e41de --- /dev/null +++ b/sample/replication/tests/replication_test_slave.sh @@ -0,0 +1,14 @@ +#!/bin/sh + +. /env-data.sh + +set -e + +echo "Check slave replication" + +# Check table exists in slave + +echo "Check table exists" +psql -d ${POSTGRES_DBNAME} -c "\dt" | grep test_replication_table + +exit $? diff --git a/setup-replication.sh b/setup-replication.sh index 463fa51..e45d6ed 100755 --- a/setup-replication.sh +++ b/setup-replication.sh @@ -25,6 +25,7 @@ done if [[ "$DESTROY_DATABASE_ON_RESTART" =~ [Tt][Rr][Uu][Ee] ]]; then echo "Get initial database from master" + chown -R postgres:postgres $(getent passwd postgres | cut -d: -f6) su - postgres -c "echo \"${REPLICATE_FROM}:${REPLICATE_PORT}:*:${POSTGRES_USER}:${POSTGRES_PASS}\" > ~/.pgpass" su - postgres -c "chmod 0600 ~/.pgpass" @@ -32,6 +33,10 @@ if [[ "$DESTROY_DATABASE_ON_RESTART" =~ [Tt][Rr][Uu][Ee] ]]; then do echo "Waiting for master to connect..." sleep 1s + if [ "$(ls -A $DATADIR)" ]; then + echo "Need empty folder. Cleaning directory..." + rm -rf $DATADIR/* + fi done fi