ogp_agent 8.0 KB

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