#!/bin/sh
@%@UCRWARNING=# @%@

### BEGIN INIT INFO
# Provides:           docker
# Required-Start:     $syslog $remote_fs
# Required-Stop:      $syslog $remote_fs
# Should-Start:       cgroupfs-mount cgroup-lite
# Should-Stop:        cgroupfs-mount cgroup-lite
# Default-Start:      2 3 4 5
# Default-Stop:       0 1 6
# Short-Description:  Create lightweight, portable, self-sufficient containers.
# Description:
#  Docker is an open-source project to easily create lightweight, portable,
#  self-sufficient containers from any application. The same container that a
#  developer builds and tests on a laptop can run at scale, in production, on
#  VMs, bare metal, OpenStack clusters, public clouds and more.
### END INIT INFO

export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin

BASE=docker

# modify these in /etc/default/$BASE (/etc/default/docker)
DOCKER=/usr/bin/$BASE
# This is the pid file managed by docker itself
DOCKER_PIDFILE=/var/run/$BASE.pid
# This is the pid file created/managed by start-stop-daemon
DOCKER_SSD_PIDFILE=/var/run/$BASE-ssd.pid
DOCKER_LOGFILE=/var/log/$BASE.log
DOCKER_OPTS=
DOCKER_DESC="Docker"

# Get lsb functions
. /lib/lsb/init-functions

# load UCS Docker shell functions
. /usr/lib/univention-docker/univention-docker_lib.sh
. /usr/share/univention-lib/ucr.sh

if [ -f /etc/default/$BASE ]; then
	. /etc/default/$BASE
fi

# see also init_is_upstart in /lib/lsb/init-functions (which isn't available in Ubuntu 12.04, or we'd use it)
if [ -x /sbin/initctl ] && /sbin/initctl version 2>/dev/null | grep -q upstart; then
	log_failure_msg "$DOCKER_DESC is managed via upstart, try using service $BASE $1"
	exit 1
fi

# Check docker is present
if [ ! -x $DOCKER ]; then
	log_failure_msg "$DOCKER not present or not executable"
	exit 1
fi

fail_unless_root() {
	if [ "$(id -u)" != '0' ]; then
		log_failure_msg "$DOCKER_DESC must be run as root"
		exit 1
	fi
}

status() {
status_of_proc -p "$DOCKER_SSD_PIDFILE" "$DOCKER" "$DOCKER_DESC"
}

case "$1" in
	start)
		if start-stop-daemon --status --pidfile "$DOCKER_SSD_PIDFILE"; then
			# already running
			status
			exit "$?"
		fi

		fail_unless_root

		# check ucr autostart setting
		if [ -f "/usr/share/univention-config-registry/init-autostart.lib" ]; then
			. "/usr/share/univention-config-registry/init-autostart.lib"
			check_autostart docker docker/autostart
		fi

		if ! dpkg --compare-versions "$(uname -r)" gt-nl 4.1.0; then
			log_failure_msg "$DOCKER_DESC requires Linux Kernel 4.1, if you just updated, a reboot may be required."
			exit 1
		fi

		touch "$DOCKER_LOGFILE"
		chown root:docker "$DOCKER_LOGFILE"
		chmod 660 "$DOCKER_LOGFILE"

		ulimit -n 1048576
		if [ "$BASH" ]; then
			ulimit -u 1048576
		else
			ulimit -p 1048576
		fi

		# check if docker bridge network overlaps with my networks
		if docker_bridge_network_conflict; then
			docker0_ip=$(ucr get docker/daemon/default/opts/bip)
			log_failure_msg "$DOCKER_DESC: docker network bridge ${docker0_ip:-172.17.42.1/16} conflicts with existing network"
			exit 1
		fi

		# TODO remove docker bridge, otherwise a new --bip config may be ignored
		# for now system has to be restarted
		# ifconfig docker0 down
		# brctl delbr docker0

		log_begin_msg "Starting $DOCKER_DESC: engine... "
		output=$(start-stop-daemon --start --background \
			--exec "$DOCKER" \
			--pidfile "$DOCKER_SSD_PIDFILE" \
			--make-pidfile \
			-- \
			-d -p "$DOCKER_PIDFILE" \
			$DOCKER_OPTS \
				2>&1
		)
		ret=$?
		if [ -n "$output" ]; then
			echo "$output" >> "$DOCKER_LOGFILE"
		fi
		if ! [ "$ret" -eq 0 ]; then
			echo "$output"
			exit "$ret"
		fi
		for i in $(seq 10); do
			if ! docker_is_running; then
				sleep 1
			fi
		done
		if ! docker_is_running; then
			echo "$output"
			log_failure_msg "$DOCKER_DESC: did not start properly"
			exit 1
		fi
		# restart firewall for docker specific rules
		if ! is_ucr_true security/packetfilter/disabled; then
			invoke-rc.d univention-firewall restart || true
		fi
		# start containers
		if previous_containers_exist; then
			if docker_is_running; then
				log_progress_msg "previously stopped containers... "
				start_previous_containers
				previous_containers_list_clean
			fi
		fi
		log_end_msg $ret
		;;

	stop)
		if ! start-stop-daemon --status --pidfile "$DOCKER_SSD_PIDFILE"; then
			# already stopped
			log_begin_msg "Docker is not running"
			log_end_msg 0
			exit 0
		fi

		fail_unless_root
		if docker_is_running; then
			log_begin_msg "Stopping $DOCKER_DESC: "
			log_progress_msg "shutting down containers... "
			shutdown_all_containers
			log_progress_msg "engine..."
		else
			log_begin_msg "Stopping $DOCKER_DESC: engine..."
		fi
		start-stop-daemon --stop --pidfile "$DOCKER_SSD_PIDFILE" --oknodo --retry TERM/300
		log_end_msg $?
		;;

	restart)
		fail_unless_root
		docker_pid=`cat "$DOCKER_SSD_PIDFILE" 2>/dev/null`
		[ -n "$docker_pid" ] \
			&& ps -p $docker_pid > /dev/null 2>&1 \
			&& $0 stop
		$0 start
		;;

	force-reload)
		fail_unless_root
		$0 restart
		;;

	status)
		status
		;;

	*)
		echo "Usage: $0 {start|stop|restart|status}"
		exit 1
		;;
esac
