Skip to content

Commit f82ebbd

Browse files
committed
Deploy database for Immich and Netbox in separate containers
- deleting doesn't delete Docker image - updating image upon (re)install - improving parameters handling
1 parent f6d1ed0 commit f82ebbd

File tree

3 files changed

+88
-57
lines changed

3 files changed

+88
-57
lines changed

tools/modules/software/module_immich.sh

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,17 @@ function module_immich () {
1717
local title="immich"
1818
local condition=$(which "$title" 2>/dev/null)
1919

20+
# Database
21+
local DATABASE_USER="immich"
22+
local DATABASE_PASSWORD="immich"
23+
local DATABASE_NAME="immich"
24+
local DATABASE_HOST="postgres-immich"
25+
local DATABASE_IMAGE="tensorchord/pgvecto-rs:pg14-v0.2.0"
26+
local DATABASE_PORT="5432"
27+
2028
if pkg_installed docker-ce; then
21-
local container=$(docker container ls -a | mawk '/immich?( |$)/{print $1}')
22-
local image=$(docker image ls -a | mawk '/immich?( |$)/{print $3}')
29+
local container=$(docker ps -q -f "name=^immich$")
30+
local image=$(docker images -q ghcr.io/imagegenius/immich)
2331
fi
2432

2533
local commands
@@ -31,6 +39,7 @@ function module_immich () {
3139
"${commands[0]}")
3240
shift
3341

42+
3443
if ! pkg_installed docker-ce; then
3544
module_docker install
3645
fi
@@ -42,38 +51,44 @@ function module_immich () {
4251
"$IMMICH_BASE"/libraries
4352
touch "$IMMICH_BASE"/photos/{backups,thumbs,profile,upload,library,encoded-video}/.immich
4453
sudo chown -R 1000:1000 "$IMMICH_BASE"/
54+
4555
# Install armbian-config dependencies
4656
if ! docker container ls -a --format '{{.Names}}' | grep -q '^redis$'; then module_redis install; fi
47-
if ! docker container ls -a --format '{{.Names}}' | grep -q '^postgres$'; then module_postgres install; fi
57+
if ! docker container ls -a --format '{{.Names}}' | grep "^$DATABASE_HOST$"; then
58+
module_postgres install $DATABASE_USER $DATABASE_PASSWORD $DATABASE_NAME $DATABASE_IMAGE $DATABASE_HOST
59+
fi
4860

49-
until docker exec -i postgres psql -U armbian -c '\q' 2>/dev/null; do
61+
until docker exec -i $DATABASE_HOST psql -U $DATABASE_USER -c '\q' 2>/dev/null; do
5062
echo "⏳ Waiting for PostgreSQL to be ready..."
5163
sleep 2
5264
done
5365
echo "✅ PostgreSQL is ready. Creating Immich DB..."
5466

55-
if docker exec -i postgres psql -U armbian -tAc "SELECT 1 FROM pg_database WHERE datname='immich';" | grep -q 1; then
67+
if docker exec -i $DATABASE_HOST psql -U $DATABASE_USER -tAc "SELECT 1 FROM pg_database WHERE datname='$DATABASE_NAME';" | grep -q 1; then
5668
echo "✅ Database 'immich' exists."
5769
else
58-
docker exec -i postgres psql -U armbian <<-EOT
59-
CREATE DATABASE immich;
60-
GRANT ALL PRIVILEGES ON DATABASE immich TO armbian;
70+
docker exec -i $DATABASE_HOST psql -U $DATABASE_USER <<-EOT
71+
CREATE DATABASE $DATABASE_NAME;
72+
GRANT ALL PRIVILEGES ON DATABASE $DATABASE_NAME TO $DATABASE_USER;
6173
EOT
6274
fi
6375

76+
# Download or update image
77+
docker pull ghcr.io/imagegenius/immich:latest
78+
6479
# Run Immich container
6580
if ! docker run -d \
6681
--name=immich \
6782
--net=lsio \
6883
-e PUID=1000 \
6984
-e PGID=1000 \
7085
-e TZ="$(cat /etc/timezone)" \
71-
-e DB_HOSTNAME=postgres \
72-
-e DB_USERNAME=armbian \
73-
-e DB_PASSWORD=armbian \
74-
-e DB_DATABASE_NAME=immich \
86+
-e DB_HOSTNAME=$DATABASE_HOST \
87+
-e DB_USERNAME=$DATABASE_USER \
88+
-e DB_PASSWORD=$DATABASE_PASSWORD \
89+
-e DB_DATABASE_NAME=$DATABASE_NAME \
7590
-e REDIS_HOSTNAME=redis \
76-
-e DB_PORT=5432 \
91+
-e DB_PORT=$DATABASE_PORT \
7792
-e REDIS_PORT=6379 \
7893
-e REDIS_PASSWORD= \
7994
-e SERVER_HOST=0.0.0.0 \
@@ -115,13 +130,13 @@ function module_immich () {
115130
if [ -n "$container" ]; then
116131
docker container rm -f "$container" >/dev/null
117132
fi
118-
119-
if [ -n "$image" ]; then
120-
docker image rm "$image" >/dev/null
121-
fi
122133
;;
123134
"${commands[2]}")
124135
module_immich "${commands[1]}"
136+
if [ -n "$image" ]; then
137+
docker image rm -f "$image"
138+
fi
139+
module_postgres purge $DATABASE_USER $DATABASE_PASSWORD $DATABASE_NAME $DATABASE_IMAGE $DATABASE_HOST
125140
if [ -n "$IMMICH_BASE" ] && [ "$IMMICH_BASE" != "/" ]; then
126141
rm -rf "$IMMICH_BASE"
127142
fi

tools/modules/software/module_netbox.sh

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,21 @@ function module_netbox () {
1818
local title="netbox"
1919
local condition=$(which "$title" 2>/dev/null)
2020

21+
# Accept optional parameters
22+
local SUPERUSER_EMAIL="$2"
23+
local SUPERUSER_PASSWORD="$3"
24+
25+
# Database
26+
local DATABASE_USER="netbox"
27+
local DATABASE_PASSWORD="netbox"
28+
local DATABASE_NAME="netbox"
29+
local DATABASE_HOST="postgres-netbox"
30+
local DATABASE_IMAGE="postgres:17-alpine"
31+
local DATABASE_PORT="5432"
32+
2133
if pkg_installed docker-ce; then
22-
local container=$(docker container ls -a | mawk '/netbox?( |$)/{print $1}')
23-
local image=$(docker image ls -a | mawk '/netbox?( |$)/{print $3}')
34+
local container=$(docker ps -q -f "name=^netbox$")
35+
local image=$(docker images -q netboxcommunity/netbox)
2436
fi
2537

2638
local commands
@@ -30,18 +42,12 @@ function module_netbox () {
3042

3143
case "$1" in
3244
"${commands[0]}")
33-
shift
34-
# Accept optional parameters
35-
local SUPERUSER_EMAIL="$1"
36-
local SUPERUSER_PASSWORD="$2"
37-
3845
# Prompt for email and password using dialog
3946
[[ -z "$SUPERUSER_EMAIL" ]] && \
40-
SUPERUSER_EMAIL=$($DIALOG --title "Enter superuser email" --inputbox "" 8 50 3>&1 1>&2 2>&3)
47+
SUPERUSER_EMAIL=$($DIALOG --title "Enter NetBox superuser email" --inputbox "" 8 50 3>&1 1>&2 2>&3)
4148
[[ -z "$SUPERUSER_EMAIL" ]] && SUPERUSER_EMAIL="[email protected]"
42-
4349
[[ -z "$SUPERUSER_PASSWORD" ]] && \
44-
SUPERUSER_PASSWORD=$($DIALOG --title "Enter admin password" --passwordbox "" 8 50 3>&1 1>&2 2>&3)
50+
SUPERUSER_PASSWORD=$($DIALOG --title "Enter NetBox admin password" --passwordbox "" 8 50 3>&1 1>&2 2>&3)
4551
[[ -z "$SUPERUSER_PASSWORD" ]] && SUPERUSER_PASSWORD="armbian"
4652

4753
clear # Clean up dialog artifacts
@@ -51,7 +57,9 @@ function module_netbox () {
5157

5258
# Install armbian-config dependencies
5359
if ! docker container ls -a --format '{{.Names}}' | grep -q '^redis$'; then module_redis install; fi
54-
if ! docker container ls -a --format '{{.Names}}' | grep -q '^postgres$'; then module_postgres install; fi
60+
if ! docker container ls -a --format '{{.Names}}' | grep -q "^$DATABASE_HOST$"; then
61+
module_postgres install $DATABASE_USER $DATABASE_PASSWORD $DATABASE_NAME $DATABASE_IMAGE $DATABASE_HOST
62+
fi
5563

5664
# Generate a random secret key (50+ chars)
5765
NETBOX_SECRET_KEY=$(tr -dc 'A-Za-z0-9!@#$%^&*()-_=+' </dev/urandom | head -c 64)
@@ -63,11 +71,11 @@ function module_netbox () {
6371
cat > "$NETBOX_BASE/config/configuration.py" <<- EOT
6472
ALLOWED_HOSTS = ['*']
6573
DATABASE = {
66-
'NAME': 'netbox',
67-
'USER': 'armbian',
68-
'PASSWORD': 'armbian',
69-
'HOST': 'postgres',
70-
'PORT': '5432',
74+
'NAME': '$DATABASE_NAME',
75+
'USER': '$DATABASE_USER',
76+
'PASSWORD': '$DATABASE_PASSWORD',
77+
'HOST': '$DATABASE_HOST',
78+
'PORT': '$DATABASE_PORT',
7179
}
7280
7381
REDIS = {
@@ -90,6 +98,9 @@ function module_netbox () {
9098
EOT
9199
fi
92100

101+
# Download or update image
102+
docker pull netboxcommunity/netbox:latest
103+
93104
if ! docker run -d \
94105
--name=netbox \
95106
--net=lsio \
@@ -140,13 +151,13 @@ function module_netbox () {
140151
if [[ -n "${container}" ]]; then
141152
docker container rm -f "$container" >/dev/null
142153
fi
143-
144-
if [[ -n "${image}" ]]; then
145-
docker image rm "$image" >/dev/null
146-
fi
147154
;;
148155
"${commands[2]}")
149156
${module_options["module_netbox,feature"]} ${commands[1]}
157+
if [[ -n "${image}" ]]; then
158+
docker image rm "$image" >/dev/null
159+
fi
160+
module_postgres purge $DATABASE_USER $DATABASE_PASSWORD $DATABASE_NAME $DATABASE_IMAGE $DATABASE_HOST
150161
if [[ -n "${NETBOX_BASE}" && "${NETBOX_BASE}" != "/" ]]; then
151162
rm -rf "${NETBOX_BASE}"
152163
fi

tools/modules/software/module_postgres.sh

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,48 +18,54 @@ function module_postgres () {
1818
local title="postgres"
1919
local condition=$(which "$title" 2>/dev/null)
2020

21+
# Accept optional parameters
22+
local POSTGRES_USER="$2"
23+
local POSTGRES_PASSWORD="$3"
24+
local POSTGRES_DB="$4"
25+
local POSTGRES_IMAGE="$5"
26+
local POSTGRES_CONTAINER="$6"
27+
28+
# Defaults if nothing is set
29+
POSTGRES_USER="${POSTGRES_USER:-armbian}"
30+
POSTGRES_PASSWORD="${POSTGRES_PASSWORD:-armbian}"
31+
POSTGRES_DB="${POSTGRES_DB:-armbian}"
32+
POSTGRES_IMAGE="${POSTGRES_IMAGE:-tensorchord/pgvecto-rs:pg14-v0.2.0}"
33+
POSTGRES_CONTAINER="${POSTGRES_CONTAINER:-postgres}"
34+
2135
if pkg_installed docker-ce; then
22-
local container=$(docker container ls -a | mawk '/postgres?( |$)/{print $1}')
23-
local image=$(docker image ls -a | mawk '/pg14-v0.2.0?( |$)/{print $3}')
36+
local container=$(docker ps -q -f "name=^${POSTGRES_CONTAINER}$")
37+
local image=$(docker images -q $POSTGRES_IMAGE)
2438
fi
2539

2640
local commands
2741
IFS=' ' read -r -a commands <<< "${module_options["module_postgres,example"]}"
2842

29-
POSTGRES_BASE="${SOFTWARE_FOLDER}/postgres"
43+
POSTGRES_BASE="${SOFTWARE_FOLDER}/${POSTGRES_CONTAINER}"
3044

3145
case "$1" in
3246
"${commands[0]}")
33-
shift
34-
# Accept optional parameters
35-
local POSTGRES_USER="$1"
36-
local POSTGRES_PASSWORD="$2"
37-
local POSTGRES_DB="$3"
3847
pkg_installed docker-ce || module_docker install
3948
[[ -d "$POSTGRES_BASE" ]] || mkdir -p "$POSTGRES_BASE" || { echo "Couldn't create storage directory: $POSTGRES_BASE"; exit 1; }
40-
# Set defaults if empty
41-
POSTGRES_USER="${POSTGRES_USER:-armbian}"
42-
POSTGRES_PASSWORD="${POSTGRES_PASSWORD:-armbian}"
43-
POSTGRES_DB="${POSTGRES_DB:-armbian}"
49+
# Download or update image
50+
docker pull $POSTGRES_IMAGE
4451
docker run -d \
45-
--name=postgres \
52+
--name=${POSTGRES_CONTAINER} \
4653
--net=lsio \
4754
-e POSTGRES_USER=${POSTGRES_USER} \
4855
-e POSTGRES_PASSWORD=${POSTGRES_PASSWORD} \
4956
-e POSTGRES_DB=${POSTGRES_DB} \
5057
-e TZ="$(cat /etc/timezone)" \
51-
-p 5432:5432 \
52-
-v "${POSTGRES_BASE}/data:/var/lib/postgresql/data" \
58+
-v "${POSTGRES_BASE}/${POSTGRES_CONTAINER}/data:/var/lib/postgresql/data" \
5359
--restart unless-stopped \
54-
tensorchord/pgvecto-rs:pg14-v0.2.0
60+
${POSTGRES_IMAGE}
5561
for i in $(seq 1 20); do
56-
if docker inspect -f '{{ index .Config.Labels "org.opencontainers.image.version" }}' postgres >/dev/null 2>&1 ; then
62+
if docker inspect -f '{{ index .Config.Labels "org.opencontainers.image.version" }}' ${POSTGRES_CONTAINER} >/dev/null 2>&1 ; then
5763
break
5864
else
5965
sleep 3
6066
fi
6167
if [ $i -eq 20 ]; then
62-
echo -e "\nTimed out waiting for ${title} to start, consult your container logs for more info (\`docker logs postgres\`)"
68+
echo -e "\nTimed out waiting for ${title} to start, consult your container logs for more info (\`docker logs ${POSTGRES_CONTAINER}\`)"
6369
exit 1
6470
fi
6571
done
@@ -68,13 +74,12 @@ function module_postgres () {
6874
if [[ -n "${container}" ]]; then
6975
docker container rm -f "${container}" >/dev/null
7076
fi
71-
7277
if [[ -n "${image}" ]]; then
7378
docker image rm "${image}" >/dev/null
7479
fi
7580
;;
7681
"${commands[2]}")
77-
${module_options["module_postgres,feature"]} ${commands[1]}
82+
${module_options["module_postgres,feature"]} ${commands[1]} $POSTGRES_USER $POSTGRES_PASSWORD $POSTGRES_DB $POSTGRES_IMAGE $POSTGRES_CONTAINER
7883
if [[ -n "${POSTGRES_BASE}" && "${POSTGRES_BASE}" != "/" ]]; then
7984
rm -rf "${POSTGRES_BASE}"
8085
fi

0 commit comments

Comments
 (0)