ogp_agent_run 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. if [ -z "$sf_update_mirror" ]
  55. then
  56. MIRROR="master"
  57. else
  58. MIRROR=$sf_update_mirror
  59. fi
  60. else
  61. AUTO_UPDATE="yes"
  62. MIRROR="master"
  63. fi
  64. while test $# -gt 0; do
  65. case "$1" in
  66. "-pidfile")
  67. PID_FILE="$2"
  68. PID_FILE_SET=1
  69. echo $$ > $PID_FILE
  70. shift ;;
  71. esac
  72. shift
  73. done
  74. if test ! -f "$AGENT"; then
  75. echo "ERROR: '$AGENT' not found, exiting"
  76. quit 1
  77. elif test ! -x "$AGENT"; then
  78. # Could try chmod but dont know what we will be
  79. # chmoding so just fail.
  80. echo "ERROR: '$AGENT' not executable, exiting"
  81. quit 1
  82. fi
  83. CMD="perl $AGENT"
  84. }
  85. syntax () {
  86. # Prints script syntax
  87. echo "Syntax:"
  88. echo "$0"
  89. }
  90. checkDepends() {
  91. CURL=`which curl 2>/dev/null`
  92. if test "$?" -gt 0; then
  93. echo "WARNING: Failed to locate curl binary."
  94. else
  95. echo "INFO: Located curl: $CURL"
  96. fi
  97. UNZIP=`which unzip 2>/dev/null`
  98. if test "$?" -gt 0; then
  99. echo "WARNING: Failed to locate unzip binary."
  100. else
  101. echo "INFO: Located unzip: $UNZIP"
  102. fi
  103. GIT=`which git 2>/dev/null`
  104. if test "$?" -gt 0; then
  105. echo "WARNING: Failed to locate git binary."
  106. else
  107. echo "INFO: Located git: $GIT"
  108. fi
  109. }
  110. update() {
  111. # Run the update
  112. if test -n "$AUTO_UPDATE"; then
  113. if [ -z "$CURL" -o -z "$UNZIP" ]; then
  114. checkDepends
  115. fi
  116. if [ -f "$CURL" -a -x "$CURL" ] && [ -f "$UNZIP" -a -x "$UNZIP" ]; then
  117. cd $AGENTDIR
  118. if [ ! -d tmp ]; then
  119. mkdir tmp
  120. fi
  121. cd tmp
  122. REPONAME=OGP-Agent-Linux
  123. REVISION=`git ls-remote --heads https://github.com/OpenGamePanel/${REPONAME}.git | grep -owE "^[^?\s]+"`
  124. curl -Os https://raw.githubusercontent.com/OpenGamePanel/${REPONAME}/${REVISION}/ogp_agent_run
  125. diff ./ogp_agent_run $AGENTDIR/ogp_agent_run &>/dev/null
  126. if test $? -ne 0; then
  127. cp -f ./ogp_agent_run $AGENTDIR/ogp_agent_run &> /dev/null
  128. if test $? -eq 0; then
  129. cd $AGENTDIR
  130. chmod ug+x ogp_agent_run 2>/dev/null
  131. echo "`date`: The agent updater has been changed, relaunching..."
  132. rm -Rf tmp
  133. ./ogp_agent_run
  134. exit 0
  135. fi
  136. fi
  137. cd $AGENTDIR
  138. CURRENT=$(cat $AGENTDIR/Cfg/Config.pm | grep version | grep -Eo '[0-9]{1,6}')
  139. if [ "$CURRENT" == "$REVISION" ]; then
  140. echo "The agent is up to date."
  141. else
  142. URL=https://github.com/OpenGamePanel/${REPONAME}/archive/${REVISION}.zip
  143. HEAD=$(curl -L -Os --head -w "%{http_code}" "$URL")
  144. if [ "$HEAD" == "200" ]; then
  145. echo "Updating agent using curl."
  146. curl -L -Os $URL
  147. if test $? -ne 0; then
  148. echo "`date`: curl failed to download the update package."
  149. else
  150. unzip -oq "${REVISION}.zip"
  151. if test $? -ne 0; then
  152. echo "`date`: Unable to unzip the update package."
  153. echo "cleaning up..."
  154. rm -Rf ${REPONAME}-* &> /dev/null
  155. else
  156. cd ${REPONAME}-${REVISION}
  157. cp -avf Schedule Time FastDownload php-query ogp_agent.pl ogp_screenrc ogp_agent_run agent_conf.sh $AGENTDIR &> /dev/null
  158. if test $? -ne 0; then
  159. echo "`date`: The agent files cannot be overwritten."
  160. echo "Cleaning up..."
  161. cd ..
  162. rm -Rf ${REPONAME}-* &> /dev/null
  163. echo "Agent update failed."
  164. else
  165. if test ! -d "$AGENTDIR/IspConfig"; then
  166. cp -Rf IspConfig $AGENTDIR/IspConfig &> /dev/null
  167. fi
  168. if test ! -d "$AGENTDIR/EHCP"; then
  169. cp -Rf EHCP $AGENTDIR/EHCP &> /dev/null
  170. fi
  171. if test ! -f "$AGENTDIR/Cfg/Preferences.pm"; then
  172. cp -f Cfg/Preferences.pm $AGENTDIR/Cfg/Preferences.pm &> /dev/null
  173. fi
  174. echo "Fixing permissions..."
  175. chmod ug+x $AGENTDIR/ogp_agent.pl &> /dev/null
  176. chmod ug+x $AGENTDIR/ogp_agent_run &> /dev/null
  177. chmod ug+x $AGENTDIR/agent_conf.sh &> /dev/null
  178. echo "Cleaning up..."
  179. cd ..
  180. rm -Rf ${REPONAME}-* &> /dev/null
  181. sed -i "s/version.*/version => 'v${REVISION}',/" $AGENTDIR/Cfg/Config.pm
  182. echo "Agent updated successfully."
  183. fi
  184. fi
  185. fi
  186. else
  187. echo "There is a update available (${REVISION}) but the download source is not ready.";
  188. echo "Try again later."
  189. fi
  190. fi
  191. else
  192. echo "Update failed."
  193. fi
  194. fi
  195. return 0
  196. }
  197. run() {
  198. update
  199. if test -n "$RESTART" ; then
  200. echo "Agent will auto-restart if there is a crash."
  201. #loop forever
  202. while true
  203. do
  204. # Run
  205. $CMD
  206. echo "`date`: Agent restart in $TIMEOUT seconds"
  207. # don't thrash the hard disk if the agent dies, wait a little
  208. sleep $TIMEOUT
  209. done # while true
  210. else
  211. $CMD
  212. fi
  213. }
  214. quit() {
  215. # Exits with the give error code, 1
  216. # if none specified.
  217. # exit code 2 also prints syntax
  218. exitcode="$1"
  219. # default to failure
  220. if test -z "$exitcode"; then
  221. exitcode=1
  222. fi
  223. case "$exitcode" in
  224. 0)
  225. echo "`date`: OGP Agent Quit" ;;
  226. 2)
  227. syntax ;;
  228. *)
  229. echo "`date`: OGP Agent Failed" ;;
  230. esac
  231. # Remove pid file
  232. if test -n "$PID_FILE" && test -f "$PID_FILE" ; then
  233. # The specified pid file
  234. rm -f $PID_FILE
  235. fi
  236. # reset SIGINT and then kill ourselves properly
  237. trap - 2
  238. kill -2 $$
  239. }
  240. # Initialise
  241. init $*
  242. # Run
  243. run
  244. # Quit normally
  245. quit 0