Skip to content

Commit

Permalink
Fixed docker build
Browse files Browse the repository at this point in the history
  • Loading branch information
toluaina committed May 22, 2020
1 parent b499d8c commit 209026e
Show file tree
Hide file tree
Showing 16 changed files with 290 additions and 165 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ Dockerfile
# Coverage
**/.coverage.xml
**/coverage.xml

.env
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,10 @@ Changelog
* this release required you to re-bootstrap your database with
- bootstrap -t
- bootstrap


1.1.1 (2020-05-18)
------------------

* Fixed authentication with Redis
* Fixed Docker build
9 changes: 4 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ FROM python:3.7
ARG WORKDIR=/code
RUN mkdir $WORKDIR
ADD ./examples/ $WORKDIR/examples
ADD ./supervisor/ $WORKDIR/supervisor
WORKDIR $WORKDIR
RUN pip install pgsync
COPY supervisor/supervisord.conf /etc/supervisor/supervisord.conf
COPY supervisor/pgsync.conf /etc/supervisor/conf.d/
ENTRYPOINT ["/bin/sh", "supervisor/supervisord_entrypoint.sh"]
CMD ["-c", "/etc/supervisor/supervisord.conf"]
COPY ./docker/wait-for-it.sh wait-for-it.sh
COPY ./docker/runserver.sh runserver.sh
RUN chmod +x wait-for-it.sh
RUN chmod +x runserver.sh
8 changes: 8 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,11 @@ History

* bootstrap -t
* bootstrap


1.1.1 (2020-05-18)
------------------

* Fixed authentication with Redis
* Fixed Docker build

20 changes: 10 additions & 10 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# PostgreSQL to Elasticsearch sync


`PGSync <https://pgsync.com>`__ is a middleware for syncing data from `Postgres <https://www.postgresql.org>`__ to `Elasticsearch <https://www.elastic.co/products/elastic-stack>`__.
It allows you to keep `Postgres <https://www.postgresql.org>`__ as your source of truth data source and
expose structured denormalized documents in `Elasticsearch <https://www.elastic.co/products/elastic-stack>`__.
`PGSync <https://pgsync.com>`_ is a middleware for syncing data from `Postgres <https://www.postgresql.org>`_ to `Elasticsearch <https://www.elastic.co/products/elastic-stack>`_.
It allows you to keep `Postgres <https://www.postgresql.org>`_ as your source of truth data source and
expose structured denormalized documents in `Elasticsearch <https://www.elastic.co/products/elastic-stack>`_.


### Requirements

- `Python <https://www.python.org>`__ 3.6+
- `Postgres <https://www.postgresql.org>`__ 9.4+
- `Redis <https://redis.io>`__
- `Elasticsearch <https://www.elastic.co/products/elastic-stack>`__ 6.3.1+
- `Python <https://www.python.org>`_ 3.6+
- `Postgres <https://www.postgresql.org>`_ 9.4+
- `Redis <https://redis.io>`_
- `Elasticsearch <https://www.elastic.co/products/elastic-stack>`_ 6.3.1+

### Postgres setup

Enable `logical decoding <https://www.postgresql.org/docs/current/logicaldecoding.html>`__ in your
Enable `logical decoding <https://www.postgresql.org/docs/current/logicaldecoding.html>`_ in your
Postgres setting.

- you would also need to set up two parameters in your Postgres config postgresql.conf
Expand All @@ -26,15 +26,15 @@ expose structured denormalized documents in `Elasticsearch <https://www.elastic.

### Installation

You can install PGSync from `PyPI <https://pypi.org>`__:
You can install PGSync from `PyPI <https://pypi.org>`_:

$ pip install pgsync

### Config

Create a schema for the application named e.g **schema.json**

`Example schema <https://github.com/toluaina/pg-sync/blob/master/examples/airbnb/schema.json>`__
`Example schema <https://github.com/toluaina/pg-sync/blob/master/examples/airbnb/schema.json>`_

Example spec

Expand Down
12 changes: 4 additions & 8 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,13 @@ services:
- "15432:5432"
environment:
- POSTGRES_USER=pgsync
- POSTGRES_DB=airbnb
- POSTGRES_PASSWORD=PLEASE_REPLACE_ME
healthcheck:
test: ["CMD-SHELL", "pg_isready -U pgsync -d airbnb"]
interval: 10s
timeout: 5s
retries: 5
- POSTGRES_DB=airbnb
redis:
image: redis
command: redis-server --requirepass PLEASE_REPLACE_ME
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:7.6.2
image: docker.elastic.co/elasticsearch/elasticsearch:7.7.0
ports:
- "9201:9200"
- "9301:9300"
Expand All @@ -30,6 +25,7 @@ services:
build:
context: .
dockerfile: Dockerfile
command: ./runserver.sh
labels:
org.label-schema.name: "pgsync"
org.label-schema.description: "Postgres to elasticsearch sync"
Expand All @@ -42,9 +38,9 @@ services:
- PG_USER=pgsync
- PG_HOST=postgres
- PG_PASSWORD=PLEASE_REPLACE_ME
- LOG_LEVEL=INFO
- ELASTICSEARCH_PORT=9200
- ELASTICSEARCH_SCHEME=http
- ELASTICSEARCH_HOST=elasticsearch
- REDIS_HOST=redis
- REDIS_AUTH=PLEASE_REPLACE_ME

10 changes: 10 additions & 0 deletions docker/runserver.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#! /bin/sh
./wait-for-it.sh postgres:5432 -t 60
./wait-for-it.sh elasticsearch:9200 -t 60
./wait-for-it.sh redis:6379 -t 60

python examples/airbnb/schema.py --config examples/airbnb/schema.json
python examples/airbnb/data.py --config examples/airbnb/schema.json

bootstrap --config examples/airbnb/schema.json
pgsync --config examples/airbnb/schema.json --daemon
177 changes: 177 additions & 0 deletions docker/wait-for-it.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
#!/usr/bin/env bash
# Use this script to test if a given TCP host/port are available

cmdname=$(basename $0)

echoerr() { if [[ $QUIET -ne 1 ]]; then echo "$@" 1>&2; fi }

usage()
{
cat << USAGE >&2
Usage:
$cmdname host:port [-s] [-t timeout] [-- command args]
-h HOST | --host=HOST Host or IP under test
-p PORT | --port=PORT TCP port under test
Alternatively, you specify the host and port as host:port
-s | --strict Only execute subcommand if the test succeeds
-q | --quiet Don't output any status messages
-t TIMEOUT | --timeout=TIMEOUT
Timeout in seconds, zero for no timeout
-- COMMAND ARGS Execute command with args after the test finishes
USAGE
exit 1
}

wait_for()
{
if [[ $TIMEOUT -gt 0 ]]; then
echoerr "$cmdname: waiting $TIMEOUT seconds for $HOST:$PORT"
else
echoerr "$cmdname: waiting for $HOST:$PORT without a timeout"
fi
start_ts=$(date +%s)
while :
do
if [[ $ISBUSY -eq 1 ]]; then
nc -z $HOST $PORT
result=$?
else
(echo > /dev/tcp/$HOST/$PORT) >/dev/null 2>&1
result=$?
fi
if [[ $result -eq 0 ]]; then
end_ts=$(date +%s)
echoerr "$cmdname: $HOST:$PORT is available after $((end_ts - start_ts)) seconds"
break
fi
sleep 1
done
return $result
}

wait_for_wrapper()
{
# In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692
if [[ $QUIET -eq 1 ]]; then
timeout $BUSYTIMEFLAG $TIMEOUT $0 --quiet --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
else
timeout $BUSYTIMEFLAG $TIMEOUT $0 --child --host=$HOST --port=$PORT --timeout=$TIMEOUT &
fi
PID=$!
trap "kill -INT -$PID" INT
wait $PID
RESULT=$?
if [[ $RESULT -ne 0 ]]; then
echoerr "$cmdname: timeout occurred after waiting $TIMEOUT seconds for $HOST:$PORT"
fi
return $RESULT
}

# process arguments
while [[ $# -gt 0 ]]
do
case "$1" in
*:* )
hostport=(${1//:/ })
HOST=${hostport[0]}
PORT=${hostport[1]}
shift 1
;;
--child)
CHILD=1
shift 1
;;
-q | --quiet)
QUIET=1
shift 1
;;
-s | --strict)
STRICT=1
shift 1
;;
-h)
HOST="$2"
if [[ $HOST == "" ]]; then break; fi
shift 2
;;
--host=*)
HOST="${1#*=}"
shift 1
;;
-p)
PORT="$2"
if [[ $PORT == "" ]]; then break; fi
shift 2
;;
--port=*)
PORT="${1#*=}"
shift 1
;;
-t)
TIMEOUT="$2"
if [[ $TIMEOUT == "" ]]; then break; fi
shift 2
;;
--timeout=*)
TIMEOUT="${1#*=}"
shift 1
;;
--)
shift
CLI=("$@")
break
;;
--help)
usage
;;
*)
echoerr "Unknown argument: $1"
usage
;;
esac
done

if [[ "$HOST" == "" || "$PORT" == "" ]]; then
echoerr "Error: you need to provide a host and port to test."
usage
fi

TIMEOUT=${TIMEOUT:-15}
STRICT=${STRICT:-0}
CHILD=${CHILD:-0}
QUIET=${QUIET:-0}

# check to see if timeout is from busybox?
# check to see if timeout is from busybox?
TIMEOUT_PATH=$(realpath $(which timeout))
if [[ $TIMEOUT_PATH =~ "busybox" ]]; then
ISBUSY=1
BUSYTIMEFLAG="-t"
else
ISBUSY=0
BUSYTIMEFLAG=""
fi

if [[ $CHILD -gt 0 ]]; then
wait_for
RESULT=$?
exit $RESULT
else
if [[ $TIMEOUT -gt 0 ]]; then
wait_for_wrapper
RESULT=$?
else
wait_for
RESULT=$?
fi
fi

if [[ $CLI != "" ]]; then
if [[ $RESULT -ne 0 && $STRICT -eq 1 ]]; then
echoerr "$cmdname: strict mode, refusing to execute subprocess"
exit $RESULT
fi
exec "${CLI[@]}"
else
exit $RESULT
fi
2 changes: 1 addition & 1 deletion pgsync/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

__author__ = 'Tolu Aina'
__email__ = 'tolu@pgsync.com'
__version__ = '1.1.0'
__version__ = '1.1.1'
Loading

0 comments on commit 209026e

Please sign in to comment.