ogp_agent_run 9.1 KB

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