install.sh 10 KB

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