khanat-server-docker-NeL/builder/debian/stretch/x86_64/build.sh

346 lines
9.7 KiB
Bash
Raw Permalink Normal View History

#!/bin/bash
#
# Script to build khaganat server
#
# 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 <https://www.gnu.org/licenses/>.
#
declare -i REMOVE=0
declare -i IMAGE=0
declare -i BUILD=1
declare -i DEBUG=0
declare MAKEOPTS=""
declare CMAKEOPTS=""
declare DOCKEROPTS=""
declare DIRBUILD=""
declare CLEANDOCKER=0
declare -i CLEANIMAGENONE=0
declare -i AUTODETEC=1
declare DOCKERBUILDOPT=""
declare OPTION=""
declare -i DONTCOPYSOURCE=0
declare IMAGEDOCKER="opennel/builder_debian_stretch_x86_64"
declare LOCALBUILDDIR="build/$IMAGEDOCKER"
declare LOCALSRC="debian/stretch/x86_64"
declare DIRPACKAGE="output/opennel_debian_stretch_x86_64/package"
declare PACKAGEBIN="$DIRPACKAGE/ryzomcore_stretch_x86_64.tar.gz"
declare PACKAGEREF="$DIRPACKAGE/khanat-ref-ressources.tar.gz"
usage()
{
cat << EOF
usage:$0 [options]
script to build under docker
Step:
1) clean old build directory
2) create image builder
3) launch build under docker (launch script build-under-docker.sh)
4) remove docker container with state exited and clean docker images NONE
options:
-h, --help : Show this help
-d, --debug : Show debug message
-r, --remove : Remove old build repository
-f, --force-create-image : force to create docker image (use to build khanat) - default autodetec.
-j [N], --jobs[=N] Allow N jobs at once; infinite jobs with no arg.
-a OPTS, --add-opts-cmake=OPTS : Adding options on cmake command (before build)
-n OPTS, --add-opts-make=OPTS : Adding options on make command (build option)
--cxxflags=[String] : adding cxx flags when generate Makefile (and build)
-c, --clean-container : remove all container in state Exited
-w, --clean-images-none : Remove images docker (with name 'none')
-m OPTS, --add-opts-docker=OPTS : Adding options on docker command (when build)
--only-build-server : adding option to build only server
--opennel-code-dir=<Directory> : localization code khanat (khanat-opennel-code)
2018-08-05 11:36:12 +00:00
--opennel-pymanager-dir=<Directory> : localization code khanat (opennel-pymanager)
--disable-build-opennel-code : Disable build on opennel-code
--disable-package-opennel-pymanager : Disable package on opennel-pymanager
--dont-copy-source : disable copy source, work directly on source (apply patch)
Example :
cd [root Khanat directory]
$0 -c -r -m '-m 20g'
$0 -c -r -j 4 -a '-DWITH_SYMBOLS=ON' -a '-DWITH_RYZOM_TOOLS=OFF' -a '-DWITH_NEL_TOOLS=OFF' -m '-m 20g' -d
$0 -a '-DCMAKE_BUILD_TYPE=Debug -DWITH_SYMBOLS=ON -DFINAL_VERSION=OFF' -f
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
#
calldir="$(dirname $0)"
basedir=$(cd $calldir; pwd)
rootdir="$(dirname $(dirname $(dirname $(dirname ${basedir}))))"
codedir="$(dirname $rootdir)/khanat-opennel-code"
2018-08-05 11:36:12 +00:00
pymanagerdir="$(dirname $rootdir)/opennel-pymanager"
while test $# -gt 0
do
case "$1" in
-h|--help)
usage
exit 1
;;
-d|--debug)
DEBUG=1
OPTION="$OPTION -d"
shift
;;
-c|--clean-container)
CLEANDOCKER=1
shift
;;
-r|--remove)
REMOVE=1
shift
;;
-f|--force-create-image)
IMAGE=1
DOCKERBUILDOPT="$DOCKERBUILDOPT --no-cache"
shift
;;
-j)
shift
# search next argument is value or new argument
if [[ ${1:0:1} == "-" ]]
then
MAKEOPTS="$MAKEOPTS -j"
else
MAKEOPTS="$MAKEOPTS -j $1"
shift
fi
;;
--jobs*)
if [[ ${1#*=} == "" ]]
then
MAKEOPTS="$MAKEOPTS -j"
else
MAKEOPTS="$MAKEOPTS -j ${1#*=}"
fi
shift
;;
-a)
shift
CMAKEOPTS="$CMAKEOPTS $1"
shift
;;
--add-opts-cmake=*)
CMAKEOPTS="$CMAKEOPTS ${1#*=}"
shift
;;
-n)
shift
MAKEOPTS="$MAKEOPTS $1"
shift
;;
--add-opts-make=*)
MAKEOPTS="$MAKEOPTS ${1#*=}"
shift
;;
-m)
shift
DOCKEROPTS="$DOCKEROPTS $1"
shift
;;
--add-opts-docker=*)
DOCKEROPTS="$DOCKEROPTS ${1#*=}"
shift
;;
--cxxflags=*)
OPTION="$OPTION --cxxflags=${1#*=}"
shift
;;
--only-build-server)
CMAKEOPTS="$CMAKEOPTS -DWITH_NEL_TESTS=OFF -DWITH_RYZOM_CLIENT=OFF -DWITH_DRIVER_OPENGL=OFF -DWITH_DRIVER_OPENAL=OFF -DWITH_NEL_SAMPLES=OFF -DWITH_SOUND=OFF"
shift
;;
--opennel-code-dir=*)
codedir="${1#*=}"
shift
;;
2018-08-05 11:36:12 +00:00
--opennel-pymanager-dir=*)
pymanagerdir="${1#*=}"
shift
;;
-w|--clean-images-none)
CLEANIMAGENONE=1
shift
;;
--dont-copy-source)
DONTCOPYSOURCE=1
OPTION="$OPTION --dont-copy-source"
shift
;;
2018-08-05 11:36:12 +00:00
--disable-build-opennel-code)
OPTION="$OPTION --disable-build-opennel-code"
shift
;;
--disable-package-opennel-pymanager)
OPTION="$OPTION --disable-package-opennel-pymanager"
shift
;;
*)
msg_error "options '$1' not recognize"
usage
exit 1
;;
esac
done
function chrashed()
{
msg_error "BUILD FAILED (code:$?)"
exit 2
}
trap chrashed EXIT
msg_debug "prg: $0"
docker -v 1>/dev/null
if [[ $? -ne 0 ]]
then
echo "*** ERROR docker not installed"
exit 2
fi
if [[ $AUTODETEC -ne 0 ]]
then
if [[ $(docker images -f "reference=$IMAGEDOCKER" | wc -l) -lt 2 ]]
then
IMAGE=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] CMAKEOPTS: '$CMAKEOPTS'"
msg_debug "[$(basename $0):$LINENO] DOCKEROPTS: '$DOCKEROPTS'"
msg_debug "[$(basename $0):$LINENO] DOCKERBUILDOPT: '$DOCKERBUILDOPT'"
msg_debug "[$(basename $0):$LINENO] codedir: '$codedir'"
2018-08-05 11:36:12 +00:00
msg_debug "[$(basename $0):$LINENO] pymanagerdir: '$pymanagerdir'"
msg_debug "[$(basename $0):$LINENO] OPTION: '$OPTION'"
msg_info "[$(basename $0):$LINENO] check khanat-opennel-code"
if [[ ! -d ${codedir} ]]
then
msg_error "[$(basename $0):$LINENO] Missing directory khanat-opennel-code [${codedir}]"
cat << EOF
You need clone repo khanat-opennel-code.
cd $(dirname ${rootdir})
git clone https://git.khaganat.net/khaganat/mmorpg_khanat/khanat-opennel-code.git
EOF
exit 2
fi
mkdir -p "${DIRBUILD}"
if [[ $REMOVE -ne 0 ]]
then
msg_info "REMOVE OLD BUILD"
rm -rf ${DIRBUILD}/* || exit 2
fi
touch ${DIRBUILD}/build.log
cat << EOF > ${DIRBUILD}/envi.sh
#!/bin/bash
export MAKEOPTS="$MAKEOPTS"
export CMAKEOPTS="$CMAKEOPTS"
EOF
if [[ $IMAGE -ne 0 ]]
then
msg_info "GENERATE DOCKER IMAGE"
msg_debug "cd $projectdir/builder; docker build . ${DOCKERBUILDOPT} -t ${IMAGEDOCKER} --file \"${basedir}/Dockerfile\""
cd $rootdir/builder; docker build . ${DOCKERBUILDOPT} -t ${IMAGEDOCKER} \
--file "${basedir}/Dockerfile" || exit 2
fi
if [[ $BUILD -ne 0 ]]
then
msg_info "BUILD"
msg_debug "cd $rootdir; docker run -it \
--hostname=builder \
-u "$(id -u $USERNAME):$(id -g $USERNAME)" \
-v $rootdir/builder:/opt/ref/builder:ro \
2018-08-05 11:36:12 +00:00
-v $codedir:/opt/ref/opennel-code:rw \
-v $pymanagerdir:/opt/ref/opennel-pymanager:rw \
-v $rootdir/build:/opt/build \
-v /etc/localtime:/etc/localtime:ro \
${DOCKEROPTS} \
${IMAGEDOCKER} \
/opt/ref/builder/${LOCALSRC}/build-under-docker.sh --add-opts-make=\"$MAKEOPTS\" --add-opts-cmake=\"$CMAKEOPTS\" --build-dir=\"/opt/${LOCALBUILDDIR}\" --code-dir=\"/opt/ref/opennel-code\" $OPTION"
cd $rootdir; docker run -it \
--hostname=builder \
-u "$(id -u $USERNAME):$(id -g $USERNAME)" \
-v $rootdir/builder:/opt/ref/builder:ro \
-v $codedir:/opt/ref/opennel-code:rw \
2018-08-05 11:36:12 +00:00
-v $pymanagerdir:/opt/ref/opennel-pymanager:ro \
-v $rootdir/build:/opt/build \
-v /etc/localtime:/etc/localtime:ro \
${DOCKEROPTS} \
${IMAGEDOCKER} \
2018-08-05 11:36:12 +00:00
/opt/ref/builder/${LOCALSRC}/build-under-docker.sh --add-opts-make="$MAKEOPTS" --add-opts-cmake="$CMAKEOPTS" --build-dir="/opt/${LOCALBUILDDIR}" --code-dir="/opt/ref/opennel-code" --opennel-pymanager-dir="/opt/ref/opennel-pymanager" $OPTION || exit 2
fi
if [[ $CLEANDOCKER -ne 0 ]]
then
msg_info "CLEAN CONTAINER"
docker rm --force `docker ps -qf 'status=exited' -f "ancestor=${IMAGEDOCKER}"` || exit 2
fi
if [[ $CLEANIMAGENONE -ne 0 ]]
then
msg_info "CLEAN IMAGE DOCKER 'NONE'"
2017-11-22 20:13:50 +00:00
listimages=( $(docker images --filter "dangling=true" --format "{{.ID}}") )
msg_debug "IMAGES NONE : ${listimages[@]}"
if [[ -n "${listimages[@]}" ]]
then
docker rmi --force "${listimages[@]}" || exit 2
fi
fi
trap '' EXIT
msg_info "END"