I found a blog post a while back which helped me overcome the problem of installing the Unifi Controller with the current MongoDB. I made a script a little while back that documented the process and tried to automate much of it. I've since updated the script some, and I figure I'd share it.
Note: this script only installs the software. It does not enable the service or do any firewall configuration.
https://gitlab.com/EddieJennings/ubiquiti/-/blob/master/install_unifi_controller_ubuntu.sh
For anyone new to scripting with BASH, here's the break-down of what it does and why I made certain decisions about how to handle things. Note that I've omitted most of the echo
statements, which tell the user what's going on as it happens.
MONGO_DL="https://repo.mongodb.org/apt/ubuntu/dists/focal/mongodb-org/5.0/multiverse/binary-amd64/mongodb-org-server_5.0.2_amd64.deb"
UNIFI_DL="https://dl.ui.com/unifi/6.2.26/unifi_sysvinit_all.deb"
MONGO_FILE="/tmp/$(echo $MONGO_DL | rev | cut -d '/' -f 1 | rev)"
UNIFI_FILE="/tmp/$(echo $UNIFI_DL | rev | cut -d '/' -f 1 | rev)"
INSTALLER_LOG_FILE="/tmp/unifi_installer_$(date +%F).log"
WAITING_MESSAGE="This may take a moment."
I set a few variables here. The main two that you would want to periodically update are MONGO_DL
and UNIFI_DL
as you'll want to have the most current installer. Also these variables are used to create some file paths used for the installation. You'll notice I'm doing everything in /tmp
. This allows for easy cleanup, as if the system is rebooted, the files will be deleted (and temporary stuff should live in /tmp
).
if [ "$(whoami)" != "root" ]; then
echo "You must run this script using sudo or"
echo "be logged in as root. Exiting."
exit 1
fi
Since we're installing things, this needs to be run as root (either being logged in as root or using sudo). I decided to check to see if the account running the script was not root, and if true, then exit the script.
apt install -y wget openjdk-8-jre-headless jsvc binutils > $INSTALLER_LOG_FILE 2>&1
Next we install the dependencies for both this script and the Unifi controller. I use 2>&1
so that both standard output and standard error gets redirected to the log file.
if [ -f "$MONGO_FILE" ]
then
echo "$MONGO_FILE was found. Continuing."
echo ""
else
echo "$MONGO_FILE was not found. Downloading now."
echo "Downloading Mongodb Community installer. $(echo $WAITING_MESSAGE)"
echo ""
wget -O $MONGO_FILE $MONGO_DL 2>> $INSTALLER_LOG_FILE
fi
This block checks to see if the MongoDB installer file exists in /tmp
and if not, wget
downloads the file to the /tmp
directory. I wanted to redirect the output of wget
's downloading of the file, and since that output is actually standard error rather than standard output 2>>
is necessary to redirect and append to the log file.
if [ -f "$UNIFI_FILE" ]
then
echo "$UNIFI_FILE was found. Continuing."
echo ""
else
echo "$UNIFI_FILE was not found. Downloading now."
echo "Downloading the Unifi Controller installer. $(echo $WAITING_MESSAGE)"
echo ""
wget -O $UNIFI_FILE $UNIFI_DL 2>> $INSTALLER_LOG_FILE
fi
And I repeat this process for the Unifi Controller installer.
dpkg -i $MONGO_FILE >> $INSTALLER_LOG_FILE 2>&1
Finally, we start installing stuff beginning with MongoDB.
dpkg --ignore-depends=mongodb-org-server -i $UNIFI_FILE >> $INSTALLER_LOG_FILE 2>&1
Next we install the Unifi Controller. I specifically tell dkpg
to ignore the mongodb-org-server
dependency, since if MongoDB greater than version 4 is installed, the Unifi Controller install will fail. Such a requirement is probably by design; however, for my lab, I wanted to see if I can use the most current MongoDB. I have not had any problems with the current MongoDB; however, that's something to think about whether or not you want to do for production.
dkpg
output and any errors it throws are then logged.
echo "################" | tee -a $INSTALLER_LOG_FILE
echo "# **Reminder** #" | tee -a $INSTALLER_LOG_FILE
echo "################" | tee -a $INSTALLER_LOG_FILE
echo "" | tee -a $INSTALLER_LOG_FILE
echo "##############################################################" | tee -a $INSTALLER_LOG_FILE
echo "# Edit the /var/lib/dpkg/status file, and remove the #" | tee -a $INSTALLER_LOG_FILE
echo "# 'mongodb-org-server (<< 4.0.0)' requirement in the depends #" | tee -a $INSTALLER_LOG_FILE
echo "# section as shown below. #" | tee -a $INSTALLER_LOG_FILE
echo "##############################################################" | tee -a $INSTALLER_LOG_FILE
echo "" | tee -a $INSTALLER_LOG_FILE
grep -A 10 -i "Package: unifi" /var/lib/dpkg/status | grep --color=always -B 10 -i "mongodb-org-server (<< 4.0.0)" | tee -a $INSTALLER_LOG_FILE
echo ""
The last section of the script is a suggestion to the user, and I wanted both the output to display to the user as well as be logged; thus, I piped the echo
statements to the tee
command.
This final step probably isn't necessary, but since we're explicitly ignoring the MongoDB dependency, I figure it would be a good idea to remove it from /var/lib/dpkg/status
file. Also, the above blog post follows that step. To date, I haven't figured out a reliable way to automate this piece. One day, I might come up with it.
Hopefully this script will be useful to someone.