ogp_agent_run 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 770 $AGENTDIR
  13. find $AGENTDIR -type f -print | xargs chmod ug=rw
  14. if [ -d "$AGENTDIR/steamcmd" ]; then
  15. chmod +x $AGENTDIR/steamcmd/linux32/*
  16. chmod +x $AGENTDIR/steamcmd/*.sh
  17. fi
  18. if [ -d "$AGENTDIR/screenlogs" ]; then
  19. chmod -Rf ug=rwx $AGENTDIR/screenlogs
  20. fi
  21. chmod +x $AGENTDIR/ogp_agent.pl
  22. chmod +x $AGENTDIR/ogp_agent_run
  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. }
  104. update() {
  105. # Run the update
  106. if test -n "$AUTO_UPDATE"; then
  107. if [ -z "$CURL" -o -z "$UNZIP" ]; then
  108. checkDepends
  109. fi
  110. if [ -f "$CURL" -a -x "$CURL" ] && [ -f "$UNZIP" -a -x "$UNZIP" ]; then
  111. CURRENT=$(cat $AGENTDIR/Cfg/Config.pm | grep version | grep -oh [0-9]*)
  112. REVISION=$(curl -s http://svn.code.sf.net/p/hldstart/code/trunk/ | grep "<h2>" | awk '{print $4}' | tr -d [:punct:])
  113. if [ "$CURRENT" == "$REVISION" ]; then
  114. echo "The agent is up to date."
  115. else
  116. URL=http://${MIRROR}.dl.sourceforge.net/project/ogpextras/Alternative-Snapshot/linux-agent-${REVISION}.zip
  117. HEAD=$(curl -Os --head -w "%{http_code}" "$URL")
  118. if [ "$HEAD" == "200" ]; then
  119. echo "Updating agent using curl."
  120. curl -Os $URL
  121. if test $? -ne 0; then
  122. echo "`date`: curl failed to download the update package."
  123. else
  124. unzip -oq linux-agent-${REVISION}.zip
  125. if test $? -ne 0; then
  126. echo "`date`: Unable to unzip the update package."
  127. echo "cleaning up..."
  128. rm -Rf linux-agent-* &> /dev/null
  129. else
  130. cd linux-agent-${REVISION}
  131. cp -avf Crypt File Frontier KKrcon ogp_agent.pl ogp_screenrc ogp_agent_run agent_conf.sh $AGENTDIR &> /dev/null
  132. if test $? -ne 0; then
  133. echo "`date`: The agent files cannot be overwritten."
  134. echo "Cleaning up..."
  135. cd ..
  136. rm -Rf linux-agent-* &> /dev/null
  137. echo "Agent update failed."
  138. else
  139. if test ! -d "$AGENTDIR/IspConfig"; then
  140. cp -Rf IspConfig $AGENTDIR/IspConfig &> /dev/null
  141. fi
  142. if test ! -d "$AGENTDIR/EHCP"; then
  143. cp -Rf EHCP $AGENTDIR/EHCP &> /dev/null
  144. fi
  145. if test ! -f "$AGENTDIR/Cfg/Preferences.pm"; then
  146. cp -f Cfg/Preferences.pm $AGENTDIR/Cfg/Preferences.pm &> /dev/null
  147. fi
  148. echo "Fixing permissions..."
  149. chmod ug+x $AGENTDIR/ogp_agent.pl &> /dev/null
  150. chmod ug+x $AGENTDIR/ogp_agent_run &> /dev/null
  151. echo "Cleaning up..."
  152. cd ..
  153. rm -Rf linux-agent-* &> /dev/null
  154. sed -i "s/version.*/version => 'v${REVISION}',/" $AGENTDIR/Cfg/Config.pm
  155. echo "Agent updated successfully."
  156. fi
  157. fi
  158. fi
  159. else
  160. echo "There is a update available (${REVISION}) but the download source is not ready.";
  161. echo "Try again later."
  162. fi
  163. fi
  164. else
  165. echo "Update failed."
  166. fi
  167. fi
  168. return 0
  169. }
  170. run() {
  171. update
  172. if test -n "$RESTART" ; then
  173. echo "Agent will auto-restart if there is a crash."
  174. #loop forever
  175. while true
  176. do
  177. # Run
  178. $CMD
  179. echo "`date`: Agent restart in $TIMEOUT seconds"
  180. # don't thrash the hard disk if the agent dies, wait a little
  181. sleep $TIMEOUT
  182. done # while true
  183. else
  184. $CMD
  185. fi
  186. }
  187. quit() {
  188. # Exits with the give error code, 1
  189. # if none specified.
  190. # exit code 2 also prints syntax
  191. exitcode="$1"
  192. # default to failure
  193. if test -z "$exitcode"; then
  194. exitcode=1
  195. fi
  196. case "$exitcode" in
  197. 0)
  198. echo "`date`: OGP Agent Quit" ;;
  199. 2)
  200. syntax ;;
  201. *)
  202. echo "`date`: OGP Agent Failed" ;;
  203. esac
  204. # Remove pid file
  205. if test -n "$PID_FILE" && test -f "$PID_FILE" ; then
  206. # The specified pid file
  207. rm -f $PID_FILE
  208. fi
  209. # reset SIGINT and then kill ourselves properly
  210. trap - 2
  211. kill -2 $$
  212. }
  213. # Initialise
  214. init $*
  215. # Run
  216. run
  217. # Quit normally
  218. quit 0