install.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. #!/bin/bash
  2. #
  3. # OGP - Open Game Panel
  4. # Copyright (C) Copyright (C) 2008 - 2013 The OGP Development Team
  5. #
  6. # http://www.opengamepanel.org/
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. #
  22. # Parameters can be passed into the install.sh script to automate OGP updates
  23. # $1 = Operation Type (Used as opType)
  24. # $2 = OGP User (Used as ogpAgentUser)
  25. # $3 = OGP User Sudo Pass (Used as ogpUserPass)
  26. # $4 = Install Path (Used as ogpInsPath)
  27. ####################
  28. # FUNCTIONs #
  29. ####################
  30. detectSystemD(){
  31. # Ops require sudo
  32. initProcessStr=$(ps -p 1 | awk '{print $4}' | tail -n 1)
  33. if [ "$initProcessStr" == "systemd" ]; then
  34. systemdPresent=1
  35. if [ -e "/lib/systemd/system" ]; then
  36. SystemDDir="/lib/systemd/system"
  37. elif [ -e "/etc/systemd/system" ]; then
  38. SystemDDir="/etc/systemd/system"
  39. else
  40. checkDir=$(ps -eaf|grep '[s]ystemd' | head -n 1 | awk '{print $8}' | grep -o ".*systemd/")
  41. if [ -e "${checkDir}system" ]; then
  42. SystemDDir="$checkDir"
  43. else
  44. # Can't find systemd dir
  45. systemdPresent=
  46. SystemDDir=
  47. fi
  48. fi
  49. fi
  50. }
  51. copySystemDInit(){
  52. AGENTDIR=${agent_home}
  53. sudoPass=${sudo_password}
  54. if [ -e "${AGENTDIR}/systemd/ogp_agent.service" ]; then
  55. if [ ! -z "$systemdPresent" ] && [ ! -z "$SystemDDir" ]; then
  56. echo -e "systemd detected as the init system with a directory of $SystemDDir. Updating OGP agent to use systemd service init script."
  57. if [ -e "/etc/init.d/ogp_agent" ] && [ ! -e "${AGENTDIR}/ogp_agent_init" ]; then
  58. echo -e "Taking care of existing OGP files."
  59. echo "$sudoPass" | sudo -S -p "" service ogp_agent stop
  60. # Kill any remaining ogp agent process
  61. ogpPID=$(ps -ef | grep -v grep | grep ogp_agent.pl | head -n 1 | awk '{print $3}')
  62. if [ ! -z "$ogpPID" ]; then
  63. echo "$sudoPass" | sudo -S -p "" kill -9 "$ogpPID"
  64. fi
  65. echo "$sudoPass" | sudo -S -p "" cp "/etc/init.d/ogp_agent" "${AGENTDIR}/ogp_agent_init"
  66. echo "$sudoPass" | sudo -S -p "" chmod +x "${AGENTDIR}/ogp_agent_init"
  67. echo "$sudoPass" | sudo -S -p "" update-rc.d ogp_agent disable
  68. echo "$sudoPass" | sudo -S -p "" chkconfig ogp_agent off
  69. echo "$sudoPass" | sudo -S -p "" rm -rf "/etc/init.d/ogp_agent"
  70. fi
  71. if [ ! -e "$SystemDDir/ogp_agent.service" ]; then
  72. echo -e "Copying ogp_agent systemd service file to $SystemDDir"
  73. echo "$sudoPass" | sudo -S -p "" cp "${AGENTDIR}/systemd/ogp_agent.service" "$SystemDDir"
  74. echo "$sudoPass" | sudo -S -p "" sed -i "s#{OGP_AGENT_PATH}#$AGENTDIR#g" "${SystemDDir}/ogp_agent.service"
  75. fi
  76. fi
  77. fi
  78. }
  79. #####################
  80. # CODE ##########
  81. #####################
  82. # Parameter notifications
  83. if [ ! -z "$1" ]; then
  84. echo -n "Received operation type of $1 as a parameter."
  85. opType="$1"
  86. fi
  87. if [ ! -z "$2" ]; then
  88. echo -n "Received OGP user of $2 as a parameter."
  89. ogpAgentUser="$2"
  90. fi
  91. if [ ! -z "$3" ]; then
  92. echo -n "Received OGP sudo password of $3 as a parameter."
  93. ogpUserPass="$3"
  94. fi
  95. if [ ! -z "$4" ]; then
  96. echo -n "Received OGP agent path of $4 as a parameter."
  97. ogpInsPath="$4"
  98. fi
  99. failed()
  100. {
  101. echo "ERROR: ${1}"
  102. exit 1
  103. }
  104. if [ "X`which screen &> /dev/null;echo $?`" != "X0" ]; then
  105. failed "You need to install software called 'screen', before you can install OGP agent.";
  106. fi
  107. if [ "X`which sed &> /dev/null;echo $?`" != "X0" ]; then
  108. failed "You need to install software called 'sed', before you can install OGP agent.";
  109. fi
  110. echo
  111. clear
  112. echo "#######################################################################"
  113. echo "# OGP Agent installation and configuration"
  114. echo "# This program will:"
  115. echo "# Create ${DEFAULT_AGENT_HOME} or user defined directory"
  116. echo "# Copy ogp_agent files to ${DEFAULT_AGENT_HOME} or user defined dir"
  117. echo "# Copy the ogp_agent init script to /etc/init.d or user defined dir"
  118. echo "# Create an initial configuration file"
  119. echo "# Thank you for using OGP. http://www.opengamepanel.org/"
  120. echo "#######################################################################"
  121. echo
  122. if [ "X`which rsync &> /dev/null;echo $?`" != "X0" ]; then
  123. echo "*** WARNING **** missing rsync client. It is not required, but needed to use the rsync game installer";
  124. fi
  125. if [ "X`whoami`" != "Xroot" ]
  126. then
  127. echo
  128. echo "Detected non-root install..."
  129. username=`whoami`
  130. echo -n "Enter sudo password: ";
  131. read sudo_password;
  132. else
  133. echo "Next you need to type the username of the user that owns the agent homes.";
  134. echo "This user must own (have access to) all the game home directories that you"
  135. echo "want to run with this agent and must to be in sudoers list so it can perform"
  136. echo "administrative tasks.";echo
  137. while [ 1 ]
  138. do
  139. if [ ! -z "$ogpAgentUser" ] ; then
  140. username="$ogpAgentUser"
  141. else
  142. echo -n "Enter user name: ";
  143. read username;
  144. fi
  145. if [ -z "$ogpUserPass" ] ; then
  146. echo -n "Enter user password: ";
  147. read sudo_password;
  148. else
  149. sudo_password="$ogpUserPass"
  150. fi
  151. if [ -z "${username}" ]
  152. then
  153. echo "Username can not be empty.";echo
  154. continue;
  155. fi
  156. if [ "Xroot" == "X${username}" ]
  157. then
  158. echo "'${username}' can not be used as user for agent.";echo
  159. continue;
  160. fi
  161. ID_OF_USER=`id -u ${username} 2> /dev/null`
  162. if [ $? != 0 ]
  163. then
  164. echo "User with entered username (${username}) does not exist.";echo
  165. continue;
  166. fi
  167. break;
  168. done
  169. fi
  170. detectSystemD
  171. readonly AGENT_USER_HOME="`cat /etc/passwd | grep "^${username}:" | cut -d':' -f6`/OGP/"
  172. echo
  173. echo "Next the directory for the agent needs to be chosen. The default directory";
  174. echo "Should be fine in most of the cases."
  175. echo
  176. if [ -z "$ogpInsPath" ]; then
  177. echo "Where do you want to install the agent?"
  178. echo -n "[Default is ${AGENT_USER_HOME}]: "
  179. read agent_home
  180. else
  181. agent_home="$ogpInsPath"
  182. fi
  183. if [ -z "${agent_home}" ]
  184. then
  185. agent_home=$AGENT_USER_HOME
  186. fi
  187. # Try to prevent users from doing damage to their systems.
  188. case ${agent_home} in
  189. /bin*|/boot*|/dev*|/etc*|/lib*|/proc*|/root*|/sbin*|/sys*|/)
  190. failed "The agent home can not be ${agent_home}";
  191. ;;
  192. esac
  193. echo "Agent install dir is ${agent_home}"
  194. echo
  195. agent_home=${agent_home%/}
  196. if [ ! -e ${agent_home} ]
  197. then
  198. mkdir -p ${agent_home} || failed "Failed to create the directory (${agent_home}) for agent."
  199. elif [ ! -w ${agent_home} ]
  200. then
  201. failed "You do not have write permissions to the directory you assigned as agent home (${agent_home})."
  202. fi
  203. if [ "X`whoami`" == "Xroot" ];
  204. then
  205. readonly DEFAULT_INIT_DIR="/etc/init.d/"
  206. else
  207. readonly DEFAULT_INIT_DIR="${agent_home}/"
  208. fi
  209. if [ -z "$systemdPresent" ]; then
  210. if [ "X`uname`" != "XLinux" ]
  211. then
  212. echo
  213. echo "Detected non-Linux platform..."
  214. echo "Where do you want to put the init scripts?"
  215. echo -n "[Default ${DEFAULT_INIT_DIR}]: "
  216. read init_dir
  217. fi
  218. if [ -z "$opType" ]; then
  219. echo "Where do you want to put the init scripts?"
  220. echo -n "[Default ${DEFAULT_INIT_DIR}]:"
  221. read init_dir
  222. fi
  223. else
  224. init_dir=${agent_home}
  225. fi
  226. if [ -z "${init_dir}" ]
  227. then
  228. init_dir=${DEFAULT_INIT_DIR}
  229. fi
  230. init_dir=${init_dir%/}
  231. echo "Copying files..."
  232. cp -avf systemd Crypt EHCP FastDownload File Frontier IspConfig KKrcon php-query Schedule Time ogp_agent.pl ogp_screenrc ogp_agent_run agent_conf.sh extPatterns.txt ${agent_home}/ || failed "Failed to copy agent files to ${agent_home}."
  233. # Create the directory for configs.
  234. mkdir -p ${agent_home}/Cfg || failed "Failed to create ${agent_home}/Cfg dir."
  235. echo
  236. if [ -e /etc/gentoo-release ]
  237. then
  238. echo "Copying ogp_agent.init.gentoo to $init_dir - Gentoo Specific Init"
  239. init_file_template='includes/ogp_agent.init.gentoo'
  240. elif [ -e /etc/sysconfig ] && [ ! -e /etc/debian_version ]
  241. then
  242. echo "Copying ogp_agent.init.rh to $init_dir - Redhat Style Init (also SuSE, and Mandrake)"
  243. init_file_template='includes/ogp_agent.init.rh'
  244. elif [ -e /etc/debian_version ]
  245. then
  246. echo "Copying ogp_agent.init.dbn to $init_dir - Debian Style Init"
  247. init_file_template='includes/ogp_agent.init.dbn'
  248. else
  249. echo "Copying the generic init script because I don't know what kind of Linux distro this is"
  250. init_file_template='includes/ogp_agent.init'
  251. fi
  252. init_file=${init_dir}/ogp_agent
  253. cp -f $init_file_template $init_file || failed "Failed to create init file ($init_file)."
  254. # Next we replace the OGP_AGENT_DIR with the actual dir in init file.
  255. sed -i "s|OGP_AGENT_DIR|${agent_home}|" ${init_file} || failed "Failed to modify init file ($init_file)."
  256. sed -i "s|OGP_USER|${username}|" ${init_file} || failed "Failed to modify init file ($init_file)."
  257. chmod a+x $init_file
  258. if [ "$init_dir" == "$agent_home" ] && [ ! -z "$systemdPresent" ]; then
  259. init_file=${init_dir}/ogp_agent_init
  260. mv ${init_dir}/ogp_agent ${init_dir}/ogp_agent_init
  261. copySystemDInit
  262. fi
  263. echo;
  264. echo "Changing files owner to user ${username}...";
  265. # Group of the files in agent_home can differ from the user so
  266. # lets leave them as they are. So no chown user:group here.
  267. chown --preserve-root -R ${username} ${agent_home} || failed "Failed to chmod the agent_home ${agent_home} for user ${username}."
  268. echo "Setting Permissions on files in ${agent_home}..."
  269. if [ -e "${init_dir}/ogp_agent" ]; then
  270. chmod 750 ${init_dir}/ogp_agent || failed "Failed to chmod ${init_dir}/ogp_agent to 750."
  271. fi
  272. if [ -e "${init_dir}/ogp_agent_init" ]; then
  273. chmod 750 ${init_dir}/ogp_agent_init || failed "Failed to chmod ${init_dir}/ogp_agent_init to 750."
  274. fi
  275. chmod 750 ${agent_home}/ogp_agent.pl || failed "Failed to chmod ${agent_home}/ogp_agent.pl to 750."
  276. chmod 750 ${agent_home}/ogp_agent_run || failed "Failed to chmod ${agent_home}/ogp_agent_run to 750."
  277. echo "Install Successful!"
  278. echo "Now configuring..."
  279. echo ""
  280. # Run the configuration script
  281. chmod +x ${agent_home}/agent_conf.sh
  282. if [ -z "$opType" ]; then
  283. bash ${agent_home}/agent_conf.sh -s $sudo_password -u $username
  284. fi
  285. echo "Attempting to start the Open Game Panel (OGP) agent..."
  286. systemctl daemon-reload
  287. chkconfig ogp_agent on
  288. rc-update add ogp_agent default
  289. update-rc.d ogp_agent defaults
  290. systemctl enable ogp_agent.service
  291. service ogp_agent restart
  292. echo;
  293. echo "OGP installation complete!"
  294. echo
  295. exit 0