ogp_agent_run 5.1 KB

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