#!/bin/sh [ $(id -u) != '0' ] && echo "root is needed" && exit 1 # get some path of docklet bindir=${0%/*} # $bindir maybe like /opt/docklet/src/../sbin # use command below to make $bindir in normal absolute path DOCKLET_BIN=$(cd $bindir; pwd) DOCKLET_HOME=${DOCKLET_BIN%/*} DOCKLET_CONF=$DOCKLET_HOME/conf LXC_SCRIPT=$DOCKLET_CONF/lxc-script DOCKLET_SRC=$DOCKLET_HOME/src DOCKLET_LIB=$DOCKLET_SRC DOCKLET_WEB=$DOCKLET_HOME/web DOCKLET_USER=$DOCKLET_HOME/user # default working directory, default to /opt/docklet FS_PREFIX=/opt/docklet RUN_DIR=$FS_PREFIX/local/run LOG_DIR=$FS_PREFIX/local/log #network interface , default is eth0 NETWORK_DEVICE=eth0 #etcd server address, default is localhost:2379 ETCD=localhost:2379 #unique cluster_name, default is docklet-vc CLUSTER_NAME=docklet-vc #web port, default is 8888 WEB_PORT=8888 USER_PORT=9100 #cluster net, default is 172.16.0.1/16 CLUSTER_NET="172.16.0.1/16" #configurable-http-proxy public port, default is 8000 PROXY_PORT=8000 #configurable-http-proxy api port, default is 8001 PROXY_API_PORT=8001 DISTRIBUTED_GATEWAY=False . $DOCKLET_CONF/docklet.conf export FS_PREFIX # This next line determines what user the script runs as. DAEMON_USER=root # settings for docklet master DAEMON_MASTER=$DOCKLET_LIB/httprest.py DAEMON_NAME_MASTER=docklet-master DAEMON_OPTS_MASTER= # The process ID of the script when it runs is stored here: PIDFILE_MASTER=$RUN_DIR/$DAEMON_NAME_MASTER.pid # settings for docklet web DAEMON_WEB=$DOCKLET_WEB/web.py DAEMON_NAME_WEB=docklet-web PIDFILE_WEB=$RUN_DIR/docklet-web.pid DAEMON_OPTS_WEB= # settings for docklet proxy, which is required for web access DAEMON_PROXY=`which configurable-http-proxy` DAEMON_NAME_PROXY=docklet-proxy PIDFILE_PROXY=$RUN_DIR/proxy.pid DAEMON_OPTS_PROXY= # settings for docklet user DAEMON_USER_MODULE=$DOCKLET_USER/user.py DAEMON_NAME_USER=docklet-user PIDFILE_USER=$RUN_DIR/docklet-user.pid DAEMON_OPTS_USER= RUNNING_CONFIG=$FS_PREFIX/local/docklet-running.conf export CONFIG=$RUNNING_CONFIG . /lib/lsb/init-functions ########### pre_start_master () { log_daemon_msg "Starting $DAEMON_NAME_MASTER in $FS_PREFIX" [ ! -d $FS_PREFIX/global ] && mkdir -p $FS_PREFIX/global [ ! -d $FS_PREFIX/local ] && mkdir -p $FS_PREFIX/local [ ! -d $FS_PREFIX/global/users ] && mkdir -p $FS_PREFIX/global/users [ ! -d $FS_PREFIX/global/sys ] && mkdir -p $FS_PREFIX/global/sys [ ! -d $FS_PREFIX/global/images/private ] && mkdir -p $FS_PREFIX/global/images/private [ ! -d $FS_PREFIX/global/images/public ] && mkdir -p $FS_PREFIX/global/images/public [ ! -d $FS_PREFIX/local/volume ] && mkdir -p $FS_PREFIX/local/volume [ ! -d $FS_PREFIX/local/temp ] && mkdir -p $FS_PREFIX/local/temp [ ! -d $FS_PREFIX/local/run ] && mkdir -p $FS_PREFIX/local/run [ ! -d $FS_PREFIX/local/log ] && mkdir -p $FS_PREFIX/local/log grep -P "^[\s]*[a-zA-Z]" $DOCKLET_CONF/docklet.conf > $RUNNING_CONFIG echo "DOCKLET_HOME=$DOCKLET_HOME" >> $RUNNING_CONFIG echo "DOCKLET_BIN=$DOCKLET_BIN" >> $RUNNING_CONFIG echo "DOCKLET_CONF=$DOCKLET_CONF" >> $RUNNING_CONFIG echo "LXC_SCRIPT=$LXC_SCRIPT" >> $RUNNING_CONFIG echo "DOCKLET_SRC=$DOCKLET_SRC" >> $RUNNING_CONFIG echo "DOCKLET_LIB=$DOCKLET_LIB" >> $RUNNING_CONFIG # iptables for NAT network for containers to access web iptables -t nat -F iptables -t nat -A POSTROUTING -s $CLUSTER_NET -j MASQUERADE } do_start_master () { pre_start_master DAEMON_OPTS_MASTER=$1 # MODE : start mode # new : clean old data in etcd, global directory and start a new cluster # recovery : start cluster and recover status from etcd and global directory # Default is "recovery" start-stop-daemon --start --oknodo --background --pidfile $PIDFILE_MASTER --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --startas $DAEMON_MASTER -- $DAEMON_OPTS_MASTER log_end_msg $? } pre_start_web () { log_daemon_msg "Starting $DAEMON_NAME_WEB in $FS_PREFIX" webip=$(ip addr show $NETWORK_DEVICE | grep -oE "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/[0-9]+") [ $? != "0" ] && echo "wrong NETWORK_DEVICE $NETWORK_DEVICE" && exit 1 webip=${webip%/*} AUTH_COOKIE_URL=http://$webip:$WEB_PORT/jupyter #echo "set AUTH_COOKIE_URL:$AUTH_COOKIE_URL in etcd with key:$CLUSTER_NAME/web/authurl" curl -XPUT http://$ETCD/v2/keys/$CLUSTER_NAME/web/authurl -d value="$AUTH_COOKIE_URL" > /dev/null 2>&1 [ $? != 0 ] && echo "set AUTH_COOKIE_URL failed in etcd" && exit 1 } do_start_web () { pre_start_web DAEMON_OPTS_WEB="-p $WEB_PORT" start-stop-daemon --start --background --pidfile $PIDFILE_WEB --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --startas $DAEMON_WEB -- $DAEMON_OPTS_WEB log_end_msg $? } do_start_proxy () { if [ "$DISTRIBUTED_GATEWAY" = "True" ] then return 1 fi log_daemon_msg "Starting $DAEMON_NAME_PROXY daemon in $FS_PREFIX" DAEMON_OPTS_PROXY="--port $PROXY_PORT --api-port $PROXY_API_PORT --default-target=http://localhost:8888" start-stop-daemon --start --background --pidfile $PIDFILE_PROXY --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --startas $DAEMON_PROXY -- $DAEMON_OPTS_PROXY log_end_msg $? } do_start_user () { log_daemon_msg "Starting $DAEMON_NAME_USER in $FS_PREFIX" DAEMON_OPTS_USER="-p $USER_PORT" start-stop-daemon --start --background --pidfile $PIDFILE_USER --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --startas $DAEMON_USER_MODULE -- $DAEMON_OPTS_USER log_end_msg $? } do_stop_master () { log_daemon_msg "Stopping $DAEMON_NAME_MASTER daemon" start-stop-daemon --stop --quiet --oknodo --remove-pidfile --pidfile $PIDFILE_MASTER --retry 10 log_end_msg $? } do_stop_web () { log_daemon_msg "Stopping $DAEMON_NAME_WEB daemon" start-stop-daemon --stop --quiet --oknodo --remove-pidfile --pidfile $PIDFILE_WEB --retry 10 log_end_msg $? } do_stop_proxy () { if [ "$DISTRIBUTED_GATEWAY" = "True" ] then return 1 fi log_daemon_msg "Stopping $DAEMON_NAME_PROXY daemon" start-stop-daemon --stop --quiet --oknodo --remove-pidfile --pidfile $PIDFILE_PROXY --retry 10 log_end_msg $? } do_stop_user () { log_daemon_msg "Stopping $DAEMON_NAME_USER daemon" start-stop-daemon --stop --quiet --oknodo --remove-pidfile --pidfile $PIDFILE_USER --retry 10 log_end_msg $? } case "$1" in init) do_start_master "new" do_start_proxy ;; start) do_start_master "recovery" do_start_proxy ;; stop) do_stop_master do_stop_proxy ;; restart) do_stop_master do_stop_proxy do_start_master "recovery" do_start_proxy ;; start_proxy) do_start_proxy ;; start_web) do_start_web ;; stop_web) do_stop_web ;; reinit) do_stop_master do_stop_proxy do_start_master "new" do_start_proxy ;; status) status=0 status_of_proc -p $PIDFILE_MASTER "$DAEMON_MASTER" "$DAEMON_NAME_MASTER" || status=$? status_of_proc -p $PIDFILE_PROXY "$DAEMON_PROXY" "$DAEMON_NAME_PROXY" || status=$? exit $status ;; *) echo "Usage: $DAEMON_NAME_MASTER {init|start|stop|restart|reinit|status|start_proxy|stop_proxy|start_web|stop_web}" exit 1 ;; esac exit 0