2015-06-23 23:26:32 +02:00

292 lines
7.1 KiB
Bash
Executable File

#!/bin/bash
# ARK: survival evolved manager
#
# Original author: LeXaT
# Maintainer: FezVrasta, Sispheor
# Check the user is not currently running this script as root
if [ "$(id -u)" == "0" ]; then
echo "This script must NOT be run as root" 1>&2
exit 1
fi
#---------------------
# Variables
#---------------------
# Global variables
if [ -f "/etc/arkmanager/arkmanager.cfg" ]; then
source /etc/arkmanager/arkmanager.cfg
fi
if [ -f "${HOME}/.arkmanager.cfg" ]; then
source "${HOME}/.arkmanager.cfg"
fi
# Local variables
info=""
thejob=""
instver=""
bnumber=""
timestamp=$( date +%T )
GREEN="\\033[1;32m"
RED="\\033[1;31m"
NORMAL="\\033[0;39m"
#---------------------
# functions
#---------------------
function testfunction(){
if [ -z $servermail ]; then
echo "mail ok"
fi
}
#
# Check if a new version is available but not apply it
#
function checkForUpdate(){
if isUpdateNeeded; then
echo -e "Current version:" "$RED" $instver "$NORMAL"
echo -e "Available version:" "$GREEN" $bnumber "$NORMAL"
echo -e "Your server needs to be restarted in order to receive the latest update."
echo -e "Run \"arkmanager update\" to do so"
else
echo -e "Current version:" "$GREEN" $instver "$NORMAL"
echo -e "Available version:" "$GREEN" $bnumber "$NORMAL"
echo "Your server is up to date!"
fi
}
#
# Set the new current version in a file
#
function setCurrentVersion(){
cd "$arkserverroot"
echo $bnumber > arkversion
}
#
# Check if the server need to be updated
# Return 0 if update is needed, else return 1
#
function isUpdateNeeded(){
getCurrentVersion
getAvailableVersion
if [ "$bnumber" -eq "$instver" ]; then
return 1 # no update needed
else
return 0 # update needed
fi
}
#
# Return the current version number
#
function getCurrentVersion(){
cd "$arkserverroot"
touch arkversion # If the file doesn't exist
instver=`cat "arkversion"`
return $instver
}
#
# Get the current available server version on steamdb
#
function getAvailableVersion(){
bnumber=`$steamcmdroot/$steamcmdexec +login anonymous +app_info_print "$appid" +quit | grep -EA 5 "^\s+\"public\"$" | grep -E "^\s+\"buildid\"\s+" | tr '[:blank:]"' ' ' | tr -s ' ' | cut -f3 | sed 's/^ //' | cut -c9-14`
return $bnumber
}
#
# Check id the server process is alive
#
function isTheServerRunning(){
SERVICE="ShooterGameServer"
ps aux | grep -v grep | grep $SERVICE > /dev/null
result=$?
return $result
}
#
# Check if the server is down and not visible in steam server list
#
function isTheServerDown(){
lsof -i |grep $arkserverport > /dev/null
result=$?
return $result
}
#
# start function
#
doStart() {
if isTheServerRunning; then
echo "The server is already running"
else
ark_SessionName=`printf "%q" "${ark_SessionName:-${sessionname}}"` # Escape SessionName to prevent conflicts in the startup sequence
ark_Port="${ark_Port:-${arkserverport}}"
ark_QueryPort="${ark_QueryPort:-${arkqueryport}}"
ark_ServerPassword="${ark_ServerPassword:-${arkserverpass}}"
ark_ServerAdminPassword="${ark_ServerAdminPassword:-${arkserverapass}}"
arkserveropts="TheIsland"
# bring in ark_... options
for varname in "${!ark_@}"; do
name="${varname#ark_}"
val="${!varname}"
# Port is actually one higher than specified
# i.e. specifying port 7777 will have the server
# use port 7778
if [ "$name" == "Port" ]; then
(( val = val - 1 ))
fi
if [ -n "$val" ]; then
arkserveropts="${arkserveropts}?${name}=${val}"
fi
done
arkserveropts="${arkserveropts}?listen"
# run the server in background
nohup "$arkserverroot/$arkserverexec" "$arkserveropts" </dev/null >/dev/null 2>&1 &
echo "$timestamp: start" >> "$logdir/arkserver.log"
fi
}
#
# stop the ARK server
#
doStop() {
if isTheServerRunning; then
echo "Stopping server..."
# kill the server with the PID
PID=`ps -ef | grep "$arkserverroot/$arkserverexec" | grep -v grep | awk '{print $2}'`
kill -9 $PID
echo "$timestamp: stop" >> "$logdir/arkserver.log"
else
echo "The server is already stopped"
fi
}
#
# install of ARK server
#
doInstall() {
mkdir -p "$arkserverroot"
cd "$steamcmdroot"
# install the server
./$steamcmdexec +login anonymous +force_install_dir "$arkserverroot" +app_update $appid validate +quit
# the current version should be the last version. We set our version
getAvailableVersion
setCurrentVersion
}
#
# Stop the server, update it and then start it back.
#
doUpdate() {
cd "$arkserverroot"
if isUpdateNeeded; then
# check if the server was alive before the update so we can launch it back after the update
serverWasAlive=0
if isTheServerRunning ;then
serverWasAlive=1
fi
doStop
cd "$steamcmdroot"
./$steamcmdexec +login anonymous +force_install_dir "$arkserverroot" +app_update $appid +quit
echo "$bnumber" > "$arkserverroot/arkversion"
echo "$timestamp: update to $bnumber complete" >> "$logdir/update.log"
# we restart the server only if it was started before the update
if [ $serverWasAlive -eq 1 ]; then
doStart
fi
else
echo "No update available"
echo "$timestamp: No update needed." >> "$logdir/update.log"
tail -n 1 "$logdir/update.log"
fi;
}
#
# Print the status of the server (running? online? version?)
#
printStatus(){
if isTheServerRunning ;then
echo -e "$NORMAL" "Server running:" "$GREEN" "Yes" "$NORMAL"
else
echo -e "$NORMAL" "Server running:" "$RED" "No" "$NORMAL"
fi
if isTheServerDown ;then
echo -e "$NORMAL" "Server online:" "$RED" " No" "$NORMAL"
else
echo -e "$NORMAL" "Server online:" "$GREEN" " Yes" "$NORMAL"
fi
getCurrentVersion
echo -e "$NORMAL" "Server version:" "$GREEN" $instver "$NORMAL"
}
#---------------------
# Main program
#---------------------
case "$1" in
start)
doStart
;;
stop)
doStop
;;
restart)
doStop
echo "$timestamp: stop" >> "$logdir/arkserver.log"
sleep 10
doStart
echo "$timestamp: start" >> "$logdir/arkserver.log"
echo "$timestamp: restart" >> "$logdir/arkserver.log"
;;
install)
doInstall
;;
update)
doUpdate
#testfunction
;;
checkupdate)
checkForUpdate
;;
broadcast)
doInfo $2
;;
status)
printStatus
;;
-h|--help)
echo -e "Usage: arkmanager [OPTION]\n"
echo "Option Description"
echo "start Starts the server"
echo "stop Stops the server"
echo "restart Stops the server and then starts it"
echo "install Install the ARK server files from steamcmd"
echo "update Check for a new ARK server version, if needed, stops the server, updates it, and starts it again"
echo "checkupdate Check for a new ARK server version"
echo "boradcast PLACEHOLDER, not supported yet"
echo "status Returns the status of the current ARK server instance"
;;
*)
echo "arkmanager: no command specified"
echo "Try 'arkmanager -h' or 'arkmanager --help' for more information."
;;
esac