2016-11-11 04:54:47 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2016-11-21 04:56:33 +00:00
|
|
|
usage () {
|
2016-11-21 05:07:09 +00:00
|
|
|
echo "Usage: launcher COMMAND [--skip-prereqs] [--docker-args STRING]"
|
2016-11-21 04:56:33 +00:00
|
|
|
echo "Commands:"
|
|
|
|
echo " start: Start/initialize a container"
|
|
|
|
echo " stop: Stop a running container"
|
|
|
|
echo " restart: Restart a container"
|
|
|
|
echo " destroy: Stop and remove a container"
|
|
|
|
echo " enter: Open a shell to run commands inside the container"
|
|
|
|
echo " logs: View the Docker logs for a container"
|
|
|
|
echo " bootstrap: Bootstrap a container for the config based on a template"
|
|
|
|
echo " rebuild: Rebuild a container (destroy old, bootstrap, start new)"
|
|
|
|
echo " cleanup: Remove all containers that have stopped for > 24 hours"
|
|
|
|
echo
|
|
|
|
echo "Options:"
|
|
|
|
echo " --skip-prereqs Don't check launcher prerequisites"
|
|
|
|
echo " --docker-args Extra arguments to pass when running docker"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2016-11-11 04:54:47 +00:00
|
|
|
set -e
|
|
|
|
set -o pipefail
|
|
|
|
|
|
|
|
version=6.0.5
|
|
|
|
image=seafileorg/server:$version
|
2016-11-15 08:59:02 +00:00
|
|
|
local_image=local_seafile/server:latest
|
2016-11-12 07:15:26 +00:00
|
|
|
dockerdir=$(cd "$(dirname $0)"; pwd -P)
|
2016-11-12 03:28:49 +00:00
|
|
|
sharedir=$dockerdir/shared
|
|
|
|
installdir=/opt/seafile/seafile-server-$version
|
2016-11-16 01:46:08 +00:00
|
|
|
bootstrap_conf=$dockerdir/bootstrap/bootstrap.conf
|
2016-11-11 04:54:47 +00:00
|
|
|
|
2016-11-12 03:28:49 +00:00
|
|
|
cd $dockerdir
|
2016-11-11 04:54:47 +00:00
|
|
|
|
|
|
|
dbg() {
|
|
|
|
if [[ $debug == "true" ]]; then
|
|
|
|
echo "dbg: $1"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-11-21 04:56:33 +00:00
|
|
|
install_docker() {
|
|
|
|
echo "Docker is not installed, you will need to install Docker in order to run Launcher"
|
|
|
|
echo "See https://docs.docker.com/installation/"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2016-11-11 04:54:47 +00:00
|
|
|
err_and_quit () {
|
|
|
|
printf "\n\n\033[33mError: %s\033[m\n\n" "$1"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2016-11-12 06:03:52 +00:00
|
|
|
init_shared() {
|
2016-11-14 06:47:40 +00:00
|
|
|
mkdir -p $sharedir/{seafile,db}
|
2016-11-12 06:03:52 +00:00
|
|
|
mkdir -p $sharedir/logs/{seafile,var-log}
|
2016-11-12 07:15:26 +00:00
|
|
|
|
|
|
|
mkdir -p $sharedir/logs/var-log/nginx
|
2016-11-12 06:03:52 +00:00
|
|
|
touch $sharedir/logs/var-log/syslog
|
2016-11-11 04:54:47 +00:00
|
|
|
|
|
|
|
local bash_history=$sharedir/.bash_history
|
|
|
|
if [[ ! -e $bash_history ]]; then
|
|
|
|
touch $bash_history
|
|
|
|
fi
|
2016-11-12 06:03:52 +00:00
|
|
|
}
|
|
|
|
|
2016-11-15 04:11:58 +00:00
|
|
|
set_ports() {
|
2016-11-21 05:07:09 +00:00
|
|
|
ports=$(docker run $user_args --rm -it \
|
2016-11-16 01:46:08 +00:00
|
|
|
-v ${dockerdir}/scripts:/scripts \
|
|
|
|
-v ${dockerdir}/bootstrap:/bootstrap:ro \
|
|
|
|
$image \
|
|
|
|
/scripts/bootstrap.py --parse-ports)
|
2016-11-15 04:11:58 +00:00
|
|
|
}
|
|
|
|
|
2016-11-15 08:59:02 +00:00
|
|
|
set_bootstrap_volumes() {
|
2016-11-12 06:03:52 +00:00
|
|
|
local mounts
|
|
|
|
init_shared
|
2016-11-11 04:54:47 +00:00
|
|
|
|
|
|
|
mounts=(
|
|
|
|
$sharedir:/shared
|
2016-11-12 05:27:24 +00:00
|
|
|
$sharedir/logs/var-log:/var/log
|
2016-11-14 06:47:40 +00:00
|
|
|
$sharedir/db:/var/lib/mysql
|
2016-11-15 08:59:02 +00:00
|
|
|
$dockerdir/bootstrap:/bootstrap
|
2016-11-12 03:28:49 +00:00
|
|
|
$dockerdir/scripts:/scripts:ro
|
2016-11-15 08:59:02 +00:00
|
|
|
$dockerdir/templates:/templates:ro
|
2016-11-12 03:28:49 +00:00
|
|
|
$dockerdir/scripts/tmp/check_init_admin.py:$installdir/check_init_admin.py:ro
|
2016-11-12 06:03:52 +00:00
|
|
|
$sharedir/.bash_history:/root/.bash_history
|
2016-11-11 04:54:47 +00:00
|
|
|
)
|
|
|
|
volumes=""
|
|
|
|
local m
|
|
|
|
for m in ${mounts[*]}; do
|
|
|
|
volumes="$volumes -v $m"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2016-11-15 08:59:02 +00:00
|
|
|
set_volumes() {
|
|
|
|
local mounts
|
|
|
|
init_shared
|
|
|
|
|
|
|
|
mounts=(
|
|
|
|
$sharedir:/shared
|
|
|
|
$sharedir/logs/var-log:/var/log
|
|
|
|
$sharedir/db:/var/lib/mysql
|
|
|
|
$sharedir/.bash_history:/root/.bash_history
|
|
|
|
)
|
|
|
|
volumes=""
|
|
|
|
local m
|
|
|
|
for m in ${mounts[*]}; do
|
|
|
|
volumes="$volumes -v $m"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2016-11-21 04:56:33 +00:00
|
|
|
set_existing_container() {
|
|
|
|
existing=$(docker ps -a | awk '{ print $1, $(NF) }' | grep " seafile$" | awk '{ print $1 }' || true)
|
|
|
|
}
|
|
|
|
|
2016-11-11 04:54:47 +00:00
|
|
|
bootstrap() {
|
2016-11-12 07:15:26 +00:00
|
|
|
if [[ ! -e $bootstrap_conf ]]; then
|
|
|
|
err_and_quit "The file $bootstrap_conf doesn't exist. Have you run seafile-server-setup?"
|
|
|
|
fi
|
2016-11-15 08:59:02 +00:00
|
|
|
|
|
|
|
# First initialize seafile server and letsencrypt
|
|
|
|
set_bootstrap_volumes
|
2016-11-15 04:11:58 +00:00
|
|
|
set_ports
|
2016-11-15 08:59:02 +00:00
|
|
|
|
2016-11-21 05:07:09 +00:00
|
|
|
docker run $user_args --rm -it --name seafile-bootstrap -e SEAFILE_BOOTSRAP=1 $volumes $ports $image /sbin/my_init -- /scripts/bootstrap.py
|
2016-11-15 08:59:02 +00:00
|
|
|
|
|
|
|
docker build -f bootstrap/generated/Dockerfile -t local_seafile/server:latest .
|
2016-11-11 04:54:47 +00:00
|
|
|
}
|
|
|
|
|
2016-11-15 08:59:02 +00:00
|
|
|
oldstart() {
|
2016-11-11 04:54:47 +00:00
|
|
|
set_volumes
|
2016-11-15 04:11:58 +00:00
|
|
|
set_ports
|
2016-11-21 05:07:09 +00:00
|
|
|
docker run $user_args --rm -it --name seafile $volumes $ports $image \
|
2016-11-15 08:59:02 +00:00
|
|
|
/sbin/my_init -- bash -l
|
|
|
|
# /sbin/my_init -- /scripts/start.py
|
|
|
|
}
|
|
|
|
|
|
|
|
start() {
|
2016-11-21 04:56:33 +00:00
|
|
|
existing=$(docker ps | awk '{ print $1, $(NF) }' | grep " seafile$" | awk '{ print $1 }' || true)
|
|
|
|
if [[ $existing != "" ]]; then
|
|
|
|
echo "Nothing to do, your container has already started!"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
set_existing_container
|
|
|
|
if [[ $existing != "" ]]; then
|
|
|
|
echo "starting up existing container"
|
|
|
|
(
|
|
|
|
set -x
|
|
|
|
docker start seafile
|
|
|
|
)
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2016-11-15 08:59:02 +00:00
|
|
|
set_volumes
|
|
|
|
set_ports
|
2016-11-21 04:56:33 +00:00
|
|
|
|
2016-11-21 05:07:09 +00:00
|
|
|
|
|
|
|
local restart_policy attach_on_run
|
|
|
|
if [[ "${SUPERVISED}" = "true" ]]; then
|
|
|
|
restart_policy="--restart=no"
|
|
|
|
attach_on_run="-a stdout -a stderr"
|
|
|
|
else
|
|
|
|
attach_on_run="-d"
|
|
|
|
fi
|
|
|
|
|
2016-11-21 04:56:33 +00:00
|
|
|
(
|
|
|
|
set -x
|
2016-11-21 05:07:09 +00:00
|
|
|
docker run $user_args $attach_on_run $restart_policy --name seafile $volumes $ports $local_image
|
2016-11-21 04:56:33 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
stop() {
|
|
|
|
set_existing_container
|
|
|
|
if [[ $existing != "" ]]; then
|
|
|
|
(
|
|
|
|
set -x
|
|
|
|
docker stop -t 10 seafile
|
|
|
|
)
|
|
|
|
else
|
|
|
|
echo "seafile was not started !"
|
|
|
|
exit 1
|
|
|
|
fi
|
2016-11-11 04:54:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enter() {
|
2016-11-21 04:56:33 +00:00
|
|
|
set_existing_container
|
|
|
|
if [[ $existing != "" ]]; then
|
|
|
|
(
|
|
|
|
set -x
|
|
|
|
docker exec -it seafile /bin/bash
|
|
|
|
)
|
|
|
|
else
|
|
|
|
echo "seafile was not started !"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
restart() {
|
|
|
|
stop
|
|
|
|
start
|
|
|
|
}
|
|
|
|
|
|
|
|
check_prereqs() {
|
2016-11-21 05:07:09 +00:00
|
|
|
if [[ $skip_prereqs == "true" ]]; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# check docker
|
2016-11-21 04:56:33 +00:00
|
|
|
if ! which docker >/dev/null; then
|
|
|
|
install_docker
|
|
|
|
fi
|
2016-11-21 05:07:09 +00:00
|
|
|
# TODO: check git version
|
|
|
|
}
|
|
|
|
|
|
|
|
logs() {
|
|
|
|
err_and_quit "Not implemented yet"
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
err_and_quit "Not implemented yet"
|
|
|
|
}
|
|
|
|
|
|
|
|
rebuild() {
|
|
|
|
err_and_quit "Not implemented yet"
|
2016-11-11 04:54:47 +00:00
|
|
|
}
|
|
|
|
|
2016-11-15 04:11:58 +00:00
|
|
|
main() {
|
2016-11-11 04:54:47 +00:00
|
|
|
local action
|
|
|
|
while [[ $# -gt 0 ]]
|
|
|
|
do
|
|
|
|
case "$1" in
|
2016-11-21 05:07:09 +00:00
|
|
|
bootstrap|oldstart|start|stop|restart|enter|destroy|logs|rebuild)
|
2016-11-21 04:56:33 +00:00
|
|
|
action=$1 ; shift 1 ;;
|
2016-11-15 08:59:02 +00:00
|
|
|
--debug) debug=true ; shift 1 ;;
|
2016-11-21 05:07:09 +00:00
|
|
|
--skip-prereqs) skip_prereqs=true ; shift 1 ;;
|
|
|
|
--docker-args) user_args=$2 ; shift 2 ;;
|
2016-11-15 08:59:02 +00:00
|
|
|
*) err_and_quit "Argument error. Please see help." ;;
|
2016-11-11 04:54:47 +00:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
"$action"
|
|
|
|
}
|
|
|
|
|
2016-11-21 04:56:33 +00:00
|
|
|
check_prereqs
|
2016-11-11 04:54:47 +00:00
|
|
|
main "$@"
|