Skip to content

Commit

Permalink
chore: add pgadmin
Browse files Browse the repository at this point in the history
  • Loading branch information
gary-van-woerkens committed Apr 19, 2024
1 parent 464e663 commit f2411bd
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 0 deletions.
75 changes: 75 additions & 0 deletions .kontinuous/env/preprod/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,78 @@ pg-strapi:
cnpg-cluster:
backup:
~tpl~enabled: "false"

pgadmin:
~chart: app
~needs: ["build-pgadmin"]
imagePackage: pgadmin
env:
- name: POSTGRES_USER
valueFrom:
secretRef:
name: pg-app
key: PGUSER
- name: POSTGRES_PASSWORD
valueFrom:
secretRef:
name: pg-app
key: PGPASSWORD
- name: POSTGRES_HOST
valueFrom:
secretRef:
name: pg-app
key: PGHOST
- name: POSTGRES_PORT
valueFrom:
secretRef:
name: pg-app
key: PGPORT
- name: POSTGRES_USER_2
valueFrom:
secretRef:
name: pg-n8n
key: PGUSER
- name: POSTGRES_PASSWORD_2
valueFrom:
secretRef:
name: pg-n8n
key: PGPASSWORD
- name: POSTGRES_HOST_2
valueFrom:
secretRef:
name: pg-n8n
key: PGHOST
- name: POSTGRES_PORT_2
valueFrom:
secretRef:
name: pg-n8n
key: PGPORT
- name: POSTGRES_USER_3
valueFrom:
secretRef:
name: pg-strapi
key: PGUSER
- name: POSTGRES_PASSWORD_3
valueFrom:
secretRef:
name: pg-strapi
key: PGPASSWORD
- name: POSTGRES_HOST_3
valueFrom:
secretRef:
name: pg-strapi
key: PGHOST
- name: POSTGRES_PORT_3
valueFrom:
secretRef:
name: pg-strapi
key: PGPORT

jobs:
~chart: jobs
runs:
build-pgadmin:
use: build
with:
imagePackage: pgadmin
context: src/pgadmin
20 changes: 20 additions & 0 deletions src/pgadmin/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
ARG BASE_VERSION=8.5
FROM dpage/pgadmin4:${BASE_VERSION}

ENV PGADMIN_DEFAULT_EMAIL="[email protected]"
ENV PGADMIN_DEFAULT_PASSWORD="admin"
ENV PGADMIN_CONFIG_SERVER_MODE="False"
ENV PGADMIN_CONFIG_MASTER_PASSWORD_REQUIRED="False"

ENV POSTGRES_HOST="postgres"
ENV POSTGRES_PORT="5432"
ENV POSTGRES_DB="*"

USER root
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh && \
touch /pgadmin4/servers.json && \
chown pgadmin:root /pgadmin4/servers.json
USER pgadmin

ENTRYPOINT /entrypoint.sh
79 changes: 79 additions & 0 deletions src/pgadmin/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env sh

set -e

## Create /var/lib/pgadmin/pgpass
# 1st database
echo "$POSTGRES_HOST:$POSTGRES_PORT:postgres:$POSTGRES_USER:$POSTGRES_PASSWORD" | tee -a "/var/lib/pgadmin/pgpass" >/dev/null
echo "$POSTGRES_HOST:$POSTGRES_PORT:$POSTGRES_DB:$POSTGRES_USER:$POSTGRES_PASSWORD" | tee -a "/var/lib/pgadmin/pgpass" >/dev/null
POSTGRES_HOST_1=$POSTGRES_HOST
POSTGRES_PORT_1=$POSTGRES_PORT
POSTGRES_DB_1=$POSTGRES_DB
POSTGRES_USER_1=$POSTGRES_USER
POSTGRES_PASSWORD_1=$POSTGRES_PASSWORD

## Create servers.json
tee /pgadmin4/servers.json >/dev/null <<EOF
{
"Servers": {
"1": {
"Name": "$POSTGRES_HOST_1",
"Group": "Servers",
"Host": "$POSTGRES_HOST_1",
"Port": $POSTGRES_PORT_1,
"MaintenanceDB": "postgres",
"Username": "$POSTGRES_USER_1",
"SSLMode": "prefer",
"PassFile": "/var/lib/pgadmin/pgpass"
}
EOF

# if there are more than 1 database then
# loop through environment variables and create password files
COUNT=2
while [ ! -z "$(eval echo \"\$POSTGRES_HOST_$COUNT\")" ]; do
POSTGRES_HOST="$(eval echo \"\$POSTGRES_HOST_$COUNT\")"
POSTGRES_PORT="$(eval echo \"\$POSTGRES_PORT_$COUNT\")"

# Set default for the postgres port
if [ -z "$POSTGRES_PORT" ]; then
POSTGRES_PORT="5432"
fi

# if POSTGRES_DB, default is "*"
POSTGRES_DB="$(eval echo \"\$POSTGRES_DB_$COUNT\")"
if [ -z "$POSTGRES_DB" ]; then
POSTGRES_DB="*"
fi
POSTGRES_USER="$(eval echo \"\$POSTGRES_USER_$COUNT\")"
POSTGRES_PASSWORD="$(eval echo \"\$POSTGRES_PASSWORD_$COUNT\")"
echo "$POSTGRES_HOST:$POSTGRES_PORT:postgres:$POSTGRES_USER:$POSTGRES_PASSWORD" | tee -a "/var/lib/pgadmin/pgpass_$COUNT" >/dev/null
echo "$POSTGRES_HOST:$POSTGRES_PORT:$POSTGRES_DB:$POSTGRES_USER:$POSTGRES_PASSWORD" | tee -a "/var/lib/pgadmin/pgpass_$COUNT" >/dev/null

tee -a /pgadmin4/servers.json >/dev/null <<EOF
,"$COUNT": {
"Name": "$POSTGRES_HOST",
"Group": "Servers",
"Host": "$POSTGRES_HOST",
"Port": $POSTGRES_PORT,
"MaintenanceDB": "postgres",
"Username": "$POSTGRES_USER",
"SSLMode": "prefer",
"PassFile": "/var/lib/pgadmin/pgpass_$COUNT"
}
EOF

COUNT=$((COUNT + 1))
done

# close servers.json
tee -a /pgadmin4/servers.json >/dev/null <<EOF
}
}
EOF

chmod 600 $(ls /var/lib/pgadmin/pgpass*)
chown pgadmin:root $(ls /var/lib/pgadmin/pgpass*)
chown pgadmin:root /pgadmin4/servers.json

exec /entrypoint.sh "$@"

0 comments on commit f2411bd

Please sign in to comment.