| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- #!/bin/bash
- #
- #
- # A wrapper script for the OGP agent perl script.
- # Performs auto-restarting of the agent on crash. You can
- # extend this to log crashes and more.
- #
- # The ogp_agent_run script should be at the top level of the agent tree
- # Make sure we are in that directory since the script assumes this is the case
- AGENTDIR="/OGP"
- BASH_PREFS_CONF="$AGENTDIR/Cfg/bash_prefs.cfg"
- chmod -Rf 770 $AGENTDIR
- find $AGENTDIR -type f -print | xargs chmod ug=rw
- if [ -d "$AGENTDIR/steamcmd" ]; then
- find $AGENTDIR/steamcmd -iname \*.dll -exec chmod +x {} \;
- find $AGENTDIR/steamcmd -iname \*.exe -exec chmod +x {} \;
- fi
- if [ -d "$AGENTDIR/screenlogs" ]; then
- chmod -Rf ug=rwx $AGENTDIR/screenlogs
- fi
- chmod +x $AGENTDIR/ogp_agent.pl &> /dev/null
- chmod +x $AGENTDIR/agent_conf.sh &> /dev/null
- chmod +x /usr/bin/ogp_agent &> /dev/null
- # Should we perform an automatic update?
- if [ -e $BASH_PREFS_CONF ]
- then
- source "$BASH_PREFS_CONF"
- if [ "X$agent_auto_update" == "X1" ]
- then
- AUTO_UPDATE="yes"
- fi
-
- if [ "X$run_pureftpd" == "X0" ]
- then
- RUN_PUREFTPD="no"
- else
- RUN_PUREFTPD="yes"
- fi
-
- if [ "X$ftp_ip" != "X" ]
- then
- FTP_IP="$ftp_ip"
- else
- FTP_IP="0.0.0.0"
- fi
-
- if [ "X$ftp_port" != "X" ]
- then
- FTP_PORT="$ftp_port"
- else
- FTP_PORT="21"
- fi
-
- if [ "X$ftp_pasv_range" != "X" ]
- then
- FTP_PASV_STRING="-p $ftp_pasv_range"
- else
- FTP_PASV_STRING=""
- fi
- else
- AUTO_UPDATE="yes"
- RUN_PUREFTPD="yes"
- FTP_IP="0.0.0.0"
- FTP_PORT="21"
- FTP_PASV_STING=""
- fi
- if test `id -u` -eq 0; then
- echo
- echo
- echo "************** WARNING ***************"
- echo "Running the OGP agent as root "
- echo "is highly discouraged. It is generally"
- echo "unnecessary to use root privileges to "
- echo "execute the agent. "
- echo "**************************************"
- echo
- echo
- timeout=10
- while test $timeout -gt 0; do
- echo -n "The agent will continue to launch in $timeout seconds\r"
- timeout=`expr $timeout - 1`
- sleep 1
- done
- fi
- init() {
- RESTART="yes"
- AGENT="$AGENTDIR/ogp_agent.pl"
- TIMEOUT=10 # time to wait after a crash (in seconds)
- PID_FILE=""
- while test $# -gt 0; do
- case "$1" in
- "-pidfile")
- PID_FILE="$2"
- PID_FILE_SET=1
- echo $$ > $PID_FILE
- shift ;;
- esac
- shift
- done
- if test ! -f "$AGENT"; then
- echo "ERROR: '$AGENT' not found, exiting"
- quit 1
- elif test ! -x "$AGENT"; then
- # Could try chmod but dont know what we will be
- # chmoding so just fail.
- echo "ERROR: '$AGENT' not executable, exiting"
- quit 1
- fi
- }
- syntax () {
- # Prints script syntax
- echo "Syntax:"
- echo "$0"
- }
- checkDepends() {
- CURL=`which curl 2>/dev/null`
- if test "$?" -gt 0; then
- echo "WARNING: Failed to locate curl binary."
- else
- echo "INFO: Located curl: $CURL"
- fi
- UNZIP=`which unzip 2>/dev/null`
- if test "$?" -gt 0; then
- echo "WARNING: Failed to locate unzip binary."
- else
- echo "INFO: Located unzip: $UNZIP"
- fi
- GIT=`which git 2>/dev/null`
- if test "$?" -gt 0; then
- echo "WARNING: Failed to locate git binary."
- else
- echo "INFO: Located git: $GIT"
- fi
- }
- update() {
- # Run the git update
- if test -n "$AUTO_UPDATE"; then
- if [ -z "$CURL" -o -z "$UNZIP" -o -z "$GIT" ]; then
- checkDepends
- fi
- if [ -f "$CURL" -a -x "$CURL" ] && [ -f "$UNZIP" -a -x "$UNZIP" ] && [ -f "$GIT" -a -x "$GIT" ]; then
- cd $AGENTDIR
- if [ ! -d tmp ]; then
- mkdir tmp
- fi
- cd tmp
- REPONAME=OGP-Agent-Windows
- REVISION=`git ls-remote --heads https://github.com/OpenGamePanel/${REPONAME}.git | grep -owE "^[^?\s]+"`
- curl -Os https://raw.githubusercontent.com/OpenGamePanel/${REPONAME}/${REVISION}/bin/ogp_agent
- diff ./ogp_agent /bin/ogp_agent &>/dev/null
- if test $? -ne 0; then
- cp -f ./ogp_agent /bin/ogp_agent &> /dev/null
- if test $? -eq 0; then
- cd /bin
- chmod ugo+x ogp_agent 2>/dev/null
- echo "`date`: The agent updater has been changed, relaunching..."
- rm -Rf tmp
- /bin/ogp_agent
- exit 0
- fi
- fi
- CURRENT=$(cat $AGENTDIR/Cfg/Config.pm | grep version | grep -Eo '[0-9a-f]{40}')
- if [ "$CURRENT" == "$REVISION" ]; then
- echo "The agent is up to date."
- else
- URL=https://github.com/OpenGamePanel/${REPONAME}/archive/${REVISION}.zip
- HEAD=$(curl -L -Os --head -w "%{http_code}" "$URL")
- if [ "$HEAD" == "200" ]; then
- echo "Updating agent using curl."
- curl -L -Os $URL
- if test $? -ne 0; then
- echo "`date`: curl failed to download the update package."
- else
- unzip -oq "${REVISION}.zip"
- if test $? -ne 0; then
- echo "`date`: Unable to unzip the update package."
- echo "cleaning up..."
- rm -Rf ${REPONAME}-* &> /dev/null
- else
- cd ${REPONAME}-${REVISION}
- cp -avf OGP/* $AGENTDIR/. &> /dev/null
- CP_APP_RET=$?
- cp -avf bin/* /usr/bin/. &> /dev/null
- CP_BIN_RET=$?
- if [ $CP_APP_RET -ne 0 -o $CP_BIN_RET -ne 0 ]; then
- echo "`date`: The agent files cannot be overwritten."
- echo "Cleaning up..."
- cd ..
- rm -Rf ${REPONAME}-* &> /dev/null
- echo "Agent update failed."
- else
- if test ! -e "$AGENTDIR/Cfg/Preferences.pm"; then
- cp -f Cfg/Preferences.pm $AGENTDIR/Cfg/Preferences.pm &> /dev/null
- fi
- echo "Fixing permissions..."
- chmod +x $AGENTDIR/ogp_agent.pl &> /dev/null
- chmod +x $AGENTDIR/agent_conf.sh &> /dev/null
- chmod +x /usr/bin/ogp_agent &> /dev/null
- echo "Cleaning up..."
- cd ..
- rm -Rf ${REPONAME}-* &> /dev/null
- sed -i "s/version.*/version => '${REVISION}',/" $AGENTDIR/Cfg/Config.pm
- echo "Agent updated successfully."
- fi
- fi
- fi
- else
- echo "There is a update available (${REVISION}) but the download source is not ready.";
- echo "Try again later."
- fi
- fi
-
- else
- echo "Update failed."
- fi
- fi
-
- return 0
- }
-
- run() {
- # Runs the update and agent
- update
- if test -n "$RESTART" ; then
- echo "Agent will auto-restart if there is a crash."
- #loop forever
- while true
- do
- # Run
- cd $AGENTDIR
- ./ogp_agent.pl
- echo "`date`: Agent restart in $TIMEOUT seconds"
- # don't thrash the hard disk if the agent dies, wait a little
- sleep $TIMEOUT
- done # while true
- else
- cd $AGENTDIR
- ./ogp_agent.pl
- fi
- }
- quit() {
- # Exits with the give error code, 1
- # if none specified.
- # exit code 2 also prints syntax
- exitcode="$1"
- # default to failure
- if test -z "$exitcode"; then
- exitcode=1
- fi
- case "$exitcode" in
- 0)
- echo "`date`: OGP Agent Quit" ;;
- 2)
- syntax ;;
- *)
- echo "`date`: OGP Agent Failed" ;;
- esac
- # Remove pid file
- if test -n "$PID_FILE" && test -f "$PID_FILE" ; then
- # The specified pid file
- rm -f $PID_FILE
- fi
- # reset SIGINT and then kill ourselves properly
- trap - 2
- kill -2 $$
- }
- PATH=/usr/local/bin:/usr/bin:${PATH}
- # Start PureFTPD if OGP has been configured to manage FTP users
- if [ ! -z "$RUN_PUREFTPD" ] && [ "$RUN_PUREFTPD" == "yes" ]; then
- /usr/sbin/pure-ftpd.exe -S ${FTP_IP},${FTP_PORT} ${FTP_PASV_STRING} -lpuredb:/etc/pureftpd.pdb -g /var/run/pure-ftpd.pid &
- fi
- # Initialise
- init $*
- # Run
- run
- # Quit normally
- quit 0
|