您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

docker-entrypoint.sh 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #!/usr/bin/env bash
  2. . /usr/unifi/functions
  3. if [ -x /usr/local/bin/docker-build.sh ]; then
  4. /usr/local/bin/docker-build.sh "${PKGURL}"
  5. fi
  6. exit_handler() {
  7. log "Exit signal received, shutting down"
  8. java -jar ${BASEDIR}/lib/ace.jar stop
  9. for i in `seq 1 10` ; do
  10. [ -z "$(pgrep -f ${BASEDIR}/lib/ace.jar)" ] && break
  11. # graceful shutdown
  12. [ $i -gt 1 ] && [ -d ${BASEDIR}/run ] && touch ${BASEDIR}/run/server.stop || true
  13. # savage shutdown
  14. [ $i -gt 7 ] && pkill -f ${BASEDIR}/lib/ace.jar || true
  15. sleep 1
  16. done
  17. # shutdown mongod
  18. if [ -f ${MONGOLOCK} ]; then
  19. mongo localhost:${MONGOPORT} --eval "db.getSiblingDB('admin').shutdownServer()" >/dev/null 2>&1
  20. fi
  21. exit ${?};
  22. }
  23. trap 'kill ${!}; exit_handler' SIGHUP SIGINT SIGQUIT SIGTERM
  24. [ "x${JAVA_HOME}" != "x" ] || set_java_home
  25. # vars similar to those found in unifi.init
  26. MONGOPORT=27117
  27. CODEPATH=${BASEDIR}
  28. DATALINK=${BASEDIR}/data
  29. LOGLINK=${BASEDIR}/logs
  30. RUNLINK=${BASEDIR}/run
  31. DIRS="${RUNDIR} ${LOGDIR} ${DATADIR} ${BASEDIR}"
  32. JVM_MAX_HEAP_SIZE=${JVM_MAX_HEAP_SIZE:-1024M}
  33. #JVM_INIT_HEAP_SIZE=
  34. JVM_EXTRA_OPTS=""
  35. #JAVA_ENTROPY_GATHER_DEVICE=
  36. #UNIFI_JVM_EXTRA_OPTS=
  37. #ENABLE_UNIFI=yes
  38. MONGOLOCK="${DATAPATH}/db/mongod.lock"
  39. JVM_EXTRA_OPTS="${JVM_EXTRA_OPTS} -Dunifi.datadir=${DATADIR} -Dunifi.logdir=${LOGDIR} -Dunifi.rundir=${RUNDIR}"
  40. PIDFILE=/var/run/unifi/unifi.pid
  41. if [ ! -z "${JVM_MAX_HEAP_SIZE}" ]; then
  42. JVM_EXTRA_OPTS="${JVM_EXTRA_OPTS} -Xmx${JVM_MAX_HEAP_SIZE}"
  43. fi
  44. if [ ! -z "${JVM_INIT_HEAP_SIZE}" ]; then
  45. JVM_EXTRA_OPTS="${JVM_EXTRA_OPTS} -Xms${JVM_INIT_HEAP_SIZE}"
  46. fi
  47. if [ ! -z "${JVM_MAX_THREAD_STACK_SIZE}" ]; then
  48. JVM_EXTRA_OPTS="${JVM_EXTRA_OPTS} -Xss${JVM_MAX_THREAD_STACK_SIZE}"
  49. fi
  50. JVM_OPTS="${JVM_EXTRA_OPTS}
  51. -Djava.awt.headless=true
  52. -Dfile.encoding=UTF-8"
  53. # Cleaning /var/run/unifi/* See issue #26, Docker takes care of exlusivity in the container anyway.
  54. rm -f /var/run/unifi/unifi.pid
  55. run-parts /usr/local/unifi/init.d
  56. run-parts /usr/unifi/init.d
  57. if [ -d "/unifi/init.d" ]; then
  58. run-parts "/unifi/init.d"
  59. fi
  60. # Used to generate simple key/value pairs, for example system.properties
  61. confSet () {
  62. file=$1
  63. key=$2
  64. value=$3
  65. if [ "$newfile" != true ] && grep -q "^${key} *=" "$file"; then
  66. ekey=$(echo "$key" | sed -e 's/[]\/$*.^|[]/\\&/g')
  67. evalue=$(echo "$value" | sed -e 's/[\/&]/\\&/g')
  68. sed -i "s/^\(${ekey}\s*=\s*\).*$/\1${evalue}/" "$file"
  69. else
  70. echo "${key}=${value}" >> "$file"
  71. fi
  72. }
  73. confFile="${DATADIR}/system.properties"
  74. if [ -e "$confFile" ]; then
  75. newfile=false
  76. else
  77. newfile=true
  78. fi
  79. declare -A settings
  80. h2mb() {
  81. awkcmd='
  82. /[0-9]$/{print $1/1024/1024;next};
  83. /[mM]$/{printf "%u\n", $1;next};
  84. /[kK]$/{printf "%u\n", $1/1024;next}
  85. /[gG]$/{printf "%u\n", $1*1024;next}
  86. '
  87. echo $1 | awk "${awkcmd}"
  88. }
  89. if ! [[ -z "$LOTSOFDEVICES" ]]; then
  90. settings["unifi.G1GC.enabled"]="true"
  91. settings["unifi.xms"]="$(h2mb $JVM_INIT_HEAP_SIZE)"
  92. settings["unifi.xmx"]="$(h2mb ${JVM_MAX_HEAP_SIZE:-1024M})"
  93. fi
  94. # Implements issue #30
  95. if ! [[ -z "$DB_URI" || -z "$STATDB_URI" || -z "$DB_NAME" ]]; then
  96. settings["db.mongo.local"]="false"
  97. settings["db.mongo.uri"]="$DB_URI"
  98. settings["statdb.mongo.uri"]="$STATDB_URI"
  99. settings["unifi.db.name"]="$DB_NAME"
  100. fi
  101. for key in "${!settings[@]}"; do
  102. confSet "$confFile" "$key" "${settings[$key]}"
  103. done
  104. UNIFI_CMD="java ${JVM_OPTS} -jar ${BASEDIR}/lib/ace.jar start"
  105. # controller writes to relative path logs/server.log
  106. cd ${BASEDIR}
  107. CUID=$(id -u)
  108. if [[ "${@}" == "unifi" ]]; then
  109. # keep attached to shell so we can wait on it
  110. log 'Starting unifi controller service.'
  111. for dir in "${DATADIR}" "${LOGDIR}"; do
  112. if [ ! -d "${dir}" ]; then
  113. if [ "${UNSAFE_IO}" == "true" ]; then
  114. rm -rf "${dir}"
  115. fi
  116. mkdir -p "${dir}"
  117. fi
  118. done
  119. if [ "${RUNAS_UID0}" == "true" ] || [ "${CUID}" != "0" ]; then
  120. if [ "${CUID}" == 0 ]; then
  121. log 'WARNING: Running UniFi in insecure (root) mode'
  122. fi
  123. ${UNIFI_CMD} &
  124. elif [ "${RUNAS_UID0}" == "false" ]; then
  125. if [ "${BIND_PRIV}" == "true" ]; then
  126. if setcap 'cap_net_bind_service=+ep' "${JAVA_HOME}/jre/bin/java"; then
  127. sleep 1
  128. else
  129. log "ERROR: setcap failed, can not continue"
  130. log "ERROR: You may either launch with -e BIND_PRIV=false and only use ports >1024"
  131. log "ERROR: or run this container as root with -e RUNAS_UID0=true"
  132. exit 1
  133. fi
  134. fi
  135. if [ "$(id unifi -u)" != "${UNIFI_UID}" ] || [ "$(id unifi -g)" != "${UNIFI_GID}" ]; then
  136. log "INFO: Changing 'unifi' UID to '${UNIFI_UID}' and GID to '${UNIFI_GID}'"
  137. usermod -o -u ${UNIFI_UID} unifi && groupmod -o -g ${UNIFI_GID} unifi
  138. fi
  139. # Using a loop here so I can check more directories easily later
  140. for dir in ${DIRS}; do
  141. if [ "$(stat -c '%u' "${dir}")" != "${UNIFI_UID}" ]; then
  142. chown -R "${UNIFI_UID}:${UNIFI_GID}" "${dir}"
  143. fi
  144. done
  145. gosu unifi:unifi ${UNIFI_CMD} &
  146. fi
  147. wait
  148. log "WARN: unifi service process ended without being singaled? Check for errors in ${LOGDIR}." >&2
  149. else
  150. log "Executing: ${@}"
  151. exec ${@}
  152. fi
  153. exit 1