#!/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 #configurable-http-proxy public port, default is 8000 PROXY_PORT=8000 #configurable-http-proxy api port, default is 8001 PROXY_API_PORT=8001 #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" # ip addresses range of containers for batch job, default is 10.16.0.0/16 BATCH_NET="10.16.0.0/16" . $DOCKLET_CONF/docklet.conf export FS_PREFIX RUN_DIR=$FS_PREFIX/local/run LOG_DIR=$FS_PREFIX/local/log # This next line determines what user the script runs as. DAEMON_USER=root # settings for docklet master DAEMON_MASTER=$DOCKLET_LIB/master/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 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 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 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 iptables -t nat -A POSTROUTING -s $BATCH_NET -j MASQUERADE } do_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" $DOCKLET_HOME/tools/nginx_config.sh 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 $? } 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 $? } 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_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_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_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_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) pre_start_master do_start_user do_start_proxy do_start_web do_start_master "new" ;; start) pre_start_master do_start_user do_start_proxy do_start_web do_start_master "recovery" ;; stop) do_stop_web do_stop_proxy do_stop_master do_stop_user ;; restart) do_stop_user do_stop_web do_stop_proxy do_stop_master pre_start_master do_start_user do_start_proxy do_start_web do_start_master "recovery" ;; start_proxy) do_start_proxy ;; stop_proxy) do_stop_proxy ;; start_web) do_start_web ;; stop_web) do_stop_web ;; reinit) do_stop_web do_stop_proxy do_stop_master do_stop_user pre_start_master do_start_user do_start_proxy do_start_web do_start_master "new" ;; 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=$? status_of_proc -p $PIDFILE_WEB "$DAEMON_WEB" "$DAEMON_NAME_WEB" || status=$? status_of_proc -p $PIDFILE_USER "$DAEMON_USER" "$DAEMON_NAME_USER" || 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