Skip to content

Commit 1deb1c4

Browse files
committed
Add proper database initialisation and launch for jira
1 parent 37e0aac commit 1deb1c4

5 files changed

+88
-2
lines changed

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ ARG JIRA_GROUP=jira
1414

1515
ENV \
1616
JIRA_SOFTWARE_VERSION=8.0.2 \
17+
POSTGRESQL_VERSION=11.1-r0 \
1718
BASH_VERSION=4.4.19-r1 \
1819
NSS_VERSION=3.41-r0 \
1920
SU_EXEC_VERSION=0.2-r0
@@ -36,6 +37,7 @@ RUN \
3637
apk add --no-cache \
3738
su-exec="${SU_EXEC_VERSION}" \
3839
nss="${NSS_VERSION}" \
40+
postgresql="${POSTGRESQL_VERSION}" \
3941
bash="${BASH_VERSION}" && \
4042
mkdir -p "${JIRA_HOME}" && \
4143
mkdir -p "${JIRA_HOME}/caches/indexes" && \

docker-compose.dev.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ services:
66
volumes:
77
- jira_home:/var/atlassian/jira
88
- jira_logs:/opt/atlassian/jira/logs
9+
environment:
10+
POSTGRESQL_APP_USER: "app"
11+
POSTGRESQL_APP_PASSWORD: "app"
12+
POSTGRESQL_DATABASE_HOSTNAME: "postgresql"
13+
JIRA_DATABASE_NAME: "jira"
914
depends_on:
1015
- postgresql
1116
networks:
@@ -22,6 +27,9 @@ services:
2227
image: ragedunicorn/postgresql:${POSTGRESQL_TAG_VERSION}-dev
2328
volumes:
2429
- postgresql_data:/var/lib/postgresql
30+
environment:
31+
POSTGRESQL_APP_USER: "app"
32+
POSTGRESQL_APP_PASSWORD: "app"
2533
networks:
2634
- jira-software
2735
expose:

docker-compose.stack.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ services:
55
volumes:
66
- jira_home:/var/atlassian/jira
77
- jira_logs:/opt/atlassian/jira/logs
8+
environment:
9+
POSTGRESQL_DATABASE_HOSTNAME: "postgresql"
10+
JIRA_DATABASE_NAME: "jira"
811
depends_on:
912
- postgresql
1013
ports:

docker-compose.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ services:
66
volumes:
77
- jira_home:/var/atlassian/jira
88
- jira_logs:/opt/atlassian/jira/logs
9+
environment:
10+
POSTGRESQL_APP_USER: "app"
11+
POSTGRESQL_APP_PASSWORD: "app"
12+
POSTGRESQL_DATABASE_HOSTNAME: "postgresql"
13+
JIRA_DATABASE_NAME: "jira"
914
depends_on:
1015
- postgresql
1116
networks:
@@ -25,6 +30,9 @@ services:
2530
image: ragedunicorn/postgresql:${POSTGRESQL_TAG_VERSION}-stable
2631
volumes:
2732
- postgresql_data:/var/lib/postgresql
33+
environment:
34+
POSTGRESQL_APP_USER: "app"
35+
POSTGRESQL_APP_PASSWORD: "app"
2836
networks:
2937
- jira-software
3038
expose:

docker-entrypoint.sh

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,72 @@
1-
#!/bin/sh
1+
#!/bin/bash
22
# @author Michael Wiesendanger <michael.wiesendanger@gmail.com>
33
# @description launch script for jira
44

55
set -euo pipefail
66

7-
exec su-exec ${JIRA_USER} /opt/atlassian/jira/bin/start-jira.sh -fg run
7+
postgresql_app_user="/run/secrets/com.ragedunicorn.postgresql.app_user"
8+
postgresql_app_password="/run/secrets/com.ragedunicorn.postgresql.app_user_password"
9+
10+
function connection_check {
11+
loop_limit=13
12+
i=1
13+
14+
while true
15+
do
16+
if [ ${i} -eq ${loop_limit} ]; then
17+
echo "$(date) [ERROR]: Timeout error, failed to connect to PostgreSQL server"
18+
exit 1
19+
fi
20+
21+
echo "$(date) [INFO]: Waiting for connection to PostgreSQL, trying ${i}/${loop_limit}..."
22+
23+
if pg_isready -h "${POSTGRESQL_DATABASE_HOSTNAME}" -U "${postgresql_app_user}"; then
24+
break
25+
fi
26+
27+
sleep 5
28+
i=$((i + 1))
29+
done
30+
}
31+
32+
function database_check {
33+
if psql -h "${POSTGRESQL_DATABASE_HOSTNAME}" -U "${postgresql_app_user}" -w -lqt | cut -d \| -f 1 | grep -qw "${JIRA_DATABASE_NAME}"; then
34+
echo "$(date) [INFO]: Jira database already exists"
35+
else
36+
echo "$(date) [INFO]: Creating Jira database"
37+
createdb -h "${POSTGRESQL_DATABASE_HOSTNAME}" -U "${postgresql_app_user}" -w "${JIRA_DATABASE_NAME}"
38+
fi
39+
}
40+
41+
function setup_credentials {
42+
if [ -f "${postgresql_app_user}" ] && [ -f "${postgresql_app_password}" ]; then
43+
echo "$(date) [INFO]: Found docker secrets - using secrets"
44+
45+
postgresql_app_user="$(cat ${postgresql_app_user})"
46+
postgresql_app_password="$(cat ${postgresql_app_password})"
47+
else
48+
echo "$(date) [INFO]: No docker secrets found - using environment variables"
49+
50+
postgresql_app_user="${POSTGRESQL_APP_USER:?Missing environment variable POSTGRESQL_APP_USER}"
51+
postgresql_app_password="${POSTGRESQL_APP_PASSWORD:?Missing environment variable POSTGRESQL_APP_PASSWORD}"
52+
fi
53+
54+
# used by is_pgready/createdb
55+
export PGPASSWORD="${postgresql_app_password}"
56+
57+
unset "${POSTGRESQL_APP_USER}"
58+
unset "${POSTGRESQL_APP_PASSWORD}"
59+
}
60+
61+
function init {
62+
setup_credentials
63+
connection_check
64+
database_check
65+
66+
unset "${PGPASSWORD}"
67+
68+
echo "$(date) [INFO]: Starting Jira..."
69+
exec su-exec "${JIRA_USER}" /opt/atlassian/jira/bin/start-jira.sh -fg run
70+
}
71+
72+
init

0 commit comments

Comments
 (0)