Skip to content

Commit 2e39f6e

Browse files
authored
Merge pull request #4 from duanhongyi/master
feat(postgres): use docker's official postgres:11 image
2 parents 263f50e + 247bc5c commit 2e39f6e

File tree

8 files changed

+110
-66
lines changed

8 files changed

+110
-66
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ sudo: required
22
language: go
33
services:
44
- docker
5-
script: make test
5+
script: make docker-build && make test
66
notifications:
77
slack:
88
secure: pWDCV3od8gxvzxh9DrOTvBL54XoCfWYhZZlwd2ZbyyOz6SS12Psg/ZuCT2253p4yMfF/LPlsz76mr7NgcCrMI0ReveTa/rnt3XBZtyY+1rlsQsy2oxgdAzbO587ENCQeMw2F/OWHaixMT8NDqxEqQd6xafK9Zmg6BeBjwgs7XfXKcR3WzNIuCO0ZG05+Yd0FIxmd/8Xm5tGiFEYr05+Ix6MLdF9MSCXZUPeu1EsYXhDljokLq49w63W1UMU10tm4t7VCEdaO+X9w6EJ5Ov8HDxb6L6IviUYY6+IGTZ01nwIoM6OrGQqfEAytYqgTKdehgQzQnAbLI6TW2wJ0twqEsLrlbTa4NW4j0KkazQJkN5kqcKYQvaeKJJhvJIG44Gi/u78pW3S6W7NU5DhrlE6bbxdIBHJW1vJBimkqu2oBNrO5ZoBB9MS9zflBsU5g/pQpVeHWMnWE8fcYDGa1PqAcr7q6wtdPsrVZhnHmmARN3PwZzIVVVsXbaIQG8VLC5grLGnwMf1Y1fz2nK3sVpCftvrYZT3G6CNAASo+eLOwYdZdiJ9jIS7WNLN1GtpIEvjeDt3QRqsDyH8YoAKUvY5h/v8IWPP/BaSwQbJwep4+Dj7xkpXX5/4wm4jEnVFV1p4xE0lD1AXvEMAVHtPhhggvscNhF9j6oeoPju6eTPcxG+5o=

Dockerfile

+27-59
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,47 @@
1-
FROM quay.io/deis/base:v0.3.6
1+
FROM postgres:11
22

3-
ENV LANG=en_US.utf8 \
4-
PG_MAJOR=9.4 \
5-
PG_VERSION=9.4.17-1.pgdg16.04+1 \
6-
PGDATA=/var/lib/postgresql/data
3+
ARG DEBIAN_FRONTEND=noninteractive
4+
ARG BUILD_DEPS='gcc git libffi-dev libssl-dev python3-dev python3-pip python3-wheel'
75

8-
# Set this separately from those above since it depends on one of them
9-
ENV PATH=/usr/lib/postgresql/$PG_MAJOR/bin:$PATH
10-
11-
# Add postgres user and group
12-
RUN adduser --system \
13-
--shell /bin/bash \
14-
--disabled-password \
15-
--group \
16-
postgres
17-
18-
RUN buildDeps='gcc git libffi-dev libssl-dev python3-dev python3-pip python3-wheel' && \
19-
localedef -i en_US -c -f UTF-8 -A /etc/locale.alias en_US.UTF-8 && \
20-
export DEBIAN_FRONTEND=noninteractive && \
21-
apt-key adv --keyserver ha.pool.sks-keyservers.net --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8 && \
22-
echo 'deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main' $PG_MAJOR > /etc/apt/sources.list.d/pgdg.list && \
23-
apt-get update && \
6+
RUN apt-get update && \
247
apt-get install -y --no-install-recommends \
25-
$buildDeps \
8+
$BUILD_DEPS \
269
gosu \
2710
lzop \
28-
postgresql-$PG_MAJOR=$PG_VERSION \
29-
postgresql-contrib-$PG_MAJOR=$PG_VERSION \
11+
libpq-dev \
3012
pv \
3113
python3 \
32-
postgresql-common \
3314
util-linux \
3415
# swift package needs pkg_resources and setuptools
3516
python3-pkg-resources \
36-
python3-setuptools && \
17+
python3-setuptools \
18+
python3-pip && \
3719
ln -sf /usr/bin/python3 /usr/bin/python && \
38-
ln -sf /usr/bin/pip3 /usr/bin/pip && \
39-
mkdir -p /run/postgresql && \
40-
chown -R postgres /run/postgresql && \
41-
# setuptools from ubuntu archives is too old for googleapis-common-protos
42-
pip install --upgrade setuptools && \
20+
ln -sf /usr/bin/pip3 /usr/bin/pip
21+
22+
# setuptools from ubuntu archives is too old for googleapis-common-protos
23+
RUN pip install --upgrade setuptools && \
4324
pip install --disable-pip-version-check --no-cache-dir \
44-
envdir==0.7 \
45-
wal-e[aws,azure,google,swift]==v1.0.2 \
46-
# pin azure-storage to version wal-e uses (see docker-entrypoint.sh)
47-
azure-storage==0.20.0 && \
48-
# "upgrade" boto to 2.43.0 + the patch to fix minio connections
49-
pip install --disable-pip-version-check --no-cache-dir --upgrade git+https://github.com/teamhephy/boto@88c980e56d1053892eb940d43a15a68af4ebb5e6 && \
50-
# cleanup
51-
apt-get purge -y --auto-remove $buildDeps && \
25+
envdir==1.0.1 \
26+
wal-e[aws,azure,google,swift]==1.1.0 \
27+
gcloud==0.18.3 \
28+
oauth2client==4.1.3 \
29+
azure-storage==0.20.0
30+
31+
# cleanup
32+
RUN apt-get purge -y --auto-remove $BUILD_DEPS && \
5233
apt-get autoremove -y && \
53-
apt-get clean -y && \
54-
# package up license files if any by appending to existing tar
55-
COPYRIGHT_TAR='/usr/share/copyrights.tar' && \
56-
gunzip -f $COPYRIGHT_TAR.gz && \
57-
tar -rf $COPYRIGHT_TAR /usr/share/doc/*/copyright && \
58-
gzip $COPYRIGHT_TAR && \
59-
rm -rf \
60-
/usr/share/doc \
61-
/usr/share/man \
62-
/usr/share/info \
63-
/usr/share/locale \
64-
/var/lib/apt/lists/* \
65-
/var/log/* \
66-
/var/cache/debconf/* \
67-
/etc/systemd \
68-
/lib/lsb \
69-
/lib/udev \
70-
/usr/lib/x86_64-linux-gnu/gconv/IBM* \
71-
/usr/lib/x86_64-linux-gnu/gconv/EBC* && \
72-
bash -c "mkdir -p /usr/share/man/man{1..8}"
34+
apt-get clean -y
7335

7436
COPY rootfs /
7537
ENV WALE_ENVDIR=/etc/wal-e.d/env
7638
RUN mkdir -p $WALE_ENVDIR
7739

40+
ARG PATCH_CMD="python3 /patcher-script.py"
41+
RUN $PATCH_CMD file /bin/create_bucket /patcher-script.d/patch_boto_s3.py
42+
RUN $PATCH_CMD file /usr/local/bin/wal-e /patcher-script.d/patch_boto_s3.py
43+
RUN $PATCH_CMD module wal_e.worker.worker_util /patcher-script.d/patch_wal_e_s3.py
44+
45+
7846
CMD ["/docker-entrypoint.sh", "postgres"]
7947
EXPOSE 5432

rootfs/bin/create_bucket

+6-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def bucket_exists(conn, name):
2020
return True
2121

2222
bucket_name = os.getenv('BUCKET_NAME')
23-
region = os.getenv('AWS_REGION')
23+
region = os.getenv('S3_REGION')
2424

2525
if os.getenv('DATABASE_STORAGE') == "s3":
2626
conn = boto.s3.connect_to_region(region)
@@ -76,15 +76,17 @@ elif os.getenv('DATABASE_STORAGE') == "swift":
7676
conn.put_container(os.getenv('BUCKET_NAME'))
7777

7878
else:
79-
botoconfig.add_section('s3')
79+
if not botoconfig.has_section("s3"):
80+
botoconfig.add_section('s3')
8081
botoconfig.set('s3', 'use-sigv4', 'True')
81-
botoconfig.add_section('Boto')
82+
if not botoconfig.has_section("Boto"):
83+
botoconfig.add_section('Boto')
8284
botoconfig.set('Boto', 'is_secure', 'False')
8385
conn = S3Connection(
8486
host=os.getenv('S3_HOST'),
8587
port=int(os.getenv('S3_PORT')),
8688
calling_format=OrdinaryCallingFormat())
8789
# HACK(bacongobbler): allow boto to connect to minio by changing the region name for s3v4 auth
88-
conn.auth_region_name = os.getenv('AWS_REGION')
90+
conn.auth_region_name = os.getenv('S3_REGION')
8991
if not bucket_exists(conn, bucket_name):
9092
conn.create_bucket(bucket_name)

rootfs/docker-entrypoint-initdb.d/001_setup_envdir.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ if [[ "$DATABASE_STORAGE" == "s3" || "$DATABASE_STORAGE" == "minio" ]]; then
3636
else
3737
echo "1" > AWS_INSTANCE_PROFILE
3838
fi
39-
echo $AWS_REGION > AWS_REGION
39+
echo $AWS_REGION > S3_REGION
4040
echo $BUCKET_NAME > BUCKET_NAME
4141
elif [ "$DATABASE_STORAGE" == "gcs" ]; then
4242
GOOGLE_APPLICATION_CREDENTIALS="/var/run/secrets/deis/objectstore/creds/key.json"

rootfs/docker-entrypoint-initdb.d/003_restore_from_backup.sh

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ gosu postgres pg_ctl -D "$PGDATA" -w restart
2020
if [[ $(envdir "$WALE_ENVDIR" wal-e --terse backup-list | wc -l) -gt "1" ]]; then
2121
echo "Found backups. Restoring from backup..."
2222
gosu postgres pg_ctl -D "$PGDATA" -w stop
23-
rm -rf "$PGDATA"
23+
rm -rf "$PGDATA/*"
2424
envdir "$WALE_ENVDIR" wal-e backup-fetch "$PGDATA" LATEST
2525
cat << EOF > "$PGDATA/postgresql.conf"
2626
# These settings are initialized by initdb, but they can be changed.
@@ -53,6 +53,12 @@ EOF
5353
gosu postgres pg_ctl -D "$PGDATA" \
5454
-o "-c listen_addresses=''" \
5555
-w start
56+
57+
echo "Waiting for recovery completion..."
58+
while [ ! -f "$PGDATA/recovery.done" ]
59+
do
60+
sleep 2
61+
done
5662
fi
5763

5864
echo "Performing an initial backup..."
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def patch_boto_s3_hmac_auth_v4_handler():
2+
import os
3+
from boto.auth import HmacAuthV4Handler
4+
_init = HmacAuthV4Handler.__init__
5+
def wrap_init(self, *args, **kwargs):
6+
_init(self, *args, **kwargs)
7+
self.region_name = os.getenv('S3_REGION', self.region_name)
8+
HmacAuthV4Handler.__init__ = wrap_init
9+
patch_boto_s3_hmac_auth_v4_handler()
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def patch_uri_put_file():
2+
import os
3+
from wal_e.blobstore import s3
4+
from wal_e.blobstore.s3 import s3_util
5+
def wrap_uri_put_file(creds, uri, fp, content_type=None, conn=None):
6+
assert fp.tell() == 0
7+
k = s3_util._uri_to_key(creds, uri, conn=conn)
8+
if content_type is not None:
9+
k.content_type = content_type
10+
if os.getenv('DATABASE_STORAGE') == 's3':
11+
encrypt_key=True
12+
else:
13+
encrypt_key=False
14+
k.set_contents_from_file(fp, encrypt_key=encrypt_key)
15+
return k
16+
s3.uri_put_file = wrap_uri_put_file
17+
s3_util.uri_put_file = wrap_uri_put_file
18+
patch_uri_put_file()

rootfs/patcher-script.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import sys
2+
3+
patch_script_tmp = """
4+
5+
def run_patch_scripts(patch_script_path):
6+
with open(patch_script_path, 'r') as f:
7+
try:
8+
exec(f.read())
9+
except:
10+
pass
11+
run_patch_scripts("%s")
12+
13+
"""
14+
15+
16+
def main(patch_file, patch_script_file):
17+
result_list = []
18+
patch_script = patch_script_tmp % patch_script_file
19+
with open(patch_file, "r") as f:
20+
has_patched = False
21+
for line in f:
22+
if (line.startswith('import') or line.startswith('from')) \
23+
and not has_patched:
24+
result_list.append(patch_script)
25+
has_patched = True
26+
result_list.append(line)
27+
if not has_patched: result_list.append(patch_script)
28+
with open(patch_file, "w") as f:
29+
for line in result_list:
30+
f.write(line)
31+
32+
if __name__ == '__main__':
33+
patch_type = sys.argv[1]
34+
if patch_type == 'file':
35+
patch_file = sys.argv[2]
36+
elif patch_type == 'module':
37+
module = __import__(sys.argv[2], fromlist=True)
38+
patch_file = module.__file__
39+
patch_script_file = sys.argv[3]
40+
main(patch_file, patch_script_file)
41+

0 commit comments

Comments
 (0)