Skip to content

Commit 81686ce

Browse files
committed
Initial import
1 parent 9c1c786 commit 81686ce

File tree

6 files changed

+260
-0
lines changed

6 files changed

+260
-0
lines changed

Dockerfile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
FROM registry.cloudogu.com/official/base:3.6-1
2+
MAINTAINER Sebastian Sdorra <[email protected]>
3+
4+
# install postgresql and gosu
5+
# Note: the current postgresql version from alpine is installed
6+
# https://pkgs.alpinelinux.org/packages?name=postgresql&branch=v3.6&repo=&arch=x86_64
7+
RUN apk add --update postgresql \
8+
&& curl -o /usr/local/bin/gosu -sSL "https://github.com/tianon/gosu/releases/download/1.2/gosu-amd64" \
9+
&& chmod +x /usr/local/bin/gosu \
10+
&& rm -rf /var/cache/apk/*
11+
12+
ENV LANG en_US.utf8
13+
ENV PGDATA /var/lib/postgresql
14+
15+
COPY resources/ /
16+
17+
# VOLUMES
18+
VOLUME "/var/lib/postgresql"
19+
20+
# MYSQL PORT
21+
EXPOSE 5432
22+
23+
# FIRE IT UP
24+
CMD ["/bin/bash", "/startup.sh"]

dogu.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"Name": "official/postgresql",
3+
"Version": "9.6.5-1",
4+
"DisplayName": "PostgreSQL",
5+
"Description": "PostgreSQL Database.",
6+
"Url": "https://www.postgresql.org/",
7+
"Category": "Base",
8+
"Tags": [
9+
"database",
10+
"db"
11+
],
12+
"Logo": "https://cloudogu.com/images/dogus/postgresql.png",
13+
"Image": "registry.cloudogu.com/official/postgresql",
14+
"Volumes": [
15+
{
16+
"Name": "data",
17+
"Path": "/var/lib/postgresql",
18+
"Owner": "1000",
19+
"Group": "1000"
20+
}
21+
],
22+
"ExposedCommands": [
23+
{
24+
"Name": "service-account-create",
25+
"Description": "Creates a new service account",
26+
"Command": "/create-sa.sh"
27+
},{
28+
"Name": "pre-upgrade",
29+
"Command": "/pre-upgrade.sh"
30+
}
31+
],
32+
"HealthChecks": [
33+
{
34+
"Type": "tcp",
35+
"Port": 5432
36+
},
37+
{
38+
"Type": "state"
39+
}
40+
]
41+
}

resources/create-sa.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
set -o errexit
3+
set -o nounset
4+
set -o pipefail
5+
6+
{
7+
SERVICE="$1"
8+
if [ X"${SERVICE}" = X"" ]; then
9+
echo "usage create-sa.sh servicename"
10+
exit 1
11+
fi
12+
13+
# create random schema suffix and password
14+
ID=$(doguctl random -l 6 | tr '[:upper:]' '[:lower:]')
15+
USER="${SERVICE}_${ID}"
16+
PASSWORD=$(doguctl random)
17+
DATABASE="${USER}"
18+
SCHEMA="${USER}"
19+
20+
# connection user
21+
ADMIN_USERNAME=$(doguctl config user)
22+
23+
# create role
24+
psql -U "${ADMIN_USERNAME}" -c "CREATE USER ${USER} WITH PASSWORD '${PASSWORD}';"
25+
26+
# create database
27+
psql -U "${ADMIN_USERNAME}" -c "CREATE DATABASE ${DATABASE} OWNER ${USER};"
28+
29+
} >/dev/null 2>&1
30+
31+
# print details
32+
echo "database: ${DATABASE}"
33+
echo "username: ${USER}"
34+
echo "password: ${PASSWORD}"

resources/pre-upgrade.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
set -o errexit
3+
set -o nounset
4+
set -o pipefail
5+
6+
pg_dumpall -U postgres -f "${PGDATA}"/postgresqlFullBackup.dump

resources/startup.sh

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#!/bin/bash
2+
set -o errexit
3+
set -o nounset
4+
set -o pipefail
5+
6+
function mask2cidr() {
7+
NBITS=0
8+
IFS=.
9+
for DEC in $1 ; do
10+
case $DEC in
11+
255) let NBITS+=8;;
12+
254) let NBITS+=7 ; break ;;
13+
252) let NBITS+=6 ; break ;;
14+
248) let NBITS+=5 ; break ;;
15+
240) let NBITS+=4 ; break ;;
16+
224) let NBITS+=3 ; break ;;
17+
192) let NBITS+=2 ; break ;;
18+
128) let NBITS+=1 ; break ;;
19+
0);;
20+
*) echo "Error: ${DEC} is not recognised"; exit 1
21+
esac
22+
done
23+
echo "${NBITS}"
24+
}
25+
26+
function create_hba() {
27+
echo '# generated, do not override'
28+
echo '# "local" is for Unix domain socket connections only'
29+
echo 'local all all trust'
30+
echo '# IPv4 local connections:'
31+
echo 'host all all 127.0.0.1/32 trust'
32+
echo '# IPv6 local connections:'
33+
echo 'host all all ::1/128 trust'
34+
echo '# container networks'
35+
for NETWITHMASK in $(netstat -nr | tail -n +3 | grep -v '^0' | awk '{print $1"/"$3}'); do
36+
NET=$(echo "${NETWITHMASK}" | awk -F'/' '{print $1}')
37+
MASK=$(echo "${NETWITHMASK}" | awk -F'/' '{print $2}')
38+
CIDR=$(mask2cidr "$MASK")
39+
echo "host all all ${NET}/${CIDR} password"
40+
done
41+
}
42+
43+
function initializePostgreSQL() {
44+
45+
# set stage for health check
46+
doguctl state installing
47+
48+
# install database
49+
gosu postgres initdb
50+
51+
# postgres user
52+
POSTGRES_USER="postgres"
53+
54+
# store the user
55+
doguctl config user "${POSTGRES_USER}"
56+
57+
# create random password
58+
POSTGRES_PASSWORD=$(doguctl random)
59+
60+
# store the password encrypted
61+
doguctl config -e password "${POSTGRES_PASSWORD}"
62+
63+
# open port
64+
sed -ri "s/^#(listen_addresses\s*=\s*)\S+/\1'*'/" "$PGDATA"/postgresql.conf
65+
66+
# set generated password
67+
echo "ALTER USER ${POSTGRES_USER} WITH SUPERUSER PASSWORD '${POSTGRES_PASSWORD}';" | 2>/dev/null 1>&2 gosu postgres postgres --single -jE
68+
69+
# create /run/postgresql
70+
mkdir -p /run/postgresql
71+
chown postgres:postgres /run/postgresql
72+
73+
# generate pg_hba.conf
74+
create_hba > "${PGDATA}"/pg_hba.conf
75+
}
76+
77+
function waitForPostgreSQLStartup() {
78+
while ! pg_isready > /dev/null; do
79+
# Postgres is not ready yet to accept connections
80+
sleep 0.1
81+
done
82+
}
83+
84+
function waitForPostgreSQLShutdown() {
85+
while pgrep -x postgres > /dev/null ; do
86+
# Postgres is still running
87+
sleep 0.1
88+
done
89+
}
90+
91+
chown -R postgres "$PGDATA"
92+
if [ -z "$(ls -A "$PGDATA")" ]; then
93+
initializePostgreSQL
94+
elif [ -e "${PGDATA}"/postgresqlFullBackup.dump ]; then
95+
# Moving backup and emptying PGDATA directory
96+
mv "${PGDATA}"/postgresqlFullBackup.dump /tmp/postgresqlFullBackup.dump
97+
# New PostgreSQL version requires completely empty folder
98+
rm -rf "${PGDATA:?}"/*
99+
rm -rf "${PGDATA:?}"/.[^.] .??*
100+
101+
initializePostgreSQL
102+
103+
echo "Restoring database dump..."
104+
# Start postgres to restore backup
105+
gosu postgres postgres &
106+
PID=$!
107+
waitForPostgreSQLStartup
108+
# Restore backup
109+
psql -U postgres -f /tmp/postgresqlFullBackup.dump postgres
110+
rm /tmp/postgresqlFullBackup.dump
111+
# Kill postgres
112+
kill ${PID}
113+
waitForPostgreSQLShutdown
114+
fi
115+
116+
# set stage for health check
117+
doguctl state ready
118+
119+
# start database
120+
exec gosu postgres postgres

spec/goss/goss.yaml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
file:
2+
/create-sa.sh:
3+
exists: true
4+
mode: "0755"
5+
owner: root
6+
group: root
7+
filetype: file
8+
/startup.sh:
9+
exists: true
10+
mode: "0755"
11+
owner: root
12+
group: root
13+
filetype: file
14+
/usr/local/bin/gosu:
15+
exists: true
16+
mode: "0755"
17+
owner: root
18+
group: root
19+
filetype: file
20+
/var/lib/postgresql:
21+
exists: true
22+
mode: "0700"
23+
owner: postgres
24+
filetype: directory
25+
package:
26+
postgresql:
27+
installed: true
28+
port:
29+
tcp:5432:
30+
listening: true
31+
ip:
32+
- 0.0.0.0
33+
process:
34+
postgres:
35+
running: true

0 commit comments

Comments
 (0)