aboutsummaryrefslogtreecommitdiff
path: root/unifi/unifi-setup.sh
blob: 86153b56ecc14c3aed99d39902e869db8d9f7ec7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/sh

set -e

USERNAME="unifi"
BASEDIR="/usr/lib/unifi"
DATA_DIR="${BASEDIR}/data"

# Default UID/GID to owner of the data directory
UNIFI_UID=${UNIFI_UID:-$(stat -L -c "%u" $DATA_DIR)}
UNIFI_GID=${UNIFI_GID:-$(stat -L -c "%u" $DATA_DIR)}

if [ "$UNIFI_GID" = 0 -o "$UNIFI_GID" = 0 ]; then
    echo "Set UNIFI_UID and UNIFI_GID in environment"
    exit 1
else
    echo "Unifi UID/GID: $UNIFI_UID $UNIFI_GID"
fi

cd ${BASEDIR}

# Create the user and group
if ! getent group ${USERNAME} > /dev/null 2>&1; then
    addgroup -g ${UNIFI_GID} -S ${USERNAME}
fi
if ! getent passwd ${USERNAME} > /dev/null 2>&1; then
    adduser -u ${UNIFI_UID} -S -h /var/lib/${USERNAME} -H -D -G ${USERNAME} ${USERNAME}
fi

# Update permissions on the root directories
chown -R ${USERNAME}:${USERNAME} \
    /var/lib/unifi \
    /var/log/unifi \
    /var/run/unifi \
    /usr/lib/unifi/dl

ln -s /var/log/unifi /logs

# Cleanup mongodb lock file if it exists otherwise the controller will freeze
# forever trying to start Mongo
[ -f data/db/mongod.lock ] && rm data/db/mongod.lock

if [ -n "$MONGO_URL" ]; then
    echo "Using external mongodb instance"
    echo "db.mongo.local=false" >> /var/lib/unifi/system.properties
    echo "db.mongo.uri=${MONGO_URL}" >> /var/lib/unifi/system.properties
    echo "statdb.mongo.uri=${MONGO_STATS_URL}" >> /var/lib/unifi/system.properties
    echo "unifi.db.name=${MONGO_DB_NAME:-ace}" >> /var/lib/unifi/system.properties
    echo "statdb.db.name=${MONGO_STATS_DB_NAME:-ace_stat}" >> /var/lib/unifi/system.properties
fi

# If this is set that the controller will start with no settings and will run
# the setup.
#
# WARNING! If this is set on a live database then the controller will delete
# all data and start fresh.
if [ -z "$START_DEFAULT" ]; then
	echo "is_default=false" >> /var/lib/unifi/system.properties
fi

# Replace the current process with a scoped-down controller. The java app
# is designed to do its own job control but it has to run with an init
# system or it doesn't get the signals from docker.
#
# Use the snappy native library installed with apk because the bundled on
# is built against libc which is not available in Alpine. Without this
# inform will fail with a decompression library error.

# NOTE: This has been migrated to simplevisor.json