Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added MONGO_REPLICASET for support to connect to existing replicaset #69

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,12 @@ services:
- TZ=Etc/UTC
- MONGO_USER=unifi
- MONGO_PASS=
- MONGO_HOST=unifi-db
- MONGO_PORT=27017
- MONGO_HOST=unifi-db[,unifi-db2[,unifi-db3]]
- MONGO_PORT=27017[,27017[,27017]]
- MONGO_DBNAME=unifi
- MEM_LIMIT=1024 #optional
- MEM_STARTUP=1024 #optional
- MONGO_REPLICASET= #optional
- MONGO_TLS= #optional
- MONGO_AUTHSOURCE= #optional
volumes:
Expand Down Expand Up @@ -179,11 +180,12 @@ docker run -d \
-e TZ=Etc/UTC \
-e MONGO_USER=unifi \
-e MONGO_PASS= \
-e MONGO_HOST=unifi-db \
-e MONGO_PORT=27017 \
-e MONGO_HOST=unifi-db[,unifi-db2[,unifi-db3]] \
-e MONGO_PORT=27017[,27017[,27017]] \
-e MONGO_DBNAME=unifi \
-e MEM_LIMIT=1024 `#optional` \
-e MEM_STARTUP=1024 `#optional` \
-e MONGO_REPLICASET= `#optional` \
-e MONGO_TLS= `#optional` \
-e MONGO_AUTHSOURCE= `#optional` \
-p 8443:8443 \
Expand Down Expand Up @@ -220,11 +222,12 @@ Containers are configured using parameters passed at runtime (such as those abov
| `-e TZ=Etc/UTC` | specify a timezone to use, see this [list](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List). |
| `-e MONGO_USER=unifi` | Mongodb Username. Only evaluated on first run. **Special characters must be [url encoded](https://en.wikipedia.org/wiki/Percent-encoding)**. |
| `-e MONGO_PASS=` | Mongodb Password. Only evaluated on first run. **Special characters must be [url encoded](https://en.wikipedia.org/wiki/Percent-encoding)**. |
| `-e MONGO_HOST=unifi-db` | Mongodb Hostname. Only evaluated on first run. |
| `-e MONGO_PORT=27017` | Mongodb Port. Only evaluated on first run. |
| `-e MONGO_HOST=unifi-db[,unifi-db2[,unifi-db3]]` | Mongodb Hostname (multiple hosts separated with comma allowed if MONGO_REPLICASET is set). Only evaluated on first run. |
| `-e MONGO_PORT=27017[,27017[,27017]]` | Mongodb Port (multiple ports separated with comma allowed if MONGO_REPLICASET is set). Only evaluated on first run. |
| `-e MONGO_DBNAME=unifi` | Mongodb Database Name (stats DB is automatically suffixed with `_stat`). Only evaluated on first run. |
| `-e MEM_LIMIT=1024` | Optionally change the Java memory limit (in Megabytes). Set to `default` to reset to default |
| `-e MEM_STARTUP=1024` | Optionally change the Java initial/minimum memory (in Megabytes). Set to `default` to reset to default |
| `-e MONGO_REPLICASET=` | Name of Mongodb (existing) [replicaset](https://www.mongodb.com/docs/manual/reference/connection-string/#replica-set-option). Only evaluated on first run. |
| `-e MONGO_TLS=` | Mongodb enable [TLS](https://www.mongodb.com/docs/manual/reference/connection-string/#mongodb-urioption-urioption.tls). Only evaluated on first run. |
| `-e MONGO_AUTHSOURCE=` | Mongodb [authSource](https://www.mongodb.com/docs/manual/reference/connection-string/#mongodb-urioption-urioption.authSource). For Atlas set to `admin`.Defaults to `MONGO_DBNAME`.Only evaluated on first run. |
| `-v /config` | All Unifi data stored here |
Expand Down Expand Up @@ -391,5 +394,6 @@ Once registered you can define the dockerfile to use with `-f Dockerfile.aarch64
## Versions

* **04.03.24:** - Install from zip package instead of deb.
* **17.02.24:** - Add environment variables for replicaSet.
* **17.10.23:** - Add environment variables for TLS and authSource to support Atlas and new MongoDB versions.
* **05.09.23:** - Initial release.
6 changes: 4 additions & 2 deletions readme-vars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@ param_usage_include_env: true
param_env_vars:
- { env_var: "MONGO_USER", env_value: "unifi", desc: "Mongodb Username. Only evaluated on first run. **Special characters must be [url encoded](https://en.wikipedia.org/wiki/Percent-encoding)**." }
- { env_var: "MONGO_PASS", env_value: "", desc: "Mongodb Password. Only evaluated on first run. **Special characters must be [url encoded](https://en.wikipedia.org/wiki/Percent-encoding)**." }
- { env_var: "MONGO_HOST", env_value: "unifi-db", desc: "Mongodb Hostname. Only evaluated on first run." }
- { env_var: "MONGO_PORT", env_value: "27017", desc: "Mongodb Port. Only evaluated on first run." }
- { env_var: "MONGO_HOST", env_value: "unifi-db[,unifi-db2[,unifi-db3]]", desc: "Mongodb Hostname (multiple hosts separated with comma allowed if MONGO_REPLICASET is set). Only evaluated on first run." }
- { env_var: "MONGO_PORT", env_value: "27017[,27017[,27017]]", desc: "Mongodb Port (multiple ports separated with comma allowed if MONGO_REPLICASET is set). Only evaluated on first run." }
- { env_var: "MONGO_DBNAME", env_value: "unifi", desc: "Mongodb Database Name (stats DB is automatically suffixed with `_stat`). Only evaluated on first run." }

# optional container parameters
opt_param_usage_include_env: true
opt_param_env_vars:
- { env_var: "MEM_LIMIT", env_value: "1024", desc: "Optionally change the Java memory limit (in Megabytes). Set to `default` to reset to default" }
- { env_var: "MEM_STARTUP", env_value: "1024", desc: "Optionally change the Java initial/minimum memory (in Megabytes). Set to `default` to reset to default" }
- { env_var: "MONGO_REPLICASET", env_value: "", desc: "Name of Mongodb (existing) [replicaset](https://www.mongodb.com/docs/manual/reference/connection-string/#replica-set-option). Only evaluated on first run." }
- { env_var: "MONGO_TLS", env_value: "", desc: "Mongodb enable [TLS](https://www.mongodb.com/docs/manual/reference/connection-string/#mongodb-urioption-urioption.tls). Only evaluated on first run." }
- { env_var: "MONGO_AUTHSOURCE", env_value: "", desc: "Mongodb [authSource](https://www.mongodb.com/docs/manual/reference/connection-string/#mongodb-urioption-urioption.authSource). For Atlas set to `admin`.Defaults to `MONGO_DBNAME`.Only evaluated on first run." }

Expand Down Expand Up @@ -124,5 +125,6 @@ app_setup_block: |
# changelog
changelogs:
- { date: "04.03.24:", desc: "Install from zip package instead of deb." }
- { date: "17.02.24:", desc: "Add environment variables for replicaSet." }
- { date: "17.10.23:", desc: "Add environment variables for TLS and authSource to support Atlas and new MongoDB versions." }
- { date: "05.09.23:", desc: "Initial release." }
4 changes: 2 additions & 2 deletions root/defaults/system.properties
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@
# unifi.throughput.port=6789
#
db.mongo.local=false
db.mongo.uri=mongodb://~MONGO_USER~:~MONGO_PASS~@~MONGO_HOST~:~MONGO_PORT~/~MONGO_DBNAME~?tls=~MONGO_TLS~~MONGO_AUTHSOURCE~
statdb.mongo.uri=mongodb://~MONGO_USER~:~MONGO_PASS~@~MONGO_HOST~:~MONGO_PORT~/~MONGO_DBNAME~_stat?tls=~MONGO_TLS~~MONGO_AUTHSOURCE~
db.mongo.uri=mongodb://~MONGO_USER~:~MONGO_PASS~@~MONGO_HOST~:~MONGO_PORT~/~MONGO_DBNAME~?tls=~MONGO_TLS~~MONGO_REPLICASET~~MONGO_AUTHSOURCE~
statdb.mongo.uri=mongodb://~MONGO_USER~:~MONGO_PASS~@~MONGO_HOST~:~MONGO_PORT~/~MONGO_DBNAME~_stat?tls=~MONGO_TLS~~MONGO_REPLICASET~~MONGO_AUTHSOURCE~
unifi.db.name=~MONGO_DBNAME~
104 changes: 84 additions & 20 deletions root/etc/s6-overlay/s6-rc.d/init-unifi-network-application-config/run
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@ for i in "${symlinks[@]}"; do
fi
done

function probe()
{
HOST=$1
PORT=$2
DBCOUNT=0
RETVAL=0
while true; do

if nc -w1 "${HOST}" "${PORT}" >/dev/null 2>&1; then
break
fi
DBCOUNT=$((DBCOUNT+1))
if [[ ${DBCOUNT} -gt 6 ]]; then
RETVAL=1
break
fi
sleep 5
done

echo $RETVAL
}

if [[ -L "/usr/lib/unifi/run" && ! "/usr/lib/unifi/run" -ef "/run/unifi" ]]; then
unlink "/usr/lib/unifi/run"
fi
Expand All @@ -28,40 +50,82 @@ if [[ ! -L "/usr/lib/unifi/run" ]]; then
fi

if [[ ! -e /config/data/system.properties ]]; then

cp -f /defaults/system.properties /defaults/system.properties.tmp

if [[ -z "${MONGO_HOST}" ]]; then
echo "*** No MONGO_HOST set, cannot configure database settings. ***"
sleep infinity
else
echo "*** Waiting for MONGO_HOST ${MONGO_HOST} to be reachable. ***"
DBCOUNT=0
while true; do
if nc -w1 "${MONGO_HOST}" "${MONGO_PORT}" >/dev/null 2>&1; then
break
if [[ -z "${MONGO_REPLICASET}" ]]; then

# Connect to stand alone instance
echo "*** Waiting for MONGO_HOST ${MONGO_HOST} to be reachable. ***"

if [ $(probe "${MONGO_HOST}" "${MONGO_PORT}") == "1" ]; then

echo "*** Defined MONGO_HOST ${MONGO_HOST} is not reachable on port ${MONGO_PORT}, cannot proceed. ***"

sleep infinity
fi
DBCOUNT=$((DBCOUNT+1))
if [[ ${DBCOUNT} -gt 6 ]]; then
echo "*** Defined MONGO_HOST ${MONGO_HOST} is not reachable, cannot proceed. ***"

sed -i "s/~MONGO_HOST~/${MONGO_HOST}/" /defaults/system.properties.tmp
sed -i "s/~MONGO_PORT~/${MONGO_PORT}/" /defaults/system.properties.tmp
sed -i "s/~MONGO_REPLICASET~//" /defaults/system.properties.tmp
else

# Connect to replicaset. Read host/port as comma separated
IFS=',' read -ra HOSTS <<< "$MONGO_HOST"
IFS=',' read -ra PORTS <<< "$MONGO_PORT"

# Compare lengths
HL=${#HOSTS[@]}
PL=${#PORTS[@]}

if [[ $HL -ne $PL ]]; then
echo "*** Defined replicaset MONGO_HOST(s) does not match length of MONGO_PORT(s), cannot proceed. ***"
sleep infinity
fi
sleep 5
done
sed -i "s/~MONGO_USER~/${MONGO_USER}/" /defaults/system.properties
sed -i "s/~MONGO_HOST~/${MONGO_HOST}/" /defaults/system.properties
sed -i "s/~MONGO_PORT~/${MONGO_PORT}/" /defaults/system.properties
sed -i "s/~MONGO_DBNAME~/${MONGO_DBNAME}/" /defaults/system.properties
sed -i "s/~MONGO_PASS~/${MONGO_PASS}/" /defaults/system.properties

NODES=""
# Probe each node
for (( i=0; i<$HL; i++ )); do

echo "*** Waiting for MONGO_HOST ${HOSTS[$i]} to be reachable. ***"

if [ $(probe "${HOSTS[$i]}" "${PORTS[$i]}") == "1" ]; then
echo "*** Defined MONGO_HOST ${HOSTS[$i]} is not reachable on port ${PORTS[$i]}, cannot proceed. ***"

sleep infinity
fi

if [[ ! -z "${NODES}" ]]; then
NODES+=",";
fi
NODES+="${HOSTS[$i]}:${PORTS[$i]}"
done

sed -i "s/~MONGO_REPLICASET~/\&replicaSet=${MONGO_REPLICASET}/" /defaults/system.properties.tmp
sed -i "s/~MONGO_HOST~:~MONGO_PORT~/${NODES}/" /defaults/system.properties.tmp
fi

sed -i "s/~MONGO_USER~/${MONGO_USER}/" /defaults/system.properties.tmp
sed -i "s/~MONGO_DBNAME~/${MONGO_DBNAME}/" /defaults/system.properties.tmp
sed -i "s/~MONGO_PASS~/${MONGO_PASS}/" /defaults/system.properties.tmp
if [[ "${MONGO_TLS,,}" = "true" ]]; then
sed -i "s/~MONGO_TLS~/true/" /defaults/system.properties
sed -i "s/~MONGO_TLS~/true/" /defaults/system.properties.tmp
else
sed -i "s/~MONGO_TLS~/false/" /defaults/system.properties
sed -i "s/~MONGO_TLS~/false/" /defaults/system.properties.tmp
fi
if [[ -z "${MONGO_AUTHSOURCE}" ]]; then
sed -i "s/~MONGO_AUTHSOURCE~//" /defaults/system.properties
sed -i "s/~MONGO_AUTHSOURCE~//" /defaults/system.properties.tmp
else
sed -i "s/~MONGO_AUTHSOURCE~/\&authSource=${MONGO_AUTHSOURCE}/" /defaults/system.properties
sed -i "s/~MONGO_AUTHSOURCE~/\&authSource=${MONGO_AUTHSOURCE}/" /defaults/system.properties.tmp
fi
cp /defaults/system.properties /config/data

fi

mv /defaults/system.properties.tmp /config/data/system.properties
fi

# generate key
Expand Down