khanat-code-old/dist/docker/server/debian/common/serverimage_init_basic.sh
2017-10-09 22:11:54 +02:00

289 lines
8.9 KiB
Bash
Executable file

#!/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 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://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 "Start Basic"
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 recoginze"
usage
exit 1
;;
esac
done
####################################
# Load Environment
####################################
msg_debug "Load environment"
if [[ ! -f /opt/khanat_config.sh ]]
then
echo "ERROR - missing /opt/khanat_config.sh"
exit 2
fi
source /opt/khanat_config.sh
####################################
# Configure account gameserver
####################################
if [[ $APACHE_GAMESERVER -ne 0 ]]
then
msg_debug "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 "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 "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 "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 "Create database"
/usr/bin/mysql_install_db --user=mysql --skip-name-resolve || exit 2
fi
# Start the MySQL daemon in the background.
msg_debug "Start database"
/usr/sbin/mysqld &
mysql_pid=$!
# Wait mysql start
msg_debug "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 "configure password root for database"
/usr/bin/mysqladmin -u root password '' || exit 2
msg_debug "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 "Stop database"
/usr/bin/mysqladmin shutdown
# Wait MySQL stop
wait $mysql_pid
####################################
# Initialize bashrc (for root)
####################################
msg_debug "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
EOF
####################################
# Initialize bashrc (for gameserver)
####################################
msg_debug "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
EOF
chown gameserver:$(id -g -n gameserver) /home/gameserver/.bashrc
####################################
# Adding sudo command
####################################
# Update sudo rule (you can execute all command as root)
msg_debug "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 "Configure message login"
cat << EOF > /etc/issue.net
*********************
* KHANAT SERVER DEV *
*********************
account gameserver
password khanat
EOF
# After Login
msg_debug "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 "Activate banner"
sed -i 's/#Banner/Banner/g' /etc/ssh/sshd_config
####################################
# End
####################################
msg_info "End Basic"