initial commit for Unifi 5

This commit is contained in:
Tommi2Day
2016-07-24 19:15:21 +02:00
parent 3f18e78c82
commit 88257b8d83
6 changed files with 200 additions and 0 deletions

37
Dockerfile.unifi Normal file
View File

@@ -0,0 +1,37 @@
FROM ubuntu:trusty
ENV DEBIAN_FRONTEND noninteractive
RUN mkdir -p /usr/lib/unifi/data /backups /logs && \
touch /usr/lib/unifi/data/.unifidatadir
# add unifi repo +keys
RUN \
echo "deb http://www.ubnt.com/downloads/unifi/debian unifi5 ubiquiti" >/etc/apt/sources.list.d/ubnt.list && \
apt-key adv --keyserver keyserver.ubuntu.com --recv C0A52C50
#try distro mongo first
#RUN \
# echo "deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen" >/etc/apt/sources.list.d/mongo.list && \
# apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
# update then install
RUN \
apt-get update -q -y && \
apt-get install -q -y mongodb-server unifi lsof
#add scripts
COPY ["unifi.sh","backup_unifi.sh","restore_unifi.sh", "/usr/lib/unifi/"]
RUN echo "10 02 * * * root /usr/lib/unifi/backup_unifi.sh >/logs/backup.log 2>&1" >/etc/cron.d/unifi_backup
#redirect path
RUN rm -f /usr/lib/unifi/logs && ln -s /logs /usr/lib/unifi/logs
RUN rm -f /usr/lib/unifi/backups && ln -s /backups /usr/lib/unifi/backups
#define interface
VOLUME /usr/lib/unifi/data
VOLUME /backups /logs
EXPOSE 8443 8880 8080 27117
WORKDIR /usr/lib/unifi
ENTRYPOINT /bin/bash
CMD ["/usr/lib/unifi/unifi.sh", "start"]

15
backup_unifi.sh Normal file
View File

@@ -0,0 +1,15 @@
#!/bin/bash
#mongo logrotate
MONGOPID=$(ps -ef|grep [m]ongod|awk '{print $2}')
if [ -n "$MONGOPID" ]; then
kill -SIGUSR1 $MONGOPID
fi
#backup
cd /usr/lib/unifi1
bash ./unifi.sh stop
sleep 5
tar -czf backups/unifi_data.$(date '+%Y%m%d').tar.gz data >/dev/null
bash ./unifi.sh start

13
build_unifi.sh Normal file
View File

@@ -0,0 +1,13 @@
#!/bin/bash
VERS=5.0
VMNAME=${1:-unifi}
if [ -r Dockerfile.$VMNAME ]; then
DOCKER_USER=${DOCKER_USER:-tommi2day}
#build the container
docker stop $VMNAME
docker rm $VMNAME
docker build -t $DOCKER_USER/$VMNAME:$VERS -f Dockerfile.$VMNAME .
else
echo "Usage: $0 VMNAME #Dockerfile.VMNAME must exists"
fi

16
restore_unifi.sh Normal file
View File

@@ -0,0 +1,16 @@
#!/bin/bash
cd /usr/lib/unifi
DATE=$(date '+%Y%m%d')
bash ./unifi.sh stop
sleep 5
FILE=${1:-unifi_data.$DATE.tar.gz}
if [ -r /backups/$FILE ]; then
tar -czf /backups/unifi_data_before_restore.$(date '+%Y%m%d_%H%M%S').tar.gz data/
rm -rf data/*
tar -xzf /backups/$FILE data/
else
echo "Backup /backups/$FILE not readable"
fi
bash ./unifi.sh start

73
run_unifi.sh Normal file
View File

@@ -0,0 +1,73 @@
#!/usr/bin/env bash
VERS=5.0
VMNAME=${2:-unifi}
CMD=$1
DOCKER_SHARED=${DOCKER_SHARED:-$(pwd)}
DOCKER_USER=${DOCKER_USER:-tommi2day}
SHARED_DIR="${DOCKER_SHARED}/${VMNAME}-shared"
#debug
if [ -z "$DEBUG" ]; then
RUN="-d "
else
RUN="--rm=true -it --entrypoint bash "
fi
#windows
if [ "$OSTYPE" = "msys" ]; then
P=/
else
S=sudo
fi
DOCKER="$S docker"
#datadir
if [ ! -d "${SHARED_DIR}" ]; then
mkdir -p "${SHARED_DIR}"
fi
#stop and delete
RUNNING=$($DOCKER inspect --format="{{ .State.Running }}" $VMNAME 2> /dev/null)
if [ $? -eq 0 ]; then
if [ "$RUNNING" == "true" ]; then
$DOCKER stop $VMNAME
fi
$DOCKER rm $VMNAME
fi
if [ "$DEBUG" = "clean" ]; then
#clean all if debug set to clean
rm -Rf ${SHARED_DIR}
$DOCKER volume rm ${VMNAME}_data >/dev/null 2>&1
fi
#check data volume and create a new one if needed
DATA=$($DOCKER docker inspect --format '{{ .Mountpoint }}' ${VMNAME}_data 2> /dev/null)
if [ $? -ne 0 ]; then
docker volume create --name ${VMNAME}_data
fi
#prepare shared dir
for d in backups logs ; do
if [ ! -d $SHARED_DIR/$d ]; then
mkdir -p $SHARED_DIR/$d
fi
done
#run it
echo "
$DOCKER run $RUN \
-v ${VMNAME}_data:/usr/lib/unifi/data \
-v "${SHARED_DIR}/backups":/backups \
-v "${SHARED_DIR}/logs":/logs \
--hostname $VMNAME \
--name ${VMNAME} \
-p 8080:8080 \
-p 8880:8880 \
-p 8443:8443 \
-p 27117:27117 \
$DOCKER_USER/$VMNAME:$VERS $1 " >starter
if [ "$OSTYPE" = "msys" ]; then
mv starter starter.ps1
powershell -File starter.ps1
else
bash starter
fi

46
unifi.sh Normal file
View File

@@ -0,0 +1,46 @@
#!/bin/bash
# description: Start Unify server
# chkconfig: 2345 99 01
UNIFI=/usr/lib/unifi
if [ ! -d $UNIFI ]; then
echo "Unifi not installed in $UNIFI"
exit 1
fi
cd $UNIFI
case "$1" in
start)
if [ ! -d logs ]; then
mkdir logs
fi
nohup java -Xmx256M -jar lib/ace.jar start >logs/unifi.log 2>&1 &
sleep 5
./$0 status
tail -f logs/server.log
;;
stop)
java -jar lib/ace.jar stop
sleep 5
./$0 status
;;
status)
if (ps -ef|grep "[l]ib/ace.jar" >/dev/null); then
echo "Unifi Controler running.."
exit 0
else
echo "Unifi Controler stopped.."
exit 1
fi
;;
*)
echo "Usage: $ME start|stop|status"
;;
esac