ogp_agent_run 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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."
  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. # Update service to use oneshot and not forking
  101. if [ -e "$SystemDDir/ogp_agent.service" ]; then
  102. # Check to see if it's using oneshot
  103. usingOneShot=$(echo "$sudoPass" | sudo -S -p "" cat "$SystemDDir/ogp_agent.service" | grep -o "oneshot")
  104. if [ -z "$usingOneShot" ]; then
  105. replaceSystemDService=true
  106. fi
  107. fi
  108. if [ ! -e "$SystemDDir/ogp_agent.service" ] || [ "$replaceSystemDService" = true ]; then
  109. echo -e "Updating OGP agent systemd service init script."
  110. echo -e "Copying ogp_agent systemd service file to $SystemDDir"
  111. echo "$sudoPass" | sudo -S -p "" cp "${AGENTDIR}/systemd/ogp_agent.service" "$SystemDDir"
  112. echo "$sudoPass" | sudo -S -p "" sed -i "s#{OGP_AGENT_PATH}#$AGENTDIR#g" "${SystemDDir}/ogp_agent.service"
  113. echo "$sudoPass" | sudo -S -p "" systemctl daemon-reload
  114. echo "$sudoPass" | sudo -S -p "" systemctl enable ogp_agent.service
  115. echo "$sudoPass" | sudo -S -p "" service ogp_agent restart
  116. exit 0
  117. fi
  118. fi
  119. fi
  120. fi
  121. }
  122. init() {
  123. RESTART="yes"
  124. AGENT="$AGENTDIR/ogp_agent.pl"
  125. TIMEOUT=10 # time to wait after a crash (in seconds)
  126. PID_FILE=""
  127. # Should we perform an automatic update?
  128. if [ -e $BASH_PREFS_CONF ]
  129. then
  130. source "$BASH_PREFS_CONF"
  131. if [ "$agent_auto_update" -eq "1" ]
  132. then
  133. AUTO_UPDATE="yes"
  134. fi
  135. # Use custom github update address
  136. if [ ! -z "$github_update_username" ]; then
  137. REVISIONTest=`curl -s https://github.com/${github_update_username}/${REPONAME}/commits/master.atom | egrep -o "([a-f0-9]{40})" | awk 'NR==1{print $1}'`
  138. if [ ! -z "$REVISIONTest" ]; then
  139. GitHubUsername=${github_update_username}
  140. fi
  141. fi
  142. else
  143. AUTO_UPDATE="yes"
  144. fi
  145. while test $# -gt 0; do
  146. case "$1" in
  147. "-pidfile")
  148. PID_FILE="$2"
  149. PID_FILE_SET=1
  150. echo $$ > $PID_FILE
  151. shift ;;
  152. esac
  153. shift
  154. done
  155. if test ! -f "$AGENT"; then
  156. echo "ERROR: '$AGENT' not found, exiting"
  157. quit 1
  158. elif test ! -x "$AGENT"; then
  159. # Could try chmod but dont know what we will be
  160. # chmoding so just fail.
  161. echo "ERROR: '$AGENT' not executable, exiting"
  162. quit 1
  163. fi
  164. CMD="perl $AGENT"
  165. }
  166. syntax () {
  167. # Prints script syntax
  168. echo "Syntax:"
  169. echo "$0"
  170. }
  171. checkDepends() {
  172. CURL=`which curl 2>/dev/null`
  173. if test "$?" -gt 0; then
  174. echo "WARNING: Failed to locate curl binary."
  175. else
  176. echo "INFO: Located curl: $CURL"
  177. fi
  178. UNZIP=`which unzip 2>/dev/null`
  179. if test "$?" -gt 0; then
  180. echo "WARNING: Failed to locate unzip binary."
  181. else
  182. echo "INFO: Located unzip: $UNZIP"
  183. fi
  184. }
  185. update() {
  186. # Check to see if limited ogpuser exists
  187. generateOGPLimitedUser
  188. # Run the update
  189. if test -n "$AUTO_UPDATE"; then
  190. if [ -z "$CURL" -o -z "$UNZIP" ]; then
  191. checkDepends
  192. fi
  193. if [ -f "$CURL" -a -x "$CURL" ] && [ -f "$UNZIP" -a -x "$UNZIP" ]; then
  194. cd $AGENTDIR
  195. if [ ! -d tmp ]; then
  196. mkdir tmp
  197. fi
  198. cd tmp
  199. REVISION=`curl -s https://github.com/${GitHubUsername}/${REPONAME}/commits/master.atom | egrep -o "([a-f0-9]{40})" | awk 'NR==1{print $1}'`
  200. curl -Os https://raw.githubusercontent.com/${GitHubUsername}/${REPONAME}/${REVISION}/ogp_agent_run
  201. currentOGPAgentRunContent=$(cat "./ogp_agent_run")
  202. # Check to make sure ogp_agent_run downloaded successfully from GitHub before we attempt to replace it.
  203. # This should fix random 404 people have been experiencing
  204. if [ -s "./ogp_agent_run" ] && [ "$(echo "$currentOGPAgentRunContent" | head -n 1)" != "404: Not Found" ] && [ ! -z "$(echo "$currentOGPAgentRunContent" | grep "ogp_agent.pl")" ]; then
  205. diff ./ogp_agent_run $AGENTDIR/ogp_agent_run &>/dev/null
  206. if test $? -ne 0; then
  207. cp -f ./ogp_agent_run $AGENTDIR/ogp_agent_run &> /dev/null
  208. if test $? -eq 0; then
  209. cd $AGENTDIR
  210. chmod ug+x ogp_agent_run 2>/dev/null
  211. echo "`date`: The agent updater has been changed, relaunching..."
  212. rm -Rf tmp
  213. ./ogp_agent_run
  214. exit 0
  215. fi
  216. fi
  217. fi
  218. cd $AGENTDIR
  219. CURRENT=$(cat $AGENTDIR/Cfg/Config.pm | grep version | grep -Eo '[0-9a-f]{40}')
  220. if [ "$CURRENT" == "$REVISION" ]; then
  221. echo "The agent is up to date."
  222. else
  223. URL=https://github.com/${GitHubUsername}/${REPONAME}/archive/${REVISION}.zip
  224. HEAD=$(curl -L -s --head -w "%{http_code}" "$URL" -o "ogp_agent_latest.zip")
  225. if [ "$HEAD" == "200" ]; then
  226. echo "Updating agent using curl."
  227. curl -L -s "$URL" -o "ogp_agent_latest.zip"
  228. if test $? -ne 0; then
  229. echo "`date`: curl failed to download the update package."
  230. else
  231. unzip -oq "ogp_agent_latest.zip"
  232. if test $? -ne 0; then
  233. echo "`date`: Unable to unzip the update package."
  234. ogpGitCleanup
  235. else
  236. cd ${REPONAME}-${REVISION}
  237. cp -avf systemd Frontier ArmaBE Minecraft Schedule Time FastDownload php-query ogp_agent.pl ogp_screenrc ogp_screenrc_bk ogp_agent_run agent_conf.sh $AGENTDIR &> /dev/null
  238. if test $? -ne 0; then
  239. echo "`date`: The agent files cannot be overwritten."
  240. cd ..
  241. ogpGitCleanup
  242. echo "Agent update failed."
  243. else
  244. if test ! -d "$AGENTDIR/IspConfig"; then
  245. cp -Rf IspConfig $AGENTDIR/IspConfig &> /dev/null
  246. fi
  247. if test ! -d "$AGENTDIR/EHCP"; then
  248. cp -Rf EHCP $AGENTDIR/EHCP &> /dev/null
  249. fi
  250. if test ! -f "$AGENTDIR/Cfg/Preferences.pm"; then
  251. cp -f Cfg/Preferences.pm $AGENTDIR/Cfg/Preferences.pm &> /dev/null
  252. fi
  253. echo "Fixing permissions..."
  254. chmod ug+x $AGENTDIR/ogp_agent.pl &> /dev/null
  255. chmod ug+x $AGENTDIR/ogp_agent_run &> /dev/null
  256. chmod ug+x $AGENTDIR/agent_conf.sh &> /dev/null
  257. cd ..
  258. ogpGitCleanup
  259. sed -i "s/version.*/version => '${REVISION}',/" $AGENTDIR/Cfg/Config.pm
  260. echo "Agent updated successfully."
  261. fi
  262. fi
  263. fi
  264. else
  265. echo "There is a update available (${REVISION}) but the download source is not ready.";
  266. echo "Try again later."
  267. fi
  268. fi
  269. else
  270. echo "Update failed."
  271. fi
  272. fi
  273. return 0
  274. }
  275. run() {
  276. getSudoPassword
  277. restrictAccess
  278. update
  279. detectSystemD
  280. if test -n "$RESTART" ; then
  281. echo "Agent will auto-restart if there is a crash."
  282. #loop forever
  283. while true
  284. do
  285. # Run
  286. $CMD
  287. echo "`date`: Agent restart in $TIMEOUT seconds"
  288. # don't thrash the hard disk if the agent dies, wait a little
  289. sleep $TIMEOUT
  290. done # while true
  291. else
  292. $CMD
  293. fi
  294. }
  295. quit() {
  296. # Exits with the give error code, 1
  297. # if none specified.
  298. # exit code 2 also prints syntax
  299. exitcode="$1"
  300. # default to failure
  301. if test -z "$exitcode"; then
  302. exitcode=1
  303. fi
  304. case "$exitcode" in
  305. 0)
  306. echo "`date`: OGP Agent Quit" ;;
  307. 2)
  308. syntax ;;
  309. *)
  310. echo "`date`: OGP Agent Failed" ;;
  311. esac
  312. # Remove pid file
  313. if test -n "$PID_FILE" && test -f "$PID_FILE" ; then
  314. # The specified pid file
  315. rm -f $PID_FILE
  316. fi
  317. # reset SIGINT and then kill ourselves properly
  318. trap - 2
  319. kill -2 $$
  320. }
  321. function generatePassword(){
  322. if [ ! -z "$1" ]; then
  323. PLENGTH="$1"
  324. else
  325. PLENGTH="10"
  326. fi
  327. #rPass=$(date +%s | sha256sum | base64 | head -c "$PLENGTH")
  328. rPass=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1 | head -c "$PLENGTH")
  329. }
  330. function generateOGPLimitedUser(){
  331. ogpUSER="ogp_server_runner"
  332. echo "$sudoPass" | sudo -S -p "<prompt>" id -u "$ogpUSER"
  333. if [ $? -eq 1 ]; then
  334. echo "Creating ogp limited user for running servers and additional security..."
  335. echo "$sudoPass" | sudo -S -p "<prompt>" groupdel ${ogpUSER}
  336. echo "$sudoPass" | sudo -S -p "<prompt>" groupadd ${ogpUSER}
  337. generatePassword "15"
  338. ogpPass="$rPass"
  339. echo "$sudoPass" | sudo -S -p "<prompt>" useradd --home "/home/$ogpUSER" -g ${ogpUSER} -m "$ogpUSER"
  340. echo "$sudoPass" | sudo -S -p "<prompt>" sh -c "echo '${ogpUSER}:${ogpPass}' | chpasswd"
  341. # Use /bin/bash shell by default per request from Omano
  342. echo "$sudoPass" | sudo -S -p "<prompt>" usermod -s /bin/bash ${ogpUSER}
  343. echo "$sudoPass" | sudo -S -p "<prompt>" sh -c "echo 'limited_ogp_user=${ogpUSER}
  344. password=${ogpPass}' > /root/ogp_server_runner_info"
  345. agentUser="$(whoami)"
  346. echo "$sudoPass" | sudo -S -p "<prompt>" usermod -a -G "$ogpUSER" "$agentUser"
  347. # Reload perms so we don't have to reboot system for new group to apply right now...
  348. echo "$sudoPass" | sudo -S -p "<prompt>" exec su -l "$agentUser"
  349. fi
  350. # Set servers to run under their own user by default:
  351. hasLinuxUser=$(cat "$AGENTDIR/Cfg/Preferences.pm" | grep -o "linux_user_per_game_server")
  352. if [ -z "$hasLinuxUser" ]; then
  353. sed -i "\$i \\\\tlinux_user_per_game_server => '1'," "$AGENTDIR/Cfg/Preferences.pm"
  354. fi
  355. }
  356. function restrictAccess(){
  357. echo "$sudoPass" | sudo -S -p "<prompt>" chmod 750 "$AGENTDIR/Cfg/Config.pm"
  358. echo "$sudoPass" | sudo -S -p "<prompt>" chmod 750 "$AGENTDIR/Cfg/Preferences.pm"
  359. echo "$sudoPass" | sudo -S -p "<prompt>" chmod 750 "$AGENTDIR/Cfg/bash_prefs.cfg"
  360. }
  361. #####################
  362. # MAIN APP CODE #
  363. #####################
  364. # Setup OGP and Read Preferences
  365. setupOGPDirPerms
  366. # Initialise
  367. init $*
  368. # Run
  369. run
  370. # Quit normally
  371. quit 0