ogp_agent_run 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #!/bin/bash
  2. #
  3. #
  4. # A wrapper script for the OGP agent perl script.
  5. # Performs auto-restarting of the agent on crash. You can
  6. # extend this to log crashes and more.
  7. #
  8. # The ogp_agent_run script should be at the top level of the agent tree
  9. # Make sure we are in that directory since the script assumes this is the case
  10. AGENTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  11. BASH_PREFS_CONF="$AGENTDIR/Cfg/bash_prefs.cfg"
  12. chmod -Rf ug+rw $AGENTDIR 2>/dev/null
  13. if [ -d "$AGENTDIR/steamcmd" ]; then
  14. chmod ug+x $AGENTDIR/steamcmd/linux32/* 2>/dev/null
  15. chmod ug+x $AGENTDIR/steamcmd/*.sh 2>/dev/null
  16. fi
  17. if [ -d "$AGENTDIR/screenlogs" ]; then
  18. chmod -Rf ug=rwx $AGENTDIR/screenlogs
  19. fi
  20. chmod ug+x $AGENTDIR/ogp_agent.pl 2>/dev/null
  21. chmod ug+x $AGENTDIR/ogp_agent_run 2>/dev/null
  22. chmod ug+x $AGENTDIR/agent_conf.sh 2>/dev/null
  23. if test `id -u` -eq 0; then
  24. echo
  25. echo
  26. echo "************** WARNING ***************"
  27. echo "Running OGP's agent as root "
  28. echo "is highly discouraged. It is generally"
  29. echo "unnecessary to use root privileges to "
  30. echo "execute the agent. "
  31. echo "**************************************"
  32. echo
  33. echo
  34. timeout=10
  35. while test $timeout -gt 0; do
  36. echo -n "The agent will continue to launch in $timeout seconds\r"
  37. timeout=`expr $timeout - 1`
  38. sleep 1
  39. done
  40. fi
  41. init() {
  42. RESTART="yes"
  43. AGENT="$AGENTDIR/ogp_agent.pl"
  44. TIMEOUT=10 # time to wait after a crash (in seconds)
  45. PID_FILE=""
  46. # Should we perform an automatic update?
  47. if [ -e $BASH_PREFS_CONF ]
  48. then
  49. source "$BASH_PREFS_CONF"
  50. if [ "$agent_auto_update" -eq "1" ]
  51. then
  52. AUTO_UPDATE="yes"
  53. fi
  54. else
  55. AUTO_UPDATE="yes"
  56. fi
  57. while test $# -gt 0; do
  58. case "$1" in
  59. "-pidfile")
  60. PID_FILE="$2"
  61. PID_FILE_SET=1
  62. echo $$ > $PID_FILE
  63. shift ;;
  64. esac
  65. shift
  66. done
  67. if test ! -f "$AGENT"; then
  68. echo "ERROR: '$AGENT' not found, exiting"
  69. quit 1
  70. elif test ! -x "$AGENT"; then
  71. # Could try chmod but dont know what we will be
  72. # chmoding so just fail.
  73. echo "ERROR: '$AGENT' not executable, exiting"
  74. quit 1
  75. fi
  76. CMD="perl $AGENT"
  77. }
  78. syntax () {
  79. # Prints script syntax
  80. echo "Syntax:"
  81. echo "$0"
  82. }
  83. checkDepends() {
  84. CURL=`which curl 2>/dev/null`
  85. if test "$?" -gt 0; then
  86. echo "WARNING: Failed to locate curl binary."
  87. else
  88. echo "INFO: Located curl: $CURL"
  89. fi
  90. UNZIP=`which unzip 2>/dev/null`
  91. if test "$?" -gt 0; then
  92. echo "WARNING: Failed to locate unzip binary."
  93. else
  94. echo "INFO: Located unzip: $UNZIP"
  95. fi
  96. }
  97. update() {
  98. # Run the update
  99. if test -n "$AUTO_UPDATE"; then
  100. if [ -z "$CURL" -o -z "$UNZIP" ]; then
  101. checkDepends
  102. fi
  103. if [ -f "$CURL" -a -x "$CURL" ] && [ -f "$UNZIP" -a -x "$UNZIP" ]; then
  104. cd $AGENTDIR
  105. if [ ! -d tmp ]; then
  106. mkdir tmp
  107. fi
  108. cd tmp
  109. REPONAME=OGP-Agent-Linux
  110. REVISION=`curl -s https://github.com/OpenGamePanel/${REPONAME}/commits/master.atom | egrep -o "([a-f0-9]{40})" | awk 'NR==1{print $1}'`
  111. curl -Os https://raw.githubusercontent.com/OpenGamePanel/${REPONAME}/${REVISION}/ogp_agent_run
  112. diff ./ogp_agent_run $AGENTDIR/ogp_agent_run &>/dev/null
  113. if test $? -ne 0; then
  114. cp -f ./ogp_agent_run $AGENTDIR/ogp_agent_run &> /dev/null
  115. if test $? -eq 0; then
  116. cd $AGENTDIR
  117. chmod ug+x ogp_agent_run 2>/dev/null
  118. echo "`date`: The agent updater has been changed, relaunching..."
  119. rm -Rf tmp
  120. ./ogp_agent_run
  121. exit 0
  122. fi
  123. fi
  124. cd $AGENTDIR
  125. CURRENT=$(cat $AGENTDIR/Cfg/Config.pm | grep version | grep -Eo '[0-9a-f]{40}')
  126. if [ "$CURRENT" == "$REVISION" ]; then
  127. echo "The agent is up to date."
  128. else
  129. URL=https://github.com/OpenGamePanel/${REPONAME}/archive/${REVISION}.zip
  130. HEAD=$(curl -L -Os --head -w "%{http_code}" "$URL")
  131. if [ "$HEAD" == "200" ]; then
  132. echo "Updating agent using curl."
  133. curl -L -Os $URL
  134. if test $? -ne 0; then
  135. echo "`date`: curl failed to download the update package."
  136. else
  137. unzip -oq "${REVISION}.zip"
  138. if test $? -ne 0; then
  139. echo "`date`: Unable to unzip the update package."
  140. echo "cleaning up..."
  141. rm -Rf ${REPONAME}-* &> /dev/null
  142. else
  143. cd ${REPONAME}-${REVISION}
  144. cp -avf Schedule Time FastDownload php-query ogp_agent.pl ogp_screenrc ogp_agent_run agent_conf.sh $AGENTDIR &> /dev/null
  145. if test $? -ne 0; then
  146. echo "`date`: The agent files cannot be overwritten."
  147. echo "Cleaning up..."
  148. cd ..
  149. rm -Rf ${REPONAME}-* &> /dev/null
  150. echo "Agent update failed."
  151. else
  152. if test ! -d "$AGENTDIR/IspConfig"; then
  153. cp -Rf IspConfig $AGENTDIR/IspConfig &> /dev/null
  154. fi
  155. if test ! -d "$AGENTDIR/EHCP"; then
  156. cp -Rf EHCP $AGENTDIR/EHCP &> /dev/null
  157. fi
  158. if test ! -f "$AGENTDIR/Cfg/Preferences.pm"; then
  159. cp -f Cfg/Preferences.pm $AGENTDIR/Cfg/Preferences.pm &> /dev/null
  160. fi
  161. echo "Fixing permissions..."
  162. chmod ug+x $AGENTDIR/ogp_agent.pl &> /dev/null
  163. chmod ug+x $AGENTDIR/ogp_agent_run &> /dev/null
  164. chmod ug+x $AGENTDIR/agent_conf.sh &> /dev/null
  165. echo "Cleaning up..."
  166. cd ..
  167. rm -Rf ${REPONAME}-* &> /dev/null
  168. sed -i "s/version.*/version => '${REVISION}',/" $AGENTDIR/Cfg/Config.pm
  169. echo "Agent updated successfully."
  170. fi
  171. fi
  172. fi
  173. else
  174. echo "There is a update available (${REVISION}) but the download source is not ready.";
  175. echo "Try again later."
  176. fi
  177. fi
  178. else
  179. echo "Update failed."
  180. fi
  181. fi
  182. return 0
  183. }
  184. run() {
  185. update
  186. if test -n "$RESTART" ; then
  187. echo "Agent will auto-restart if there is a crash."
  188. #loop forever
  189. while true
  190. do
  191. # Run
  192. $CMD
  193. echo "`date`: Agent restart in $TIMEOUT seconds"
  194. # don't thrash the hard disk if the agent dies, wait a little
  195. sleep $TIMEOUT
  196. done # while true
  197. else
  198. $CMD
  199. fi
  200. }
  201. quit() {
  202. # Exits with the give error code, 1
  203. # if none specified.
  204. # exit code 2 also prints syntax
  205. exitcode="$1"
  206. # default to failure
  207. if test -z "$exitcode"; then
  208. exitcode=1
  209. fi
  210. case "$exitcode" in
  211. 0)
  212. echo "`date`: OGP Agent Quit" ;;
  213. 2)
  214. syntax ;;
  215. *)
  216. echo "`date`: OGP Agent Failed" ;;
  217. esac
  218. # Remove pid file
  219. if test -n "$PID_FILE" && test -f "$PID_FILE" ; then
  220. # The specified pid file
  221. rm -f $PID_FILE
  222. fi
  223. # reset SIGINT and then kill ourselves properly
  224. trap - 2
  225. kill -2 $$
  226. }
  227. # Initialise
  228. init $*
  229. # Run
  230. run
  231. # Quit normally
  232. quit 0