khanat-server-docker-NeL/server/debian/common/serverimage_init_basic.sh

326 lines
11 KiB
Bash
Raw Permalink Normal View History

#!/bin/bash
#
# Install all package we need to prepare and install khanat 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 DEBUG=0
declare MYSQL_GAMESERVER=0
declare APACHE_GAMESERVER=0
usage()
{
cat << EOF
usage:$0 [options]
script to intialize server
options:
-h, --help : Show this help
-d, --debug : Show debug message
-m, --mysql-gameserver : create database on gameserver account
-a, --apache-gameserver : configure apache to use gameserver account
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
}
msg_info "[$(basename $0):$LINENO] => START"
while test $# -gt 0
do
case "$1" in
-h|--help)
usage
exit 1
;;
-d|--debug)
DEBUG=1
shift
;;
-m|--mysql-gameserver)
MYSQL_GAMESERVER=1
shift
;;
-a|--apache-gameserver)
APACHE_GAMESERVER=1
shift
;;
*)
msg_error "options '$1' not recognize"
usage
exit 1
;;
esac
done
####################################
# Load Environment
####################################
msg_debug "[$(basename $0):$LINENO] Load environment"
if [[ ! -f /opt/khanat_config.sh ]]
then
2017-11-22 20:13:50 +00:00
echo "ERROR - [$(basename $0):$LINENO] missing /opt/khanat_config.sh"
exit 2
fi
source /opt/khanat_config.sh
####################################
# Configure account gameserver
####################################
if [[ $APACHE_GAMESERVER -ne 0 ]]
then
msg_debug "[$(basename $0):$LINENO] Create gameserver account (shared with host)"
groupadd --gid $GIDGAMESERVER gameserver || echo "group id already exist"
useradd --uid $UIDGAMESERVER --gid $GIDGAMESERVER -G sudo -c /home -d /home/gameserver -c "Khanat account GAME" -m -p '$6$nxHX/3u.$azS0.eldpfKqxqOLDjgZj8.hPOLC64arXDTUVX0fs7RZvRBX/pNqPzDR89ccP5XkEE/daOyaD3wVtDGDUND5b/' -s /bin/bash gameserver
echo "group : $(id -g -n gameserver)"
else
msg_debug "[$(basename $0):$LINENO] Create gameserver account"
useradd -G sudo,www-data -c /home -d /home/gameserver -c "Khanat account GAME" -m -p '$6$nxHX/3u.$azS0.eldpfKqxqOLDjgZj8.hPOLC64arXDTUVX0fs7RZvRBX/pNqPzDR89ccP5XkEE/daOyaD3wVtDGDUND5b/' -s /bin/bash -U gameserver
fi
####################################
# Initialize apache
####################################
if [[ $APACHE_GAMESERVER -ne 0 ]]
then
msg_debug "[$(basename $0):$LINENO] Configure apache log"
sed -i -r 's/^export[[:space:]]+APACHE_RUN_USER=(.*)/export APACHE_RUN_USER=gameserver/g' /etc/apache2/envvars || exit 2
sed -i -r 's/^export[[:space:]]+APACHE_RUN_GROUP=(.*)/export APACHE_RUN_GROUP='$(id -g -n gameserver)'/g' /etc/apache2/envvars || exit 2
sed -i -r 's/^export[[:space:]]+APACHE_LOG_DIR=(.*)/export APACHE_LOG_DIR=\/home\/gameserver\/log\/apache2\$SUFFIX/g' /etc/apache2/envvars || exit 2
mkdir -p /home/gameserver/log/apache2 || exit 2
chown -R gameserver:$(id -g -n gameserver) /home/gameserver/log || exit 2
fi
####################################
# Initialize database
####################################
if [[ $MYSQL_GAMESERVER -ne 0 ]]
then
msg_debug "[$(basename $0):$LINENO] Create database for account gameserver"
# Create database on gameserver account (and change directory database)
sed -i -r 's/^user[[:space:]]+=[[:space:]]+(.*)/user = gameserver/g' /etc/mysql/my.cnf || exit 2
sed -i -r 's/^datadir[[:space:]]+=[[:space:]]+(.*)/datadir = \/home\/gameserver\/database/g' /etc/mysql/my.cnf || exit 2
sed -i -r 's/^log_error[[:space:]]+=[[:space:]]+(.*)/log_error = \/home\/gameserver\/log\/mysql\/error\.log/g' /etc/mysql/my.cnf || exit 2
sed -i -r 's/^(#*)general_log_file[[:space:]]+=(.*)/general_log_file = \/home\/gameserver\/log\/mysql\/mysql\.log/g' /etc/mysql/my.cnf || exit 2
sed -i -r 's/^(#*)general_log[[:space:]]+=(.*)/general_log = 1/g' /etc/mysql/my.cnf || exit 2
sed -i -r 's/^(#*)slow_query_log_file[[:space:]]+=(.*)/slow_query_log_file = \/home\/gameserver\/log\/mysql\/mysql-slow\.log/g' /etc/mysql/my.cnf || exit 2
sed -i -r 's/^(#*)slow_query_log[[:space:]]+=(.*)/slow_query_log = 1/g' /etc/mysql/my.cnf || exit 2
sed -i -r 's/^(#*)long_query_time[[:space:]]+=(.*)/long_query_time = 2/g' /etc/mysql/my.cnf || exit 2
sed -i -r 's/^(#*)log_queries_not_using_indexes(.*)/log_queries_not_using_indexes/g' /etc/mysql/my.cnf || exit 2
mkdir -p /home/gameserver/database/ || exit 2
chown gameserver:$(id -g -n gameserver) /home/gameserver/database/ || exit 2
mkdir -p /home/gameserver/log/mysql || exit 2
chown -R gameserver:$(id -g -n gameserver) /home/gameserver/log || exit 2
/usr/bin/mysql_install_db --user=gameserver --skip-name-resolve || exit 2
chown gameserver:$(id -g -n gameserver) /var/run/mysqld/ || exit 2
else
msg_debug "[$(basename $0):$LINENO] Create database"
/usr/bin/mysql_install_db --user=mysql --skip-name-resolve || exit 2
fi
# Start the MySQL daemon in the background.
msg_debug "[$(basename $0):$LINENO] Start database"
/usr/sbin/mysqld &
mysql_pid=$!
# Wait mysql start
msg_debug "[$(basename $0):$LINENO] Check database is started"
until /usr/bin/mysqladmin ping >/dev/null 2>&1
do
echo -n "."
sleep 1
done
# Initialize password root (to empty)
msg_debug "[$(basename $0):$LINENO] configure password root for database"
/usr/bin/mysqladmin -u root password '' || exit 2
msg_debug "[$(basename $0):$LINENO] configure phpmyadmin"
ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf-available/phpmyadmin.conf || exit 2
a2enconf phpmyadmin.conf || exit 2
cp /etc/phpmyadmin/config.inc.php /etc/phpmyadmin/config.inc.php.ref || exit 2
awk '{if($0 ~ /AllowNoPassword/){$1="";}; print $0;}' /etc/phpmyadmin/config.inc.php.ref > /etc/phpmyadmin/config.inc.php || exit 2
# Stop MySQL
msg_debug "[$(basename $0):$LINENO] Stop database"
/usr/bin/mysqladmin shutdown
# Wait MySQL stop
wait $mysql_pid
####################################
# Initialize bashrc (for root)
####################################
msg_debug "[$(basename $0):$LINENO] Configure bashrc (for root)"
cat << EOF > /root/.bashrc
# bashrc: executed by bash(1) for non-login shells.
# You may uncomment the following lines if you want 'ls' to be colorized:
export SHELL=/bin/bash
export LS_OPTIONS='--color=auto'
eval "\`dircolors\`"
alias ls='ls \$LS_OPTIONS'
alias ll='ls \$LS_OPTIONS -l'
alias l='ls \$LS_OPTIONS -lA'
# Some more alias to avoid making mistakes:
# alias rm='rm -i'
# alias cp='cp -i'
# alias mv='mv -i'
# Autocompletion
if [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
# configure environment
export KHANAT_HOME=/home/gameserver
export KHANAT_PATH=/home/gameserver/khanat
export PATH=\$PATH:/usr/local/bin:/usr/local/sbin:\$KHANAT_PATH/tools/scripts/linux
export KHANAT_DATA=/home/gameserver/khanat-ressources
export PATCH_HOME=/home/gameserver
export PATCH_CLIENT_SYSTEM=\$PATCH_HOME/patch_service
export DIRCLIENT="$DIRCLIENT"
export PACKAGECLIENT="$PACKAGECLIENT"
EOF
####################################
# Initialize bashrc (for gameserver)
####################################
msg_debug "[$(basename $0):$LINENO] Configure bashrc (for gameserver)"
cat << EOF > /home/gameserver/.bashrc
# bashrc: executed by bash(1) for non-login shells.
# You may uncomment the following lines if you want 'ls' to be colorized:
export SHELL=/bin/bash
export LS_OPTIONS='--color=auto'
eval "\`dircolors\`"
alias ls='ls \$LS_OPTIONS'
alias ll='ls \$LS_OPTIONS -l'
alias l='ls \$LS_OPTIONS -lA'
# Some more alias to avoid making mistakes:
# alias rm='rm -i'
# alias cp='cp -i'
# alias mv='mv -i'
# Autocompletion
if [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
# configure environment
export KHANAT_HOME=/home/gameserver
export KHANAT_PATH=/home/gameserver/khanat
export PATH=\$PATH:/usr/local/bin:/usr/local/sbin:\$KHANAT_PATH/tools/scripts/linux
export KHANAT_DATA=/home/gameserver/khanat-ressources
export PATCH_HOME=/home/gameserver
export PATCH_CLIENT_SYSTEM=\$PATCH_HOME/patch_service
export DIRCLIENT="$DIRCLIENT"
export PACKAGECLIENT="$PACKAGECLIENT"
EOF
chown gameserver:$(id -g -n gameserver) /home/gameserver/.bashrc
####################################
# Initialize shard.sh
####################################
# configure environment
msg_debug "[$(basename $0):$LINENO] Configure shard.sh"
cat << EOF > /opt/shard.sh
export KHANAT_HOME=/home/gameserver
export KHANAT_PATH=/home/gameserver/khanat
export PATH=\$PATH:/usr/local/bin:/usr/local/sbin:\$KHANAT_PATH/tools/scripts/linux
export KHANAT_DATA=/home/gameserver/khanat-ressources
export PATCH_HOME=/home/gameserver
export PATCH_CLIENT_SYSTEM=\$PATCH_HOME/patch_service
export WORKDIRCLIENT="$WORKDIRCLIENT"
export PACKAGECLIENT="$PACKAGECLIENT"
EOF
####################################
# Adding sudo command
####################################
# Update sudo rule (you can execute all command as root)
msg_debug "[$(basename $0):$LINENO] Configure sudo"
cat << EOF > /etc/sudoers.d/gameserver
# User privilege specification
gameserver ALL=NOPASSWD: ALL
EOF
####################################
# LOGING HEADER
####################################
# Message see when connect on ssh
# Before login
msg_debug "[$(basename $0):$LINENO] Configure message login"
cat << EOF > /etc/issue.net
*********************
* KHANAT SERVER DEV *
*********************
account gameserver
password khanat
EOF
# After Login
msg_debug "[$(basename $0):$LINENO] Configure banner"
cat << EOF > /etc/motd
***************************************************
connect to root use gameserver account and launch sudo command
like :
sudo bash
---------------------------------------------------
mysql : account root (no password)
---------------------------------------------------
log khanat server : /home/gameserver/khanat/server/log/log.log
***************************************************
EOF
# Activate banner
msg_debug "[$(basename $0):$LINENO] Activate banner"
sed -i 's/#Banner/Banner/g' /etc/ssh/sshd_config
####################################
# End
####################################
msg_info "[$(basename $0):$LINENO] => END"