Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
csandanov committed May 12, 2017
1 parent 38ae291 commit 6c00a91
Show file tree
Hide file tree
Showing 48 changed files with 4,678 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
35 changes: 35 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
language: python

services:
- docker

env:
global:
- LATEST_VERSION=9.6
matrix:
- VERSION=9.6
- VERSION=9.5
- VERSION=9.4
- VERSION=9.3
- VERSION=9.2

script:
- cd ./$VERSION
- make && make test

after_success: |
if [ "$TRAVIS_PULL_REQUEST" == "false" ] && ([ "$TRAVIS_BRANCH" == "master" ] || [ -n "$TRAVIS_TAG" ]); then
docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD
make release
if [ -n "$TRAVIS_TAG" ]; then
make VERSION="$VERSION-$TRAVIS_TAG" release
fi
if [ "$VERSION" == "$LATEST_VERSION" ]; then
make VERSION=latest release
fi
fi
after_failure:
- make logs
21 changes: 21 additions & 0 deletions 9.2/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM postgres:9.2-alpine

ENV GOTPL_VER 0.1.5
ENV POSTGRES_USER postgres

RUN apk add --no-cache \
ca-certificates \
make \
pwgen \
tar \
wget && \

wget -qO- https://github.com/wodby/gotpl/releases/download/${GOTPL_VER}/gotpl-alpine-linux-amd64-${GOTPL_VER}.tar.gz | tar xz -C /usr/local/bin

COPY actions /usr/local/bin

COPY *.tpl /etc/gotpl/
COPY docker-entrypoint.sh /

ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["-c", "config_file=/etc/postgresql/postgresql.conf"]
40 changes: 40 additions & 0 deletions 9.2/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
-include env.mk

VERSION ?= 9.2

REPO = wodby/postgres
NAME = postgres-9.2

.PHONY: build test push shell run start stop logs clean release

build:
docker build -t $(REPO):$(VERSION) ./

test:
NAME=$(NAME) IMAGE=$(REPO):$(VERSION) ./test.sh

push:
docker push $(REPO):$(VERSION)

shell:
docker run --rm --name $(NAME) -i -t $(PORTS) $(VOLUMES) $(ENV) $(REPO):$(VERSION) /bin/bash

run:
docker run --rm --name $(NAME) $(LINKS) $(PORTS) $(VOLUMES) $(ENV) $(REPO):$(VERSION) $(CMD)

start:
docker run -d --name $(NAME) $(PORTS) $(VOLUMES) $(ENV) $(REPO):$(VERSION)

stop:
docker stop $(NAME)

logs:
docker logs $(NAME)

clean:
-docker rm -f $(NAME)

release: build
make push -e VERSION=$(VERSION)

default: build
41 changes: 41 additions & 0 deletions 9.2/actions/actions.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
.PHONY: import backup query query-silent query-root check-ready check-live

check_defined = \
$(strip $(foreach 1,$1, \
$(call __check_defined,$1,$(strip $(value 2)))))
__check_defined = \
$(if $(value $1),, \
$(error Required parameter is missing: $1$(if $2, ($2))))

user ?= $(POSTGRES_USER)
password ?= $(POSTGRES_PASSWORD)
db ?= $(POSTGRES_DB)
host ?= localhost
max_try ?= 1
wait_seconds ?= 1
binary ?= 0
ignore ?= ""

default: query

import:
$(call check_defined, source)
import.sh $(user) $(password) $(host) $(db) $(source) $(binary)

backup:
$(call check_defined, filepath)
backup.sh $(user) $(password) $(host) $(db) $(filepath) "$(ignore)"

query:
$(call check_defined, query)
PGPASSWORD=$(password) psql -U$(user) -h$(host) -d$(db) -c "$(query)"

query-silent:
$(call check_defined, query)
@PGPASSWORD=$(password) psql -tA -U$(user) -h$(host) -d$(db) -c "$(query)"

check-ready:
wait-for-postgres.sh $(user) $(password) $(db) $(host) $(max_try) $(wait_seconds)

check-live:
@echo "OK"
29 changes: 29 additions & 0 deletions 9.2/actions/backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash

set -e

if [[ ! -z "${DEBUG}" ]]; then
set -x
fi

user=$1
password=$2
host=$3
db=$4
filepath=$5
exclude_tables=$6
filename="dump.sql"
tmp_dir="/tmp/$RANDOM"
ignore=()

IFS=';' read -ra ADDR <<< "${exclude_tables}"
for table in "${ADDR[@]}"; do
ignore+=("--exclude-table-data=${table}")
done

mkdir -p "${tmp_dir}"
cd "${tmp_dir}"
PGPASSWORD="${password}" pg_dump "${ignore[@]}" -U"${user}" -h"${host}" "${db}" > "${filename}"
gzip "${filename}"
mv "${filename}.gz" "${filepath}"
stat -c "RESULT=%s" "${filepath}"
59 changes: 59 additions & 0 deletions 9.2/actions/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env bash

set -e

if [[ ! -z "${DEBUG}" ]]; then
set -x
fi

user=$1
password=$2
host=$3
db=$4
source=$5
binary=$6
tmp_dir="/tmp/import"

[ -d "${tmp_dir}" ] && rm -rf "${tmp_dir}"
mkdir -p "${tmp_dir}"
cd "${tmp_dir}"

if [[ "${source}" =~ ^https?:// ]]; then
wget -q "${source}"
else
mv "${source}" .
fi

archive_file=$(find -type f)

if [[ "${archive_file}" =~ \.zip$ ]]; then
unzip "${archive_file}"
rm -f "${archive_file}"
elif [[ "${archive_file}" =~ \.tgz$ ]] || [[ "${archive_file}" =~ \.tar.gz$ ]]; then
tar -zxf "${archive_file}"
rm -f "${archive_file}"
elif [[ "${archive_file}" =~ \.gz$ ]]; then
gunzip "${archive_file}"
else
echo >&2 'Unsupported file format. Expecting single file compressed as .gz .zip .tar.gz .tgz'
exit 1
fi

if [ "$(find -type f | wc -l)" != "1" ]; then
echo >&2 "Expecting single .sql file, none or multiple found: $(find -type f)"
exit 1
fi

dump_file=$(find -type f)

PGPASSWORD="${password}" dropdb -U"${user}" -h"${host}" "${db}"
PGPASSWORD="${password}" createdb -U"${user}" -h"${host}" "${db}"

if [[ "${binary}" == 1 ]]; then
PGPASSWORD="${password}" pg_restore -U"${user}" -h"${host}" -d"${db}" "${dump_file}"
else
PGPASSWORD="${password}" psql -U"${user}" -h"${host}" -d"${db}" -f "${dump_file}"
fi


rm -rf "${tmp_dir}"
31 changes: 31 additions & 0 deletions 9.2/actions/wait-for-postgres.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash

set -e

if [[ ! -z "${DEBUG}" ]]; then
set -x
fi

started=0
user=$1
password=$2
db=$3
host=$4
max_try=$5
wait_seconds=$6

for i in $(seq 1 "${max_try}"); do
if PGPASSWORD="${password}" psql -U"${user}" -h"${host}" -d"${db}" -c 'SELECT 1' &> /dev/null; then
started=1
break
fi
echo 'PostgreSQL is starting...'
sleep "${wait_seconds}"
done

if [[ "${started}" -eq '0' ]]; then
echo >&2 'Error. PostgreSQL is unreachable.'
exit 1
fi

echo 'PostgreSQL has started!'
24 changes: 24 additions & 0 deletions 9.2/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

set -e

if [[ -n "${DEBUG}" ]]; then
set -x
fi

if [[ -z "${POSTGRES_PASSWORD}" ]]; then
exit 1
fi

if [[ -z "${POSTGRES_DB}" ]]; then
export POSTGRES_DB="${POSTGRES_USER}"
fi

mkdir -p /etc/postgresql/
gotpl "/etc/gotpl/postgresql.conf.tpl" > "/etc/postgresql/postgresql.conf"

if [[ $1 == 'make' ]]; then
exec "${@}" -f /usr/local/bin/actions.mk
else
exec /usr/local/bin/docker-entrypoint.sh "${@}"
fi
Loading

0 comments on commit 6c00a91

Please sign in to comment.