docklet/bin/docklet-master

233 lines
6.6 KiB
Plaintext
Raw Normal View History

#!/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
# default working directory, default to /opt/docklet
FS_PREFIX=/opt/docklet
RUN_DIR=$FS_PREFIX/local/run
LOG_DIR=$FS_PREFIX/local/log
#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
#cluster net, default is 172.16.0.1/16
CLUSTER_NET="172.16.0.1/16"
. $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 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=
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 ] && mkdir -p $FS_PREFIX/global/images
[ ! -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 $?
}
do_start_proxy () {
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_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 () {
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 $?
}
case "$1" in
init)
do_start_master "new"
do_start_proxy
do_start_web
;;
start)
do_start_master "recovery"
do_start_proxy
do_start_web
;;
stop)
do_stop_web
do_stop_proxy
do_stop_master
;;
restart)
do_stop_web
do_stop_proxy
do_stop_master
do_start_master "recovery"
do_start_proxy
do_start_web
;;
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_start_master "new"
do_start_proxy
do_start_web
;;
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=$?
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