ogp_agent_run 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. chmod +x $AGENTDIR/agent_conf.sh
  24. if test `id -u` -eq 0; then
  25. echo
  26. echo
  27. echo "************** WARNING ***************"
  28. echo "Running OGP's agent as root "
  29. echo "is highly discouraged. It is generally"
  30. echo "unnecessary to use root privileges to "
  31. echo "execute the agent. "
  32. echo "**************************************"
  33. echo
  34. echo
  35. timeout=10
  36. while test $timeout -gt 0; do
  37. echo -n "The agent will continue to launch in $timeout seconds\r"
  38. timeout=`expr $timeout - 1`
  39. sleep 1
  40. done
  41. fi
  42. init() {
  43. RESTART="yes"
  44. AGENT="$AGENTDIR/ogp_agent.pl"
  45. TIMEOUT=10 # time to wait after a crash (in seconds)
  46. PID_FILE=""
  47. # Should we perform an automatic update?
  48. if [ -e $BASH_PREFS_CONF ]
  49. then
  50. source "$BASH_PREFS_CONF"
  51. if [ "$agent_auto_update" -eq "1" ]
  52. then
  53. AUTO_UPDATE="yes"
  54. fi
  55. if [ -z "$sf_update_mirror" ]
  56. then
  57. MIRROR="master"
  58. else
  59. MIRROR=$sf_update_mirror
  60. fi
  61. else
  62. AUTO_UPDATE="yes"
  63. MIRROR="master"
  64. fi
  65. while test $# -gt 0; do
  66. case "$1" in
  67. "-pidfile")
  68. PID_FILE="$2"
  69. PID_FILE_SET=1
  70. echo $$ > $PID_FILE
  71. shift ;;
  72. esac
  73. shift
  74. done
  75. if test ! -f "$AGENT"; then
  76. echo "ERROR: '$AGENT' not found, exiting"
  77. quit 1
  78. elif test ! -x "$AGENT"; then
  79. # Could try chmod but dont know what we will be
  80. # chmoding so just fail.
  81. echo "ERROR: '$AGENT' not executable, exiting"
  82. quit 1
  83. fi
  84. CMD="perl $AGENT"
  85. }
  86. syntax () {
  87. # Prints script syntax
  88. echo "Syntax:"
  89. echo "$0"
  90. }
  91. checkDepends() {
  92. CURL=`which curl 2>/dev/null`
  93. if test "$?" -gt 0; then
  94. echo "WARNING: Failed to locate curl binary."
  95. else
  96. echo "INFO: Located curl: $CURL"
  97. fi
  98. UNZIP=`which unzip 2>/dev/null`
  99. if test "$?" -gt 0; then
  100. echo "WARNING: Failed to locate unzip binary."
  101. else
  102. echo "INFO: Located unzip: $UNZIP"
  103. fi
  104. }
  105. update() {
  106. # Run the update
  107. if test -n "$AUTO_UPDATE"; then
  108. if [ -z "$CURL" -o -z "$UNZIP" ]; then
  109. checkDepends
  110. fi
  111. if [ -f "$CURL" -a -x "$CURL" ] && [ -f "$UNZIP" -a -x "$UNZIP" ]; then
  112. CURRENT=$(cat $AGENTDIR/Cfg/Config.pm | grep version | grep -oh [0-9]*)
  113. REVISION=$(curl -s http://svn.code.sf.net/p/hldstart/code/trunk/ | grep "<h2>" | awk '{print $4}' | tr -d [:punct:])
  114. if [ "$CURRENT" == "$REVISION" ]; then
  115. echo "The agent is up to date."
  116. else
  117. URL=http://${MIRROR}.dl.sourceforge.net/project/ogpextras/Alternative-Snapshot/linux-agent-${REVISION}.zip
  118. HEAD=$(curl -Os --head -w "%{http_code}" "$URL")
  119. if [ "$HEAD" == "200" ]; then
  120. echo "Updating agent using curl."
  121. curl -Os $URL
  122. if test $? -ne 0; then
  123. echo "`date`: curl failed to download the update package."
  124. else
  125. unzip -oq linux-agent-${REVISION}.zip
  126. if test $? -ne 0; then
  127. echo "`date`: Unable to unzip the update package."
  128. echo "cleaning up..."
  129. rm -Rf linux-agent-* &> /dev/null
  130. else
  131. cd linux-agent-${REVISION}
  132. cp -avf Crypt File Frontier KKrcon ogp_agent.pl ogp_screenrc ogp_agent_run agent_conf.sh $AGENTDIR &> /dev/null
  133. if test $? -ne 0; then
  134. echo "`date`: The agent files cannot be overwritten."
  135. echo "Cleaning up..."
  136. cd ..
  137. rm -Rf linux-agent-* &> /dev/null
  138. echo "Agent update failed."
  139. else
  140. if test ! -d "$AGENTDIR/IspConfig"; then
  141. cp -Rf IspConfig $AGENTDIR/IspConfig &> /dev/null
  142. fi
  143. if test ! -d "$AGENTDIR/EHCP"; then
  144. cp -Rf EHCP $AGENTDIR/EHCP &> /dev/null
  145. fi
  146. if test ! -f "$AGENTDIR/Cfg/Preferences.pm"; then
  147. cp -f Cfg/Preferences.pm $AGENTDIR/Cfg/Preferences.pm &> /dev/null
  148. fi
  149. echo "Fixing permissions..."
  150. chmod ug+x $AGENTDIR/ogp_agent.pl &> /dev/null
  151. chmod ug+x $AGENTDIR/ogp_agent_run &> /dev/null
  152. chmod ug+x $AGENTDIR/agent_conf.sh &> /dev/null
  153. echo "Cleaning up..."
  154. cd ..
  155. rm -Rf linux-agent-* &> /dev/null
  156. sed -i "s/version.*/version => 'v${REVISION}',/" $AGENTDIR/Cfg/Config.pm
  157. echo "Agent updated successfully."
  158. fi
  159. fi
  160. fi
  161. else
  162. echo "There is a update available (${REVISION}) but the download source is not ready.";
  163. echo "Try again later."
  164. fi
  165. fi
  166. else
  167. echo "Update failed."
  168. fi
  169. fi
  170. return 0
  171. }
  172. run() {
  173. update
  174. if test -n "$RESTART" ; then
  175. echo "Agent will auto-restart if there is a crash."
  176. #loop forever
  177. while true
  178. do
  179. # Run
  180. $CMD
  181. echo "`date`: Agent restart in $TIMEOUT seconds"
  182. # don't thrash the hard disk if the agent dies, wait a little
  183. sleep $TIMEOUT
  184. done # while true
  185. else
  186. $CMD
  187. fi
  188. }
  189. quit() {
  190. # Exits with the give error code, 1
  191. # if none specified.
  192. # exit code 2 also prints syntax
  193. exitcode="$1"
  194. # default to failure
  195. if test -z "$exitcode"; then
  196. exitcode=1
  197. fi
  198. case "$exitcode" in
  199. 0)
  200. echo "`date`: OGP Agent Quit" ;;
  201. 2)
  202. syntax ;;
  203. *)
  204. echo "`date`: OGP Agent Failed" ;;
  205. esac
  206. # Remove pid file
  207. if test -n "$PID_FILE" && test -f "$PID_FILE" ; then
  208. # The specified pid file
  209. rm -f $PID_FILE
  210. fi
  211. # reset SIGINT and then kill ourselves properly
  212. trap - 2
  213. kill -2 $$
  214. }
  215. # Initialise
  216. init $*
  217. # Run
  218. run
  219. # Quit normally
  220. quit 0