ogp_agent_run 9.8 KB

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