ogp_agent_run 9.1 KB

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