Advertisement
Guest User

Untitled

a guest
Apr 7th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.23 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #/**
  4. # *
  5. # * Copyright © 2010-2016 by xhost.ch GmbH
  6. # *
  7. # * All rights reserved.
  8. # *
  9. # **/
  10.  
  11. CFG_FILE="setup.config"
  12.  
  13. ### Function definitions
  14.  
  15. function ask {
  16. if [ ! "`eval echo \\$$1`" = "" ]; then
  17. var="`eval echo \\$$1`"
  18. eval echo $4
  19. return
  20. fi
  21. def=$2
  22. if [ "$1" = "MC_DAEMON_PW" -o "$1" = "MC_DB_PASS" ]; then
  23. extra="s"
  24. else
  25. extra=""
  26. fi
  27. eval read -r$extra -p '"'$3' "'
  28. if [ "$REPLY" = "" ]; then
  29. export "$1"="$def"
  30. else
  31. export "$1"="$REPLY"
  32. fi
  33. }
  34.  
  35. #function askSave {
  36. # read -p "Save entered settings? ([y]/n) "
  37. # if [ "$REPLY" != "n" ]; then
  38. # save
  39. # fi
  40. #}
  41.  
  42. function save {
  43. echo "Saving settings to '$CFG_FILE'."
  44. export | grep ' MC_' > "$CFG_FILE"
  45. chmod o-rwx "$CFG_FILE"
  46. echo "IMPORTANT: Make sure this file is not accessible by unauthorized users."
  47. echo
  48. }
  49.  
  50. function quit {
  51. #askSave
  52. exit
  53. }
  54.  
  55. function sigQuit {
  56. echo
  57. quit
  58. }
  59.  
  60. trap sigQuit SIGINT SIGTERM
  61.  
  62. INSTALL="bin/ jar/ launcher/ scripts/ templates/ eula.txt multicraft.conf.dist default_server.conf.dist server_configs.conf.dist"
  63.  
  64. ### Basic checks
  65.  
  66. for i in $INSTALL; do
  67. if [ ! -e "$i" ]; then
  68. echo "Error: Can't find '$i'! This script needs to be started from inside the Multicraft package directory."
  69. echo "Aborting setup."
  70. quit
  71. fi
  72. done
  73.  
  74. ### Begin
  75.  
  76. echo
  77. echo "***"
  78. echo "*** Welcome to Multicraft!"
  79. echo "***"
  80. echo
  81. echo "This installer will help you get Multicraft up and running."
  82. echo "No changes are made to the system until all of the required information has been collected."
  83. echo
  84. echo "NOTE: This script automates the installation as described on the Multicraft website. Use it at your own risk."
  85. echo
  86. echo
  87.  
  88. FILE_ARCH="64bit"
  89. SYS_ARCH="64bit"
  90. if [ "`uname -m | grep 'x86_64'`" = "" ]; then
  91. SYS_ARCH="32bit"
  92. fi
  93.  
  94. if [ ! "$FILE_ARCH" = "$SYS_ARCH" ]; then
  95. echo
  96. echo "WARNING: It seems that the system architecture ($SYS_ARCH) does not match the package you've downloaded ($FILE_ARCH). Please download the correct version of Multicraft for your system."
  97. read -p "Press [Enter] to proceed anyway, Ctrl-C to abort."
  98. fi
  99.  
  100.  
  101. ### Load cfg?
  102. if [ -e "$CFG_FILE" ] ; then
  103. #ask "LOAD_CFG" "y" "Found '$CFG_FILE', load settings from this file? [\$def]/n" "-"
  104. #if [ "$LOAD_CFG" = "y" ]; then
  105. source "$CFG_FILE"
  106. #fi
  107. fi
  108.  
  109. ### Collect basic information
  110.  
  111. ask "MC_MULTIUSER" "y" "Run each Minecraft server under its own user? (Multicraft will create system users): [\$def]/n" "Create system user for each Minecraft server: \$var"
  112.  
  113. if [ "$USER" = "root" ]; then
  114. def="minecraft"
  115. else
  116. def="$USER"
  117. fi
  118. ask "MC_USER" "$def" "Run Multicraft under this user: [\$def]" "Multicraft will run as \$var"
  119.  
  120. if [ "`cat /etc/passwd | awk -F: '{ print $1 }' | grep $MC_USER`" = "" ]; then
  121. ask "MC_CREATE_USER" "y" "User not found. Create user '$MC_USER' on start of installation? [\$def]/n" "Create user '$MC_USER': \$var"
  122. if [ "$MC_CREATE_USER" != "y" ]; then
  123. echo "Won't create user, aborting."
  124. quit
  125. fi
  126. MC_USER_EXISTS="n"
  127. else
  128. MC_USER_ESISTS="y"
  129. fi
  130.  
  131. ask "MC_DIR" "/home/$MC_USER/multicraft" "Install Multicraft in: [\$def]" "Installation directory: \$var"
  132. if [ -e "$MC_DIR" ]; then
  133. ask "MC_DIR_OVERWRITE" "y" "Warning: '$MC_DIR' exists! Continue installing in this directory? [\$def]/n" "Installing in existing directory: \$var"
  134. if [ "$MC_DIR_OVERWRITE" != "y" ]; then
  135. echo "Won't install in existing directory, aborting."
  136. quit
  137. fi
  138. fi
  139.  
  140. ask "MC_KEY" "no" "If you have a license key you can enter it now: [\$def]" "License key: \$var"
  141. ask "MC_DAEMON_ID" "1" "If you control multiple machines from one control panel you need to assign each daemon a unique ID (requires a Dynamic or custom license). Daemon ID? [\$def]" "Daemon ID: \$var"
  142. echo
  143. echo
  144.  
  145. ### Local installation?
  146.  
  147. ask "MC_LOCAL" "y" "Will the PHP frontend run on this machine? [\$def]/n" "Local front end: \$var"
  148.  
  149. IP="`ifconfig 2>/dev/null | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}' | head -n1`"
  150. if [ "$MC_LOCAL" != "y" ]; then
  151. export MC_DB_TYPE="mysql"
  152.  
  153. ask "MC_DAEMON_IP" "$IP" "IP the daemon will bind to: [\$def]" "Daemon listening on IP: \$var"
  154. IP="$MC_DAEMON_IP"
  155. ask "MC_DAEMON_PORT" "25465" "Port the daemon to listen on: [\$def]" "Daemon port: \$var"
  156. else
  157. W_USR="www-data"
  158. W_DIR="/var/www"
  159. if [ ! "`cat /etc/issue | grep CentOS`" = "" ]; then
  160. W_USR="apache"
  161. W_DIR="/var/www/html"
  162. elif [ -d "/var/www/html" ]; then
  163. W_DIR="/var/www/html"
  164. fi
  165.  
  166. ask "MC_WEB_USER" "$W_USR" "User of the webserver: [\$def]" "Webserver user: \$var"
  167. ask "MC_WEB_DIR" "$W_DIR/multicraft" "Location of the PHP frontend: [\$def]" "PHP frontend directory: \$var"
  168. if [ -e "$MC_WEB_DIR" ]; then
  169. ask "MC_WEB_DIR_OVERWRITE" "y" "Warning: '$MC_WEB_DIR' exists! Continue installing the PHP frontend in this directory? [\$def]/n" "Installing in existing PHP frontend directory: \$var"
  170. if [ "$MC_WEB_DIR_OVERWRITE" != "y" ]; then
  171. echo "Won't install in existing PHP frontend directory, aborting."
  172. quit
  173. fi
  174. fi
  175. fi
  176. ask "MC_DAEMON_PW" "none" "Please enter a new daemon password (use the same password in the last step of the panel installer) [\$def]" "Daemon connection password: \$var"
  177.  
  178. echo
  179. echo
  180.  
  181. ### FTP Server
  182.  
  183. ask "MC_FTP_SERVER" "y" "Enable builtin FTP server? [\$def]/n" "Enable builtin FTP server: \$var"
  184.  
  185. if [ "$MC_FTP_SERVER" = "y" ]; then
  186. ask "MC_FTP_IP" "$IP" "IP the FTP server will listen on (empty for same as daemon): [\$def]" "FTP server IP: \$var"
  187. ask "MC_FTP_PORT" "21" "FTP server port: [\$def]" "FTP server port: \$var"
  188.  
  189. ask "MC_PLUGINS" "y" "Block FTP upload of .jar files and other executables (potentially dangerous plugins)? [\$def]/n" "Block .jar and executable upload: \$var"
  190. echo
  191. fi
  192.  
  193. echo
  194. echo
  195.  
  196. ### DB settings
  197. echo "MySQL is the recommended database type but it requires you to have a MySQL server available."
  198. echo "SQLite is more light weight and it will work fine for small installations up to 10 servers."
  199. echo "For multiple daemons on a single panel MySQL is required."
  200. echo
  201. ask "MC_DB_TYPE" "sqlite" "What kind of database do you want to use? [\$def]/mysql" "Database type: \$var"
  202.  
  203. if [ "$MC_DB_TYPE" = "mysql" ]; then
  204. echo
  205. echo "NOTE: This is for the daemon config, the front end has an installation routine for database configuration and initialization."
  206. ask "MC_DB_HOST" "127.0.0.1" "Database host: [\$def]" "Database host: \$var"
  207. ask "MC_DB_NAME" "multicraft_daemon" "Database name: [\$def]" "Database name: \$var"
  208. ask "MC_DB_USER" "root" "Database user: [\$def]" "Database user: \$var"
  209. ask "MC_DB_PASS" "" "Database password: [\$def]" "Database password: \$var"
  210. echo
  211. elif [ "$MC_DB_TYPE" = "sqlite" ]; then
  212. echo
  213. echo "The database will be located at: '$MC_DIR/data/data.db'"
  214. else
  215. echo "Unsupported database type '$MC_DB_TYPE'!"
  216. echo "Aborting."
  217. quit
  218. fi
  219. echo "***"
  220. echo "*** Please use the control panel to initialize the database."
  221. echo "***"
  222.  
  223. echo
  224.  
  225. ### Check user/group create/delete utilities
  226.  
  227. MC_JAVA="`which java`"
  228. MC_ZIP="`which zip`"
  229. MC_UNZIP="`which unzip`"
  230. if [ "$MC_JAVA" = "" ]; then
  231. ask "MC_JAVA" "/usr/bin/java" "Path to java program: [\$def]" "Path to java: \$var"
  232. fi
  233. if [ "$MC_ZIP" = "" ]; then
  234. ask "MC_ZIP" "/usr/bin/zip" "Path to zip program: [\$def]" "Path to zip: \$var"
  235. fi
  236. if [ "$MC_UNZIP" = "" ]; then
  237. ask "MC_UNZIP" "/usr/bin/unzip" "Path to unzip program: [\$def]" "Path to unzip: \$var"
  238. fi
  239. if [ "$MC_MULTIUSER" = "y" -o "$MC_CREATE_USER" = "y" ]; then
  240. MC_USERADD="`which useradd`"
  241. MC_GROUPADD="`which groupadd`"
  242. MC_USERDEL="`which userdel`"
  243. MC_GROUPDEL="`which groupdel`"
  244. if [ "$MC_USERADD" = "" ]; then
  245. ask "MC_USERADD" "/usr/sbin/useradd" "Path to useradd program: [\$def]" "Path to useradd program: \$var"
  246. fi
  247. if [ "$MC_GROUPADD" = "" ]; then
  248. ask "MC_GROUPADD" "/usr/sbin/groupadd" "Path to groupadd program: [\$def]" "Path to groupadd program: \$var"
  249. fi
  250. if [ "$MC_USERDEL" = "" ]; then
  251. ask "MC_USERDEL" "/usr/sbin/userdel" "Path to userdel program: [\$def]" "Path to userdel program: \$var"
  252. fi
  253. if [ "$MC_GROUPDEL" = "" ]; then
  254. ask "MC_GROUPDEL" "/usr/sbin/groupdel" "Path to groupdel program: [\$def]" "Path to groupdel program: \$var"
  255. fi
  256. fi
  257.  
  258. ### Installation
  259.  
  260. echo
  261. echo "NOTE: Any running daemon will be stopped!"
  262. #ask "START_INSTALL" "y" "Ready to install Multicraft. Start installation? [\$def]/n" "-"
  263. #if [ "$START_INSTALL" != "y" ]; then
  264. # echo "Not installing."
  265. # quit
  266. #fi
  267.  
  268. echo
  269. echo "***"
  270. echo "*** INSTALLING"
  271. echo "***"
  272. echo
  273.  
  274. if [ -e "$MC_DIR/bin/multicraft" ]; then
  275. echo "Stopping daemon if running..."
  276. "$MC_DIR/bin/multicraft" stop
  277. "$MC_DIR/bin/multicraft" stop_ftp
  278. echo "done."
  279. sleep 1
  280. fi
  281.  
  282. ### Multicraft user & directory setup
  283.  
  284. if [ "$MC_USER_EXISTS" = "n" ]; then
  285. echo
  286. echo "Creating user '$MC_USER'"
  287. "$MC_GROUPADD" "$MC_USER"
  288. if [ ! "$?" = "0" ]; then
  289. echo "Error: Can't create group '$MC_USER'! Please create this group manually and re-run the setup script."
  290. fi
  291.  
  292. "$MC_USERADD" "$MC_USER" -g "$MC_USER" -s /bin/false
  293. if [ ! "$?" = "0" ]; then
  294. echo "Error: Can't create user '$MC_USER'! Please create this user manually and re-run the setup script."
  295. fi
  296. fi
  297.  
  298. echo
  299. echo "Creating directory '$MC_DIR'"
  300. mkdir -p "$MC_DIR"
  301.  
  302. echo
  303. echo "Ensuring the home directory exists and is owned and writable by the user"
  304. MC_HOME="`grep "^$MC_USER:" /etc/passwd | awk -F":" '{print $6}'`"
  305. mkdir -p "$MC_HOME"
  306. chown "$MC_USER":"$MC_USER" "$MC_HOME"
  307. chmod u+rwx "$MC_HOME"
  308. chmod go+x "$MC_HOME"
  309.  
  310. echo
  311. if [ -e "$MC_DIR/bin" -a "$( cd "bin/" && pwd )" != "$( cd "$MC_DIR/bin" 2>/dev/null && pwd )" ]; then
  312. mv "$MC_DIR/bin" "$MC_DIR/bin.bak"
  313. fi
  314. for i in $INSTALL; do
  315. echo "Installing '$i' to '$MC_DIR/'"
  316. cp -a "$i" "$MC_DIR/"
  317. done
  318. rm -f "$MC_DIR/bin/_weakref.so"
  319. rm -f "$MC_DIR/bin/collections.so"
  320. rm -f "$MC_DIR/bin/libpython2.5.so.1.0"
  321. rm -f "$MC_DIR/bin/"*-py2.5*.egg
  322.  
  323. if [ "$MC_KEY" != "no" ]; then
  324. echo
  325. echo "Installing license key"
  326. echo "$MC_KEY" > "$MC_DIR/multicraft.key"
  327. fi
  328.  
  329.  
  330. ### Generate config
  331.  
  332. echo
  333. CFG="$MC_DIR/multicraft.conf"
  334. if [ -e "$CFG" ]; then
  335. ask "OVERWRITE_CONF" "n" "The 'multicraft.conf' file already exists, overwrite? y/[\$def]" "-"
  336. fi
  337.  
  338. if [ "$MC_DB_TYPE" = "mysql" ]; then
  339. DB_STR="mysql:host=$MC_DB_HOST;dbname=$MC_DB_NAME"
  340. fi
  341.  
  342. function repl {
  343. LINE="$SETTING = `echo $1 | sed "s/['\\&,]/\\\\&/g"`"
  344. }
  345. if [ ! -e "$CFG" -o "$OVERWRITE_CONF" = "y" ]; then
  346.  
  347. if [ -e "$CFG" ]; then
  348. echo "Multicraft.conf exists, backing up..."
  349. cp -a "$CFG" "$CFG.bak"
  350. fi
  351.  
  352. echo "Generating 'multicraft.conf'"
  353. > "$CFG"
  354.  
  355. SECTION=""
  356. cat "$CFG.dist" | while IFS="" read -r LINE
  357. do
  358. if [ "`echo $LINE | grep "^ *\[\w\+\] *$"`" ]; then
  359. SECTION="$LINE"
  360. SETTING=""
  361. else
  362. SETTING="`echo $LINE | sed -n 's/^ *\#\? *\([^ ]\+\) *=.*/\1/p'`"
  363. fi
  364. case "$SECTION" in
  365. "[multicraft]")
  366. case "$SETTING" in
  367. "user") repl "$MC_USER" ;;
  368. "ip") if [ "$MC_LOCAL" != "y" ]; then repl "$MC_DAEMON_IP"; fi ;;
  369. "port") if [ "$MC_LOCAL" != "y" ]; then repl "$MC_DAEMON_PORT"; fi ;;
  370. "password") repl "$MC_DAEMON_PW" ;;
  371. "id") repl "$MC_DAEMON_ID" ;;
  372. "database") if [ "$MC_DB_TYPE" = "mysql" ]; then repl "$DB_STR"; fi ;;
  373. "dbUser") if [ "$MC_DB_TYPE" = "mysql" ]; then repl "$MC_DB_USER"; fi ;;
  374. "dbPassword") if [ "$MC_DB_TYPE" = "mysql" ]; then repl "$MC_DB_PASS"; fi ;;
  375. "webUser") if [ "$MC_DB_TYPE" = "mysql" ]; then repl ""; else repl "$MC_WEB_USER"; fi ;;
  376. "baseDir") repl "$MC_DIR" ;;
  377. esac
  378. ;;
  379. "[ftp]")
  380. case "$SETTING" in
  381. "enabled") if [ "$MC_FTP_SERVER" = "y" ]; then repl "true"; else repl "false"; fi ;;
  382. "ftpIp") repl "$MC_FTP_IP" ;;
  383. "ftpPort") repl "$MC_FTP_PORT" ;;
  384. "forbiddenFiles") if [ "$MC_PLUGINS" = "n" ]; then repl ""; fi ;;
  385. esac
  386. ;;
  387. "[minecraft]")
  388. case "$SETTING" in
  389. "java") repl "$MC_JAVA" ;;
  390. esac
  391. ;;
  392. "[system]")
  393. case "$SETTING" in
  394. "unpackCmd") repl "$MC_UNZIP"' -quo "{FILE}"' ;;
  395. "packCmd") repl "$MC_ZIP"' -qr "{FILE}" .' ;;
  396. esac
  397. if [ "$MC_MULTIUSER" = "y" ]; then
  398. case "$SETTING" in
  399. "multiuser") repl "true" ;;
  400. "addUser") repl "$MC_USERADD"' -c "Multicraft Server {ID}" -d "{DIR}" -g "{GROUP}" -s /bin/false "{USER}"' ;;
  401. "addGroup") repl "$MC_GROUPADD"' "{GROUP}"' ;;
  402. "delUser") repl "$MC_USERDEL"' "{USER}"' ;;
  403. "delGroup") repl "$MC_GROUPDEL"' "{GROUP}"' ;;
  404. esac
  405. fi
  406. ;;
  407. "[backup]")
  408. case "$SETTING" in
  409. "command") repl "$MC_ZIP"' -qr "{WORLD}-tmp.zip" . -i "{WORLD}"*/*' ;;
  410. esac
  411. ;;
  412. esac
  413. echo "$LINE" >> "$CFG"
  414. done
  415. fi
  416.  
  417. echo
  418. echo "Setting owner of '$MC_DIR' to '$MC_USER'"
  419. chown "$MC_USER":"$MC_USER" "$MC_DIR"
  420. chown -R "$MC_USER":"$MC_USER" "$MC_DIR/bin"
  421. chown -R "$MC_USER":"$MC_USER" "$MC_DIR/launcher"
  422. chmod 555 "$MC_DIR/launcher/launcher"
  423. chown -R "$MC_USER":"$MC_USER" "$MC_DIR/jar"
  424. chown -R "$MC_USER":"$MC_USER" "$MC_DIR/scripts"
  425. chmod 555 "$MC_DIR/scripts/getquota.sh"
  426. chown -R "$MC_USER":"$MC_USER" "$MC_DIR/templates"
  427. chown "$MC_USER":"$MC_USER" "$MC_DIR/default_server.conf.dist"
  428. chown "$MC_USER":"$MC_USER" "$MC_DIR/server_configs.conf.dist"
  429.  
  430. echo "Setting special file permissions"
  431. if [ "$MC_MULTIUSER" = "y" ]; then
  432. chown 0:"$MC_USER" "$MC_DIR/bin/useragent"
  433. chmod 4550 "$MC_DIR/bin/useragent"
  434. fi
  435. chmod 755 "$MC_DIR/jar/"*.jar 2> /dev/null
  436.  
  437. ### Install PHP frontend
  438.  
  439. if [ "$MC_LOCAL" = "y" ]; then
  440. echo
  441.  
  442. if [ -e "$MC_WEB_DIR" -a -e "$MC_WEB_DIR/protected/data/data.db" ]; then
  443. echo "Web directory exists, backing up protected/data/data.db"
  444. cp -a "$MC_WEB_DIR/protected/data/data.db" "$MC_WEB_DIR/protected/data/data.db.bak"
  445. fi
  446.  
  447. echo "Creating directory '$MC_WEB_DIR'"
  448. mkdir -p "$MC_WEB_DIR"
  449.  
  450. echo "Installing PHP frontend files from 'panel/' to '$MC_WEB_DIR'"
  451. cp -a panel/* "$MC_WEB_DIR"
  452. cp -a panel/.ht* "$MC_WEB_DIR"
  453.  
  454.  
  455. echo "Setting owner of '$MC_WEB_DIR' to '$MC_WEB_USER'"
  456. chown -R "$MC_WEB_USER":"$MC_WEB_USER" "$MC_WEB_DIR"
  457. echo "Setting permissions of '$MC_WEB_DIR'"
  458. chmod -R o-rwx "$MC_WEB_DIR"
  459.  
  460. else
  461. ### PHP frontend not on local machine
  462. echo
  463. echo "* NOTE: The PHP frontend will not be installed on this machine. Please put the contents of the directory 'panel/' in the www root of the machine you want to run the PHP frontend on and run the installer (install.php)."
  464. fi
  465.  
  466. echo "Temporarily starting daemon to set DB permissions."
  467. "$MC_DIR/bin/multicraft" set_permissions
  468.  
  469. echo
  470. echo
  471. echo "***"
  472. echo "*** Installation complete!"
  473. echo "***"
  474. echo "***"
  475. echo
  476. echo "PLEASE READ:"
  477. echo
  478. echo "Before starting the daemon you need to run the control panel installer to initialize your database. (example: http://your.address/multicraft/install.php)"
  479. echo
  480. echo "The daemon WILL NOT work correctly as long as the database hasn't been initialized."
  481. echo
  482. echo
  483. echo "After running the control panel installer start the daemon using the following command:"
  484. echo "$MC_DIR/bin/multicraft start"
  485. echo
  486. echo
  487. echo "If there are any issues, please check the log file: '$MC_DIR/multicraft.log'"
  488. echo
  489. echo
  490. #read -p "After reading the instructions above, press [Enter] to continue."
  491. echo
  492. echo
  493. echo "In case you want to rerun this script you can save the entered settings."
  494. quit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement