Skip to content

Commit

Permalink
Dedicated container for database
Browse files Browse the repository at this point in the history
  • Loading branch information
nadvornik committed Nov 13, 2024
1 parent b4ca421 commit 7c34adc
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 0 deletions.
30 changes: 30 additions & 0 deletions containers/server-postgres-image/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# SPDX-License-Identifier: MIT
#!BuildTag: uyuni/server-postgres:latest

ARG BASE=registry.opensuse.org/opensuse/postgres:16
# or ARG BASE=registry.suse.com/suse/postgres:16
FROM $BASE

COPY uyuni-postgres-config.sh /docker-entrypoint-initdb.d/uyuni-postgres-config.sh
RUN chmod a+x /docker-entrypoint-initdb.d/uyuni-postgres-config.sh

# LABELs
ARG PRODUCT=Uyuni
ARG VENDOR="Uyuni project"
ARG URL="https://www.uyuni-project.org/"
ARG REFERENCE_PREFIX="registry.opensuse.org/uyuni"

# Build Service required labels
# labelprefix=org.opensuse.uyuni.server-postgres
LABEL org.opencontainers.image.name=server-postgres
LABEL org.opencontainers.image.title="${PRODUCT} Hub XML-RPC API container"
LABEL org.opencontainers.image.description="${PRODUCT} Hub XML-RPC API image"
LABEL org.opencontainers.image.created="%BUILDTIME%"
LABEL org.opencontainers.image.vendor="${VENDOR}"
LABEL org.opencontainers.image.url="${URL}"
LABEL org.opencontainers.image.version=5.1.1
LABEL org.openbuildservice.disturl="%DISTURL%"
LABEL org.opensuse.reference="${REFERENCE_PREFIX}/server-postgres:${PRODUCT_VERSION}.%RELEASE%"
# endlabelprefix
LABEL org.uyuni.version="${PRODUCT_VERSION}"

4 changes: 4 additions & 0 deletions containers/server-postgres-image/_service
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<services>
<service mode="buildtime" name="kiwi_metainfo_helper"/>
<service mode="buildtime" name="docker_label_helper"/>
</services>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- First release
3 changes: 3 additions & 0 deletions containers/server-postgres-image/tito.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[buildconfig]
tagger = tito.tagger.SUSEContainerTagger
builder = custom.ContainerBuilder
93 changes: 93 additions & 0 deletions containers/server-postgres-image/uyuni-postgres-config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/bin/bash
# Adjust postgresql.conf for Uyuni
# This is run automatically at the first start of the container
# or it can be run manually later.

POSTGRESQL=/var/lib/pgsql/data/postgresql.conf
SSL_CERT=/etc/pki/tls/certs/spacewalk.crt
SSL_KEY=/etc/pki/tls/private/pg-spacewalk.key

postgres_reconfig() {
echo "Setting $1 = $2"
if grep -E "^$1[[:space:]]*=" $POSTGRESQL >/dev/null; then
sed -i "s|^$1[[:space:]]*=.*|$1 = $2|" $POSTGRESQL
else
echo "$1 = $2" >> $POSTGRESQL
fi
}

# Get total memory in KB
TOTAL_MEM_KB=$(grep MemTotal: /proc/meminfo | sed -e 's|MemTotal:[[:space:]]*\([0-9]*\).*|\1|')

# Check minimum memory requirement (255KB)
if [ "$TOTAL_MEM_KB" -lt $((0xff * 1024)) ]; then
echo "WARNING: low memory: $TOTAL_MEM_KB"
TOTAL_MEM_KB=$((0xff * 1024))
fi

# Binary rounding function
bin_rnd() {
local value=$1
local mbt=1
while [ $value -gt 16 ]; do
value=$((value / 2))
mbt=$((mbt * 2))
done
echo $((mbt * value))
}

# Convert to MB
to_mb() {
echo "$(($1 / 1024))MB"
}

# Get max_connections from current postgresql.conf
MAX_CONNECTIONS=$(grep -E "^max_connections[[:space:]]*=" $POSTGRESQL | sed -e 's/.*=[[:space:]]*\([0-9]*\).*/\1/')
[ -z "$MAX_CONNECTIONS" -o "$MAX_CONNECTIONS" -lt 400 ] && MAX_CONNECTIONS=500

# Check if system uses SSD
if [ "$(cat /sys/block/$(mount | grep " /var/lib/pgsql/data " | cut -d' ' -f1 |sed -e 's/[0-9][0-9p]*//g' -e 's|/dev/||' )/queue/rotational)" -gt 0 ]; then
IS_SSD=0
echo "Rotational HDD detected."
else
IS_SSD=1
echo "SSD detected."
fi

# Calculate values
SHARED_BUFFERS=$(bin_rnd $((TOTAL_MEM_KB / 4)))
EFFECTIVE_CACHE_SIZE=$(bin_rnd $((TOTAL_MEM_KB * 3 / 4)))
WORK_MEM=$(bin_rnd $(((TOTAL_MEM_KB - SHARED_BUFFERS) / (3 * MAX_CONNECTIONS))))
MAINTENANCE_WORK_MEM=$(bin_rnd $(( TOTAL_MEM_KB / 16 < 1048576 ? TOTAL_MEM_KB / 16 : 1048576 ))) # 1GB

# Apply configurations
postgres_reconfig "shared_buffers" "$(to_mb $SHARED_BUFFERS)"
postgres_reconfig "effective_cache_size" "$(to_mb $EFFECTIVE_CACHE_SIZE)"
postgres_reconfig "work_mem" "$(to_mb $WORK_MEM)"
postgres_reconfig "maintenance_work_mem" "$(to_mb $MAINTENANCE_WORK_MEM)"
postgres_reconfig "max_wal_size" "4096MB"
postgres_reconfig "min_wal_size" "1024MB"
postgres_reconfig "checkpoint_completion_target" "0.9"
postgres_reconfig "wal_buffers" "16MB"
postgres_reconfig "constraint_exclusion" "off"
postgres_reconfig "max_connections" "$MAX_CONNECTIONS"

if [ "$IS_SSD" -eq 1 ]; then
postgres_reconfig "random_page_cost" "1.1"
postgres_reconfig "effective_io_concurrency" "200"
else
postgres_reconfig "random_page_cost" "4"
postgres_reconfig "effective_io_concurrency" "2"
fi

postgres_reconfig jit off

if [ -f $SSL_KEY ] ; then
chown postgres $SSL_KEY
chmod 400 $SSL_KEY
postgres_reconfig "ssl" "on"
postgres_reconfig "ssl_cert_file" "'$SSL_CERT'"
postgres_reconfig "ssl_key_file" "'$SSL_KEY'"
fi

echo "postgresql.conf updated"

0 comments on commit 7c34adc

Please sign in to comment.