ogp_agent 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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="/OGP"
  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. find $AGENTDIR/steamcmd -iname \*.dll -exec chmod +x {} \;
  16. find $AGENTDIR/steamcmd -iname \*.exe -exec chmod +x {} \;
  17. fi
  18. if [ -d "$AGENTDIR/screenlogs" ]; then
  19. chmod -Rf ug=rwx $AGENTDIR/screenlogs
  20. fi
  21. chmod +x $AGENTDIR/ogp_agent.pl &> /dev/null
  22. chmod +x $AGENTDIR/agent_conf.sh &> /dev/null
  23. chmod +x /usr/bin/ogp_agent &> /dev/null
  24. # Should we perform an automatic update?
  25. if [ -e $BASH_PREFS_CONF ]
  26. then
  27. source "$BASH_PREFS_CONF"
  28. if [ "X$agent_auto_update" == "X1" ]
  29. then
  30. AUTO_UPDATE="yes"
  31. fi
  32. if [ "X$run_pureftpd" == "X0" ]
  33. then
  34. RUN_PUREFTPD="no"
  35. else
  36. RUN_PUREFTPD="yes"
  37. fi
  38. if [ "X$ftp_ip" != "X" ]
  39. then
  40. FTP_IP="$ftp_ip"
  41. else
  42. FTP_IP="0.0.0.0"
  43. fi
  44. if [ "X$ftp_port" != "X" ]
  45. then
  46. FTP_PORT="$ftp_port"
  47. else
  48. FTP_PORT="21"
  49. fi
  50. if [ "X$ftp_pasv_range" != "X" ]
  51. then
  52. FTP_PASV_STRING="-p $ftp_pasv_range"
  53. else
  54. FTP_PASV_STRING=""
  55. fi
  56. else
  57. AUTO_UPDATE="yes"
  58. RUN_PUREFTPD="yes"
  59. FTP_IP="0.0.0.0"
  60. FTP_PORT="21"
  61. FTP_PASV_STING=""
  62. fi
  63. if test `id -u` -eq 0; then
  64. echo
  65. echo
  66. echo "************** WARNING ***************"
  67. echo "Running the OGP agent as root "
  68. echo "is highly discouraged. It is generally"
  69. echo "unnecessary to use root privileges to "
  70. echo "execute the agent. "
  71. echo "**************************************"
  72. echo
  73. echo
  74. timeout=10
  75. while test $timeout -gt 0; do
  76. echo -n "The agent will continue to launch in $timeout seconds\r"
  77. timeout=`expr $timeout - 1`
  78. sleep 1
  79. done
  80. fi
  81. init() {
  82. RESTART="yes"
  83. AGENT="$AGENTDIR/ogp_agent.pl"
  84. TIMEOUT=10 # time to wait after a crash (in seconds)
  85. PID_FILE=""
  86. while test $# -gt 0; do
  87. case "$1" in
  88. "-pidfile")
  89. PID_FILE="$2"
  90. PID_FILE_SET=1
  91. echo $$ > $PID_FILE
  92. shift ;;
  93. esac
  94. shift
  95. done
  96. if test ! -f "$AGENT"; then
  97. echo "ERROR: '$AGENT' not found, exiting"
  98. quit 1
  99. elif test ! -x "$AGENT"; then
  100. # Could try chmod but dont know what we will be
  101. # chmoding so just fail.
  102. echo "ERROR: '$AGENT' not executable, exiting"
  103. quit 1
  104. fi
  105. }
  106. syntax () {
  107. # Prints script syntax
  108. echo "Syntax:"
  109. echo "$0"
  110. }
  111. checkDepends() {
  112. CURL=`which curl 2>/dev/null`
  113. if test "$?" -gt 0; then
  114. echo "WARNING: Failed to locate curl binary."
  115. else
  116. echo "INFO: Located curl: $CURL"
  117. fi
  118. UNZIP=`which unzip 2>/dev/null`
  119. if test "$?" -gt 0; then
  120. echo "WARNING: Failed to locate unzip binary."
  121. else
  122. echo "INFO: Located unzip: $UNZIP"
  123. fi
  124. }
  125. update() {
  126. # Run the git update
  127. if test -n "$AUTO_UPDATE"; then
  128. if [ -z "$CURL" -o -z "$UNZIP" ]; then
  129. checkDepends
  130. fi
  131. if [ -f "$CURL" -a -x "$CURL" ] && [ -f "$UNZIP" -a -x "$UNZIP" ]; then
  132. cd $AGENTDIR
  133. if [ ! -d tmp ]; then
  134. mkdir tmp
  135. fi
  136. cd tmp
  137. REPONAME=OGP-Agent-Windows
  138. REVISION=`curl -Lks https://github.com/OpenGamePanel/${REPONAME}/commits/master.atom | grep -Eo "([a-f0-9]{40})" | head -n 1`
  139. curl -Os https://raw.githubusercontent.com/OpenGamePanel/${REPONAME}/${REVISION}/bin/ogp_agent
  140. diff ./ogp_agent /bin/ogp_agent &>/dev/null
  141. if test $? -ne 0; then
  142. cp -f ./ogp_agent /bin/ogp_agent &> /dev/null
  143. if test $? -eq 0; then
  144. cd /bin
  145. chmod ugo+x ogp_agent 2>/dev/null
  146. echo "`date`: The agent updater has been changed, relaunching..."
  147. rm -Rf tmp
  148. /bin/ogp_agent
  149. exit 0
  150. fi
  151. fi
  152. CURRENT=$(cat $AGENTDIR/Cfg/Config.pm | grep version | grep -Eo '[0-9a-f]{40}')
  153. if [ "$CURRENT" == "$REVISION" ]; then
  154. echo "The agent is up to date."
  155. else
  156. URL=https://github.com/OpenGamePanel/${REPONAME}/archive/${REVISION}.zip
  157. HEAD=$(curl -L -Os --head -w "%{http_code}" "$URL")
  158. if [ "$HEAD" == "200" ]; then
  159. echo "Updating agent using curl."
  160. curl -L -Os $URL
  161. if test $? -ne 0; then
  162. echo "`date`: curl failed to download the update package."
  163. else
  164. unzip -oq "${REVISION}.zip"
  165. if test $? -ne 0; then
  166. echo "`date`: Unable to unzip the update package."
  167. echo "cleaning up..."
  168. rm -Rf ${REPONAME}-* &> /dev/null
  169. else
  170. cd ${REPONAME}-${REVISION}
  171. cp -avf OGP/* $AGENTDIR/. &> /dev/null
  172. CP_APP_RET=$?
  173. cp -avf bin/* /usr/bin/. &> /dev/null
  174. CP_BIN_RET=$?
  175. if [ $CP_APP_RET -ne 0 -o $CP_BIN_RET -ne 0 ]; then
  176. echo "`date`: The agent files cannot be overwritten."
  177. echo "Cleaning up..."
  178. cd ..
  179. rm -Rf ${REPONAME}-* &> /dev/null
  180. echo "Agent update failed."
  181. else
  182. if test ! -e "$AGENTDIR/Cfg/Preferences.pm"; then
  183. cp -f Cfg/Preferences.pm $AGENTDIR/Cfg/Preferences.pm &> /dev/null
  184. fi
  185. echo "Fixing permissions..."
  186. chmod +x $AGENTDIR/ogp_agent.pl &> /dev/null
  187. chmod +x $AGENTDIR/agent_conf.sh &> /dev/null
  188. chmod +x /usr/bin/ogp_agent &> /dev/null
  189. echo "Cleaning up..."
  190. cd ..
  191. rm -Rf ${REPONAME}-* &> /dev/null
  192. sed -i "s/version.*/version => '${REVISION}',/" $AGENTDIR/Cfg/Config.pm
  193. echo "Agent updated successfully."
  194. fi
  195. fi
  196. fi
  197. else
  198. echo "There is a update available (${REVISION}) but the download source is not ready.";
  199. echo "Try again later."
  200. fi
  201. fi
  202. else
  203. echo "Update failed."
  204. fi
  205. fi
  206. return 0
  207. }
  208. run() {
  209. # Runs the update and agent
  210. update
  211. if test -n "$RESTART" ; then
  212. echo "Agent will auto-restart if there is a crash."
  213. #loop forever
  214. while true
  215. do
  216. # Run
  217. cd $AGENTDIR
  218. ./ogp_agent.pl
  219. echo "`date`: Agent restart in $TIMEOUT seconds"
  220. # don't thrash the hard disk if the agent dies, wait a little
  221. sleep $TIMEOUT
  222. done # while true
  223. else
  224. cd $AGENTDIR
  225. ./ogp_agent.pl
  226. fi
  227. }
  228. quit() {
  229. # Exits with the give error code, 1
  230. # if none specified.
  231. # exit code 2 also prints syntax
  232. exitcode="$1"
  233. # default to failure
  234. if test -z "$exitcode"; then
  235. exitcode=1
  236. fi
  237. case "$exitcode" in
  238. 0)
  239. echo "`date`: OGP Agent Quit" ;;
  240. 2)
  241. syntax ;;
  242. *)
  243. echo "`date`: OGP Agent Failed" ;;
  244. esac
  245. # Remove pid file
  246. if test -n "$PID_FILE" && test -f "$PID_FILE" ; then
  247. # The specified pid file
  248. rm -f $PID_FILE
  249. fi
  250. # reset SIGINT and then kill ourselves properly
  251. trap - 2
  252. kill -2 $$
  253. }
  254. PATH=/usr/local/bin:/usr/bin:${PATH}
  255. # Start PureFTPD if OGP has been configured to manage FTP users
  256. if [ ! -z "$RUN_PUREFTPD" ] && [ "$RUN_PUREFTPD" == "yes" ]; then
  257. /usr/sbin/pure-ftpd.exe -S ${FTP_IP},${FTP_PORT} ${FTP_PASV_STRING} -lpuredb:/etc/pureftpd.pdb -g /var/run/pure-ftpd.pid &
  258. fi
  259. # Initialise
  260. init $*
  261. # Run
  262. run
  263. # Quit normally
  264. quit 0