#!/bin/bash # # Script to start khanat server (with docker) # All data are include on host # # Copyright (C) 2017 AleaJactaEst # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . declare -i REMOVE=0 declare -i IMAGE=1 declare -i BASICSERVER=0 declare -i KHANATSERVER=0 declare -i CONFIGUREKHANATSERVER=0 declare -i LAUNCHKHANAT=1 declare -i UPDATEDATA=0 declare -i DEBUG=0 declare -i AUTODETEC=1 declare -i STOPKHANAT=0 declare -i CLEANCONTAINERKHANAT=0 declare -i CONNECTSSHKHANAT=0 declare -i KHANAT_CLIENT_VERSION=1 declare -i SHOWIPKHANATSERVER=0 declare METHODSTARTSERVER="--start-khanat-with-screen" declare -i CLEANIMAGENONE=0 declare DOCKERBUILDOPT="" declare IMAGEGENERICSERVER="opennel/server_generic_debian_jessie_i686" declare IMAGEKHANATSERVER="opennel/servercontainer_debian_jessie_i686" declare LOCALBUILDDIR="build/opennel/builder_debian_jessie_i686" declare DIROUTPUT="output/opennel_debian_jessie_i686" declare ROOTDATAKHANAT="$DIROUTPUT/gameserver" declare DIRLOG="$ROOTDATAKHANAT/log" declare DIRDATABASE="$ROOTDATAKHANAT/database" declare ENVIFILE="$DIROUTPUT/khanat_config.sh" declare DIRKHANAT="$ROOTDATAKHANAT/khanat" declare KHANAT_RESSOURCES_DIR="" declare KHANAT_CLIENT_DATA_DIR="" declare OPENNEL_CODE_DIR="" declare STEP_CONFIGURE_OK="$ROOTDATAKHANAT/khanat/step_configure.ok" declare DIRCLIENT="Khanat_Linux32" declare PACKAGECLIENT="smokey_linux32" usage() { cat << EOF usage:$0 [options] script to build under docker Step: 1) create image basic server 2) create image khanat server 3) configure khagant server 4) launch khagant server options: -h, --help : Show this help -d, --debug : Show debug message -r, --remove : Remove directory data khanat -b, --force-basic : Force create/recreate image basic server -k, --force-khanat : Force create/recreate image khanat server -u, --update-data : generate a new patch (client & update server) -g, --configure-khanat : Force configure khanat server -n, --no-launch-khanat : Doesn't launch khanat server -s, --stop-server : Stop server khanat -c, --clean-container-khanat : Remove old server khanat (stopped) -w, --clean-images-none : Remove images docker (with name 'none') -i, --show-ip-khanat-server : show ip address for khanat server (launch under docker) --option-docker-build=[STRING] : you can add option in "docker build" --khanat-ressources-dir=[DIR] : localization khanat-ressources --khanat-client-data-dir=[DIR] : localization khanat-client-data --opennel-code-dir=[DIR] : localization opennel-code --start-khanat-with-screen : start with screen --start-khanat-with-khanat-log : start server khanat and show khanat log --start-khanat-with-full-log : start server khanat and show full log --start-khanat-with-watch-state : start server khanat and show state (loop) --start-khanat-with-watch-state-nagios : start server khanat and show state [nagios format] (loop) --start-khanat-with-bash-after : start server khanat and launch bash --start-with-manager : start khanat with manager --ssh : connect on khanat server (with ssh) [Exclusive action, can't execute other action] --client-version=[INTEGER] : version client khanat (we need to communicate with our server) Example : cd [root Khanat directory] ./server.sh ./server.sh -k ./server.sh --ssh EOF } function msg_debug() { if [[ $DEBUG -ne 0 ]] then echo "$(date "+%Y/%m/%d %H:%M:%S") DEBUG - $*" fi } function msg_info() { echo "$(date "+%Y/%m/%d %H:%M:%S") INFO - $*" } function msg_error() { echo "$(date "+%Y/%m/%d %H:%M:%S") ERROR - $*" >&2 } # # MAIN # #msg_info "START" calldir="$(dirname $0)" basedir=$(cd $calldir; pwd) rootdir="$(dirname $(dirname $(dirname $(dirname ${basedir}))))" ressourcedir="$(dirname ${rootdir})/khanat-ressources" dataclientdir="$(dirname ${rootdir})/khanat-client-data" while test $# -gt 0 do case "$1" in -h|--help) usage exit 1 ;; -d|--debug) DEBUG=1 shift ;; --ssh) CONNECTSSHKHANAT=1 shift ;; -r|--remove) REMOVE=1 CONFIGUREKHANATSERVER=1 shift ;; -c|--clean-container-khanat) CLEANCONTAINERKHANAT=1 shift ;; -s|--stop-server) STOPKHANAT=1 shift ;; -b|--force-basic) BASICSERVER=1 shift ;; -n|--no-launch-khanat) LAUNCHKHANAT=0 shift ;; -k|--force-khanat) KHANATSERVER=1 shift ;; -g|--configure-khanat) CONFIGUREKHANATSERVER=1 shift ;; -i|--show-ip-khanat-server) SHOWIPKHANATSERVER=1 shift ;; --start-khanat-with-screen) METHODSTARTSERVER="--start-khanat-with-screen" shift ;; --start-khanat-with-khanat-log) METHODSTARTSERVER="--show-khanat-log" shift ;; --start-khanat-with-full-log) METHODSTARTSERVER="--show-all-log" shift ;; --start-khanat-with-watch-state) METHODSTARTSERVER="--show-status" shift ;; --start-khanat-with-watch-state-nagios) METHODSTARTSERVER="--show-status-nagios" shift ;; --start-khanat-with-bash-after) METHODSTARTSERVER="--bash-after-start" shift ;; --start-with-manager) METHODSTARTSERVER="--start-with-manager" shift ;; --client-version*) KHANAT_CLIENT_VERSION="${1#*=}" shift ;; --khanat-ressources-dir=*) KHANAT_RESSOURCES_DIR="${1#*=}" shift ;; --khanat-client-data-dir=*) KHANAT_CLIENT_DATA_DIR="${1#*=}" shift ;; --opennel-code-dir=*) OPENNEL_CODE_DIR="${1#*=}" shift ;; -w|--clean-images-none) CLEANIMAGENONE=1 shift ;; -u|--update-data) UPDATEDATA=1 shift ;; --option-docker-build) DOCKERBUILDOPT="$DOCKERBUILDOPT ${1#*=}" shift ;; *) msg_error "[$(basename $0):$LINENO] options '$1' not recognize" usage exit 1 ;; esac done function chrashed() { msg_error "[$(basename $0):$LINENO] BUILD FAILED (code:$?)" exit 2 } trap chrashed EXIT if [[ $SHOWIPKHANATSERVER -eq 0 ]] then msg_info "[$(basename $0):$LINENO] START" fi msg_debug "[$(basename $0):$LINENO] prg: $0" docker -v 1>/dev/null if [[ $? -ne 0 ]] then msg_error "[$(basename $0):$LINENO] docker not installed" exit 2 fi case "$(uname -m)" in x86_64) ;; i686) ;; *) msg_error "[$(basename $0):$LINENO] Unknown archi : $(uname -m)" exit 2 ;; esac if [[ $REMOVE -ne 0 ]] then msg_info "[$(basename $0):$LINENO] REMOVE DATA KHANAT : $ROOTDATAKHANAT" if [[ -n "$ROOTDATAKHANAT" ]] then rm -rf ${ROOTDATAKHANAT}/* || exit 2 fi fi msg_debug "[$(basename $0):$LINENO] Create directory on host ($ROOTDATAKHANAT)" mkdir -p $ROOTDATAKHANAT || exit 2 chmod g+s $ROOTDATAKHANAT if [[ ($CONNECTSSHKHANAT -ne 0) || ($SHOWIPKHANATSERVER -ne 0) ]] then msg_debug "[$(basename $0):$LINENO] clean container" listcontainer="$(docker ps -qf 'status=running' -f 'ancestor='"${IMAGEKHANATSERVER}"'')" if [[ ${#listcontainer[@]} -eq 1 ]] then ipaddress=$(docker inspect --format="{{ .NetworkSettings.IPAddress }}" ${listcontainer[@]}) fi fi if [[ $SHOWIPKHANATSERVER -ne 0 ]] then trap '' EXIT echo "$ipaddress" exit 0 fi if [[ $CONNECTSSHKHANAT -ne 0 ]] then ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no gameserver@$ipaddress trap '' EXIT msg_info "[$(basename $0):$LINENO] END SSH" exit 0 fi if [[ $AUTODETEC -ne 0 ]] then msg_debug "[$(basename $0):$LINENO] Autodetect state" if [[ $(docker images -f "reference=$IMAGEGENERICSERVER" | wc -l) -lt 2 ]] then BASICSERVER=1 fi if [[ $(docker images -f "reference=$IMAGEKHANATSERVER" | wc -l) -lt 2 ]] then KHANATSERVER=1 fi if [[ -z "$KHANAT_RESSOURCES_DIR" ]] then KHANAT_RESSOURCES_DIR="$(dirname $rootdir)/khanat-ressources" fi if [[ -z "$KHANAT_CLIENT_DATA_DIR" ]] then KHANAT_CLIENT_DATA_DIR="$(dirname $rootdir)/khanat-client-data" fi if [[ -z "$OPENNEL_CODE_DIR" ]] then OPENNEL_CODE_DIR="$(dirname $rootdir)/khanat-opennel-code" fi if [[ ! (-f $rootdir/$STEP_CONFIGURE_OK) ]] then CONFIGUREKHANATSERVER=1 fi fi DIRBUILD="${rootdir}/${LOCALBUILDDIR}" msg_debug "[$(basename $0):$LINENO] calldir: $calldir" msg_debug "[$(basename $0):$LINENO] basedir: $basedir" msg_debug "[$(basename $0):$LINENO] rootdir: $rootdir" msg_debug "[$(basename $0):$LINENO] ressourcedir: $ressourcedir" msg_debug "[$(basename $0):$LINENO] generate basic image: $BASICSERVER" msg_debug "[$(basename $0):$LINENO] localization khanat-ressources : ${KHANAT_RESSOURCES_DIR}" msg_debug "[$(basename $0):$LINENO] localization khanat-client-data: ${KHANAT_CLIENT_DATA_DIR}" msg_debug "[$(basename $0):$LINENO] localization opennel-code-client : ${OPENNEL_CODE_DIR}" msg_debug "[$(basename $0):$LINENO] generate khanat image: $KHANATSERVER" msg_debug "[$(basename $0):$LINENO] launch khanat: $LAUNCHKHANAT" msg_debug "[$(basename $0):$LINENO] stop khanat: $STOPKHANAT" msg_info "[$(basename $0):$LINENO] check khanat-ressources" if [[ ! -d ${KHANAT_RESSOURCES_DIR} ]] then msg_error "[$(basename $0):$LINENO] Missing directory khanat-ressources [${KHANAT_RESSOURCES_DIR}]" cat << EOF You need clone repo khanat-ressource. cd $(dirname ${KHANAT_RESSOURCES_DIR}) git clone https://git.khaganat.net/khaganat/mmorpg_khanat/khanat-ressources.git EOF exit 2 fi msg_info "[$(basename $0):$LINENO] check khanat-client-data" if [[ ! -d ${KHANAT_CLIENT_DATA_DIR} ]] then msg_error "[$(basename $0):$LINENO] Missing directory khanat-client-data [${KHANAT_CLIENT_DATA_DIR}]" cat << EOF You need clone repo khanat-client-data. cd $(dirname ${KHANAT_CLIENT_DATA_DIR}) git clone https://git.khaganat.net/khaganat/mmorpg_khanat/khanat-client-data.git EOF exit 2 fi if [[ ! -d ${OPENNEL_CODE_DIR} ]] then msg_error "[$(basename $0):$LINENO] Missing directory opennel-code [${OPENNEL_CODE_DIR}]" cat << EOF You need clone repo khanat-opennel-code. cd $(dirname ${KHANAT_RESSOURCES_DIR}) git clone https://git.khaganat.net/khaganat/mmorpg_khanat/khanat-opennel-code.git EOF exit 2 fi if [[ $STOPKHANAT -ne 0 ]] then msg_info "[$(basename $0):$LINENO] STOP SERVER KHANAT" listcontainer="$(docker ps -qf 'status=running' -f 'ancestor='"${IMAGEKHANATSERVER}"'')" msg_debug "[$(basename $0):$LINENO] CONTAINER KHANAT UP : ${listcontainer[@]}" if [[ -n "$listcontainer" ]] then docker stop "$listcontainer" || exit 2 fi fi if [[ $CLEANCONTAINERKHANAT -ne 0 ]] then msg_info "[$(basename $0):$LINENO] CLEAN CONTAINER KHANAT" listcontainer=( $(docker ps -qf 'status=exited' -f 'ancestor='"${IMAGEKHANATSERVER}"'') ) msg_debug "[$(basename $0):$LINENO] CONTAINER KHANAT EXITED : ${listcontainer[@]}" if [[ -n "${listcontainer[@]}" ]] then docker rm --force "${listcontainer[@]}" || exit 2 fi fi if [[ $CLEANIMAGENONE -ne 0 ]] then msg_info "[$(basename $0):$LINENO] CLEAN IMAGE DOCKER 'NONE'" listimages=( $(docker images --filter "dangling=true" --format "{{.ID}}") ) msg_debug "[$(basename $0):$LINENO] IMAGES NONE : ${listimages[@]}" if [[ -n "${listimages[@]}" ]] then docker rmi --force "${listimages[@]}" || exit 2 fi fi if [[ $BASICSERVER -ne 0 ]] then msg_info "[$(basename $0):$LINENO] GENERATE DOCKER IMAGE BASIC SERVER" cd $rootdir; docker build . ${DOCKERBUILDOPT} -t ${IMAGEGENERICSERVER} \ --file "${basedir}/Dockerfile" || exit 2 fi if [[ $KHANATSERVER -ne 0 ]] then msg_info "[$(basename $0):$LINENO] GENERATE DOCKER IMAGE KHANAT SERVER" cat << EOF > $rootdir/${ENVIFILE} #!/bin/bash # Temporary file, use only to send some information on build docker export KHANAT_CLIENT_VERSION=$KHANAT_CLIENT_VERSION export UIDGAMESERVER=$(id -u) export GIDGAMESERVER=$(id -g) export DIRCLIENT="$DIRCLIENT" export PACKAGECLIENT="$PACKAGECLIENT" EOF cd $rootdir; docker build . ${DOCKERBUILDOPT} -t ${IMAGEKHANATSERVER} \ --file "${basedir}/Dockerfile.khanat.container" || exit 2 fi if [[ $CONFIGUREKHANATSERVER -ne 0 ]] then msg_info "[$(basename $0):$LINENO] CONFIGURE KHANAT SERVER" mkdir -p $DIRLOG || exit 2 mkdir -p $DIRDATABASE || exit 2 msg_debug "[$(basename $0):$LINENO] command : cd $rootdir; docker run -it --hostname=khanat \ -v /etc/localtime:/etc/localtime:ro \ -v ${rootdir}/${LOCALBUILDDIR}/bin:/usr/local/bin:ro \ -v ${OPENNEL_CODE_DIR}/code/web/:/home/gameserver/ext/khanatweb:ro \ -v ${OPENNEL_CODE_DIR}/code/ryzom:/home/gameserver/ext/ryzom-ressources:ro \ -v ${KHANAT_RESSOURCES_DIR}:/home/gameserver/ext/khanat-ressources:ro \ -v ${KHANAT_CLIENT_DATA_DIR}:/home/gameserver/ext/khanat-client-data:ro \ -v ${rootdir}/$DIRLOG:/home/gameserver/log:rw \ -v ${rootdir}/$DIRDATABASE:/home/gameserver/database:rw \ -v ${rootdir}/$DIRKHANAT:/home/gameserver/khanat:rw \ -v ${rootdir}/server/debian/common/:/opt/ext:ro \ ${IMAGEKHANATSERVER} /opt/ext/servercontainer_configure_auto.sh" cd $rootdir; docker run -it --hostname=khanat \ -v /etc/localtime:/etc/localtime:ro \ -v ${rootdir}/${LOCALBUILDDIR}/bin:/usr/local/bin:ro \ -v ${OPENNEL_CODE_DIR}/code/web/:/home/gameserver/ext/khanatweb:ro \ -v ${OPENNEL_CODE_DIR}/code/ryzom:/home/gameserver/ext/ryzom-ressources:ro \ -v ${KHANAT_RESSOURCES_DIR}:/home/gameserver/ext/khanat-ressources:ro \ -v ${KHANAT_CLIENT_DATA_DIR}:/home/gameserver/ext/khanat-client-data:ro \ -v ${rootdir}/$DIRLOG:/home/gameserver/log:rw \ -v ${rootdir}/$DIRDATABASE:/home/gameserver/database:rw \ -v ${rootdir}/$DIRKHANAT:/home/gameserver/khanat:rw \ -v ${rootdir}/server/debian/common/:/opt/ext:ro \ ${IMAGEKHANATSERVER} /opt/ext/servercontainer_configure_auto.sh || exit 2 fi if [[ $UPDATEDATA -ne 0 ]] then msg_info "[$(basename $0):$LINENO] UPDATE KHANAT SERVER" msg_debug "[$(basename $0):$LINENO] command: cd $rootdir; docker run -it --hostname=khanat \ -v /etc/localtime:/etc/localtime:ro \ -v ${rootdir}/${LOCALBUILDDIR}/bin:/usr/local/bin:ro \ -v ${OPENNEL_CODE_DIR}/code/web/:/home/gameserver/ext/khanatweb:ro \ -v ${OPENNEL_CODE_DIR}/code/ryzom:/home/gameserver/ext/ryzom-ressources:ro \ -v ${KHANAT_RESSOURCES_DIR}:/home/gameserver/ext/khanat-ressources:ro \ -v ${KHANAT_CLIENT_DATA_DIR}:/home/gameserver/ext/khanat-client-data:ro \ -v ${rootdir}/$DIRLOG:/home/gameserver/log:rw \ -v ${rootdir}/$DIRDATABASE:/home/gameserver/database:rw \ -v ${rootdir}/$DIRKHANAT:/home/gameserver/khanat:rw \ -v ${rootdir}/server/debian/common/:/opt/ext:ro \ ${IMAGEKHANATSERVER} /opt/ext/servercontainer_update_auto.sh" cd $rootdir; docker run -it --hostname=khanat \ -v /etc/localtime:/etc/localtime:ro \ -v ${rootdir}/${LOCALBUILDDIR}/bin:/usr/local/bin:ro \ -v ${OPENNEL_CODE_DIR}/code/web/:/home/gameserver/ext/khanatweb:ro \ -v ${OPENNEL_CODE_DIR}/code/ryzom:/home/gameserver/ext/ryzom-ressources:ro \ -v ${KHANAT_RESSOURCES_DIR}:/home/gameserver/ext/khanat-ressources:ro \ -v ${KHANAT_CLIENT_DATA_DIR}:/home/gameserver/ext/khanat-client-data:ro \ -v ${rootdir}/$DIRLOG:/home/gameserver/log:rw \ -v ${rootdir}/$DIRDATABASE:/home/gameserver/database:rw \ -v ${rootdir}/$DIRKHANAT:/home/gameserver/khanat:rw \ -v ${rootdir}/server/debian/common/:/opt/ext:ro \ ${IMAGEKHANATSERVER} /opt/ext/servercontainer_update_auto.sh fi if [[ $LAUNCHKHANAT -ne 0 ]] then msg_info "[$(basename $0):$LINENO] START KHANAT SERVER" cd $rootdir; docker run -it --hostname=khanat \ -u "$(id -u $USERNAME):$(id -g $USERNAME)" \ -v /etc/localtime:/etc/localtime:ro \ -v ${rootdir}/${LOCALBUILDDIR}/bin:/usr/local/bin:ro \ -v ${OPENNEL_CODE_DIR}/code/web/:/home/gameserver/ext/khanatweb:ro \ -v ${OPENNEL_CODE_DIR}/code/ryzom:/home/gameserver/ext/ryzom-ressources:ro \ -v ${KHANAT_RESSOURCES_DIR}:/home/gameserver/ext/khanat-ressources:ro \ -v ${KHANAT_CLIENT_DATA_DIR}:/home/gameserver/ext/khanat-client-data:ro \ -v ${rootdir}/$DIRLOG:/home/gameserver/log:rw \ -v ${rootdir}/$DIRDATABASE:/home/gameserver/database:rw \ -v ${rootdir}/$DIRKHANAT:/home/gameserver/khanat:rw \ -v ${rootdir}/server/debian/common/:/opt/ext:ro \ ${IMAGEKHANATSERVER} /opt/ext/servercontainer_launch_auto.sh $METHODSTARTSERVER -d fi trap '' EXIT msg_info "[$(basename $0):$LINENO] END"