ogp_agent 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. GIT=`which git 2>/dev/null`
  125. if test "$?" -gt 0; then
  126. echo "WARNING: Failed to locate git binary."
  127. else
  128. echo "INFO: Located git: $GIT"
  129. fi
  130. }
  131. update() {
  132. # Run the git update
  133. if test -n "$AUTO_UPDATE"; then
  134. if [ -z "$CURL" -o -z "$UNZIP" -o -z "$GIT" ]; then
  135. checkDepends
  136. fi
  137. if [ -f "$CURL" -a -x "$CURL" ] && [ -f "$UNZIP" -a -x "$UNZIP" ] && [ -f "$GIT" -a -x "$GIT" ]; then
  138. cd $AGENTDIR
  139. if [ ! -d tmp ]; then
  140. mkdir tmp
  141. fi
  142. cd tmp
  143. REPONAME=OGP-Agent-Windows
  144. REVISION=`git ls-remote --heads https://github.com/OpenGamePanel/${REPONAME}.git | grep -owE "^[^?\s]+"`
  145. curl -Os https://raw.githubusercontent.com/OpenGamePanel/${REPONAME}/${REVISION}/bin/ogp_agent
  146. diff ./ogp_agent /bin/ogp_agent &>/dev/null
  147. if test $? -ne 0; then
  148. cp -f ./ogp_agent /bin/ogp_agent &> /dev/null
  149. if test $? -eq 0; then
  150. cd /bin
  151. chmod ugo+x ogp_agent 2>/dev/null
  152. echo "`date`: The agent updater has been changed, relaunching..."
  153. rm -Rf tmp
  154. /bin/ogp_agent
  155. exit 0
  156. fi
  157. fi
  158. CURRENT=$(cat $AGENTDIR/Cfg/Config.pm | grep version | grep -Eo '[0-9a-f]{40}')
  159. if [ "$CURRENT" == "$REVISION" ]; then
  160. echo "The agent is up to date."
  161. else
  162. URL=https://github.com/OpenGamePanel/${REPONAME}/archive/${REVISION}.zip
  163. HEAD=$(curl -L -Os --head -w "%{http_code}" "$URL")
  164. if [ "$HEAD" == "200" ]; then
  165. echo "Updating agent using curl."
  166. curl -L -Os $URL
  167. if test $? -ne 0; then
  168. echo "`date`: curl failed to download the update package."
  169. else
  170. unzip -oq "${REVISION}.zip"
  171. if test $? -ne 0; then
  172. echo "`date`: Unable to unzip the update package."
  173. echo "cleaning up..."
  174. rm -Rf ${REPONAME}-* &> /dev/null
  175. else
  176. cd ${REPONAME}-${REVISION}
  177. cp -avf OGP/* $AGENTDIR/. &> /dev/null
  178. CP_APP_RET=$?
  179. cp -avf bin/* /usr/bin/. &> /dev/null
  180. CP_BIN_RET=$?
  181. if [ $CP_APP_RET -ne 0 -o $CP_BIN_RET -ne 0 ]; then
  182. echo "`date`: The agent files cannot be overwritten."
  183. echo "Cleaning up..."
  184. cd ..
  185. rm -Rf ${REPONAME}-* &> /dev/null
  186. echo "Agent update failed."
  187. else
  188. if test ! -e "$AGENTDIR/Cfg/Preferences.pm"; then
  189. cp -f Cfg/Preferences.pm $AGENTDIR/Cfg/Preferences.pm &> /dev/null
  190. fi
  191. echo "Fixing permissions..."
  192. chmod +x $AGENTDIR/ogp_agent.pl &> /dev/null
  193. chmod +x $AGENTDIR/agent_conf.sh &> /dev/null
  194. chmod +x /usr/bin/ogp_agent &> /dev/null
  195. echo "Cleaning up..."
  196. cd ..
  197. rm -Rf ${REPONAME}-* &> /dev/null
  198. sed -i "s/version.*/version => '${REVISION}',/" $AGENTDIR/Cfg/Config.pm
  199. echo "Agent updated successfully."
  200. fi
  201. fi
  202. fi
  203. else
  204. echo "There is a update available (${REVISION}) but the download source is not ready.";
  205. echo "Try again later."
  206. fi
  207. fi
  208. else
  209. echo "Update failed."
  210. fi
  211. fi
  212. return 0
  213. }
  214. run() {
  215. # Runs the update and agent
  216. update
  217. if test -n "$RESTART" ; then
  218. echo "Agent will auto-restart if there is a crash."
  219. #loop forever
  220. while true
  221. do
  222. # Run
  223. cd $AGENTDIR
  224. ./ogp_agent.pl
  225. echo "`date`: Agent restart in $TIMEOUT seconds"
  226. # don't thrash the hard disk if the agent dies, wait a little
  227. sleep $TIMEOUT
  228. done # while true
  229. else
  230. cd $AGENTDIR
  231. ./ogp_agent.pl
  232. fi
  233. }
  234. quit() {
  235. # Exits with the give error code, 1
  236. # if none specified.
  237. # exit code 2 also prints syntax
  238. exitcode="$1"
  239. # default to failure
  240. if test -z "$exitcode"; then
  241. exitcode=1
  242. fi
  243. case "$exitcode" in
  244. 0)
  245. echo "`date`: OGP Agent Quit" ;;
  246. 2)
  247. syntax ;;
  248. *)
  249. echo "`date`: OGP Agent Failed" ;;
  250. esac
  251. # Remove pid file
  252. if test -n "$PID_FILE" && test -f "$PID_FILE" ; then
  253. # The specified pid file
  254. rm -f $PID_FILE
  255. fi
  256. # reset SIGINT and then kill ourselves properly
  257. trap - 2
  258. kill -2 $$
  259. }
  260. PATH=/usr/local/bin:/usr/bin:${PATH}
  261. # Start PureFTPD if OGP has been configured to manage FTP users
  262. if [ ! -z "$RUN_PUREFTPD" ] && [ "$RUN_PUREFTPD" == "yes" ]; then
  263. /usr/sbin/pure-ftpd.exe -S ${FTP_IP},${FTP_PORT} ${FTP_PASV_STRING} -lpuredb:/etc/pureftpd.pdb -g /var/run/pure-ftpd.pid &
  264. fi
  265. # Initialise
  266. init $*
  267. # Run
  268. run
  269. # Quit normally
  270. quit 0